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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*
* diffraction-gpu.c
*
* Calculate diffraction patterns by Fourier methods (GPU version)
*
* (c) 2006-2010 Thomas White <taw@physics.org>
*
* Part of CrystFEL - crystallography with a FEL
*
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <complex.h>
#include <cl.h>
#include "image.h"
#include "utils.h"
#include "cell.h"
#include "diffraction.h"
#include "sfac.h"
#include "cl-utils.h"
#include "beam-parameters.h"
#define SAMPLING (4)
#define BWSAMPLING (10)
#define SINC_LUT_ELEMENTS (4096)
struct gpu_context
{
cl_context ctx;
cl_command_queue cq;
cl_program prog;
cl_kernel kern;
cl_mem intensities;
cl_mem flags;
cl_mem tt;
size_t tt_size;
cl_mem diff;
size_t diff_size;
/* Array of sinc LUTs */
cl_mem *sinc_luts;
cl_float **sinc_lut_ptrs;
int max_sinc_lut; /* Number of LUTs, i.e. one greater than the maximum
* index. This equals the highest allowable "n". */
};
static void check_sinc_lut(struct gpu_context *gctx, int n)
{
cl_int err;
cl_image_format fmt;
int i;
if ( n > gctx->max_sinc_lut ) {
gctx->sinc_luts = realloc(gctx->sinc_luts,
n*sizeof(*gctx->sinc_luts));
gctx->sinc_lut_ptrs = realloc(gctx->sinc_lut_ptrs,
n*sizeof(*gctx->sinc_lut_ptrs));
for ( i=gctx->max_sinc_lut; i<n; i++ ) {
gctx->sinc_lut_ptrs[i] = NULL;
}
gctx->max_sinc_lut = n;
}
if ( gctx->sinc_lut_ptrs[n-1] != NULL ) return;
/* Create a new sinc LUT */
gctx->sinc_lut_ptrs[n-1] = malloc(SINC_LUT_ELEMENTS*sizeof(cl_float));
gctx->sinc_lut_ptrs[n-1][0] = n;
if ( n == 1 ) {
for ( i=1; i<SINC_LUT_ELEMENTS; i++ ) {
gctx->sinc_lut_ptrs[n-1][i] = 1.0;
}
} else {
for ( i=1; i<SINC_LUT_ELEMENTS; i++ ) {
double x, val;
x = (double)i/SINC_LUT_ELEMENTS;
val = fabs(sin(M_PI*n*x)/sin(M_PI*x));
gctx->sinc_lut_ptrs[n-1][i] = val;
}
}
fmt.image_channel_order = CL_INTENSITY;
fmt.image_channel_data_type = CL_FLOAT;
gctx->sinc_luts[n-1] = clCreateImage2D(gctx->ctx,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
&fmt, SINC_LUT_ELEMENTS, 1, 0,
gctx->sinc_lut_ptrs[n-1], &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't create LUT for %i\n", n);
return;
}
}
void get_diffraction_gpu(struct gpu_context *gctx, struct image *image,
int na, int nb, int nc, UnitCell *ucell)
{
cl_int err;
double ax, ay, az;
double bx, by, bz;
double cx, cy, cz;
float klow, khigh;
cl_event *event;
int p;
float *tt_ptr;
int x, y;
cl_float16 cell;
float *diff_ptr;
cl_int4 ncells;
const int sampling = SAMPLING;
cl_float bwstep;
if ( gctx == NULL ) {
ERROR("GPU setup failed.\n");
return;
}
cell_get_cartesian(ucell, &ax, &ay, &az, &bx, &by, &bz, &cx, &cy, &cz);
cell.s[0] = ax; cell.s[1] = ay; cell.s[2] = az;
cell.s[3] = bx; cell.s[4] = by; cell.s[5] = bz;
cell.s[6] = cx; cell.s[7] = cy; cell.s[8] = cz;
/* Calculate wavelength */
klow = 1.0/(image->lambda*(1.0 + image->beam->bandwidth/2.0));
khigh = 1.0/(image->lambda*(1.0 - image->beam->bandwidth/2.0));
bwstep = (khigh-klow) / BWSAMPLING;
ncells.s[0] = na;
ncells.s[1] = nb;
ncells.s[2] = nc;
ncells.s[3] = 0; /* unused */
/* Ensure all required LUTs are available */
check_sinc_lut(gctx, na);
check_sinc_lut(gctx, nb);
check_sinc_lut(gctx, nc);
err = clSetKernelArg(gctx->kern, 0, sizeof(cl_mem), &gctx->diff);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 0: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 1, sizeof(cl_mem), &gctx->tt);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 1: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 2, sizeof(cl_float), &klow);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 2: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 3, sizeof(cl_int), &image->width);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 3: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 8, sizeof(cl_float16), &cell);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 8: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 9, sizeof(cl_mem), &gctx->intensities);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 9: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 12, sizeof(cl_int), &sampling);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 12: %s\n", clError(err));
return;
}
/* Local memory for reduction */
clSetKernelArg(gctx->kern, 13,
BWSAMPLING*SAMPLING*SAMPLING*sizeof(cl_float), NULL);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 13: %s\n", clError(err));
return;
}
/* Bandwidth sampling step */
clSetKernelArg(gctx->kern, 14, sizeof(cl_float), &bwstep);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 14: %s\n", clError(err));
return;
}
/* LUT in 'a' direction */
clSetKernelArg(gctx->kern, 15, sizeof(cl_mem), &gctx->sinc_luts[na-1]);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 15: %s\n", clError(err));
return;
}
/* LUT in 'b' direction */
clSetKernelArg(gctx->kern, 16, sizeof(cl_mem), &gctx->sinc_luts[nb-1]);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 16: %s\n", clError(err));
return;
}
/* LUT in 'c' direction */
clSetKernelArg(gctx->kern, 17, sizeof(cl_mem), &gctx->sinc_luts[nc-1]);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 17: %s\n", clError(err));
return;
}
/* Flag array */
clSetKernelArg(gctx->kern, 18, sizeof(cl_mem), &gctx->flags);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set flag array: %s\n", clError(err));
return;
}
/* Iterate over panels */
event = malloc(image->det->n_panels * sizeof(cl_event));
for ( p=0; p<image->det->n_panels; p++ ) {
size_t dims[3];
size_t ldims[3] = {SAMPLING, SAMPLING, BWSAMPLING};
/* In a future version of OpenCL, this could be done
* with a global work offset. But not yet... */
dims[0] = 1+image->det->panels[0].max_x-image->det->panels[0].min_x;
dims[1] = 1+image->det->panels[0].max_y-image->det->panels[0].min_y;
dims[0] *= SAMPLING;
dims[1] *= SAMPLING;
dims[2] = BWSAMPLING;
clSetKernelArg(gctx->kern, 4, sizeof(cl_float),
&image->det->panels[p].cx);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 4: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 5, sizeof(cl_float),
&image->det->panels[p].cy);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 5: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 6, sizeof(cl_float),
&image->det->panels[p].res);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 6: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 7, sizeof(cl_float),
&image->det->panels[p].clen);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 7: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 10, sizeof(cl_int),
&image->det->panels[p].min_x);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 10: %s\n", clError(err));
return;
}
clSetKernelArg(gctx->kern, 11, sizeof(cl_int),
&image->det->panels[p].min_y);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't set arg 11: %s\n", clError(err));
return;
}
err = clEnqueueNDRangeKernel(gctx->cq, gctx->kern, 3, NULL,
dims, ldims, 0, NULL, &event[p]);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't enqueue diffraction kernel: %s\n",
clError(err));
return;
}
}
diff_ptr = clEnqueueMapBuffer(gctx->cq, gctx->diff, CL_TRUE,
CL_MAP_READ, 0, gctx->diff_size,
image->det->n_panels, event, NULL, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't map diffraction buffer: %s\n", clError(err));
return;
}
tt_ptr = clEnqueueMapBuffer(gctx->cq, gctx->tt, CL_TRUE, CL_MAP_READ, 0,
gctx->tt_size, image->det->n_panels, event,
NULL, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't map tt buffer\n");
return;
}
free(event);
image->data = calloc(image->width * image->height, sizeof(float));
image->twotheta = calloc(image->width * image->height, sizeof(double));
for ( x=0; x<image->width; x++ ) {
for ( y=0; y<image->height; y++ ) {
float val, tt;
val = diff_ptr[x + image->width*y];
if ( isinf(val) ) {
ERROR("Extracting infinity at %i,%i\n", x, y);
}
if ( val < 0.0 ) {
ERROR("Extracting negative at %i,%i\n", x, y);
}
if ( isnan(val) ) {
ERROR("Extracting NaN at %i,%i\n", x, y);
}
tt = tt_ptr[x + image->width*y];
image->data[x + image->width*y] = val;
image->twotheta[x + image->width*y] = tt;
}
}
clEnqueueUnmapMemObject(gctx->cq, gctx->diff, diff_ptr, 0, NULL, NULL);
clEnqueueUnmapMemObject(gctx->cq, gctx->tt, tt_ptr, 0, NULL, NULL);
}
/* Setup the OpenCL stuff, create buffers, load the structure factor table */
struct gpu_context *setup_gpu(int no_sfac, struct image *image,
const double *intensities, unsigned char *flags,
const char *sym, int dev_num)
{
struct gpu_context *gctx;
cl_uint nplat;
cl_platform_id platforms[8];
cl_context_properties prop[3];
cl_int err;
cl_device_id dev;
size_t intensities_size;
float *intensities_ptr;
size_t flags_size;
float *flags_ptr;
size_t maxwgsize;
int i;
char cflags[512] = "";
STATUS("Setting up GPU...\n");
err = clGetPlatformIDs(8, platforms, &nplat);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't get platform IDs: %i\n", err);
return NULL;
}
if ( nplat == 0 ) {
ERROR("Couldn't find at least one platform!\n");
return NULL;
}
prop[0] = CL_CONTEXT_PLATFORM;
prop[1] = (cl_context_properties)platforms[0];
prop[2] = 0;
gctx = malloc(sizeof(*gctx));
gctx->ctx = clCreateContextFromType(prop, CL_DEVICE_TYPE_GPU,
NULL, NULL, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't create OpenCL context: %i\n", err);
free(gctx);
return NULL;
}
dev = get_cl_dev(gctx->ctx, dev_num);
gctx->cq = clCreateCommandQueue(gctx->ctx, dev, 0, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't create OpenCL command queue\n");
free(gctx);
return NULL;
}
/* Create buffer for the picture */
gctx->diff_size = image->width*image->height*sizeof(cl_float);
gctx->diff = clCreateBuffer(gctx->ctx, CL_MEM_WRITE_ONLY,
gctx->diff_size, NULL, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't allocate diffraction memory\n");
free(gctx);
return NULL;
}
/* Create a single-precision version of the scattering factors */
intensities_size = IDIM*IDIM*IDIM*sizeof(cl_float);
intensities_ptr = malloc(intensities_size);
if ( intensities != NULL ) {
for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
intensities_ptr[i] = intensities[i];
}
} else {
for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
intensities_ptr[i] = 1e5;
}
strncat(cflags, "-DFLAT_INTENSITIES ", 511-strlen(cflags));
}
gctx->intensities = clCreateBuffer(gctx->ctx,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
intensities_size, intensities_ptr, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't allocate intensities memory\n");
free(gctx);
return NULL;
}
free(intensities_ptr);
if ( strcmp(sym, "1") == 0 ) {
strncat(cflags, "-DPG1 ", 511-strlen(cflags));
} else if ( strcmp(sym, "-1") == 0 ) {
strncat(cflags, "-DPG1BAR ", 511-strlen(cflags));
} else if ( strcmp(sym, "6/mmm") == 0 ) {
strncat(cflags, "-DPG6MMM ", 511-strlen(cflags));
} else if ( strcmp(sym, "6") == 0 ) {
strncat(cflags, "-DPG6 ", 511-strlen(cflags));
} else if ( strcmp(sym, "6/m") == 0 ) {
strncat(cflags, "-DPG6M ", 511-strlen(cflags));
} else {
ERROR("Sorry! Point group '%s' is not currently supported"
" on the GPU. I'm using '1' instead.\n", sym);
strncat(cflags, "-DPG1 ", 511-strlen(cflags));
}
/* Create a flag array */
flags_size = IDIM*IDIM*IDIM*sizeof(cl_float);
flags_ptr = malloc(flags_size);
if ( flags != NULL ) {
for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
flags_ptr[i] = flags[i];
}
} else {
for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
flags_ptr[i] = 1.0;
}
}
gctx->flags = clCreateBuffer(gctx->ctx,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
flags_size, flags_ptr, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't allocate flag buffer\n");
free(gctx);
return NULL;
}
free(flags_ptr);
gctx->tt_size = image->width*image->height*sizeof(cl_float);
gctx->tt = clCreateBuffer(gctx->ctx, CL_MEM_WRITE_ONLY, gctx->tt_size,
NULL, &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't allocate twotheta memory\n");
free(gctx);
return NULL;
}
gctx->prog = load_program(DATADIR"/crystfel/diffraction.cl", gctx->ctx,
dev, &err, cflags);
if ( err != CL_SUCCESS ) {
free(gctx);
return NULL;
}
gctx->kern = clCreateKernel(gctx->prog, "diffraction", &err);
if ( err != CL_SUCCESS ) {
ERROR("Couldn't create kernel\n");
free(gctx);
return NULL;
}
gctx->max_sinc_lut = 0;
gctx->sinc_lut_ptrs = NULL;
gctx->sinc_luts = NULL;
clGetDeviceInfo(dev, CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t), &maxwgsize, NULL);
STATUS("Maximum work group size = %lli\n", (long long int)maxwgsize);
return gctx;
}
void cleanup_gpu(struct gpu_context *gctx)
{
int i;
clReleaseProgram(gctx->prog);
clReleaseMemObject(gctx->diff);
clReleaseMemObject(gctx->tt);
clReleaseMemObject(gctx->intensities);
/* Release LUTs */
for ( i=1; i<=gctx->max_sinc_lut; i++ ) {
if ( gctx->sinc_lut_ptrs[i-1] != NULL ) {
clReleaseMemObject(gctx->sinc_luts[i-1]);
free(gctx->sinc_lut_ptrs[i-1]);
}
}
free(gctx->sinc_luts);
free(gctx->sinc_lut_ptrs);
clReleaseCommandQueue(gctx->cq);
clReleaseContext(gctx->ctx);
free(gctx);
}
|