Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/lib/cl-target/main.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/lib/cl-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1664 - (view) (download) (as text)

1 : jhr 1267 /*! \file main.c
2 :     *
3 :     * \author John Reppy
4 :     */
5 :    
6 :     /*
7 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :    
11 : jhr 1275 #include <Diderot/diderot.h>
12 : jhr 1411 #include "clinfo.h"
13 : lamonts 1271 #include <sys/sysctl.h>
14 :     #include <sys/stat.h>
15 : jhr 1267
16 : lamonts 1488 typedef struct {
17 : jhr 1492 cl_int blkIdx; //!< the id of this block
18 :     cl_int nActive; //!< number of active (status != DIE or STABLE) strands
19 :     cl_int nDead; //!< number of strands in the DIE state
20 :     cl_int nStabilizing; //!< number of new strands in the STABILIZE state
21 :     cl_int nDying; //!< number of new strands in the DIE state
22 : lamonts 1488 } StrandBlock_t;
23 :    
24 :     typedef struct {
25 : jhr 1492 cl_int numStrands; //!< number of strands
26 :     cl_int sId; //!< the index into the todo list or queue
27 : lamonts 1501 cl_int nextStrand; //!< index of the next strand to retrieve from the pool
28 :     cl_int clearQueueSz; //!< an indicator on whether the queue size should be cleared
29 : jhr 1492 cl_int queueSize; //!< number of blocks on the scheduler's queue
30 :     cl_int todoSize; //!< number of blocks on the scheduler's todo list
31 : jhr 1519 cl_int numAvailable; //!< number of active strands left to process
32 : lamonts 1488 } SchedState_t;
33 :    
34 : jhr 1473 typedef struct {
35 :     cl_kernel kern; //!< OpenCL kernel that implements the program
36 :     size_t workGrpSize; //!< size of workgroup for this kernel
37 :     cl_ulong localSzb; //!< size of local memory used by kernel
38 :     } GPUKernel_t;
39 :    
40 : jhr 1267 struct struct_world {
41 : jhr 1519 STRUCT_WORLD_PREFIX
42 :     // FIXME: document these fields!
43 : lamonts 1341 void *inState;
44 :     void *outState;
45 : jhr 1649 DeviceInfo_t *device; //!< info about OpenCL device that we are using.
46 :     uint32_t nWorkers; //!< number of work groups to create
47 : jhr 1471 cl_context context; //!< OpenCL execution context
48 :     cl_command_queue cmdQ; //!< OpenCL command queue
49 : lamonts 1500 GPUKernel_t kernel[3]; //!< OpenCL kernel that implements the program
50 : jhr 1267 };
51 : jhr 1471
52 : jhr 1519 // FIXME: need documentation for this stuct!
53 : lamonts 1459 typedef struct {
54 : jhr 1519 cl_mem schedMem;
55 :     cl_mem outMem;
56 :     cl_mem inMem;
57 :     cl_mem blocksMem;
58 :     cl_mem strandBlocksIdxsMem;
59 :     cl_mem queueMem;
60 :     cl_mem todoMem;
61 :     cl_mem statusMem;
62 :     } KernelArgs_t;
63 : jhr 1267
64 : jhr 1291 static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld);
65 : jhr 1473 static void CheckErrorCode (cl_int sts, const char * msg);
66 : jhr 1519 static void SetPhase1Args (cl_kernel kernel, int *argCount, KernelArgs_t *args);
67 :     static void SetPhase2Args (cl_kernel kernel, int *argCount, KernelArgs_t *args, int blk_sz);
68 :     static void SetScheduleKernelArgs (cl_kernel kernel, int *argCount, KernelArgs_t *args);
69 : lamonts 1500
70 : jhr 1473 static bool CreateKernel (cl_device_id dev, cl_program prog, const char *name, GPUKernel_t *kern);
71 :    
72 : jhr 1358 extern void Diderot_LoadGlobals (cl_context cxt, cl_kernel kernel, cl_command_queue cmdQ, int argStart);
73 :    
74 : jhr 1267 int main (int argc, const char **argv)
75 :     {
76 :     // get information about OpenCL support
77 :     CLInfo_t *clInfo = GetCLInfo();
78 :     if (clInfo == 0) {
79 : jhr 1327 fprintf (stderr, "no OpenCL support\n");
80 :     exit (1);
81 : jhr 1267 }
82 :    
83 : lamonts 1664 // run the generated global initialization code
84 :     if (VerboseFlg)
85 :     fprintf (stderr, "initializing globals ...\n");
86 :     Diderot_InitGlobals ();
87 :    
88 : jhr 1654 Diderot_int_t nWorkersPerCU = 4;
89 : jhr 1648 Diderot_Options_t *opts = Diderot_OptNew ();
90 : jhr 1654 Diderot_OptAddInt (opts, "np", "specify number of workers/CU", &nWorkersPerCU, true);
91 : jhr 1648 Diderot_RegisterGlobalOpts (opts);
92 :     Diderot_OptProcess (opts, argc, argv);
93 :     Diderot_OptFree (opts);
94 : lamonts 1617
95 :     if (VerboseFlg)
96 :     PrintCLInfo (stdout, clInfo);
97 :    
98 : jhr 1291 Diderot_World_t *wrld = Diderot_Initially (); // this may not be right for OpenCL
99 : lamonts 1459
100 : jhr 1291 if (! InitCL(clInfo, wrld))
101 :     exit (1);
102 : jhr 1282
103 : jhr 1654 // set the number of work groups
104 :     if ((0 < nWorkersPerCU) && (nWorkersPerCU < wrld->device->maxWISize[0] / wrld->device->cuWidth))
105 :     wrld->nWorkers = nWorkersPerCU * wrld->device->numCUs;
106 : jhr 1649 else
107 : jhr 1654 wrld->nWorkers = wrld->device->numCUs; // fallback to one worker per CU
108 : jhr 1649 if (VerboseFlg)
109 :     fprintf (stderr, "using %d x %d threads\n",
110 :     wrld->nWorkers, wrld->device->cuWidth);
111 :    
112 :     // Conversion of strands from their host types to their shadow types
113 : jhr 1472 void *shadowInState = CheckedAlloc(Diderot_Strands[0]->shadowStrandSzb * wrld->numStrands);
114 : lamonts 1488 void *shadowOutState = CheckedAlloc(Diderot_Strands[0]->shadowStrandSzb * wrld->numStrands);
115 : lamonts 1462 uint8_t *strandPtr = (uint8_t *)wrld->inState;
116 :     uint8_t *strandShadowPtr = (uint8_t *)shadowInState;
117 : jhr 1649 uint8_t *strandShadowOutPtr = (uint8_t *)shadowOutState;
118 : lamonts 1462 size_t shadowSize = Diderot_Strands[0]->shadowStrandSzb * wrld->numStrands;
119 :    
120 : jhr 1469 for (int i = 0; i < wrld->numStrands; i++) {
121 :     Diderot_Strands[0]->strandCopy (strandPtr, strandShadowPtr);
122 : lamonts 1488 Diderot_Strands[0]->strandCopy (strandPtr, strandShadowOutPtr);
123 : jhr 1469 strandPtr += Diderot_Strands[0]->stateSzb;
124 :     strandShadowPtr += Diderot_Strands[0]->shadowStrandSzb;
125 : lamonts 1488 strandShadowOutPtr += Diderot_Strands[0]->shadowStrandSzb;
126 : lamonts 1462 }
127 : jhr 1519 // FIXME: it is confusing to use the inState/outState pointers for two different purposes.
128 : jhr 1471 free (wrld->inState);
129 : jhr 1519 free (wrld->outState);
130 : lamonts 1462 wrld->inState = shadowInState;
131 : lamonts 1488 wrld->outState = shadowOutState;
132 : lamonts 1462
133 : jhr 1469 // hack to make the invariant part of the state the same in both copies
134 : lamonts 1462 memcpy (wrld->outState, wrld->inState, shadowSize);
135 :    
136 : jhr 1327 int argCount = 0;
137 :     cl_int sts = CL_SUCCESS;
138 : lamonts 1316
139 : jhr 1519 KernelArgs_t kernelArgs;
140 : lamonts 1488 SchedState_t scheduler;
141 : lamonts 1346
142 : lamonts 1488 scheduler.numStrands = wrld->numStrands;
143 : lamonts 1500 scheduler.nextStrand = 0;
144 : lamonts 1488 scheduler.todoSize = 0;
145 :     scheduler.sId = 0;
146 : lamonts 1501 scheduler.clearQueueSz= 1;
147 : lamonts 1500 scheduler.numAvailable = wrld->numStrands;
148 : lamonts 1488
149 : lamonts 1465 size_t globalWorkSize[1] = {0};
150 : lamonts 1488 size_t localWorkSize[1] = {0};
151 : jhr 1649 globalWorkSize[0] = wrld->nWorkers * wrld->device->cuWidth;
152 : jhr 1654 localWorkSize[0] = wrld->device->cuWidth;
153 :    
154 :     // compute the number of scheduler blocks
155 :     int numberOfBlocks = (wrld->numStrands + wrld->device->cuWidth - 1) / wrld->device->cuWidth;
156 :     assert (numberOfBlocks * wrld->device->cuWidth >= wrld->numStrands);
157 : lamonts 1488
158 : jhr 1649 size_t strandBlkMemSize = sizeof(int) * numberOfBlocks * wrld->device->cuWidth;
159 : lamonts 1488 size_t schedListMemSize = sizeof(int) * numberOfBlocks;
160 : jhr 1492 int *schedulerQueue = (int *)CheckedAlloc(schedListMemSize);
161 :     int *schedulerTodoList = (int *)CheckedAlloc(schedListMemSize);
162 :     int *strandBlocksIdxs = (int *)CheckedAlloc(strandBlkMemSize);
163 : lamonts 1488 scheduler.queueSize = numberOfBlocks;
164 : lamonts 1513
165 : lamonts 1488 size_t strandblkMemSize = sizeof(StrandBlock_t) * numberOfBlocks;
166 : jhr 1654 StrandBlock_t *strandBlks = (StrandBlock_t *)CheckedAlloc(strandblkMemSize);
167 : lamonts 1470
168 : jhr 1492 for (int i = 0; i < numberOfBlocks; i++) {
169 : lamonts 1488 schedulerQueue[i] = i;
170 :     strandBlks[i].blkIdx = i;
171 :     strandBlks[i].nActive = 0;
172 :     strandBlks[i].nDead = 0;
173 :     strandBlks[i].nStabilizing = 0;
174 : jhr 1492 strandBlks[i].nDying = 0;
175 : jhr 1432 }
176 : lamonts 1459
177 : lamonts 1488 kernelArgs.schedMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
178 :     sizeof(SchedState_t), &scheduler, &sts);
179 :     CheckErrorCode (sts, "error creating OpenCL scheduler buffer\n");
180 : lamonts 1459
181 : lamonts 1488 kernelArgs.blocksMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
182 :     strandblkMemSize, strandBlks , &sts);
183 :     CheckErrorCode (sts, "error creating OpenCL strand blocks buffer\n");
184 : lamonts 1465
185 : lamonts 1488 kernelArgs.strandBlocksIdxsMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
186 :     strandBlkMemSize, strandBlocksIdxs , &sts);
187 :     CheckErrorCode (sts, "error creating OpenCL strand blocks indices buffer\n");
188 : lamonts 1425
189 : lamonts 1488 kernelArgs.queueMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
190 :     schedListMemSize, schedulerQueue , &sts);
191 :     CheckErrorCode (sts, "error creating OpenCL scheduler queue buffer\n");
192 :    
193 :     kernelArgs.todoMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
194 :     schedListMemSize, schedulerTodoList , &sts);
195 :     CheckErrorCode (sts, "error creating OpenCL scheduler todo buffer\n");
196 :    
197 :     kernelArgs.inMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
198 :     shadowSize, wrld->inState, &sts);
199 :     CheckErrorCode (sts, "error creating OpenCL strand in-state buffer\n");
200 :    
201 :     kernelArgs.outMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
202 :     shadowSize, wrld->outState, &sts);
203 :     CheckErrorCode (sts, "error creating OpenCL strand out-state buffer\n");
204 :    
205 :     kernelArgs.statusMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
206 :     sizeof(int) * wrld->numStrands, wrld->status, &sts);
207 :     CheckErrorCode (sts, "error creating OpenCL world status buffer\n");
208 :    
209 : lamonts 1513 //Setup the Update Kernel's arguments
210 : jhr 1649 SetPhase1Args (wrld->kernel[0].kern, &argCount, &kernelArgs);
211 :     Diderot_LoadGlobals (wrld->context, wrld->kernel[0].kern, wrld->cmdQ, argCount);
212 : lamonts 1459
213 : lamonts 1513 //Setup the Compaction Kernel's arguments
214 : lamonts 1488 argCount=0;
215 : jhr 1649 SetPhase2Args (wrld->kernel[1].kern, &argCount, &kernelArgs, wrld->device->cuWidth);
216 : lamonts 1488
217 : lamonts 1513 //Setup the Scheduler Kernel's arguments
218 : lamonts 1500 argCount = 0;
219 : jhr 1649 SetScheduleKernelArgs (wrld->kernel[2].kern, &argCount, &kernelArgs);
220 : lamonts 1500
221 : jhr 1603 double t0 = airTime();
222 : lamonts 1459
223 : lamonts 1500 while (scheduler.numAvailable > 0) {
224 : jhr 1519 // Runs the update kernel on all strands
225 : lamonts 1488 sts = clEnqueueNDRangeKernel(wrld->cmdQ, wrld->kernel[0].kern, 1, NULL,
226 : jhr 1519 globalWorkSize, localWorkSize, 0, NULL, NULL);
227 : jhr 1649 CheckErrorCode (sts, "error in executing update kernel\n");
228 : lamonts 1488
229 : jhr 1649 sts = clEnqueueTask(wrld->cmdQ,wrld->kernel[2].kern, 0, NULL, NULL);
230 :     CheckErrorCode (sts, "error in executing scheduler update kernel before compaction\n");
231 : lamonts 1513
232 : jhr 1649 // Run the compaction kernel on all strands
233 :     sts = clEnqueueNDRangeKernel(wrld->cmdQ, wrld->kernel[1].kern, 1, NULL,
234 : jhr 1492 globalWorkSize, localWorkSize, 0, NULL, NULL);
235 : jhr 1649 CheckErrorCode (sts, "error ccccn executing compaction kernel\n");
236 : lamonts 1501
237 : jhr 1649 sts = clEnqueueTask(wrld->cmdQ,wrld->kernel[2].kern,0,NULL,NULL);
238 :     CheckErrorCode (sts, "error in executing scheduler update kernel after compaction\n");
239 :    
240 :     sts = clEnqueueReadBuffer(wrld->cmdQ, kernelArgs.schedMem, CL_TRUE, 0, sizeof(SchedState_t),
241 : lamonts 1500 &scheduler, 0, NULL, NULL);
242 : jhr 1649 CheckErrorCode (sts, "error reading back scheduler information\n");
243 : lamonts 1459 }
244 : lamonts 1470
245 : lamonts 1488 sts = clEnqueueReadBuffer(wrld->cmdQ, kernelArgs.outMem, CL_TRUE, 0,shadowSize,
246 : jhr 1469 wrld->outState, 0, NULL, NULL);
247 : jhr 1471 CheckErrorCode (sts, "error in reading back output\n");
248 : lamonts 1459
249 : jhr 1469 sts = clEnqueueReadBuffer(
250 : lamonts 1488 wrld->cmdQ, kernelArgs.statusMem, CL_TRUE, 0, sizeof(int) * wrld->numStrands,
251 : jhr 1469 wrld->status, 0, NULL, NULL);
252 : jhr 1471 CheckErrorCode (sts, "error in reading back status\n");
253 : jhr 1342
254 : lamonts 1526 wrld->outputSzb = Diderot_Strands[0]->shadowStrandSzb;
255 : jhr 1469 clFinish (wrld->cmdQ);
256 : jhr 1473 clReleaseKernel(wrld->kernel[0].kern);
257 :     clReleaseKernel(wrld->kernel[1].kern);
258 : lamonts 1316 clReleaseCommandQueue(wrld->cmdQ);
259 :     clReleaseContext(wrld->context);
260 :    
261 : jhr 1603 double totalTime = airTime() - t0;
262 : jhr 1287
263 :     if (VerboseFlg)
264 :     fprintf (stderr, "done in %f seconds\n", totalTime);
265 :     else if (TimingFlg)
266 :     printf ("usr=%f\n", totalTime);
267 :    
268 : nseltzer 1453 // here we have the final state of all of the strands in the "in" buffer
269 : lamonts 1513 // output the final strand states
270 :     if (NrrdOutputFlg)
271 : jhr 1519 Diderot_Output (wrld, Diderot_Strands[0]->shadowStrandSzb);
272 : lamonts 1513 else
273 : jhr 1519 Diderot_Print (wrld);
274 : jhr 1287
275 : lamonts 1488 Diderot_Shutdown (wrld);
276 : jhr 1287
277 :     return 0;
278 :    
279 : lamonts 1271 }
280 : jhr 1469
281 : jhr 1519 static void SetPhase1Args (cl_kernel kernel, int *argCount, KernelArgs_t *args)
282 : lamonts 1459 {
283 : jhr 1492 int count = *argCount;
284 : lamonts 1459 cl_int sts = CL_SUCCESS;
285 : jhr 1282
286 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->inMem);
287 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL strand in-state argument\n");
288 : lamonts 1488
289 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->outMem);
290 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL strand out-state argument\n");
291 : lamonts 1459
292 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->statusMem);
293 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL world status argument\n");
294 : lamonts 1459
295 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->schedMem);
296 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL scheduler argument\n");
297 : lamonts 1459
298 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->blocksMem);
299 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL strand blocks argument\n");
300 : lamonts 1459
301 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->strandBlocksIdxsMem);
302 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL strand blocks' indices argument\n");
303 : lamonts 1459
304 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->queueMem);
305 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL scheduler queue argument\n");
306 : lamonts 1488
307 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->todoMem);
308 : lamonts 1500 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL scheduler todo argument\n");
309 : lamonts 1488
310 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(StrandBlock_t), NULL);
311 : lamonts 1513 CheckErrorCode (sts, "Update Kernel: error Setting OpenCL local strand blockargument\n");
312 : lamonts 1488
313 : jhr 1492 *argCount = count;
314 :    
315 : jhr 1469 }
316 :    
317 : jhr 1519 static void SetPhase2Args (cl_kernel kernel, int *argCount, KernelArgs_t *args, int blk_sz)
318 : lamonts 1459 {
319 : jhr 1492 int count = *argCount;
320 :     cl_int sts = CL_SUCCESS;
321 : lamonts 1459
322 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->statusMem);
323 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL world status argument\n");
324 : lamonts 1459
325 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->schedMem);
326 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL scheduler argument\n");
327 : lamonts 1488
328 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->blocksMem);
329 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL strand blocks argument\n");
330 : lamonts 1488
331 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->strandBlocksIdxsMem);
332 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL strand blocks' indices argument\n");
333 : lamonts 1488
334 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->queueMem);
335 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL scheduler queue argument\n");
336 : lamonts 1488
337 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->todoMem);
338 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL scheduler todo argument\n");
339 : lamonts 1488
340 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(StrandBlock_t), NULL);
341 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL local strand blockargument\n");
342 : lamonts 1488
343 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(int) * blk_sz, NULL);
344 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL local preStable argument\n");
345 : lamonts 1488
346 : jhr 1492 sts = clSetKernelArg (kernel, count++, sizeof(int) * blk_sz, NULL);
347 : lamonts 1500 CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL local preDead argument\n");
348 : lamonts 1488
349 : lamonts 1500 sts = clSetKernelArg (kernel, count++, sizeof(int) * blk_sz * 2, NULL);
350 :     CheckErrorCode (sts, "Compaction Kernel: error Setting OpenCL local temporary array for the prefix scan of preStable preDead argument\n");
351 : lamonts 1488
352 : lamonts 1513
353 : jhr 1492 *argCount = count;
354 : jhr 1469 }
355 : jhr 1649
356 : jhr 1519 static void SetScheduleKernelArgs(cl_kernel kernel, int *argCount, KernelArgs_t *args)
357 : lamonts 1500 {
358 :     int count = *argCount;
359 :     cl_int sts = CL_SUCCESS;
360 :    
361 :     sts = clSetKernelArg (kernel, count++, sizeof(cl_mem), &args->schedMem);
362 :     CheckErrorCode (sts, "Scheduler Kernel: error setting OpenCL scheduler argument\n");
363 : jhr 1469
364 : lamonts 1500 *argCount = count;
365 :     }
366 :    
367 : jhr 1471 static void CheckErrorCode (cl_int sts, const char *msg)
368 : lamonts 1459 {
369 :     if (sts != CL_SUCCESS) {
370 : jhr 1464 fprintf (stderr, "%s", msg);
371 : lamonts 1459 exit(1);
372 :     }
373 : jhr 1469 }
374 :    
375 : jhr 1287 /*! \brief load OpenCL code from a file
376 :     */
377 : jhr 1291 static char *LoadSource (const char *filename)
378 : lamonts 1271 {
379 :     struct stat statbuf;
380 : jhr 1287 if (stat(filename, &statbuf) < 0) {
381 : jhr 1327 fprintf (stderr, "unable to stat OpenCL source file %s\n", filename);
382 :     exit (1);
383 : jhr 1287 }
384 :    
385 : jhr 1472 char *source = (char *)CheckedAlloc(statbuf.st_size + 1);
386 : jhr 1287 if (source == 0) {
387 : jhr 1327 fprintf (stderr, "unable to allocate memory for OpenCL source\n");
388 :     exit (1);
389 : jhr 1287 }
390 :    
391 :     FILE *fh = fopen(filename, "r");
392 :     if ((fh == 0)
393 :     || (fread(source, statbuf.st_size, 1, fh) != 1)) {
394 : jhr 1327 fprintf (stderr, "unable to read OpenCL source from %s\n", filename);
395 :     exit (1);
396 : jhr 1287 }
397 :     source[statbuf.st_size] = '\0';
398 :     fclose (fh);
399 : lamonts 1271
400 :     return source;
401 : jhr 1342 }
402 :    
403 : jhr 1473 //! create a kernel object from a program
404 :     static bool CreateKernel (cl_device_id dev, cl_program prog, const char *name, GPUKernel_t *kern)
405 :     {
406 :     cl_int sts;
407 :    
408 :     kern->kern = clCreateKernel(prog, name, &sts);
409 :     if (sts != CL_SUCCESS) {
410 :     fprintf (stderr, "error getting %s from program\n", name);
411 :     return false;
412 :     }
413 :     sts = clGetKernelWorkGroupInfo (
414 :     kern->kern, dev, CL_KERNEL_WORK_GROUP_SIZE,
415 :     sizeof(size_t), &(kern->workGrpSize), 0);
416 :     if (sts != CL_SUCCESS) {
417 :     fprintf (stderr, "error getting workgroup size for %s\n", name);
418 :     return false;
419 :     }
420 :     sts = clGetKernelWorkGroupInfo (
421 :     kern->kern, dev, CL_KERNEL_LOCAL_MEM_SIZE,
422 :     sizeof(cl_ulong), &(kern->localSzb), 0);
423 :     if (sts != CL_SUCCESS) {
424 :     fprintf (stderr, "error getting local memory size for %s\n", name);
425 :     return false;
426 :     }
427 :    
428 :     if (VerboseFlg) {
429 :     fprintf(stderr, "kernel %s: workgroup size = %d, local memory = %d bytes\n",
430 :     name, (int)kern->workGrpSize, (int)kern->localSzb);
431 :     }
432 :    
433 :     return true;
434 :     }
435 :    
436 : jhr 1352 static void LogMessagesToStderr (const char *errstr, const void *private_info, size_t cb, void *user_data)
437 : lamonts 1328 {
438 : jhr 1480 fprintf(stderr, "***** error log *****\n");
439 : jhr 1342 fprintf(stderr, "%s\n", errstr);
440 : jhr 1480 fprintf(stderr, "***** end error log *****\n");
441 : jhr 1342 }
442 : jhr 1267
443 : jhr 1417 /*! \brief initialize the OpenCL execution context, including loading and compiling the OpenCL
444 :     * program.
445 :     * \param clInfo points to the summary information about the available OpenCL devices.
446 :     * \param wrld the Diderot execution information.
447 :     * \return true if initialization is successful, otherwise false.
448 : jhr 1291 */
449 :     static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld)
450 :     {
451 : jhr 1327 cl_int sts;
452 : jhr 1649 int pltIx = 0; // main patform index
453 : jhr 1291
454 :     // find a GPU on platform[0]
455 : jhr 1420 DeviceInfo_t *dev = 0;
456 : jhr 1291 int i;
457 : jhr 1649 for (i = 0; i < clInfo->platforms[pltIx].numDevices; i++) {
458 :     if (isGPUDevice (&(clInfo->platforms[pltIx].devices[i]))
459 :     && clInfo->platforms[pltIx].devices[i].isAvail) {
460 :     dev = &(clInfo->platforms[pltIx].devices[i]);
461 : jhr 1291 break;
462 :     }
463 :     }
464 : lamonts 1488
465 : jhr 1420 if (dev == 0) {
466 : jhr 1291 fprintf (stderr, "unable to find GPU device\n");
467 :     return false;
468 :     }
469 :    
470 : jhr 1469 if (VerboseFlg) {
471 : jhr 1649 fprintf (stderr, "using platform %d, device %d: %s\n",
472 :     pltIx, i, clInfo->platforms[0].devices[i].name);
473 : jhr 1469 }
474 :    
475 : jhr 1291 // create the context
476 : jhr 1420 cl_context cxt = clCreateContext(0, 1, &(dev->id), LogMessagesToStderr, 0, &sts);
477 : jhr 1291 if (sts != CL_SUCCESS) {
478 : jhr 1327 fprintf (stderr, "error creating OpenCL context\n");
479 : jhr 1291 return false;
480 :     }
481 :    
482 :     // create the program from the source
483 :     int fnameLen = strlen(wrld->name) + 4; // name + ".cl\0"
484 : jhr 1472 char *fname = (char *)CheckedAlloc(fnameLen);
485 : jhr 1291 snprintf(fname, fnameLen, "%s.cl", wrld->name);
486 : lamonts 1459 char *updateSource = LoadSource (fname);
487 : jhr 1464 free (fname);
488 : lamonts 1459
489 : lamonts 1642 const char *src[1] = {updateSource};
490 :     cl_program prog = clCreateProgramWithSource(cxt, 1, src, NULL, &sts);
491 : jhr 1492 free (updateSource);
492 : jhr 1291 if (sts != CL_SUCCESS) {
493 : jhr 1327 fprintf (stderr, "error creating program\n");
494 : jhr 1291 return false;
495 :     }
496 :    
497 :     // build the program
498 : jhr 1420 char options[1024];
499 :     snprintf (options, sizeof(options),
500 : jhr 1472 "-D DIDEROT_CL_VERSION=%d -D DIDEROT_CU_WIDTH=%d -I %s -w",
501 : jhr 1420 100*dev->majorVersion + dev->minorVersion,
502 : jhr 1472 dev->cuWidth,
503 : jhr 1420 DIDEROT_INCLUDE_PATH);
504 : jhr 1472 if (VerboseFlg) {
505 :     fprintf (stderr, "clBuildProgram options: %s\n", options);
506 :     }
507 : jhr 1420 sts = clBuildProgram (prog, 1, &(dev->id), options, 0, 0);
508 : jhr 1291 if (sts != CL_SUCCESS) {
509 : jhr 1352 size_t logSzb;
510 : jhr 1420 clGetProgramBuildInfo (prog, dev->id, CL_PROGRAM_BUILD_LOG, 0, 0, &logSzb);
511 : jhr 1472 char *log = CheckedAlloc(logSzb+1);
512 : jhr 1420 clGetProgramBuildInfo (prog, dev->id, CL_PROGRAM_BUILD_LOG, logSzb, log, &logSzb);
513 : jhr 1352 log[logSzb] = '\0';
514 :     fprintf (stderr, "error compiling program:\n%s\n", log);
515 :     free (log);
516 : jhr 1291 return false;
517 :     }
518 :    
519 : jhr 1473 // extract the kernels from the program
520 : lamonts 1488 if ((! CreateKernel (dev->id, prog, "Diderot_UpdateKernel", &(wrld->kernel[0])))
521 : lamonts 1500 || (! CreateKernel (dev->id, prog, "Diderot_CompactionKernel", &(wrld->kernel[1])))
522 :     || (! CreateKernel (dev->id, prog, "Diderot_SchedUpdateKernel", &(wrld->kernel[2]))))
523 : jhr 1291 return false;
524 :    
525 : jhr 1417 // create the command queue
526 : jhr 1420 cl_command_queue q = clCreateCommandQueue(cxt, dev->id, 0, &sts);
527 : jhr 1417 if (sts != CL_SUCCESS) {
528 :     fprintf (stderr, "error creating OpenCL command queue\n");
529 :     return false;
530 :     }
531 :    
532 : jhr 1291 // initialize world info
533 : jhr 1649 wrld->device = dev;
534 : jhr 1291 wrld->context = cxt;
535 :     wrld->cmdQ = q;
536 :    
537 :     return true;
538 :     }
539 : jhr 1327
540 : jhr 1267 // this should be the part of the scheduler
541 :     void *Diderot_AllocStrand (Strand_t *strand)
542 :     {
543 : jhr 1472 return CheckedAlloc(strand->stateSzb);
544 : jhr 1267 }
545 :    
546 :     // block allocation of an initial collection of strands
547 : jhr 1519 Diderot_World_t *Diderot_AllocInitially (
548 : jhr 1287 const char *name, // the name of the program
549 : jhr 1267 Strand_t *strand, // the type of strands being allocated
550 :     bool isArray, // is the initialization an array or collection?
551 :     uint32_t nDims, // depth of iteration nesting
552 :     int32_t *base, // nDims array of base indices
553 :     uint32_t *size) // nDims array of iteration sizes
554 :     {
555 : jhr 1472 Diderot_World_t *wrld = NEW(Diderot_World_t);
556 : jhr 1267 if (wrld == 0) {
557 :     fprintf (stderr, "unable to allocate world\n");
558 :     exit (1);
559 :     }
560 :    
561 : jhr 1287 wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
562 : jhr 1267 wrld->isArray = isArray;
563 :     wrld->nDims = nDims;
564 : jhr 1472 wrld->base = NEWVEC(int32_t, nDims);
565 :     wrld->size = NEWVEC(uint32_t, nDims);
566 : jhr 1267 size_t numStrands = 1;
567 :     for (int i = 0; i < wrld->nDims; i++) {
568 :     numStrands *= size[i];
569 :     wrld->base[i] = base[i];
570 :     wrld->size[i] = size[i];
571 :     }
572 :    
573 : jhr 1378 if (VerboseFlg) {
574 :     printf("AllocInitially: %d", size[0]);
575 :     for (int i = 1; i < nDims; i++) printf(" x %d", size[i]);
576 :     printf("\n");
577 :     }
578 : jhr 1267
579 :     // allocate the strand state pointers
580 :     wrld->numStrands = numStrands;
581 : jhr 1519 wrld->inState = CheckedAlloc (strand->stateSzb * numStrands);
582 : jhr 1472 wrld->outState = CheckedAlloc (strand->shadowStrandSzb * numStrands);
583 : jhr 1519 wrld->status = NEWVEC(StatusInt_t, numStrands);
584 : jhr 1267
585 :     // initialize strand state pointers etc.
586 :     for (size_t i = 0; i < numStrands; i++) {
587 : lamonts 1351 wrld->status[i] = DIDEROT_ACTIVE;
588 : jhr 1267 }
589 :    
590 :     return wrld;
591 :    
592 :     }
593 :    
594 :     // get strand state pointers
595 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
596 :     {
597 :     assert (i < wrld->numStrands);
598 : jhr 1650 return wrld->inState + (i * Diderot_Strands[0]->stateSzb);
599 : jhr 1267 }
600 :    
601 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
602 :     {
603 :     assert (i < wrld->numStrands);
604 : jhr 1650 // FIXME: this function is called when outState points to the shadow state,
605 :     // but it is confusing to me -- JHR
606 :     return wrld->outState + (i * Diderot_Strands[0]->shadowStrandSzb);
607 : jhr 1267 }
608 :    
609 : jhr 1354 /***** Support for shadow image values *****/
610 : jhr 1435
611 : jhr 1354 void ShadowImage1D (cl_context cxt, Shadow_image1D_t *dst, Diderot_image1D_t *src)
612 :     {
613 :     dst->size[0] = src->size[0];
614 :     dst->s = src->s;
615 : lamonts 1617 dst->t = src->t;
616 : jhr 1354 }
617 : jhr 1435
618 : jhr 1354 void ShadowImage2D (cl_context cxt, Shadow_image2D_t *dst, Diderot_image2D_t *src)
619 :     {
620 : lamonts 1461 dst->size[0] = src->size[0];
621 : jhr 1354 dst->size[1] = src->size[1];
622 : jhr 1355 ShadowMat2x2 (dst->w2i, src->w2i);
623 : lamonts 1461 ShadowVec2 (&dst->tVec, src->tVec);
624 : jhr 1355 ShadowMat2x2 (dst->w2iT, src->w2iT);
625 : jhr 1354 }
626 :    
627 :     void ShadowImage3D (cl_context cxt, Shadow_image3D_t *dst, Diderot_image3D_t *src)
628 :     {
629 :     dst->size[0] = src->size[0];
630 :     dst->size[1] = src->size[1];
631 :     dst->size[2] = src->size[2];
632 : jhr 1355 ShadowMat3x3 (dst->w2i, src->w2i);
633 : lamonts 1461 ShadowVec3 (&dst->tVec, src->tVec);
634 : jhr 1435 ShadowMat3x3 (dst->w2iT, src->w2iT);
635 : jhr 1354 }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0