File: GPURecursiveGaussianImageFilter.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 (360 lines) | stat: -rw-r--r-- 11,898 bytes parent folder | download | duplicates (4)
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
/*=========================================================================
 *
 *  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::RecursiveGaussianImageFilter

#define _ELASTIX_USE_OPENCL_OPTIMIZATIONS 0

//------------------------------------------------------------------------------
// Exact copy of FilterDataArray from RecursiveSeparableImageFilter
void filter_data_array( BUFFPIXELTYPE *outs,
                        const BUFFPIXELTYPE *data,
                        BUFFPIXELTYPE *scratch,
                        const unsigned int ln,
                        const float4 N, const float4 D, const float4 M,
                        const float4 BN, const float4 BM )
{
  /**
  * Causal direction pass
  */
  // this value is assumed to exist from the border to infinity.
  const BUFFPIXELTYPE outV1 = data[0];

  /**
  * Initialize borders
  */
  scratch[0] = ( outV1   * N.x +   outV1 * N.y + outV1   * N.z + outV1 * N.w );
  scratch[1] = ( data[1] * N.x +   outV1 * N.y + outV1   * N.z + outV1 * N.w );
  scratch[2] = ( data[2] * N.x + data[1] * N.y + outV1   * N.z + outV1 * N.w );
  scratch[3] = ( data[3] * N.x + data[2] * N.y + data[1] * N.z + outV1 * N.w );

  // note that the outV1 value is multiplied by the Boundary coefficients m_BNi
  scratch[0] -= ( outV1      * BN.x + outV1      * BN.y + outV1      * BN.z + outV1 * BN.w );
  scratch[1] -= ( scratch[0] * D.x  + outV1      * BN.y + outV1      * BN.z + outV1 * BN.w );
  scratch[2] -= ( scratch[1] * D.x  + scratch[0] * D.y  + outV1      * BN.z + outV1 * BN.w );
  scratch[3] -= ( scratch[2] * D.x  + scratch[1] * D.y  + scratch[0] * D.z  + outV1 * BN.w );

  /**
  * Recursively filter the rest
  */
  float4 data_small, scratch_small;
  for ( uint i = 4; i < ln; ++i )
  {
#if _ELASTIX_USE_OPENCL_OPTIMIZATIONS
    data_small    = (float4)( data[i], data[i - 1], data[i - 2], data[i - 3] );
    scratch_small = (float4)( scratch[i - 1], scratch[i - 2], scratch[i - 3], scratch[i - 4] );
    scratch[i]  = dot( data_small, N );
    scratch[i] -= dot( scratch_small, D );
#else
    scratch[i]  = data[i]        * N.x + data[i - 1]    * N.y + data[i - 2]    * N.z + data[i - 3]    * N.w;
    scratch[i] -= scratch[i - 1] * D.x + scratch[i - 2] * D.y + scratch[i - 3] * D.z + scratch[i - 4] * D.w;
#endif
  }

  /**
  * Store the causal result
  */
  for ( uint i = 0; i < ln; ++i )
  {
    outs[i] = scratch[i];
  }

  /**
  * AntiCausal direction pass
  */
  // this value is assumed to exist from the border to infinity.
  const BUFFPIXELTYPE outV2 = data[ln - 1];

  /**
  * Initialize borders
  */
  scratch[ln - 1] = ( outV2        * M.x + outV2        * M.y + outV2        * M.z + outV2 * M.w );
  scratch[ln - 2] = ( data[ln - 1] * M.x + outV2        * M.y + outV2        * M.z + outV2 * M.w );
  scratch[ln - 3] = ( data[ln - 2] * M.x + data[ln - 1] * M.y + outV2        * M.z + outV2 * M.w );
  scratch[ln - 4] = ( data[ln - 3] * M.x + data[ln - 2] * M.y + data[ln - 1] * M.z + outV2 * M.w );

  // note that the outV2value is multiplied by the Boundary coefficients m_BMi
  scratch[ln - 1] -= ( outV2           * BM.x + outV2           * BM.y + outV2           * BM.z + outV2 * BM.w );
  scratch[ln - 2] -= ( scratch[ln - 1] * D.x  + outV2           * BM.y + outV2           * BM.z + outV2 * BM.w );
  scratch[ln - 3] -= ( scratch[ln - 2] * D.x  + scratch[ln - 1] * D.y  + outV2           * BM.z + outV2 * BM.w );
  scratch[ln - 4] -= ( scratch[ln - 3] * D.x  + scratch[ln - 2] * D.y  + scratch[ln - 1] * D.z  + outV2 * BM.w );

  /**
  * Recursively filter the rest
  */
  for ( unsigned int i = ln - 4; i > 0; i-- )
  {
#if _ELASTIX_USE_OPENCL_OPTIMIZATIONS
    data_small    = (float4)( data[i], data[i + 1], data[i + 2], data[i + 3] );
    scratch_small = (float4)( scratch[i], scratch[i + 1], scratch[i + 2], scratch[i + 3] );
    scratch[i - 1]  = dot( data_small, M );
    scratch[i - 1] -= dot( scratch_small, D );
#else
    scratch[i - 1] = data[i]     * M.x + data[i + 1]    * M.y + data[i + 2]    * M.z + data[i + 3]    * M.w;
    scratch[i - 1] -= scratch[i] * D.x + scratch[i + 1] * D.y + scratch[i + 2] * D.z + scratch[i + 3] * D.w;
#endif
  }

  /**
  * Roll the antiCausal part into the output
  */
  for ( uint i = 0; i < ln; ++i )
  {
    outs[i] += scratch[i];
  }
}

