File: GPUBSplineDecompositionImageFilter.cl

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (367 lines) | stat: -rw-r--r-- 10,579 bytes parent folder | download | duplicates (5)
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
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
//
// \author Denis P. Shamonin and Marius Staring. Division of Image Processing,
// Department of Radiology, Leiden, The Netherlands
//
// \note This work was funded by the Netherlands Organisation for
// Scientific Research (NWO NRG-2010.02 and NWO 639.021.124).
//
// OpenCL implementation of itk::BSplineDecompositionImageFilter

//------------------------------------------------------------------------------
void set_initial_causal_coefficient(BUFFPIXELTYPE *scratch,
                                    const float z, const ulong data_length)
{
  float       sum;
  float       zn, z2n, iz;
  const float tolerance = 1e-10f;

  // this initialization corresponds to mirror boundaries
  uint horizon = data_length;

  zn = z;
  if(tolerance > 0.0f)
  {
    horizon = (uint)( ceil( log(tolerance) / log( fabs(z) ) ) );
  }
  if(horizon < data_length)
  {
    // accelerated loop
    sum = scratch[0]; // verify this
    for(uint n = 1; n < horizon; ++n)
    {
      //sum += zn * scratch[n];
      sum = mad(zn, scratch[n], sum);
      zn *= z;
    }
    scratch[0] = sum;
  }
  else
  {
    // full loop
    iz = 1.0f / z;
    z2n = pow( z, (float)(data_length - 1L) );
    sum = mad(z2n, scratch[data_length - 1L], scratch[0]);
    z2n *= z2n * iz;
    for(uint n = 1; n <= (data_length - 2); ++n)
    {
      //sum += (zn + z2n) * scratch[n];
      sum = mad(zn + z2n, scratch[n], sum);
      zn *= z;
      z2n *= iz;
    }
    scratch[0] = sum / (1.0f - pown(zn, 2));
  }
}

//------------------------------------------------------------------------------
// OpenCL implementation of
// BSplineDecompositionImageFilter::SetInitialAntiCausalCoefficient()
void set_initial_anticausal_coefficient(BUFFPIXELTYPE *scratch,
                                        const float z, const ulong data_length)
{
  // this initialization corresponds to mirror boundaries
  // See Unser, 1999, Box 2 for explanation
  // Also see erratum at http://bigwww.epfl.ch/publications/unser9902.html
  scratch[data_length - 1] =
    ( z / (pown(z, 2) - 1.0f) )
    * ( mad(z, scratch[data_length - 2], scratch[data_length - 1]) );
}

//------------------------------------------------------------------------------
// OpenCL implementation of
// BSplineDecompositionImageFilter::DataToCoefficients1D()
bool data_to_coefficients_1d(BUFFPIXELTYPE *scratch,
                             const uint3 image_size,
                             const float2 in_spline_poles,
                             const int number_of_poles,
                             const uint direction)
{
  // Define data_length
  uint data_length = 0;
  if(direction == 0)
  {
    data_length = image_size.x;
  }
  else if(direction == 1)
  {
    data_length = image_size.y;
  }
  else if(direction == 2)
  {
    data_length = image_size.z;
  }

  if(data_length == 1) //Required by mirror boundaries
  {
    return false;
  }

  // compute overall gain
  float c0 = 1.0f;
  if(number_of_poles == 1)
  {
    float t1 = 1.0f - in_spline_poles.x;
    float t2 = 1.0f - (1.0f / in_spline_poles.x);
    c0 = t1 * t2;
  }
  else if (number_of_poles == 2)
  {
    float t1 = 1.0f - in_spline_poles.x;
    float t2 = 1.0f - (1.0f / in_spline_poles.x);
    float t3 = 1.0f - in_spline_poles.y;
    float t4 = 1.0f - (1.0f / in_spline_poles.y);
    c0 = t1 * t2 * t3 * t4;
  }

  // apply the gain
  for(uint n = 0; n < data_length; ++n)
  {
    scratch[n] *= c0;
  }

  // define spline_poles[2], it is better for loops
  float spline_poles[2];
  spline_poles[0] = in_spline_poles.x;
  spline_poles[1] = in_spline_poles.y;

  // loop over all poles
  for(uint k = 0; k < number_of_poles; ++k)
  {
    // causal initialization
    set_initial_causal_coefficient(scratch, spline_poles[k], data_length);
    // causal recursion
    for(uint n = 1; n < data_length; ++n)
    {
      //scratch[n] += spline_poles[k] * scratch[n - 1];
      scratch[n] = mad(spline_poles[k], scratch[n - 1], scratch[n]);
    }
    // anticausal initialization
    set_initial_anticausal_coefficient(scratch, spline_poles[k], data_length);
    // anticausal recursion
    for(int n = data_length - 2; 0 <= n; n--)
    {
      scratch[n] = spline_poles[k] * (scratch[n + 1] - scratch[n]);
    }
  }
  return true;
}

