summaryrefslogtreecommitdiff
path: root/cl_simple.c
blob: 1dc565eb7f64e5e8f898e37a7303c9d8fb23aa39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <CL/cl.h>

#include <fcntl.h>
#include <stdio.h>
#include <string.h>

#include "cl_simple.h"
#include "cl_util.h"

/*============================================================================*/
/*========== DIRECT WRAPPERS =================================================*/
/*============================================================================*/

unsigned clSimpleCreateContext(cl_context * context, cl_device_id device_id)
{
   cl_int error;
   *context = clCreateContext(NULL, /* Properties */
                           1, /* Number of devices */
                           &device_id, /* Device pointer */
                           NULL, /* Callback for reporting errors */
                           NULL, /* User data to pass to error callback */
                           &error); /* Error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateContext() failed: %s\n",
                      clUtilErrorString(error));
      return 0;
   }
   return 1;
}

unsigned clSimpleCreateCommandQueue(cl_command_queue * command_queue,
                               cl_context context, cl_device_id device_id)
{
   cl_int error;
   *command_queue = clCreateCommandQueue(context,
                                        device_id,
                                        0, /* Command queue properties */
                                        &error); /* Error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateCommandQueue() failed: %s\n",
                      clUtilErrorString(error));
      return 0;
   }
   return 1;
}

unsigned clSimpleKernelSetArg(cl_kernel kernel, cl_uint index, size_t size,
                         const void * value)
{
   cl_int error;

   error = clSetKernelArg(kernel, index, size, value);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clSetKernelArg failed: %s\n", clUtilErrorString(error));
      return 0;
   }

   fprintf(stderr, "clSetKernelArg() succeeded.\n");

   return 1;
}

unsigned clSimpleCreateBuffer(cl_mem * buffer, cl_context context,
                              cl_mem_flags flags, size_t size)
{
   cl_int error;

   *buffer = clCreateBuffer(context,
                            flags, /* Flags */
                            size, /* Size of buffer */
                            NULL, /* Pointer to the data */
                            &error); /* error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateBuffer(%p, %p, %u, %u) failed: %s\n",
                       buffer, context, flags, size,
                       clUtilErrorString(error));
      return 0;
   }

   return 1;
}

unsigned clSimpleEnqueueWriteBuffer(cl_command_queue command_queue,
      cl_mem buffer, size_t buffer_size, void * ptr)
{

   cl_int error = clEnqueueWriteBuffer(command_queue,
                                       buffer,
                                       CL_TRUE, /* Blocking write */
                                       0, /* Offset into buffer */
                                       buffer_size,
                                       ptr,
                                       0, /* Events in waiting list */
                                       NULL, /* Wait list */
                                       NULL); /* Event */
   if (error != CL_SUCCESS) {
      return 0;
   }

   return 1;
}

unsigned clSimpleEnqueueNDRangeKernel(cl_command_queue command_queue,
      cl_kernel kernel, cl_uint work_dim, const size_t * global_work_size,
      const size_t * local_work_size)
{
   cl_int error = clEnqueueNDRangeKernel(command_queue,
                                  kernel,
                                  work_dim, /* Number of dimensions */
                                  NULL, /* Global work offset */
                                  global_work_size,
                                  local_work_size, /* local work size */
                                  0, /* Events in wait list */
                                  NULL, /* Wait list */
                                  NULL); /* Event object for this event */
   if (error != CL_SUCCESS) {
      fprintf(stderr, "clEnqueueNDRangeKernel() failed: %s\n",
                      clUtilErrorString(error));
      return 0;
   }

   error = clFinish(command_queue);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clFinish() failed: %s\n", clUtilErrorString(error));
      return 0;
   }

   return 1;
}

/*============================================================================*/
/*========== CONVENIENCE WRAPPERS=============================================*/
/*============================================================================*/

unsigned clSimpleInitGpuDevice(cl_device_id * device_id)
{
   cl_int error;

   cl_uint total_platforms;
   cl_platform_id platform_id;

   cl_uint total_gpu_devices;

   error = clGetPlatformIDs(
                           1, /* Max number of platform IDs to return */
                           &platform_id, /* Pointer to platform_id */
                           &total_platforms); /* Total number of platforms
                                               * found on the system */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clGetPlatformIDs() failed: %s\n", clUtilErrorString(error));
      return 0;
   }

   fprintf(stderr, "There are %u platforms.\n", total_platforms);

   error = clGetDeviceIDs(platform_id,
                          CL_DEVICE_TYPE_GPU,
                          1,
                          device_id,
                          &total_gpu_devices);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clGetDeviceIDs() failed: %s\n", clUtilErrorString(error));
      return 0;
   }

   fprintf(stderr, "There are %u GPU devices.\n", total_gpu_devices);

   return 1;
}

#define CODE_CHUNK 64

