1
////////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
18
// Shengen Yan,yanshengen@gmail.com
20
// Redistribution and use in source and binary forms, with or without modification,
21
// are permitted provided that the following conditions are met:
23
// * Redistribution's of source code must retain the above copyright notice,
24
// this list of conditions and the following disclaimer.
26
// * Redistribution's in binary form must reproduce the above copyright notice,
27
// this list of conditions and the following disclaimer in the documentation
28
// and/or other materials provided with the distribution.
30
// * The name of the copyright holders may not be used to endorse or promote products
31
// derived from this software without specific prior written permission.
33
// This software is provided by the copyright holders and contributors as is and
34
// any express or implied warranties, including, but not limited to, the implied
35
// warranties of merchantability and fitness for a particular purpose are disclaimed.
36
// In no event shall the Intel Corporation or contributors be liable for any direct,
37
// indirect, incidental, special, exemplary, or consequential damages
38
// (including, but not limited to, procurement of substitute goods or services;
39
// loss of use, data, or profits; or business interruption) however caused
40
// and on any theory of liability, whether in contract, strict liability,
41
// or tort (including negligence or otherwise) arising in any way out of
42
// the use of this software, even if advised of the possibility of such damage.
47
#pragma OPENCL EXTENSION cl_amd_fp64:enable
48
#elif defined (cl_khr_fp64)
49
#pragma OPENCL EXTENSION cl_khr_fp64:enable
53
#if defined OP_NORM_INF_MASK
65
#define MIN_VAL -32768
68
#define MIN_VAL INT_MIN
69
#define MAX_VAL INT_MAX
71
#define MIN_VAL (-FLT_MAX)
72
#define MAX_VAL FLT_MAX
74
#define MIN_VAL (-DBL_MAX)
75
#define MAX_VAL DBL_MAX
81
#endif // min/max stuff
90
#define MASK_INDEX int mask_index = id + mask_offset;
92
#define MASK_INDEX int mask_index = mad24(id / cols, mask_step, mask_offset + (id % cols))
96
#define loadpix(addr) *(__global const srcT *)(addr)
97
#define storepix(val, addr) *(__global dstT *)(addr) = val
99
#define srcTSIZE (int)sizeof(srcT)
101
#define srcTSIZE (int)sizeof(srcT1)
103
#define dstTSIZE (int)sizeof(dstT)
105
#define loadpix(addr) vload3(0, (__global const srcT1 *)(addr))
106
#define storepix(val, addr) vstore3(val, 0, (__global dstT1 *)(addr))
107
#define srcTSIZE ((int)sizeof(srcT1)*3)
108
#define dstTSIZE ((int)sizeof(dstT1)*3)
112
#define SUM_ABS(a) convertFromU(abs(a))
113
#define SUM_ABS2(a, b) convertFromU(abs_diff(a, b))
115
#define SUM_ABS(a) fabs(a)
116
#define SUM_ABS2(a, b) fabs(a - b)
121
#define EXTRA_PARAMS , __global const uchar * mask, int mask_step, int mask_offset, __global const uchar * src2ptr, int src2_step, int src2_offset
123
#define EXTRA_PARAMS , __global const uchar * mask, int mask_step, int mask_offset
127
#define EXTRA_PARAMS , __global const uchar * src2ptr, int src2_step, int src2_offset
133
// accumulative reduction stuff
134
#if defined OP_SUM || defined OP_SUM_ABS || defined OP_SUM_SQR || defined OP_DOT
138
#define FUNC(a, b, c) a = mad24(b, c, a)
140
#define FUNC(a, b, c) a = mad(b, c, a)
144
#define FUNC(a, b) a += b
146
#elif defined OP_SUM_ABS
147
#define FUNC(a, b) a += SUM_ABS(b)
149
#elif defined OP_SUM_SQR
151
#define FUNC(a, b) a = mad24(b, b, a)
153
#define FUNC(a, b) a = mad(b, b, a)
158
#define DECLARE_LOCAL_MEM \
159
__local dstT localmem[WGS2_ALIGNED], localmem2[WGS2_ALIGNED]
160
#define DEFINE_ACCUMULATOR \
161
dstT accumulator = (dstT)(0), accumulator2 = (dstT)(0)
163
#define DECLARE_LOCAL_MEM \
164
__local dstT localmem[WGS2_ALIGNED]
165
#define DEFINE_ACCUMULATOR \
166
dstT accumulator = (dstT)(0)
171
#define PROCESS_ELEMS \
172
dstT temp = convertToDT(loadpix(srcptr + src_index)); \
173
dstT temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
174
temp = SUM_ABS2(temp, temp2); \
175
temp2 = SUM_ABS(temp2); \
176
FUNC(accumulator2, temp2); \
177
FUNC(accumulator, temp)
179
#define PROCESS_ELEMS \
180
dstT temp = convertToDT(loadpix(srcptr + src_index)); \
181
dstT temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
182
temp = SUM_ABS2(temp, temp2); \
183
FUNC(accumulator, temp)
186
#define PROCESS_ELEMS \
187
dstT temp = convertToDT(loadpix(srcptr + src_index)); \
188
FUNC(accumulator, temp)
192
#define REDUCE_GLOBAL \
194
if (mask[mask_index]) \
200
#ifdef HAVE_SRC2_CONT
201
#define SRC2_INDEX int src2_index = mad24(id, srcTSIZE, src2_offset);
203
#define SRC2_INDEX int src2_index = mad24(id / cols, src2_step, mad24(id % cols, srcTSIZE, src2_offset))
207
#define REDUCE_GLOBAL \
209
dstTK temp = convertToDT(loadpix(srcptr + src_index)), temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
210
FUNC(accumulator, temp, temp2)
212
#define REDUCE_GLOBAL \
214
dstTK temp = convertToDT(loadpix(srcptr + src_index)), temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
215
FUNC(accumulator, temp.s0, temp2.s0); \
216
FUNC(accumulator, temp.s1, temp2.s1)
218
#define REDUCE_GLOBAL \
220
dstTK temp = convertToDT(loadpix(srcptr + src_index)), temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
221
FUNC(accumulator, temp.s0, temp2.s0); \
222
FUNC(accumulator, temp.s1, temp2.s1); \
223
FUNC(accumulator, temp.s2, temp2.s2); \
224
FUNC(accumulator, temp.s3, temp2.s3)
226
#define REDUCE_GLOBAL \
228
dstTK temp = convertToDT(loadpix(srcptr + src_index)), temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
229
FUNC(accumulator, temp.s0, temp2.s0); \
230
FUNC(accumulator, temp.s1, temp2.s1); \
231
FUNC(accumulator, temp.s2, temp2.s2); \
232
FUNC(accumulator, temp.s3, temp2.s3); \
233
FUNC(accumulator, temp.s4, temp2.s4); \
234
FUNC(accumulator, temp.s5, temp2.s5); \
235
FUNC(accumulator, temp.s6, temp2.s6); \
236
FUNC(accumulator, temp.s7, temp2.s7)
238
#define REDUCE_GLOBAL \
240
dstTK temp = convertToDT(loadpix(srcptr + src_index)), temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
241
FUNC(accumulator, temp.s0, temp2.s0); \
242
FUNC(accumulator, temp.s1, temp2.s1); \
243
FUNC(accumulator, temp.s2, temp2.s2); \
244
FUNC(accumulator, temp.s3, temp2.s3); \
245
FUNC(accumulator, temp.s4, temp2.s4); \
246
FUNC(accumulator, temp.s5, temp2.s5); \
247
FUNC(accumulator, temp.s6, temp2.s6); \
248
FUNC(accumulator, temp.s7, temp2.s7); \
249
FUNC(accumulator, temp.s8, temp2.s8); \
250
FUNC(accumulator, temp.s9, temp2.s9); \
251
FUNC(accumulator, temp.sA, temp2.sA); \
252
FUNC(accumulator, temp.sB, temp2.sB); \
253
FUNC(accumulator, temp.sC, temp2.sC); \
254
FUNC(accumulator, temp.sD, temp2.sD); \
255
FUNC(accumulator, temp.sE, temp2.sE); \
256
FUNC(accumulator, temp.sF, temp2.sF)
259
#else // sum or norm with 2 args
261
#ifdef OP_CALC2 // norm relative
263
#define REDUCE_GLOBAL \
264
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
265
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
266
temp = SUM_ABS2(temp, temp2); \
267
temp2 = SUM_ABS(temp2); \
268
FUNC(accumulator, temp); \
269
FUNC(accumulator2, temp2)
271
#define REDUCE_GLOBAL \
272
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
273
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
274
temp = SUM_ABS2(temp, temp2); \
275
temp2 = SUM_ABS(temp2); \
276
FUNC(accumulator, temp.s0); \
277
FUNC(accumulator, temp.s1); \
278
FUNC(accumulator2, temp2.s0); \
279
FUNC(accumulator2, temp2.s1)
281
#define REDUCE_GLOBAL \
282
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
283
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
284
temp = SUM_ABS2(temp, temp2); \
285
temp2 = SUM_ABS(temp2); \
286
FUNC(accumulator, temp.s0); \
287
FUNC(accumulator, temp.s1); \
288
FUNC(accumulator, temp.s2); \
289
FUNC(accumulator, temp.s3); \
290
FUNC(accumulator2, temp2.s0); \
291
FUNC(accumulator2, temp2.s1); \
292
FUNC(accumulator2, temp2.s2); \
293
FUNC(accumulator2, temp2.s3)
295
#define REDUCE_GLOBAL \
296
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
297
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
298
temp = SUM_ABS2(temp, temp2); \
299
temp2 = SUM_ABS(temp2); \
300
FUNC(accumulator, temp.s0); \
301
FUNC(accumulator, temp.s1); \
302
FUNC(accumulator, temp.s2); \
303
FUNC(accumulator, temp.s3); \
304
FUNC(accumulator, temp.s4); \
305
FUNC(accumulator, temp.s5); \
306
FUNC(accumulator, temp.s6); \
307
FUNC(accumulator, temp.s7); \
308
FUNC(accumulator2, temp2.s0); \
309
FUNC(accumulator2, temp2.s1); \
310
FUNC(accumulator2, temp2.s2); \
311
FUNC(accumulator2, temp2.s3); \
312
FUNC(accumulator2, temp2.s4); \
313
FUNC(accumulator2, temp2.s5); \
314
FUNC(accumulator2, temp2.s6); \
315
FUNC(accumulator2, temp2.s7)
317
#define REDUCE_GLOBAL \
318
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
319
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
320
temp = SUM_ABS2(temp, temp2); \
321
temp2 = SUM_ABS(temp2); \
322
FUNC(accumulator, temp.s0); \
323
FUNC(accumulator, temp.s1); \
324
FUNC(accumulator, temp.s2); \
325
FUNC(accumulator, temp.s3); \
326
FUNC(accumulator, temp.s4); \
327
FUNC(accumulator, temp.s5); \
328
FUNC(accumulator, temp.s6); \
329
FUNC(accumulator, temp.s7); \
330
FUNC(accumulator, temp.s8); \
331
FUNC(accumulator, temp.s9); \
332
FUNC(accumulator, temp.sA); \
333
FUNC(accumulator, temp.sB); \
334
FUNC(accumulator, temp.sC); \
335
FUNC(accumulator, temp.sD); \
336
FUNC(accumulator, temp.sE); \
337
FUNC(accumulator, temp.sF); \
338
FUNC(accumulator2, temp2.s0); \
339
FUNC(accumulator2, temp2.s1); \
340
FUNC(accumulator2, temp2.s2); \
341
FUNC(accumulator2, temp2.s3); \
342
FUNC(accumulator2, temp2.s4); \
343
FUNC(accumulator2, temp2.s5); \
344
FUNC(accumulator2, temp2.s6); \
345
FUNC(accumulator2, temp2.s7); \
346
FUNC(accumulator2, temp2.s8); \
347
FUNC(accumulator2, temp2.s9); \
348
FUNC(accumulator2, temp2.sA); \
349
FUNC(accumulator2, temp2.sB); \
350
FUNC(accumulator2, temp2.sC); \
351
FUNC(accumulator2, temp2.sD); \
352
FUNC(accumulator2, temp2.sE); \
353
FUNC(accumulator2, temp2.sF)
355
#else // norm with 2 args
357
#define REDUCE_GLOBAL \
358
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
359
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
360
temp = SUM_ABS2(temp, temp2); \
361
FUNC(accumulator, temp)
363
#define REDUCE_GLOBAL \
364
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
365
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
366
temp = SUM_ABS2(temp, temp2); \
367
FUNC(accumulator, temp.s0); \
368
FUNC(accumulator, temp.s1)
370
#define REDUCE_GLOBAL \
371
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
372
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
373
temp = SUM_ABS2(temp, temp2); \
374
FUNC(accumulator, temp.s0); \
375
FUNC(accumulator, temp.s1); \
376
FUNC(accumulator, temp.s2); \
377
FUNC(accumulator, temp.s3)
379
#define REDUCE_GLOBAL \
380
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
381
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
382
temp = SUM_ABS2(temp, temp2); \
383
FUNC(accumulator, temp.s0); \
384
FUNC(accumulator, temp.s1); \
385
FUNC(accumulator, temp.s2); \
386
FUNC(accumulator, temp.s3); \
387
FUNC(accumulator, temp.s4); \
388
FUNC(accumulator, temp.s5); \
389
FUNC(accumulator, temp.s6); \
390
FUNC(accumulator, temp.s7)
392
#define REDUCE_GLOBAL \
393
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
394
dstTK temp2 = convertToDT(loadpix(src2ptr + src2_index)); \
395
temp = SUM_ABS2(temp, temp2); \
396
FUNC(accumulator, temp.s0); \
397
FUNC(accumulator, temp.s1); \
398
FUNC(accumulator, temp.s2); \
399
FUNC(accumulator, temp.s3); \
400
FUNC(accumulator, temp.s4); \
401
FUNC(accumulator, temp.s5); \
402
FUNC(accumulator, temp.s6); \
403
FUNC(accumulator, temp.s7); \
404
FUNC(accumulator, temp.s8); \
405
FUNC(accumulator, temp.s9); \
406
FUNC(accumulator, temp.sA); \
407
FUNC(accumulator, temp.sB); \
408
FUNC(accumulator, temp.sC); \
409
FUNC(accumulator, temp.sD); \
410
FUNC(accumulator, temp.sE); \
411
FUNC(accumulator, temp.sF)
417
#define REDUCE_GLOBAL \
418
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
419
FUNC(accumulator, temp)
421
#define REDUCE_GLOBAL \
422
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
423
FUNC(accumulator, temp.s0); \
424
FUNC(accumulator, temp.s1)
426
#define REDUCE_GLOBAL \
427
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
428
FUNC(accumulator, temp.s0); \
429
FUNC(accumulator, temp.s1); \
430
FUNC(accumulator, temp.s2); \
431
FUNC(accumulator, temp.s3)
433
#define REDUCE_GLOBAL \
434
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
435
FUNC(accumulator, temp.s0); \
436
FUNC(accumulator, temp.s1); \
437
FUNC(accumulator, temp.s2); \
438
FUNC(accumulator, temp.s3); \
439
FUNC(accumulator, temp.s4); \
440
FUNC(accumulator, temp.s5); \
441
FUNC(accumulator, temp.s6); \
442
FUNC(accumulator, temp.s7)
444
#define REDUCE_GLOBAL \
445
dstTK temp = convertToDT(loadpix(srcptr + src_index)); \
446
FUNC(accumulator, temp.s0); \
447
FUNC(accumulator, temp.s1); \
448
FUNC(accumulator, temp.s2); \
449
FUNC(accumulator, temp.s3); \
450
FUNC(accumulator, temp.s4); \
451
FUNC(accumulator, temp.s5); \
452
FUNC(accumulator, temp.s6); \
453
FUNC(accumulator, temp.s7); \
454
FUNC(accumulator, temp.s8); \
455
FUNC(accumulator, temp.s9); \
456
FUNC(accumulator, temp.sA); \
457
FUNC(accumulator, temp.sB); \
458
FUNC(accumulator, temp.sC); \
459
FUNC(accumulator, temp.sD); \
460
FUNC(accumulator, temp.sE); \
461
FUNC(accumulator, temp.sF)
467
#define SET_LOCAL_1 \
468
localmem[lid] = accumulator; \
469
localmem2[lid] = accumulator2
470
#define REDUCE_LOCAL_1 \
471
localmem[lid - WGS2_ALIGNED] += accumulator; \
472
localmem2[lid - WGS2_ALIGNED] += accumulator2
473
#define REDUCE_LOCAL_2 \
474
localmem[lid] += localmem[lid2]; \
475
localmem2[lid] += localmem2[lid2]
476
#define CALC_RESULT \
477
storepix(localmem[0], dstptr + dstTSIZE * gid); \
478
storepix(localmem2[0], dstptr + mad24(groupnum, dstTSIZE, dstTSIZE * gid))
480
#define SET_LOCAL_1 \
481
localmem[lid] = accumulator
482
#define REDUCE_LOCAL_1 \
483
localmem[lid - WGS2_ALIGNED] += accumulator
484
#define REDUCE_LOCAL_2 \
485
localmem[lid] += localmem[lid2]
486
#define CALC_RESULT \
487
storepix(localmem[0], dstptr + dstTSIZE * gid)
490
// countNonZero stuff
491
#elif defined OP_COUNT_NON_ZERO
493
#define DECLARE_LOCAL_MEM \
494
__local dstT localmem[WGS2_ALIGNED]
495
#define DEFINE_ACCUMULATOR \
496
dstT accumulator = (dstT)(0); \
497
srcT1 zero = (srcT1)(0), one = (srcT1)(1)
499
#define REDUCE_GLOBAL \
500
accumulator += loadpix(srcptr + src_index) == zero ? zero : one
502
#define REDUCE_GLOBAL \
503
srcT value = loadpix(srcptr + src_index); \
504
accumulator += value.s0 == zero ? zero : one; \
505
accumulator += value.s1 == zero ? zero : one
507
#define REDUCE_GLOBAL \
508
srcT value = loadpix(srcptr + src_index); \
509
accumulator += value.s0 == zero ? zero : one; \
510
accumulator += value.s1 == zero ? zero : one; \
511
accumulator += value.s2 == zero ? zero : one; \
512
accumulator += value.s3 == zero ? zero : one
514
#define REDUCE_GLOBAL \
515
srcT value = loadpix(srcptr + src_index); \
516
accumulator += value.s0 == zero ? zero : one; \
517
accumulator += value.s1 == zero ? zero : one; \
518
accumulator += value.s2 == zero ? zero : one; \
519
accumulator += value.s3 == zero ? zero : one; \
520
accumulator += value.s4 == zero ? zero : one; \
521
accumulator += value.s5 == zero ? zero : one; \
522
accumulator += value.s6 == zero ? zero : one; \
523
accumulator += value.s7 == zero ? zero : one
525
#define REDUCE_GLOBAL \
526
srcT value = loadpix(srcptr + src_index); \
527
accumulator += value.s0 == zero ? zero : one; \
528
accumulator += value.s1 == zero ? zero : one; \
529
accumulator += value.s2 == zero ? zero : one; \
530
accumulator += value.s3 == zero ? zero : one; \
531
accumulator += value.s4 == zero ? zero : one; \
532
accumulator += value.s5 == zero ? zero : one; \
533
accumulator += value.s6 == zero ? zero : one; \
534
accumulator += value.s7 == zero ? zero : one; \
535
accumulator += value.s8 == zero ? zero : one; \
536
accumulator += value.s9 == zero ? zero : one; \
537
accumulator += value.sA == zero ? zero : one; \
538
accumulator += value.sB == zero ? zero : one; \
539
accumulator += value.sC == zero ? zero : one; \
540
accumulator += value.sD == zero ? zero : one; \
541
accumulator += value.sE == zero ? zero : one; \
542
accumulator += value.sF == zero ? zero : one
545
#define SET_LOCAL_1 \
546
localmem[lid] = accumulator
547
#define REDUCE_LOCAL_1 \
548
localmem[lid - WGS2_ALIGNED] += accumulator
549
#define REDUCE_LOCAL_2 \
550
localmem[lid] += localmem[lid2]
551
#define CALC_RESULT \
552
storepix(localmem[0], dstptr + dstTSIZE * gid)
555
#error "No operation"
560
#define EXTRA_PARAMS , __global uchar * src2ptr, int src2_step, int src2_offset
563
__kernel void reduce(__global const uchar * srcptr, int src_step, int src_offset, int cols,
564
int total, int groupnum, __global uchar * dstptr EXTRA_PARAMS)
566
int lid = get_local_id(0);
567
int gid = get_group_id(0);
568
int id = get_global_id(0) * kercn;
570
srcptr += src_offset;
572
src2ptr += src2_offset;
578
for (int grain = groupnum * WGS * kercn; id < total; id += grain)
581
int src_index = id * srcTSIZE;
583
int src_index = mad24(id / cols, src_step, mul24(id % cols, srcTSIZE));
586
#ifdef HAVE_SRC2_CONT
587
int src2_index = id * srcTSIZE;
589
int src2_index = mad24(id / cols, src2_step, mul24(id % cols, srcTSIZE));
595
if (lid < WGS2_ALIGNED)
599
barrier(CLK_LOCAL_MEM_FENCE);
601
if (lid >= WGS2_ALIGNED && total >= WGS2_ALIGNED)
605
barrier(CLK_LOCAL_MEM_FENCE);
607
for (int lsize = WGS2_ALIGNED >> 1; lsize > 0; lsize >>= 1)
611
int lid2 = lsize + lid;
614
barrier(CLK_LOCAL_MEM_FENCE);