-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl-helper.c
302 lines (231 loc) · 10 KB
/
cl-helper.c
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
/*
* Copyright (c) 2010 Andreas Kloeckner
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "cl-helper.h"
#include <string.h>
const char *cl_error_to_str(cl_int e)
{
switch (e)
{
case CL_SUCCESS: return "success";
case CL_DEVICE_NOT_FOUND: return "device not found";
case CL_DEVICE_NOT_AVAILABLE: return "device not available";
#if !(defined(CL_PLATFORM_NVIDIA) && CL_PLATFORM_NVIDIA == 0x3001)
case CL_COMPILER_NOT_AVAILABLE: return "device compiler not available";
#endif
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "mem object allocation failure";
case CL_OUT_OF_RESOURCES: return "out of resources";
case CL_OUT_OF_HOST_MEMORY: return "out of host memory";
case CL_PROFILING_INFO_NOT_AVAILABLE: return "profiling info not available";
case CL_MEM_COPY_OVERLAP: return "mem copy overlap";
case CL_IMAGE_FORMAT_MISMATCH: return "image format mismatch";
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "image format not supported";
case CL_BUILD_PROGRAM_FAILURE: return "build program failure";
case CL_MAP_FAILURE: return "map failure";
case CL_INVALID_VALUE: return "invalid value";
case CL_INVALID_DEVICE_TYPE: return "invalid device type";
case CL_INVALID_PLATFORM: return "invalid platform";
case CL_INVALID_DEVICE: return "invalid device";
case CL_INVALID_CONTEXT: return "invalid context";
case CL_INVALID_QUEUE_PROPERTIES: return "invalid queue properties";
case CL_INVALID_COMMAND_QUEUE: return "invalid command queue";
case CL_INVALID_HOST_PTR: return "invalid host ptr";
case CL_INVALID_MEM_OBJECT: return "invalid mem object";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "invalid image format descriptor";
case CL_INVALID_IMAGE_SIZE: return "invalid image size";
case CL_INVALID_SAMPLER: return "invalid sampler";
case CL_INVALID_BINARY: return "invalid binary";
case CL_INVALID_BUILD_OPTIONS: return "invalid build options";
case CL_INVALID_PROGRAM: return "invalid program";
case CL_INVALID_PROGRAM_EXECUTABLE: return "invalid program executable";
case CL_INVALID_KERNEL_NAME: return "invalid kernel name";
case CL_INVALID_KERNEL_DEFINITION: return "invalid kernel definition";
case CL_INVALID_KERNEL: return "invalid kernel";
case CL_INVALID_ARG_INDEX: return "invalid arg index";
case CL_INVALID_ARG_VALUE: return "invalid arg value";
case CL_INVALID_ARG_SIZE: return "invalid arg size";
case CL_INVALID_KERNEL_ARGS: return "invalid kernel args";
case CL_INVALID_WORK_DIMENSION: return "invalid work dimension";
case CL_INVALID_WORK_GROUP_SIZE: return "invalid work group size";
case CL_INVALID_WORK_ITEM_SIZE: return "invalid work item size";
case CL_INVALID_GLOBAL_OFFSET: return "invalid global offset";
case CL_INVALID_EVENT_WAIT_LIST: return "invalid event wait list";
case CL_INVALID_EVENT: return "invalid event";
case CL_INVALID_OPERATION: return "invalid operation";
case CL_INVALID_GL_OBJECT: return "invalid gl object";
case CL_INVALID_BUFFER_SIZE: return "invalid buffer size";
case CL_INVALID_MIP_LEVEL: return "invalid mip level";
#if defined(cl_khr_gl_sharing) && (cl_khr_gl_sharing >= 1)
case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR: return "invalid gl sharegroup reference number";
#endif
#ifdef CL_VERSION_1_1
case CL_MISALIGNED_SUB_BUFFER_OFFSET: return "misaligned sub-buffer offset";
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST: return "exec status error for events in wait list";
case CL_INVALID_GLOBAL_WORK_SIZE: return "invalid global work size";
#endif
default: return "invalid/unknown error code";
}
}
void print_platforms_devices()
{
cl_uint plat_count;
CALL_CL_GUARDED(clGetPlatformIDs, (0, NULL, &plat_count));
cl_platform_id *platforms =
(cl_platform_id *) malloc(plat_count*sizeof(cl_platform_id));
CHECK_SYS_ERROR(!platforms, "allocating platform array");
CALL_CL_GUARDED(clGetPlatformIDs, (plat_count, platforms, NULL));
for (cl_uint i = 0; i < plat_count; ++i)
{
char buf[100];
CALL_CL_GUARDED(clGetPlatformInfo, (platforms[i], CL_PLATFORM_VENDOR,
sizeof(buf), buf, NULL));
printf("plat %d: vendor '%s'\n", i, buf);
cl_uint dev_count;
CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
0, NULL, &dev_count));
cl_device_id *devices =
(cl_device_id *) malloc(dev_count*sizeof(cl_device_id));
CHECK_SYS_ERROR(!devices, "allocating device array");
CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
dev_count, devices, NULL));
for (cl_uint j = 0; j < dev_count; ++j)
{
char buf[100];
CALL_CL_GUARDED(clGetDeviceInfo, (devices[j], CL_DEVICE_NAME,
sizeof(buf), buf, NULL));
printf(" dev %d '%s'\n", j, buf);
}
free(devices);
}
free(platforms);
}
void create_context_on(const char *plat_name, const char*dev_name, cl_uint idx,
cl_context *ctx, cl_command_queue *queue, int enable_profiling)
{
cl_uint plat_count;
CALL_CL_GUARDED(clGetPlatformIDs, (0, NULL, &plat_count));
cl_platform_id *platforms =
(cl_platform_id *) malloc(plat_count*sizeof(cl_platform_id));
CHECK_SYS_ERROR(!platforms, "allocating platform array");
CALL_CL_GUARDED(clGetPlatformIDs, (plat_count, platforms, NULL));
for (cl_uint i = 0; i < plat_count; ++i)
{
char buf[100];
CALL_CL_GUARDED(clGetPlatformInfo, (platforms[i], CL_PLATFORM_VENDOR,
sizeof(buf), buf, NULL));
if (!plat_name || strstr(buf, plat_name))
{
cl_uint dev_count;
CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
0, NULL, &dev_count));
cl_device_id *devices =
(cl_device_id *) malloc(dev_count*sizeof(cl_device_id));
CHECK_SYS_ERROR(!devices, "allocating device array");
CALL_CL_GUARDED(clGetDeviceIDs, (platforms[i], CL_DEVICE_TYPE_ALL,
dev_count, devices, NULL));
for (cl_uint j = 0; j < dev_count; ++j)
{
char buf[100];
CALL_CL_GUARDED(clGetDeviceInfo, (devices[j], CL_DEVICE_NAME,
sizeof(buf), buf, NULL));
if (!dev_name || strstr(buf, dev_name))
{
if (idx == 0)
{
cl_platform_id plat = platforms[i];
cl_device_id dev = devices[j];
free(devices);
free(platforms);
cl_context_properties cps[3] = {
CL_CONTEXT_PLATFORM, (cl_context_properties) plat, 0 };
cl_int status;
*ctx = clCreateContext(
cps, 1, &dev, NULL, NULL, &status);
CHECK_CL_ERROR(status, "clCreateContext");
cl_command_queue_properties qprops = 0;
if (enable_profiling)
qprops |= CL_QUEUE_PROFILING_ENABLE;
*queue = clCreateCommandQueue(*ctx, dev, qprops, &status);
CHECK_CL_ERROR(status, "clCreateCommandQueue");
return;
}
else
--idx;
}
}
free(devices);
}
}
free(platforms);
fputs("create_context_on: specified device not found.\n", stderr);
abort();
}
char *read_file(const char *filename)
{
FILE *f = fopen(filename, "r");
CHECK_SYS_ERROR(!f, "read_file: opening file");
CHECK_SYS_ERROR(fseek(f, 0, SEEK_END) < 0, "read_file: seeking to end");
long size = ftell(f);
CHECK_SYS_ERROR(fseek(f, 0, SEEK_SET) != 0,
"read_file: seeking to start");
char *result = (char *) malloc(size+1);
CHECK_SYS_ERROR(!result, "read_file: allocating file contents");
CHECK_SYS_ERROR(fread(result, 1, size, f) < size,
"read_file: reading file contents");
CHECK_SYS_ERROR(fclose(f), "read_file: closing file");
result[size] = '\0';
return result;
}
cl_kernel kernel_from_string(cl_context ctx,
char const *knl, char const *knl_name, char const *options)
{
size_t sizes[] = { strlen(knl) };
cl_int status;
cl_program program = clCreateProgramWithSource(ctx, 1, &knl, sizes, &status);
CHECK_CL_ERROR(status, "clCreateProgramWithSource");
status = clBuildProgram(program, 0, NULL, options, NULL, NULL);
if (status != CL_SUCCESS)
{
// build failed, get build log.
cl_device_id dev;
CALL_CL_GUARDED(clGetProgramInfo, (program, CL_PROGRAM_DEVICES,
sizeof(dev), &dev, NULL));
size_t log_size;
CALL_CL_GUARDED(clGetProgramBuildInfo, (program, dev, CL_PROGRAM_BUILD_LOG,
0, NULL, &log_size));
char *log = malloc(log_size);
CHECK_SYS_ERROR(!log, "kernel_from_string: allocate log");
char devname[100];
CALL_CL_GUARDED(clGetDeviceInfo, (dev, CL_DEVICE_NAME,
sizeof(devname), devname, NULL));
CALL_CL_GUARDED(clGetProgramBuildInfo, (program, dev, CL_PROGRAM_BUILD_LOG,
log_size, log, NULL));
fprintf(stderr, "*** build of '%s' on '%s' failed:\n%s\n*** (end of error)\n",
knl_name, devname, log);
abort();
}
else
CHECK_CL_ERROR(status, "clBuildProgram");
cl_kernel kernel = clCreateKernel(program, knl_name, &status);
CHECK_CL_ERROR(status, "clCreateKernel");
CALL_CL_GUARDED(clReleaseProgram, (program));
return kernel;
}