unsigned clSimpleCreateKernel(cl_context context, cl_device_id device_id,
                              cl_kernel * kernel, const char * kernel_name)
{
   char * filename;
   char * code = NULL;
   size_t code_len = 0;
   int fd;
   int bytes_read;

   /* +3 .cl
    * +1 NULL byte
    * --
    * +4
    */
   unsigned filename_len = strlen(kernel_name) + 4;

   /* Determine file name */
   filename = malloc (filename_len + 4);
   if (!filename) {
      fprintf(stderr, "Failed to malloc filename.\n");
      return 0;
   }

   snprintf(filename, filename_len, "%s.cl", kernel_name);

   /* Open file */
   fd = open(filename, O_RDONLY);
   if (fd < 0) {
      fprintf(stderr, "Failed to open file: %s\n", filename);
      return 0;
   }

   /* Read code */
   do {
      code = realloc(code, (code_len + CODE_CHUNK) * sizeof(unsigned char));
      if (!code) {
         fprintf(stderr, "Failed to realloc code.\n");
         return 0;
      }

      bytes_read = read(fd, code + code_len, CODE_CHUNK);
      if (bytes_read < 0) {
         fprintf(stderr, "Failed to read code.\n");
         return 0;
      }
      code_len += bytes_read;
   } while(bytes_read == CODE_CHUNK);

   return clSimpleCreateKernelString(context, device_id, kernel, kernel_name,
                                     code, code_len);
}

unsigned clSimpleCreateKernelString(cl_context context, cl_device_id device_id,
                                    cl_kernel * kernel, const char * kernel_name,
                                    const char * code, size_t code_len)
{
   cl_int error;
   cl_program program;

   /* Create program */
   program = clCreateProgramWithSource(context,
                                       1, /* Number of strings */
                                       &code,
                                       &code_len, /* String lengths, 0 means all the
                                              * strings are NULL terminated. */
                                       &error);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateProgramWithSource() failed: %s\n",
                      clUtilErrorString(error));
      return 0;
   }

   fprintf(stderr, "clCreateProgramWithSource() suceeded.\n");

   /* Build program */
   error = clBuildProgram(program,
                          1, /* Number of devices */
                          &device_id,
                          NULL, /* options */
                          NULL, /* callback function when compile is complete */
                          NULL); /* user data for callback */


   if (error != CL_SUCCESS) {
      char build_str[10000];
      error = clGetProgramBuildInfo(program,
                                    device_id,
                                    CL_PROGRAM_BUILD_LOG,
                                    10000, /* Size of output string */
                                    build_str, /* pointer to write the log to */
                                    NULL); /* Number of bytes written to the log */
      if (error != CL_SUCCESS) {
         fprintf(stderr, "clGetProgramBuildInfo() failed: %s\n",
                          clUtilErrorString(error));
      } else {
         fprintf(stderr, "Build Log: \n%s\n\n", build_str);
      }
      return 0;
   }

   fprintf(stderr, "clBuildProgram() suceeded.\n");

   *kernel = clCreateKernel(program, kernel_name, &error);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateKernel() failed: %s\n", clUtilErrorString(error));
      return 0;
   }

   fprintf(stderr, "clCreateKernel() suceeded.\n");

   return 1;
}

unsigned clSimpleSimpleInit(struct cl_simple_context * context, const char * kernel_name)
{
   if (!clSimpleInitGpuDevice(&context->device_id)) {
      return 0;
   }

   if (!clSimpleCreateContext(&context->cl_ctx, context->device_id)) {
      return 0;
   }

   if (!clSimpleCreateCommandQueue(&context->command_queue, context->cl_ctx,
                                                      context->device_id)) {
      return 0;
   }

   if (!clSimpleCreateKernel(context->cl_ctx, context->device_id, &context->kernel,
                                                           kernel_name)) {
      return 0;
   }

   return 1;
}

unsigned clSimpleSetOutputBuffer(struct cl_simple_context * context,
                               unsigned buffer_size)
{
   cl_int error;

   if (!clSimpleCreateBuffer(&context->out_buffer, context->cl_ctx,
                             CL_MEM_WRITE_ONLY, buffer_size)) {
      return 0;
   }

   if (!clSimpleKernelSetArg(context->kernel, 0, sizeof(cl_mem),
                                               &context->out_buffer)) {
      return 0;
   }

   return 1;
}

unsigned clSimpleReadOutput(struct cl_simple_context * context, void * data,
      size_t data_bytes)
{
   cl_int error = clEnqueueReadBuffer(context->command_queue,
                                      context->out_buffer,
                                      CL_TRUE, /* TRUE means it is a blocking read. */
                                      0, /* Buffer offset to read from. */
                                      data_bytes, /* Bytes to read */
                                      data, /* Pointer to store the data */
                                      0, /* Events in wait list */
                                      NULL, /* Wait list */
                                      NULL); /* Event object */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clEnqueueReadBuffer() failed: %s\n",
                      clUtilErrorString(error));
      return 0;
   }

   return 1;
}