source: proiecte/NBody/NBody 2.0/SetupKernelsCL.cpp @ 35

Last change on this file since 35 was 35, checked in by (none), 14 years ago
File size: 5.0 KB
Line 
1#include "NBody.hpp"
2
3extern int numBodies;   
4
5/////////////////////////////////// NBody::setupCLKernels Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
6//
7// This function only sets the Kernel arguments
8//
9/////////////////////////////////// NBody::setupCLKernels Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
10
11int NBody::setupCLKernels()
12{
13    cl_int   status;
14
15        /* get a kernel object handle for a kernel with the given name */
16    kernel = clCreateKernel(
17                 program,
18                 kernelFunctionName,
19                 &status);
20       
21        if(status != CL_SUCCESS)
22    {
23        std::cout << "clCreateKernel failed." << std::endl;
24        return 1;
25    }
26
27        //================================================
28        // Arguments specific to all kernels:
29
30    /*** Set appropriate arguments to the kernel ***/
31
32    /* Particle positions */
33    status = clSetKernelArg(
34                 kernel,
35                 0,                                     // Argument one;
36                 sizeof(cl_mem),
37                 (void *)&updatedPos);
38
39        if(status != CL_SUCCESS)
40    {
41        std::cout << "clSetKernelArg failed. (updatedPos)" << std::endl;
42        return 1;
43    }
44
45    /* Particle velocity */
46    status = clSetKernelArg(
47                 kernel,
48                 1,                                     // Argument two;
49                 sizeof(cl_mem),
50                 (void *)&updatedVel);
51       
52        if(status != CL_SUCCESS)
53    {
54        std::cout << "clSetKernelArg failed. (updatedVel)" << std::endl;
55        return 1;
56    }
57 
58    /* numBodies */
59    status = clSetKernelArg(
60                 kernel,
61                 2,                                     // Argument three;
62                 sizeof(cl_int),
63                 (void *)&numBodies);
64
65        if(status != CL_SUCCESS)
66    {
67        std::cout << "clSetKernelArg failed. (numBodies)" << std::endl;
68        return 1;
69    }
70
71    /* time step */
72    status = clSetKernelArg(
73                 kernel,
74                 3,                                     // Argument four;
75                 sizeof(cl_float),
76                 (void *)&delT);
77        if(status != CL_SUCCESS)
78    {
79        std::cout << "clSetKernelArg failed. (delT)" << std::endl;
80        return 1;
81    }
82
83    /* upward Pseudoprobability */
84    status = clSetKernelArg(
85                 kernel,
86                 4,                                     // Argument five;
87                 sizeof(cl_float),
88                 (void *)&espSqr);
89
90        if(status != CL_SUCCESS)
91    {
92        std::cout << "clSetKernelArg failed. (espSqr)" << std::endl;
93        return 1;
94    }
95
96    /* local memory */
97    status = clSetKernelArg(
98                 kernel,
99                 5,                                     // Argument six;
100                 GROUP_SIZE * 4 * sizeof(float),
101                 NULL);
102
103        if(status != CL_SUCCESS)
104    {
105        std::cout << "clSetKernelArg failed. (localPos)" << std::endl;
106        return 1;
107    }
108
109        //================================================
110        // Arguments specific to Leapfrog kernel:
111
112        if (integrator == LEAPFROG){
113
114                /* local memory */
115                status = clSetKernelArg(
116                                         kernel,
117                                         6,                                     // Argument seven;
118                                         GROUP_SIZE * 4 * sizeof(float),
119                                         NULL);
120
121                if(status != CL_SUCCESS)
122                {
123                        std::cout << "clSetKernelArg failed. (localVel)" << std::endl;
124                        return 1;
125                }
126
127                /* Particle acceleration */
128                status = clSetKernelArg(
129                                         kernel,
130                                         7,                                     // Argument eight;
131                                         sizeof(cl_mem),
132                                         (void *)&updatedAcc);
133
134                if(status != CL_SUCCESS)
135                {
136                        std::cout << "clSetKernelArg failed. (updatedAcc)" << std::endl;
137                        return 1;
138                }
139
140                /* If it is the first run and we do not have the acc computed yet */
141                status = clSetKernelArg(
142                 kernel,
143                 8,                                     // Argument nine;
144                 sizeof(cl_int),
145                 (void *)&initialAccComputation);
146
147                if(status != CL_SUCCESS)
148                {
149                        std::cout << "clSetKernelArg failed. (initialAccComputation)" << std::endl;
150                        return 1;
151                }
152
153                /* If it is the first run and we do not have the acc computed yet */
154                status = clSetKernelArg(
155                 kernel,
156                 9,                                     // Argument 10;
157                 sizeof(cl_mem),
158                 (void *)&updatedCollTime);
159
160                if(status != CL_SUCCESS)
161                {
162                        std::cout << "clSetKernelArg failed. (updatedCollTime)" << std::endl;
163                        return 1;
164                }
165        }
166
167        //================================================
168        // Try to see if there is enough local memory
169        // on the device for the above parameter
170        // declared to be __local:
171    status = clGetKernelWorkGroupInfo(kernel,
172                                      devices[0],
173                                      CL_KERNEL_LOCAL_MEM_SIZE,
174                                      sizeof(cl_ulong),
175                                      &usedLocalMemory,
176                                      NULL);
177        if(status != CL_SUCCESS)
178    {
179        std::cout << "clGetKernelWorkGroupInfo CL_KERNEL_LOCAL_MEM_SIZE failed." << std::endl;
180        return 1;
181    }
182
183    if(usedLocalMemory > totalLocalMemory)
184    {
185        std::cout << "Unsupported: Insufficient local memory on device." << std::endl;
186        return 1;
187    }
188
189    return 0;
190}
Note: See TracBrowser for help on using the repository browser.