//------------------------------------------------------------------------------
// Get global memory offset
uint get_image_offset(const uint gix,
                      const uint giy,
                      const uint giz,
                      const uint width, const uint height)
{
  uint gidx = mad24(width, mad24(giz, height, giy), gix);
  return gidx;
}

//------------------------------------------------------------------------------
#ifdef DIM_1
__kernel void BSplineDecompositionImageFilter(__global const INPIXELTYPE *in,
                                              __global OUTPIXELTYPE *out,
                                              uint2 image_size,
                                              float2 spline_poles,
                                              uint number_of_poles,
                                              uint direction)
{
  uint gi = get_global_id(0);
  // Define length
  uint length = 0;

  if(direction == 0)
  {
    length = image_size.x;
  }
  else if(direction == 1)
  {
    length = image_size.y;
  }

  if(gi < length)
  {
    // Local scratch buffer
    BUFFPIXELTYPE scratch[BUFFSIZE];

    // Copy coefficients to scratch
    uint id = 0;
    uint lidx = 0;
    for(uint i = 0; i < length; ++i)
    {
      if(image_size.y != 0)
      {
        if(direction == 0)
        {
          lidx = get_image_offset(i, 0, gi, image_size.x, 1);
        }
        else if(direction == 1)
        {
          lidx = get_image_offset(gi, 0, i, image_size.x, 1);
        }
      }
      else
      {
        lidx = i;
      }
      scratch[id++] = (BUFFPIXELTYPE)(out[lidx]);
    }

    // Perform 1D BSpline calculations
    data_to_coefficients_1d(scratch, (uint3)(image_size.x, image_size.y, 0),
                            spline_poles, number_of_poles, direction);

    // Copy scratch back to coefficients
    id = 0;
    lidx = 0;
    for(uint i = 0; i < length; ++i)
    {
      if(image_size.y != 0)
      {
        if(direction == 0)
        {
          lidx = get_image_offset(i, 0, gi, image_size.x, 1);
        }
        else if(direction == 1)
        {
          lidx = get_image_offset(gi, 0, i, image_size.x, 1);
        }
      }
      else
      {
        lidx = i;
      }
      out[lidx] = (OUTPIXELTYPE)(scratch[id++]);
    }
  }
}

#endif

//------------------------------------------------------------------------------
#ifdef DIM_2
__kernel void BSplineDecompositionImageFilter(__global const INPIXELTYPE *in,
                                              __global OUTPIXELTYPE *out,
                                              uint3 image_size,
                                              float2 spline_poles,
                                              int number_of_poles,
                                              uint direction)
{
  uint2 index = (uint2)( get_global_id(0), get_global_id(1) );

  // 0 (direction x) : y/z
  // 1 (direction y) : x/z
  // 2 (direction z) : x/y
  uint3 length;

  if(direction == 0)
  {
    length.x = image_size.y;
    length.y = image_size.z;
    length.z = image_size.x; // looping over
  }
  else if(direction == 1)
  {
    length.x = image_size.x;
    length.y = image_size.z;
    length.z = image_size.y; // looping over
  }
  else if(direction == 2)
  {
    length.x = image_size.x;
    length.y = image_size.y;
    length.z = image_size.z; // looping over
  }

  if(index.x < length.x && index.y < length.y)
  {
    // Local scratch buffer
    BUFFPIXELTYPE scratch[BUFFSIZE];

    // Copy coefficients to scratch
    uint id = 0;
    uint lidx = 0;

    // Having if() statement inside loop will cost performance penalty.
    // Therefore we are moving for() loop inside the if () statement.
    // The code become less compact, but faster.
    if(direction == 0)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(i, index.x, index.y, image_size.x, image_size.y);
        scratch[id++] = (BUFFPIXELTYPE)(out[lidx]);
      }
    }
    else if(direction == 1)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(index.x, i, index.y, image_size.x, image_size.y);
        scratch[id++] = (BUFFPIXELTYPE)(out[lidx]);
      }
    }
    else if(direction == 2)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(index.x, index.y, i, image_size.x, image_size.y);
        scratch[id++] = (BUFFPIXELTYPE)(out[lidx]);
      }
    }

    // Perform 1D BSpline calculations
    data_to_coefficients_1d(scratch, image_size,
                            spline_poles, number_of_poles, direction);

    // Copy scratch back to coefficients
    id = 0;

    // Having if() statement inside loop will cost performance penalty.
    // Therefore we are moving for() loop inside the if () statement.
    // The code become less compact, but faster.
    if(direction == 0)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(i, index.x, index.y, image_size.x, image_size.y);
        out[lidx] = (OUTPIXELTYPE)(scratch[id++]);
      }
    }
    else if(direction == 1)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(index.x, i, index.y, image_size.x, image_size.y);
        out[lidx] = (OUTPIXELTYPE)(scratch[id++]);
      }
    }
    else if(direction == 2)
    {
      for(uint i = 0; i < length.z; ++i)
      {
        lidx = get_image_offset(index.x, index.y, i, image_size.x, image_size.y);
        out[lidx] = (OUTPIXELTYPE)(scratch[id++]);
      }
    }
  }
}

#endif