//------------------------------------------------------------------------------
// 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 RecursiveGaussianImageFilter( __global const INPIXELTYPE *in,
                                            __global OUTPIXELTYPE *out,
                                            unsigned int ln, int direction,
                                            float4 N, float4 D, float4 M,
                                            float4 BN, float4 BM,
                                            uint width, uint height )
{
  uint index = get_global_id( 0 );

  // Define length
  uint length = 0;

  if ( direction == 0 )
  {
    length = width;
  }
  else if ( direction == 1 )
  {
    length = height;
  }

  if ( index < length )
  {
    // local buffers
    BUFFPIXELTYPE inps[BUFFSIZE];
    BUFFPIXELTYPE outs[BUFFSIZE];
    BUFFPIXELTYPE scratch[BUFFSIZE];

    // fill local input buffer
    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 ( height != 0 )
    {
      if ( direction == 0 )
      {
        for ( uint i = 0; i < length; ++i )
        {
          lidx = get_image_offset( i, 0, index, width, 1 );
          inps[id++] = (BUFFPIXELTYPE)( in[lidx] );
        }
      }
      else if ( direction == 1 )
      {
        for ( uint i = 0; i < length; ++i )
        {
          lidx = get_image_offset( index, 0, i, width, 1 );
          inps[id++] = (BUFFPIXELTYPE)( in[lidx] );
        }
      }
    }
    else
    {
      for ( uint i = 0; i < length; ++i )
      {
        inps[id++] = (BUFFPIXELTYPE)( in[i] );
      }
    }

    // Apply the recursive Filter to an array of data. This method is called
    // for each line of the volume. Parameter "scratch" is a scratch
    // area used for internal computations that is the same size as the
    // parameters "outs" and "data".
    filter_data_array( outs, inps, scratch, ln, N, D, M, BN, BM );

    // copy to output
    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 ( height != 0 )
    {
      if ( direction == 0 )
      {
        for ( uint i = 0; i < length; ++i )
        {
          lidx = get_image_offset( i, 0, index, width, 1 );
          out[lidx] = (OUTPIXELTYPE)( outs[id++] );
        }
      }
      else if ( direction == 1 )
      {
        for ( uint i = 0; i < length; ++i )
        {
          lidx = get_image_offset( index, 0, i, width, 1 );
          out[lidx] = (OUTPIXELTYPE)( outs[id++] );
        }
      }
    }
    else
    {
      for ( uint i = 0; i < length; ++i )
      {
        out[i] = (OUTPIXELTYPE)( outs[id++] );
      }
    }
  }
}

#endif

//------------------------------------------------------------------------------
#ifdef DIM_2
__kernel void RecursiveGaussianImageFilter( __global const INPIXELTYPE *in,
                                            __global OUTPIXELTYPE *out,
                                            unsigned int ln, int direction,
                                            float4 N, float4 D, float4 M,
                                            float4 BN, float4 BM,
                                            uint width, uint height, uint depth )
{
  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 = height;
    length.y = depth;
    length.z = width;  // looping over
  }
  else if ( direction == 1 )
  {
    length.x = width;
    length.y = depth;
    length.z = height; // looping over
  }
  else if ( direction == 2 )
  {
    length.x = width;
    length.y = height;
    length.z = depth;  // looping over
  }

  if ( index.x < length.x && index.y < length.y )
  {
    // local buffers
    BUFFPIXELTYPE inps[BUFFSIZE];
    BUFFPIXELTYPE outs[BUFFSIZE];
    BUFFPIXELTYPE scratch[BUFFSIZE];

    // fill local input buffer
    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, width, height );
        inps[id++] = (BUFFPIXELTYPE)( in[lidx] );
      }
    }
    else if ( direction == 1 )
    {
      for ( uint i = 0; i < length.z; ++i )
      {
        lidx = get_image_offset( index.x, i, index.y, width, height );
        inps[id++] = (BUFFPIXELTYPE)( in[lidx] );
      }
    }
    else if ( direction == 2 )
    {
      for ( uint i = 0; i < length.z; ++i )
      {
        lidx = get_image_offset( index.x, index.y, i, width, height );
        inps[id++] = (BUFFPIXELTYPE)( in[lidx] );
      }
    }

    // Apply the recursive Filter to an array of data. This method is called
    // for each line of the volume. Parameter "scratch" is a scratch
    // area used for internal computations that is the same size as the
    // parameters "outs" and "data".
    filter_data_array( outs, inps, scratch, ln, N, D, M, BN, BM );

    // copy to output
    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, width, height );
        out[lidx] = (OUTPIXELTYPE)( outs[id++] );
      }
    }
    else if ( direction == 1 )
    {
      for ( uint i = 0; i < length.z; ++i )
      {
        lidx = get_image_offset( index.x, i, index.y, width, height );
        out[lidx] = (OUTPIXELTYPE)( outs[id++] );
      }
    }
    else if ( direction == 2 )
    {
      for ( uint i = 0; i < length.z; ++i )
      {
        lidx = get_image_offset( index.x, index.y, i, width, height );
        out[lidx] = (OUTPIXELTYPE)( outs[id++] );
      }
    }
  }
}

#endif