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 1327 - (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 1267 #include <string.h>
13 :     #include <stdio.h>
14 :     #include <assert.h>
15 :     #include "clinfo.h"
16 : lamonts 1271 #include <sys/sysctl.h>
17 :     #include <sys/stat.h>
18 : jhr 1267
19 :     // NOTE: we probably should put this in a file that supports runtime printing
20 :     static bool VerboseFlg = false;
21 : jhr 1287 static bool TimingFlg = false;
22 : jhr 1267
23 :     struct struct_world {
24 : jhr 1287 const char *name; // the program name
25 : jhr 1267 bool isArray; // is the initialization an array or collection?
26 :     uint32_t nDims; // depth of iteration nesting
27 :     int32_t *base; // nDims array of base indices
28 :     uint32_t *size; // nDims array of iteration sizes
29 :     uint32_t numStrands; // number of strands in the world
30 : jhr 1327 size_t strandSize; // the sizeof of the strand buffers
31 : jhr 1267 void **inState;
32 :     void **outState;
33 :     uint8_t *status; // array of strand status flags
34 : jhr 1291 cl_device_id device; // OpenCL device
35 :     cl_context context; // OpenCL execution context
36 :     cl_command_queue cmdQ; // OpenCL command queue
37 :     cl_kernel kernel; // OpenCL Kernel that implements the program
38 : jhr 1267 };
39 :    
40 : jhr 1291 static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld);
41 : jhr 1267
42 : lamonts 1316 extern void Diderot_LoadGlobals (cl_context context, cl_kernel kernel, int argStart);
43 : jhr 1267 int main (int argc, const char **argv)
44 :     {
45 :     // get information about OpenCL support
46 :     CLInfo_t *clInfo = GetCLInfo();
47 :     if (clInfo == 0) {
48 : jhr 1327 fprintf (stderr, "no OpenCL support\n");
49 :     exit (1);
50 : jhr 1267 }
51 :    
52 :     Diderot_Options_t *opts = Diderot_OptNew ();
53 :    
54 :     Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
55 :     Diderot_RegisterGlobalOpts (opts);
56 :     Diderot_OptProcess (opts, argc, argv);
57 :     Diderot_OptFree (opts);
58 :    
59 :     // run the generated global initialization code
60 : jhr 1275 if (VerboseFlg) printf("initializing globals ...\n");
61 : lamonts 1316
62 : jhr 1291 Diderot_InitGlobals();
63 : jhr 1267
64 :     /***** FIXME: OpenCL specific stuff goes here. Things to do:
65 :     **
66 :     ** 1) copy data to GPU
67 :     ** 2) initialize strands
68 :     ** 3) run strands to termination
69 :     ** 4) load results from GPU
70 :     **/
71 : lamonts 1271
72 : jhr 1291 Diderot_World_t *wrld = Diderot_Initially (); // this may not be right for OpenCL
73 :     if (! InitCL(clInfo, wrld))
74 :     exit (1);
75 : jhr 1282
76 : jhr 1327 int argCount = 0;
77 : jhr 1291
78 : jhr 1327 cl_int sts = CL_SUCCESS;
79 : lamonts 1316
80 : jhr 1327 /* Create the strand in-state and out-state buffers */
81 :     cl_mem stateInMem, stateOutMem;
82 : lamonts 1316
83 : jhr 1327 stateInMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
84 :     wrld->strandSize, *(wrld->inState), &sts);
85 :     if (sts != CL_SUCCESS) {
86 :     fprintf (stderr, "error creating OpenCL strand in-state buffer\n");
87 : lamonts 1316 exit(1);
88 :     }
89 :    
90 : jhr 1327 stateOutMem = clCreateBuffer (wrld->context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
91 :     wrld->strandSize, *(wrld->outState), &sts);
92 :     if (sts != CL_SUCCESS) {
93 :     fprintf (stderr, "error creating OpenCL strand in-state buffer\n");
94 :     exit(1);
95 : lamonts 1316 }
96 : jhr 1327
97 :     /* Set the in-state and out-state strand agruments */
98 :     sts = clSetKernelArg (wrld->kernel, argCount++, sizeof(cl_mem), &stateInMem);
99 :     if (sts != CL_SUCCESS) {
100 :     fprintf (stderr, "error Setting OpenCL strand in-state argument\n");
101 :     exit(1);
102 : lamonts 1316 }
103 :    
104 : jhr 1327 sts = clSetKernelArg (wrld->kernel, argCount++, sizeof(cl_mem), &stateOutMem);
105 :     if (sts != CL_SUCCESS) {
106 :     fprintf (stderr, "error Setting OpenCL strand out-state argument\n");
107 :     exit(1);
108 :     }
109 : lamonts 1316
110 : jhr 1327 /* Setup up global/local work sizes and fire the kernel for execution */
111 :     if (wrld->nDims == 2) {
112 :     size_t global_work_size[2], local_work_size[2];
113 :     global_work_size[0] = wrld->size[0];
114 :     global_work_size[1] = wrld->size[1];
115 :     local_work_size[0] = 1;
116 :     local_work_size[1] = 1;
117 :     cl_int width = global_work_size[1];
118 : lamonts 1316
119 : jhr 1327 sts = clSetKernelArg(wrld->kernel, argCount++, sizeof(cl_int), &width);
120 :     if (sts != CL_SUCCESS) {
121 :     fprintf (stderr, "error Setting OpenCL width argument\n");
122 :     exit(1);
123 :     }
124 : lamonts 1316
125 : jhr 1327 Diderot_LoadGlobals(wrld->context, wrld->kernel, argCount);
126 : lamonts 1316
127 : jhr 1327 sts = clEnqueueNDRangeKernel(wrld->cmdQ, wrld->kernel, 2, NULL, global_work_size,
128 :     local_work_size, 0, NULL, NULL);
129 :     if (sts != CL_SUCCESS) {
130 :     fprintf (stderr, "error in executing kernel code:%d\n",sts);
131 :     exit(1);
132 :     }
133 : lamonts 1316
134 : jhr 1327 clFinish(wrld->cmdQ);
135 : lamonts 1316
136 : jhr 1327 sts = clEnqueueReadBuffer(wrld->cmdQ, stateOutMem, CL_TRUE, 0, wrld->strandSize,
137 :     wrld->outState, 0, NULL, NULL);
138 :    
139 :     if (sts != CL_SUCCESS) {
140 :     fprintf (stderr, "error in reading back output code:%d\n",sts);
141 :     exit(1);
142 :     }
143 :     }
144 : lamonts 1316
145 : jhr 1327 /* FIXME: release all OpenCL objects */
146 : lamonts 1316 clReleaseKernel(wrld->kernel);
147 :     clReleaseCommandQueue(wrld->cmdQ);
148 :     clReleaseContext(wrld->context);
149 :    
150 : jhr 1287 double t0 = GetTime();
151 : jhr 1267
152 : jhr 1327 /* SOMETHING */
153 : jhr 1287
154 :     double totalTime = GetTime() - t0;
155 :    
156 :     if (VerboseFlg)
157 :     fprintf (stderr, "done in %f seconds\n", totalTime);
158 :     else if (TimingFlg)
159 :     printf ("usr=%f\n", totalTime);
160 :    
161 : jhr 1267 // here we have the final state of all of the strands in the "in" buffer
162 : jhr 1287 int outFileNameLen = strlen(wrld->name) + 5;
163 :     char *outFileName = (char *)malloc(outFileNameLen);
164 :     snprintf (outFileName, outFileNameLen, "%s.txt", wrld->name);
165 :     FILE *outS = fopen(outFileName, "w");
166 : jhr 1267 if (outS == NULL) {
167 : jhr 1287 fprintf (stderr, "Cannot open output file %s\n", outFileName);
168 : jhr 1267 exit (8);
169 : jhr 1287 }
170 :    
171 :     for (int i = 0; i < wrld->numStrands; i++) {
172 :     if (wrld->status[i] == DIDEROT_STABLE)
173 : lamonts 1316 Diderot_Strands[0]->print (outS, wrld->outState[i]);
174 : jhr 1287 }
175 :     fclose (outS);
176 :    
177 :     Diderot_Shutdown (wrld);
178 :    
179 :     return 0;
180 :    
181 : lamonts 1271 }
182 : jhr 1282
183 : jhr 1287 /*! \brief load OpenCL code from a file
184 :     */
185 : jhr 1291 static char *LoadSource (const char *filename)
186 : lamonts 1271 {
187 :     struct stat statbuf;
188 : jhr 1287 if (stat(filename, &statbuf) < 0) {
189 : jhr 1327 fprintf (stderr, "unable to stat OpenCL source file %s\n", filename);
190 :     exit (1);
191 : jhr 1287 }
192 :    
193 :     char *source = (char *) malloc(statbuf.st_size + 1);
194 :     if (source == 0) {
195 : jhr 1327 fprintf (stderr, "unable to allocate memory for OpenCL source\n");
196 :     exit (1);
197 : jhr 1287 }
198 :    
199 :     FILE *fh = fopen(filename, "r");
200 :     if ((fh == 0)
201 :     || (fread(source, statbuf.st_size, 1, fh) != 1)) {
202 : jhr 1327 fprintf (stderr, "unable to read OpenCL source from %s\n", filename);
203 :     exit (1);
204 : jhr 1287 }
205 :     source[statbuf.st_size] = '\0';
206 :     fclose (fh);
207 : lamonts 1271
208 :     return source;
209 :     }
210 : jhr 1267
211 : jhr 1291 /*! \brief build an OpenCL program from source.
212 :     */
213 :     static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld)
214 :     {
215 : jhr 1327 cl_int sts;
216 : jhr 1291
217 :     // find a GPU on platform[0]
218 :     cl_device_id dev;
219 :     int i;
220 :     for (i = 0; i < clInfo->platforms[0].numDevices; i++) {
221 :     if (clInfo->platforms[0].devices[i].ty == CL_DEVICE_TYPE_GPU) {
222 :     dev = clInfo->platforms[0].devices[i].id;
223 :     break;
224 :     }
225 :     }
226 :     if (i == clInfo->platforms[0].numDevices) {
227 :     fprintf (stderr, "unable to find GPU device\n");
228 :     return false;
229 :     }
230 :    
231 :     // create the context
232 :     cl_context cxt = clCreateContext(0, 1, &dev, 0, 0, &sts);
233 :     if (sts != CL_SUCCESS) {
234 : jhr 1327 fprintf (stderr, "error creating OpenCL context\n");
235 : jhr 1291 return false;
236 :     }
237 :    
238 :     // create the command queue
239 :     cl_command_queue q = clCreateCommandQueue(cxt, dev, 0, &sts);
240 :     if (sts != CL_SUCCESS) {
241 : jhr 1327 fprintf (stderr, "error creating OpenCL command queue\n");
242 : jhr 1291 return false;
243 :     }
244 :    
245 :     // create the program from the source
246 :     int fnameLen = strlen(wrld->name) + 4; // name + ".cl\0"
247 :     char *fname = (char *)malloc(fnameLen);
248 :     snprintf(fname, fnameLen, "%s.cl", wrld->name);
249 :     const char *src = LoadSource (fname);
250 :     free (fname);
251 :     cl_program prog = clCreateProgramWithSource(cxt, 1, &src, NULL, &sts);
252 :     if (sts != CL_SUCCESS) {
253 : jhr 1327 fprintf (stderr, "error creating program\n");
254 : jhr 1291 return false;
255 :     }
256 :     free ((void *)src);
257 :    
258 :     // build the program
259 : jhr 1327 const char *options = "-I " DIDEROT_INCLUDE_PATH;
260 : jhr 1291 sts = clBuildProgram (prog, 1, &dev, options, 0, 0);
261 :     if (sts != CL_SUCCESS) {
262 :     size_t logSzb;
263 :     clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, 0, 0, &logSzb);
264 :     char *log = malloc(logSzb+1);
265 :     clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, logSzb, log, &logSzb);
266 :     log[logSzb] = '\0';
267 : jhr 1327 fprintf (stderr, "error compiling program:\n%s\n", log);
268 : jhr 1291 free (log);
269 :     return false;
270 :     }
271 :    
272 :     // extract the kernel from the program
273 : jhr 1327 cl_kernel kernel = clCreateKernel(prog, "Diderot_KernelMain", &sts);
274 : jhr 1291 if (sts != CL_SUCCESS) {
275 : jhr 1327 fprintf (stderr, "error getting kernel from program\n");
276 : jhr 1291 return false;
277 :     }
278 :    
279 :     // initialize world info
280 :     wrld->device = dev;
281 :     wrld->context = cxt;
282 :     wrld->cmdQ = q;
283 :     wrld->kernel = kernel;
284 :    
285 :     return true;
286 :     }
287 : jhr 1327
288 : jhr 1267 // this should be the part of the scheduler
289 :     void *Diderot_AllocStrand (Strand_t *strand)
290 :     {
291 :     return malloc(strand->stateSzb);
292 :     }
293 :    
294 :     // block allocation of an initial collection of strands
295 :     Diderot_World_t *Diderot_AllocInitially (
296 : jhr 1287 const char *name, // the name of the program
297 : jhr 1267 Strand_t *strand, // the type of strands being allocated
298 :     bool isArray, // is the initialization an array or collection?
299 :     uint32_t nDims, // depth of iteration nesting
300 :     int32_t *base, // nDims array of base indices
301 :     uint32_t *size) // nDims array of iteration sizes
302 :     {
303 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
304 :     if (wrld == 0) {
305 :     fprintf (stderr, "unable to allocate world\n");
306 :     exit (1);
307 :     }
308 :    
309 : jhr 1287 wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
310 : jhr 1267 wrld->isArray = isArray;
311 :     wrld->nDims = nDims;
312 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
313 :     wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
314 :     size_t numStrands = 1;
315 :     for (int i = 0; i < wrld->nDims; i++) {
316 :     numStrands *= size[i];
317 :     wrld->base[i] = base[i];
318 :     wrld->size[i] = size[i];
319 :     }
320 :    
321 :     if (VerboseFlg) {
322 :     printf("AllocInitially: %d", size[0]);
323 :     for (int i = 1; i < nDims; i++) printf(" x %d", size[i]);
324 :     printf("\n");
325 :     }
326 :    
327 :     // allocate the strand state pointers
328 :     wrld->numStrands = numStrands;
329 : jhr 1327 wrld->strandSize = sizeof(strand) * numStrands;
330 : jhr 1267 wrld->inState = (void **) malloc (numStrands * sizeof(void *));
331 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
332 :     wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
333 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
334 :     fprintf (stderr, "unable to allocate strand states\n");
335 :     exit (1);
336 :     }
337 :    
338 :     // initialize strand state pointers etc.
339 :     for (size_t i = 0; i < numStrands; i++) {
340 :     wrld->inState[i] = Diderot_AllocStrand (strand);
341 :     wrld->outState[i] = Diderot_AllocStrand (strand);
342 :     wrld->status[i] = DIDEROT_ACTIVE;
343 :     }
344 :    
345 :     return wrld;
346 :    
347 :     }
348 :    
349 :     // get strand state pointers
350 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
351 :     {
352 :     assert (i < wrld->numStrands);
353 :     return wrld->inState[i];
354 :     }
355 :    
356 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
357 :     {
358 :     assert (i < wrld->numStrands);
359 :     return wrld->outState[i];
360 :     }
361 :    
362 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
363 :     {
364 :     assert (i < wrld->numStrands);
365 :     return !wrld->status[i];
366 : jhr 1287 }

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