-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdevice.cu
More file actions
432 lines (373 loc) · 11.6 KB
/
cdevice.cu
File metadata and controls
432 lines (373 loc) · 11.6 KB
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
#include "cdevice.cuh"
#include <cuComplex.h>
#include <math.h>
#include <cuda_runtime.h>
#include <cufft.h>
#include <stdio.h>
#include "ccube.h"
#include "ccomplex.h"
__device__ __host__ Complex cAdd(Complex a, Complex b) {
/*
Add the real and imaginary components of two complex numbers.
*/
Complex c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
__device__ __host__ Complex cDivide(Complex a, double b) {
/*
Divide a complex number [a] by a real number [b].
*/
Complex c;
c.x = a.x / b;
c.y = a.y / b;
return c;
}
__device__ __host__ Complex cExp(Complex a) {
/*
Calculate the exponential complex number [a].
exp(a) = exp(x + iy) = exp(x) * exp (iy) = exp(x) * (cos(y) + i sin(y))
*/
Complex tmp1, tmp2;
tmp1.x = exp(a.x); tmp1.y = 0;
tmp2.x = cos(a.y); tmp2.y = sin(a.y);
Complex c;
c = cMultiply(tmp1, tmp2);
return c;
}
__device__ __host__ Complex cMultiply(Complex a, Complex b) {
/*
Multiply a complex number [a] by a complex number [b].
*/
double e, f, g, h, eg, fh, eh, fg, x, y;
e = a.x; f = a.y;
g = b.x; h = b.y;
eg = e * g; fh = f * h;
eh = e * h; fg = f * g;
x = eg - fh; y = eh + fg;
Complex c;
c.x = x;
c.y = y;
return c;
}
__device__ __host__ Complex cMultiply(Complex a, double s) {
Complex c;
c.x = a.x * s;
c.y = a.y * s;
return c;
}
__device__ __host__ Complex cSub(Complex a, Complex b) {
/*
Subtract a complex number [a] from a complex number [b].
*/
Complex c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
__device__ __host__ void cCompareArray(int** a, int* b, long index, long spaxel_idx, long n_slices) {
for (int i = 0; i < n_slices; i++) {
if (a[spaxel_idx][i] != a[index][i]) {
b[spaxel_idx] = 0;
return;
}
}
b[spaxel_idx] = 1;
}
__device__ __host__ double cGetAmplitude(Complex a) {
/*
Get the amplitude of the complex number [a].
*/
double abs = sqrt(pow(a.x, 2) + pow(a.y, 2));
return abs;
}
__device__ __host__ double cGetPhase(Complex a) {
/*
Get the phase of the complex number [a].
*/
double phase = atan2(a.y, a.x);
return phase;
}
__device__ __host__ void cGetSpaxelData(Complex** a, Complex* b, long spaxel_idx, long n_slices) {
for (int i = 0; i < n_slices; i++) {
b[i] = a[i][spaxel_idx];
}
}
__device__ __host__ long cGet1DIndexFrom2DXY(long2 xy, long dim1) {
/*
Given a pair of coordinates [xy] and array x dimension [dim1], find the corresponding 1D index.
*/
long index = (xy.y*dim1) + xy.x;
return index;
}
__device__ __host__ long2 cGet2DXYFrom1DIndex(long index, long dim1) {
/*
Given a 1D index [index] and array x dimension [dim1], find the corresponding pair of coordinates xy.
*/
long2 xy;
xy.x = index % dim1;
xy.y = (long)(index / dim1);
return xy;
}
__device__ __host__ quadrant cGet2DQuadrantFrom1DIndex(long index, long dim1, long x_split, long y_split) {
/*
Given a 1D index [index] and array x dimension [dim1], find the quadrant in which the index would lie
in a 2D array given the x/y split positions [x_split, y_split].
*/
long2 xy = cGet2DXYFrom1DIndex(index, dim1);
if (xy.x < x_split) {
if (xy.y < y_split) {
return Q1;
}
else {
return Q3;
}
}
else {
if (xy.y < y_split) {
return Q2;
}
else {
return Q4;
}
}
}
__device__ __host__ void cMakeBitmask(Complex** a, int* b, long spaxel_idx, long n_slices) {
for (int i = 0; i < n_slices; i++) {
if (a[spaxel_idx][i].x > 0.) {
b[i] = 1;
} else {
b[i] = 0;
}
}
}
__device__ __host__ void cPolySub(Complex* in, Complex* coeffs, long n_coeffs, long x) {
double c = 0.;
for (int i = 0; i < n_coeffs; i++) {
c += powf(x, i) * coeffs[i].x;
}
in->x -= c;
}
__device__ __host__ Complex cTranslate(Complex a, long dim1, long dim2, long2 position, double2 translation) {
Complex b;
double x_e = ((double)position.x * -translation.x) / (double)dim1;
double y_e = ((double)position.y * -translation.y) / (double)dim2;
b.x = 0;
b.y = -2 * M_PI * (x_e + y_e);
return cMultiply(a, cExp(b));
}
__global__ void cAdd2D(Complex* a, Complex* b, long size) {
/*
Add the numbers from complex array [a] with [size] elements to complex array [b] pointwise.
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
a[i] = cAdd(a[i], b[i]);
}
}
__global__ void cCompareArray2D(int** a, int *b, long index, long n_slices, long n_spaxels_per_slice) {
/*
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < n_spaxels_per_slice; i += numThreads) {
cCompareArray(a, b, index, i, n_slices);
}
}
__global__ void cDivideByRealComponent2D(Complex* a, Complex* b, Complex* c, long size) {
/*
Divide the numbers from complex array [a] with [size] elements by real component of complex array [b]
pointwise storing the result in [c].
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
c[i] = cDivide(a[i], b[i].x);
}
}
__global__ void cGetSpaxelData2D(Complex** a, Complex **b, long n_slices, long n_spaxels_per_slice) {
/*
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < n_spaxels_per_slice; i += numThreads) {
cGetSpaxelData(a, b[i], i, n_slices);
}
}
__global__ void cFftShift2D(Complex* a, Complex* b, long dim) {
/*
Perform an fftshift on a complex array [a] to yield [b]. This routine only handles arrays with dimensions of equal size [dim].
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
long size = dim*dim;
long Q1_offset, Q2_offset, Q3_offset, Q4_offset;
if (dim % 2 == 0) {
Q1_offset = (dim*((dim) / 2.)) + ((dim) / 2);
Q2_offset = (dim*((dim) / 2)) - ((dim) / 2);
Q3_offset = -(dim*((dim) / 2)) + ((dim) / 2);
Q4_offset = -(dim*((dim) / 2)) - ((dim) / 2);
}
else {
Q1_offset = (dim*ceil(dim / 2.)) + ceil(dim / 2.);
Q2_offset = (dim*ceil(dim / 2.)) - floor(dim / 2.);
Q3_offset = -(dim*floor(dim / 2.)) + ceil(dim / 2.);
Q4_offset = -(dim*floor(dim / 2.)) - floor(dim / 2.);
}
long x_split = floor(dim / 2.);
long y_split = floor(dim / 2.);
for (int i = threadID; i < size; i += numThreads) {
switch (cGet2DQuadrantFrom1DIndex(i, dim, x_split, y_split)) {
case Q1:
b[i] = a[i + Q1_offset];
break;
case Q2:
b[i] = a[i + Q2_offset];
break;
case Q3:
b[i] = a[i + Q3_offset];
break;
case Q4:
b[i] = a[i + Q4_offset];
break;
}
}
}
__global__ void cIFftShift2D(Complex* a, Complex* b, long dim) {
/*
Perform an ifftshift on a complex array [a] to yield [b]. This routine only handles arrays with dimensions of equal size [dim].
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
long size = dim*dim;
long Q1_offset, Q2_offset, Q3_offset, Q4_offset;
if (dim % 2 == 0) {
Q1_offset = (dim*((dim) / 2)) + ((dim) / 2);
Q2_offset = (dim*((dim) / 2)) - ((dim) / 2);
Q3_offset = -(dim*((dim) / 2)) + ((dim) / 2);
Q4_offset = -(dim*((dim) / 2)) - ((dim) / 2);
}
else {
Q1_offset = (dim*floor(dim / 2.)) + floor(dim / 2.);
Q2_offset = (dim*floor(dim / 2.)) - ceil(dim / 2.);
Q3_offset = -(dim*ceil(dim / 2.)) + floor(dim / 2.);
Q4_offset = -(dim*ceil(dim / 2.)) - ceil(dim / 2.);
}
long x_split = ceil(dim / 2.);
long y_split = ceil(dim / 2.);
for (int i = threadID; i < size; i += numThreads) {
switch (cGet2DQuadrantFrom1DIndex(i, dim, x_split, y_split)) {
case Q1:
b[i] = a[i + Q1_offset];
break;
case Q2:
b[i] = a[i + Q2_offset];
break;
case Q3:
b[i] = a[i + Q3_offset];
break;
case Q4:
b[i] = a[i + Q4_offset];
break;
}
}
}
__global__ void cMakeBitmask2D(Complex** a, int **b, long n_slices, long n_spaxels_per_slice) {
/*
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < n_spaxels_per_slice; i += numThreads) {
cMakeBitmask(a, b[i], i, n_slices);
}
}
__global__ void cMultiplyHadamard2D(Complex* a, Complex* b, Complex* c, long n_spaxels_per_slice) {
/*
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < n_spaxels_per_slice; i += numThreads) {
c[i] = cMultiply(a[i], b[i]);
}
}
__global__ void cPolySub2D(Complex** in, int** mask, Complex** coeffs, long n_coeffs, int* wavelengths, long n_slices, long n_spaxels_per_slice) {
/*
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < n_slices*n_spaxels_per_slice; i += numThreads) { //n_slices*n_spaxels_per_slice
int spaxel_idx = i / n_slices;
int slice_idx = i % n_slices;
if (mask[spaxel_idx][slice_idx] == 1) {
cPolySub(&in[slice_idx][spaxel_idx], coeffs[spaxel_idx], n_coeffs, wavelengths[slice_idx]);
}
}
}
__global__ void cScale2D(Complex *a, double scale, long size) {
/*
Scale complex array [a] with [size] elements by [scale] pointwise.
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
a[i] = cMultiply(a[i], scale);
}
}
__global__ void cSub2D(Complex* a, Complex* b, long size) {
/*
Subtract array [b] with [size] elements from array [a] pointwise.
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
a[i] = cSub(a[i], b[i]);
}
}
__global__ void cSetComplexRealAsAmplitude2D(Complex *a, long size) {
/*
Set the real component of array [a] with [size] elements to the amplitude and zero the
imaginary part.
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
a[i].x = cGetAmplitude(a[i]);
a[i].y = 0;
}
}
__global__ void cTranslate2D(Complex *a, double2 translation, long dim) {
/*
Apply translation to complex array [a] by [translation] pointwise. This routine only handles arrays with dimensions of equal size [dim].
*/
const int numThreads = blockDim.x * gridDim.x;
const int threadID = blockIdx.x * blockDim.x + threadIdx.x;
long size = dim*dim;
// this is required as one thread may need to do multiple
// computations, i.e. if numThreads < size
for (int i = threadID; i < size; i += numThreads) {
long2 position = cGet2DXYFrom1DIndex(i, dim);
a[i] = cTranslate(a[i], dim, dim, position, translation);
}
}