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 1288 - (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 :     void **inState;
31 :     void **outState;
32 :     uint8_t *status; // array of strand status flags
33 :     };
34 :    
35 : lamonts 1271 extern void Diderot_GPU_Init (cl_device_id device);
36 : jhr 1267
37 :     int main (int argc, const char **argv)
38 :     {
39 :     // get information about OpenCL support
40 :     CLInfo_t *clInfo = GetCLInfo();
41 :     if (clInfo == 0) {
42 :     fprintf (stderr, "no OpenCL support\n");
43 :     exit (1);
44 :     }
45 :    
46 :     Diderot_Options_t *opts = Diderot_OptNew ();
47 :    
48 :     Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
49 :     Diderot_RegisterGlobalOpts (opts);
50 :     Diderot_OptProcess (opts, argc, argv);
51 :     Diderot_OptFree (opts);
52 :    
53 :     // run the generated global initialization code
54 : jhr 1275 if (VerboseFlg) printf("initializing globals ...\n");
55 : lamonts 1271
56 : jhr 1275 /* Globals are loaded from the openCL code */
57 : lamonts 1271 // Diderot_InitGlobals();
58 : jhr 1267
59 :     /***** FIXME: OpenCL specific stuff goes here. Things to do:
60 :     **
61 :     ** 1) copy data to GPU
62 :     ** 2) initialize strands
63 :     ** 3) run strands to termination
64 :     ** 4) load results from GPU
65 :     **/
66 : lamonts 1271
67 :     /* FIXME: For now we'll just use the very first platform */
68 :     printf("name:%s\n",clInfo->platforms[0].devices[0].name);
69 :     Diderot_GPU_Init(clInfo->platforms[0].devices[0].id);
70 : jhr 1282
71 : jhr 1287 Diderot_World_t *wrld = Diderot_Initially (); // this may not be right for OpenCL
72 :     double t0 = GetTime();
73 : jhr 1267
74 : jhr 1287 /* SOMETHING */
75 :    
76 :     double totalTime = GetTime() - t0;
77 :    
78 :     if (VerboseFlg)
79 :     fprintf (stderr, "done in %f seconds\n", totalTime);
80 :     else if (TimingFlg)
81 :     printf ("usr=%f\n", totalTime);
82 :    
83 : jhr 1267 // here we have the final state of all of the strands in the "in" buffer
84 : jhr 1287 int outFileNameLen = strlen(wrld->name) + 5;
85 :     char *outFileName = (char *)malloc(outFileNameLen);
86 :     snprintf (outFileName, outFileNameLen, "%s.txt", wrld->name);
87 :     FILE *outS = fopen(outFileName, "w");
88 : jhr 1267 if (outS == NULL) {
89 : jhr 1287 fprintf (stderr, "Cannot open output file %s\n", outFileName);
90 : jhr 1267 exit (8);
91 : jhr 1287 }
92 :    
93 :     for (int i = 0; i < wrld->numStrands; i++) {
94 :     if (wrld->status[i] == DIDEROT_STABLE)
95 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
96 :     }
97 :     fclose (outS);
98 :    
99 :     Diderot_Shutdown (wrld);
100 :    
101 :     return 0;
102 :    
103 : lamonts 1271 }
104 : jhr 1282
105 : jhr 1288 /*! \brief build an OpenCL program from source.
106 :     */
107 :     static cl_program BuildProgram (cl_device_id dev, cl_context ctx, const char *src)
108 :     {
109 :     cl_device_id devs[1] = { dev };
110 :     const char *options = "-I " DIDEROT_INCLUDE_PATH;
111 :     cl_int sts;
112 :    
113 :     cl_program prog = clCreateProgramWithSource(ctx, 1, &src, NULL, &sts);
114 :     if (sts != CL_SUCCESS) {
115 :     fprintf (stderr, "error creating program\n");
116 :     exit (1);
117 :     }
118 :    
119 :     sts = clBuildProgram (prog, 1, devs, options, 0, 0);
120 :     if (sts != CL_SUCCESS) {
121 :     fprintf (stderr, "error compiling program\n");
122 :     exit (1);
123 :     }
124 :    
125 :     return prog;
126 :     }
127 :    
128 : jhr 1287 /*! \brief load OpenCL code from a file
129 :     */
130 :     char *Diderot_LoadSource (const char *filename)
131 : lamonts 1271 {
132 :     struct stat statbuf;
133 : jhr 1287 if (stat(filename, &statbuf) < 0) {
134 :     fprintf (stderr, "unable to stat OpenCL source file %s\n", filename);
135 :     exit (1);
136 :     }
137 :    
138 :     char *source = (char *) malloc(statbuf.st_size + 1);
139 :     if (source == 0) {
140 :     fprintf (stderr, "unable to allocate memory for OpenCL source\n");
141 :     exit (1);
142 :     }
143 :    
144 :     FILE *fh = fopen(filename, "r");
145 :     if ((fh == 0)
146 :     || (fread(source, statbuf.st_size, 1, fh) != 1)) {
147 :     fprintf (stderr, "unable to read OpenCL source from %s\n", filename);
148 :     exit (1);
149 :     }
150 :     source[statbuf.st_size] = '\0';
151 :     fclose (fh);
152 : lamonts 1271
153 :     return source;
154 :     }
155 : jhr 1267
156 :     // this should be the part of the scheduler
157 :     void *Diderot_AllocStrand (Strand_t *strand)
158 :     {
159 :     return malloc(strand->stateSzb);
160 :     }
161 :    
162 :     // block allocation of an initial collection of strands
163 :     Diderot_World_t *Diderot_AllocInitially (
164 : jhr 1287 const char *name, // the name of the program
165 : jhr 1267 Strand_t *strand, // the type of strands being allocated
166 :     bool isArray, // is the initialization an array or collection?
167 :     uint32_t nDims, // depth of iteration nesting
168 :     int32_t *base, // nDims array of base indices
169 :     uint32_t *size) // nDims array of iteration sizes
170 :     {
171 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
172 :     if (wrld == 0) {
173 :     fprintf (stderr, "unable to allocate world\n");
174 :     exit (1);
175 :     }
176 :    
177 : jhr 1287 wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
178 : jhr 1267 wrld->isArray = isArray;
179 :     wrld->nDims = nDims;
180 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
181 :     wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
182 :     size_t numStrands = 1;
183 :     for (int i = 0; i < wrld->nDims; i++) {
184 :     numStrands *= size[i];
185 :     wrld->base[i] = base[i];
186 :     wrld->size[i] = size[i];
187 :     }
188 :    
189 :     if (VerboseFlg) {
190 :     printf("AllocInitially: %d", size[0]);
191 :     for (int i = 1; i < nDims; i++) printf(" x %d", size[i]);
192 :     printf("\n");
193 :     }
194 :    
195 :     // allocate the strand state pointers
196 :     wrld->numStrands = numStrands;
197 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
198 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
199 :     wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
200 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
201 :     fprintf (stderr, "unable to allocate strand states\n");
202 :     exit (1);
203 :     }
204 :    
205 :     // initialize strand state pointers etc.
206 :     for (size_t i = 0; i < numStrands; i++) {
207 :     wrld->inState[i] = Diderot_AllocStrand (strand);
208 :     wrld->outState[i] = Diderot_AllocStrand (strand);
209 :     wrld->status[i] = DIDEROT_ACTIVE;
210 :     }
211 :    
212 :     return wrld;
213 :    
214 :     }
215 :    
216 :     // get strand state pointers
217 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
218 :     {
219 :     assert (i < wrld->numStrands);
220 :     return wrld->inState[i];
221 :     }
222 :    
223 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
224 :     {
225 :     assert (i < wrld->numStrands);
226 :     return wrld->outState[i];
227 :     }
228 :    
229 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
230 :     {
231 :     assert (i < wrld->numStrands);
232 :     return !wrld->status[i];
233 : jhr 1287 }

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