aboutsummaryrefslogtreecommitdiff
path: root/src/cl-utils.c
blob: b3ce0de0a22afc74c62f21e3a63f7eb8c85e8ab1 (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
/*
 * cl-utils.c
 *
 * OpenCL utility functions
 *
 * Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2010-2014 Thomas White <taw@physics.org>
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include <CL/cl.h>

#include "utils.h"


const char *clError(cl_int err)
{
	switch ( err ) {

		case CL_SUCCESS :
		return "no error";

		case CL_DEVICE_NOT_AVAILABLE :
		return "device not available";

		case CL_DEVICE_NOT_FOUND :
		return "device not found";

		case CL_INVALID_DEVICE_TYPE :
		return "invalid device type";

		case CL_INVALID_PLATFORM :
		return "invalid platform";

		case CL_INVALID_KERNEL :
		return "invalid kernel";

		case CL_INVALID_ARG_INDEX :
		return "invalid argument index";

		case CL_INVALID_ARG_VALUE :
		return "invalid argument value";

		case CL_INVALID_MEM_OBJECT :
		return "invalid memory object";

		case CL_INVALID_SAMPLER :
		return "invalid sampler";

		case CL_INVALID_ARG_SIZE :
		return "invalid argument size";

		case CL_INVALID_COMMAND_QUEUE :
		return "invalid command queue";

		case CL_INVALID_CONTEXT :
		return "invalid context";

		case CL_INVALID_VALUE :
		return "invalid value";

		case CL_INVALID_EVENT_WAIT_LIST :
		return "invalid wait list";

		case CL_MAP_FAILURE :
		return "map failure";

		case CL_MEM_OBJECT_ALLOCATION_FAILURE :
		return "object allocation failure";

		case CL_OUT_OF_HOST_MEMORY :
		return "out of host memory";

		case CL_OUT_OF_RESOURCES :
		return "out of resources";

		case CL_INVALID_KERNEL_NAME :
		return "invalid kernel name";

		case CL_INVALID_KERNEL_ARGS :
		return "invalid kernel arguments";

		case CL_INVALID_WORK_GROUP_SIZE :
		return "invalid work group size";

		case CL_IMAGE_FORMAT_NOT_SUPPORTED :
		return "image format not supported";

		case CL_INVALID_WORK_DIMENSION :
		return "invalid work dimension";

		default :
		return "unknown error";
	}
}


static char *get_device_string(cl_device_id dev, cl_device_info info)
{
	int r;
	size_t size;
	char *val;

	r = clGetDeviceInfo(dev, info, 0, NULL, &size);
	if ( r != CL_SUCCESS ) {
		ERROR("Couldn't get device vendor size: %s\n",
		      clError(r));
		return NULL;
	}
	val = malloc(size);
	r = clGetDeviceInfo(dev, info, size, val, NULL);
	if ( r != CL_SUCCESS ) {
		ERROR("Couldn't get dev vendor: %s\n", clError(r));
		return NULL;
	}

	return val;
}


cl_device_id get_cl_dev(cl_context ctx, int n)
{
	cl_device_id *dev;
	cl_int r;
	size_t size;
	int i, num_devs;

	/* Get the required size of the array */
	r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, NULL, &size);
	if ( r != CL_SUCCESS ) {
		ERROR("Couldn't get array size for devices: %s\n", clError(r));
		return 0;
	}

	dev = malloc(size);
	r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, size, dev, NULL);
	if ( r != CL_SUCCESS ) {
		ERROR("Couldn't get device: %s\n", clError(r));
		return 0;
	}
	num_devs = size/sizeof(cl_device_id);

	if ( n >= num_devs ) {
		ERROR("Device ID out of range\n");
		return 0;
	}

	if ( n < 0 ) {

		STATUS("Available devices:\n");
		for ( i=0; i<num_devs; i++ ) {

			char *vendor;
			char *name;

			vendor = get_device_string(dev[i], CL_DEVICE_VENDOR);
			name = get_device_string(dev[i], CL_DEVICE_NAME);

			STATUS("Device %i: %s %s\n", i, vendor, name);

		}
		n = 0;

		STATUS("Using device 0.  Use --gpu-dev to choose another.\n");

	} else {

		char *vendor;
		char *name;

		vendor = get_device_string(dev[n], CL_DEVICE_VENDOR);
		name = get_device_string(dev[n], CL_DEVICE_NAME);

		STATUS("Using device %i: %s %s\n", n, vendor, name);

	}

	return dev[n];
}


static void show_build_log(cl_program prog, cl_device_id dev)
{
	cl_int r;
	char log[4096];
	size_t s;

	r = clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, 4096, log,
	                          &s);

	STATUS("Status: %i\n", r);
	STATUS("%s\n", log);
}


cl_program load_program(const char *filename, cl_context ctx,
                        cl_device_id dev, cl_int *err, const char *extra_cflags,
                        const char *insert_stuff)
{
	FILE *fh;
	cl_program prog;
	char *source;
	size_t len;
	cl_int r;
	char cflags[1024] = "";
	char *insert_pos;
	size_t il;

	fh = fopen(filename, "r");
	if ( fh == NULL ) {
		ERROR("Couldn't open '%s'\n", filename);
		*err = CL_INVALID_PROGRAM;
		return 0;
	}
	source = malloc(16384);
	if ( source == NULL ) return 0;
	len = fread(source, 1, 16383, fh);
	fclose(fh);
	source[len] = '\0';

	if ( insert_stuff != NULL ) {
		insert_pos = strstr(source, "INSERT_HERE");

		if ( insert_pos != NULL ) {

			char *source2;
			source2 = malloc(strlen(source)+strlen(insert_stuff)+1);
			if ( source2 == NULL ) return 0;

			il = insert_pos - source;
			memcpy(source2, source, il);
			memcpy(source2+il, insert_stuff, strlen(insert_stuff)+1);
			memcpy(source2+il+strlen(insert_stuff),
			       source+il+11, strlen(source+il+11)+1);
			free(source);
			source = source2;

		}
	}

	prog = clCreateProgramWithSource(ctx, 1, (const char **)&source,
	                                 NULL, err);
	if ( *err != CL_SUCCESS ) {
		ERROR("Couldn't load source\n");
		return 0;
	}

	cflags[0] = '\0';
	strncat(cflags, "-I"DATADIR"/crystfel/ ", 1023-strlen(cflags));
	strncat(cflags, "-cl-no-signed-zeros ", 1023-strlen(cflags));
	strncat(cflags, extra_cflags, 1023-strlen(cflags));

	r = clBuildProgram(prog, 0, NULL, cflags, NULL, NULL);
	if ( r != CL_SUCCESS ) {
		ERROR("Couldn't build program '%s'\n", filename);
		show_build_log(prog, dev);
		*err = r;
		return 0;
	}

	free(source);
	*err = CL_SUCCESS;
	return prog;
}