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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1290, Tue Jun 7 16:22:05 2011 UTC revision 1291, Tue Jun 7 17:33:50 2011 UTC
# Line 30  Line 30 
30      void                **inState;      void                **inState;
31      void                **outState;      void                **outState;
32      uint8_t             *status;        // array of strand status flags      uint8_t             *status;        // array of strand status flags
33        cl_device_id        device;         // OpenCL device
34        cl_context          context;        // OpenCL execution context
35        cl_command_queue    cmdQ;           // OpenCL command queue
36        cl_kernel           kernel;         // OpenCL Kernel that implements the program
37  };  };
38    
39  extern void Diderot_GPU_Init (cl_device_id device);  static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld);
40    
41    extern void Diderot_GPU_Init (cl_device_id device); // obsolete
42    
43  int main (int argc, const char **argv)  int main (int argc, const char **argv)
44  {  {
# Line 54  Line 60 
60      if (VerboseFlg) printf("initializing globals ...\n");      if (VerboseFlg) printf("initializing globals ...\n");
61    
62    /* Globals are loaded from the openCL code */    /* Globals are loaded from the openCL code */
63    //  Diderot_InitGlobals();      Diderot_InitGlobals();
64    
65    /***** FIXME: OpenCL specific stuff goes here.  Things to do:    /***** FIXME: OpenCL specific stuff goes here.  Things to do:
66     **     **
# Line 64  Line 70 
70     ** 4) load results from GPU     ** 4) load results from GPU
71     **/     **/
72    
   /* FIXME: For now we'll just use the very first platform */  
   printf("name:%s\n",clInfo->platforms[0].devices[0].name);  
   Diderot_GPU_Init(clInfo->platforms[0].devices[0].id);  
   
73      Diderot_World_t *wrld = Diderot_Initially ();  // this may not be right for OpenCL      Diderot_World_t *wrld = Diderot_Initially ();  // this may not be right for OpenCL
74        if (! InitCL(clInfo, wrld))
75            exit (1);
76    
77        Diderot_GPU_Init(clInfo->platforms[0].devices[0].id);  // obsolete
78    
79      double t0 = GetTime();      double t0 = GetTime();
80    
81  /* SOMETHING */  /* SOMETHING */
# Line 102  Line 109 
109    
110  }  }
111    
 /*! \brief build an OpenCL program from source.  
  */  
 static cl_program BuildProgram (cl_device_id dev, cl_context ctx, const char *src)  
 {  
     cl_device_id        devs[1] = { dev };  
     const char          *options = "-I " DIDEROT_INCLUDE_PATH;  
     cl_int              sts;  
   
     cl_program prog = clCreateProgramWithSource(ctx, 1, &src, NULL, &sts);  
     if (sts != CL_SUCCESS) {  
         fprintf (stderr, "error creating program\n");  
         exit (1);  
     }  
   
     sts = clBuildProgram (prog, 1, devs, options, 0, 0);  
     if (sts != CL_SUCCESS) {  
         fprintf (stderr, "error compiling program\n");  
         exit (1);  
     }  
   
     return prog;  
 }  
   
112  /*! \brief load OpenCL code from a file  /*! \brief load OpenCL code from a file
113   */   */
114  char *Diderot_LoadSource (const char *filename)  static char *LoadSource (const char *filename)
115  {  {
116      struct stat statbuf;      struct stat statbuf;
117      if (stat(filename, &statbuf) < 0) {      if (stat(filename, &statbuf) < 0) {
# Line 153  Line 137 
137      return source;      return source;
138  }  }
139    
140    /*! \brief build an OpenCL program from source.
141     */
142    static bool InitCL (CLInfo_t *clInfo, Diderot_World_t *wrld)
143    {
144        cl_int              sts;
145    
146      // find a GPU on platform[0]
147        cl_device_id dev;
148        int i;
149        for (i = 0;  i < clInfo->platforms[0].numDevices;  i++) {
150            if (clInfo->platforms[0].devices[i].ty == CL_DEVICE_TYPE_GPU) {
151                dev = clInfo->platforms[0].devices[i].id;
152                break;
153            }
154        }
155        if (i == clInfo->platforms[0].numDevices) {
156            fprintf (stderr, "unable to find GPU device\n");
157            return false;
158        }
159    
160      // create the context
161        cl_context cxt = clCreateContext(0, 1, &dev, 0, 0, &sts);
162        if (sts != CL_SUCCESS) {
163            fprintf (stderr, "error creating OpenCL context\n");
164            return false;
165        }
166    
167      // create the command queue
168        cl_command_queue q = clCreateCommandQueue(cxt, dev, 0, &sts);
169        if (sts != CL_SUCCESS) {
170            fprintf (stderr, "error creating OpenCL command queue\n");
171            return false;
172        }
173    
174      // create the program from the source
175        int fnameLen = strlen(wrld->name) + 4;  // name + ".cl\0"
176        char *fname = (char *)malloc(fnameLen);
177        snprintf(fname, fnameLen, "%s.cl", wrld->name);
178        const char *src = LoadSource (fname);
179        free (fname);
180        cl_program prog = clCreateProgramWithSource(cxt, 1, &src, NULL, &sts);
181        if (sts != CL_SUCCESS) {
182            fprintf (stderr, "error creating program\n");
183            return false;
184        }
185        free ((void *)src);
186    
187      // build the program
188        const char          *options = "-I " DIDEROT_INCLUDE_PATH;
189        sts = clBuildProgram (prog, 1, &dev, options, 0, 0);
190        if (sts != CL_SUCCESS) {
191            size_t logSzb;
192            clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, 0, 0, &logSzb);
193            char *log = malloc(logSzb+1);
194            clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, logSzb, log, &logSzb);
195            log[logSzb] = '\0';
196            fprintf (stderr, "error compiling program:\n%s\n", log);
197            free (log);
198            return false;
199        }
200    
201      // extract the kernel from the program
202        cl_kernel kernel = clCreateKernel(prog, "Diderot_main", &sts);
203        if (sts != CL_SUCCESS) {
204            fprintf (stderr, "error getting kernel from program\n");
205            return false;
206        }
207    
208      // initialize world info
209        wrld->device = dev;
210        wrld->context = cxt;
211        wrld->cmdQ = q;
212        wrld->kernel = kernel;
213    
214        return true;
215    }
216    
217  // this should be the part of the scheduler  // this should be the part of the scheduler
218  void *Diderot_AllocStrand (Strand_t *strand)  void *Diderot_AllocStrand (Strand_t *strand)
219  {  {

Legend:
Removed from v.1290  
changed lines
  Added in v.1291

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