forked from kif/3D-CDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregrid.cl
257 lines (233 loc) · 9.22 KB
/
regrid.cl
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
//Storage for that many voxel per pixel
#define STORAGE_SIZE 64
// Function to perform an atom addition in global memory (does not exist in OpenCL)
inline void atomic_add_global_float(volatile global float *addr, float val)
{
union {
uint u32;
float f32;
} next, expected, current;
current.f32 = *addr;
do {
expected.f32 = current.f32;
next.f32 = expected.f32 + val;
current.u32 = atomic_cmpxchg( (volatile global uint *)addr,
expected.u32, next.u32);
} while( current.u32 != expected.u32 );
}
// Performs the centering/scaling in the detector plane. Image flipping must be implemented here
float2 inline calc_position_real(float2 index,
float2 center,
float pixel_size)
{
return (index - center) * pixel_size;
}
// Transforms a 2D position in the image into a 3D coordinate in the volume
float3 inline calc_position_rec(float2 index,
float2 center,
float pixel_size,
float distance,
float3 Rx,
float3 Ry,
float3 Rz)
{
float2 pos2 = calc_position_real(index, center, pixel_size);
// float d = sqrt(distance*distance + dot(pos2, pos2));
float d = fast_length((float3)(distance, pos2));
float3 pos3 = (float3)(pos2.x/d, pos2.y/d, distance/d-1.0f);
float scale = distance/pixel_size;
return scale * (float3)(dot(Rx, pos3), dot(Ry, pos3), dot(Rz, pos3));
}
/* Performs the regridding of an image on a 3D volume
*
* 2D kernel, one thread per input pixel. Scatter-like kernel with atomics.
*
* pixel start at 0 and finish at 1, the center is at 0.5
* thread ids follow the memory location convention (zyx) not the math x,y,z convention
*
* Basic oversampling implemented but slows down the processing, mainly for calculating
* Atomic operations are the second bottleneck
*/
kernel void regid_CDI_simple(global float* image,
const int height,
const int width,
const float pixel_size,
const float distance,
const float phi,
const float center_x,
const float center_y,
global float* signal,
global float* norm,
const int shape,
int oversampling)
{
int tmp, shape_2, i, j, k;
size_t where_in, where_out;
float value, cos_phi, sin_phi, delta, start;
float2 pos2, center = (float2)(center_x, center_y);
float3 Rx, Ry, Rz, recip;
if ((get_global_id(0)>=height) || (get_global_id(1)>=width))
return;
where_in = width*get_global_id(0)+get_global_id(1);
shape_2 = shape/2;
oversampling = (oversampling<1?1:oversampling);
start = 0.5f / oversampling;
delta = 2 * start;
cos_phi = cos(phi*M_PI_F/180.0f);
sin_phi = sin(phi*M_PI_F/180.0f);
Rx = (float3)(cos_phi, 0.0f, sin_phi);
Ry = (float3)(0.0f, 1.0f, 0.0f);
Rz = (float3)(-sin_phi, 0.0f, cos_phi);
// No oversampling for now
//this is the center of the pixel
//pos2 = (float2)(get_global_id(1)+0.5f, get_global_id(0) + 0.5f);
//Basic oversampling
for (i=0; i<oversampling; i++)
{
for (j=0; j<oversampling; j++)
{
pos2 = (float2)(get_global_id(1) + start + i*delta,
get_global_id(0) + start + j*delta);
recip = calc_position_rec(pos2, center, pixel_size, distance, Rx, Ry, Rz);
value = image[where_in];
tmp = convert_int_rtn(recip.x) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out = tmp;
tmp = convert_int_rtn(recip.y) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out += tmp * shape;
tmp = convert_int_rtn(recip.z) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out += ((long)tmp) * shape * shape;
atomic_add_global_float(&signal[where_out], value);
atomic_add_global_float(&norm[where_out], 1.0f);
}
}
}
}
}
}
kernel void regid_CDI(global float* image,
global uchar* mask,
const int height,
const int width,
const float dummy,
const float pixel_size,
const float distance,
const float phi,
float dphi,
const float center_x,
const float center_y,
global float* signal,
global int* norm,
const int shape,
int oversampling_pixel,
int oversampling_phi)
{
int tmp, shape_2, i, j, k;
size_t where_in, where_out;
float value, delta;
float2 pos2, center = (float2)(center_x, center_y);
float3 Rx, Ry, Rz, recip;
//This is local storage of voxels to be written
int last=0;
size_t index[STORAGE_SIZE];
float2 store[STORAGE_SIZE];
where_in = width*get_global_id(0)+get_global_id(1);
shape_2 = shape/2;
oversampling_pixel = (oversampling_pixel<1?1:oversampling_pixel);
oversampling_phi = (oversampling_phi<1?1:oversampling_phi);
delta = 1.0f / oversampling_pixel;
dphi /= oversampling_phi;
{ //Manual mask definition
int y = get_global_id(0),
x = get_global_id(1);
if ((x >= width) ||
(y >= height))
return;
}
{ // static mask
if (mask[where_in])
return;
}
{//dynamic masking
value = image[where_in];
if (value < -10.0f)
return;
else if (value <=0.0f)
value= 0.0f;
if (! isfinite(value))
return;
}
// No oversampling for now
//this is the center of the pixel
//pos2 = (float2)(get_global_id(1)+0.5f, get_global_id(0) + 0.5f);
//Basic oversampling
for (int dr=0; dr<oversampling_phi; dr++)
{
float cos_phi, sin_phi, rphi;
rphi = (phi + (0.0f + dr)*dphi) * M_PI_F/180.0f;
cos_phi = cos(rphi);
sin_phi = sin(rphi);
Rx = (float3)(cos_phi, 0.0f, sin_phi);
Ry = (float3)(0.0f, 1.0f, 0.0f);
Rz = (float3)(-sin_phi, 0.0f, cos_phi);
for (i=0; i<oversampling_pixel; i++)
{
for (j=0; j<oversampling_pixel; j++)
{
pos2 = (float2)(get_global_id(1) + (i + 0.5f)*delta,
get_global_id(0) + (j + 0.5f)*delta);
recip = calc_position_rec(pos2, center, pixel_size, distance, Rx, Ry, Rz);
tmp = convert_int_rtn(recip.x) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out = tmp;
tmp = convert_int_rtn(recip.y) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out += tmp * shape;
tmp = convert_int_rtn(recip.z) + shape_2;
if ((tmp>=0) && (tmp<shape))
{
where_out += ((long)tmp) * shape * shape;
//storage locally
int found = 0;
for (k=0; k<last; k++)
{
if (where_out == index[k])
{
store[k] += (float2)(value, 1.0f);
found = 1;
k = last;
}
}
if (found == 0)
{
if (last >= STORAGE_SIZE)
printf("Too many voxels covered by pixel\n");
else
{
index[last] = where_out;
store[last] = (float2)(value, 1.0f);
last++;
}
}
}
}
}
}
}
}
// Finally we update the global memory with atomic writes
for (k=0; k<last; k++)
{
atomic_add_global_float(&signal[index[k]], store[k].s0);
atomic_add(&norm[index[k]], (int)store[k].s1);
//signal[index[k]] += store[k].s0;
//norm[index[k]] += (int)store[k].s1;
}
}