ocl: compare with scalar
[profile/ivi/opencv.git] / modules / core / perf / opencl / perf_arithm.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the OpenCV Foundation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "perf_precomp.hpp"
43 #include "opencv2/ts/ocl_perf.hpp"
44
45 #ifdef HAVE_OPENCL
46
47 namespace cvtest {
48 namespace ocl {
49
50 ///////////// Lut ////////////////////////
51
52 typedef Size_MatType LUTFixture;
53
54 OCL_PERF_TEST_P(LUTFixture, LUT,
55           ::testing::Combine(OCL_TEST_SIZES,
56                              OCL_TEST_TYPES))
57 {
58     const Size_MatType_t params = GetParam();
59     const Size srcSize = get<0>(params);
60     const int type = get<1>(params), cn = CV_MAT_CN(type);
61
62     checkDeviceMaxMemoryAllocSize(srcSize, type);
63
64     UMat src(srcSize, CV_8UC(cn)), lut(1, 256, type);
65     int dstType = CV_MAKETYPE(lut.depth(), src.channels());
66     UMat dst(srcSize, dstType);
67
68     declare.in(src, lut, WARMUP_RNG).out(dst);
69
70     OCL_TEST_CYCLE() cv::LUT(src, lut, dst);
71
72     SANITY_CHECK(dst);
73 }
74
75 ///////////// Exp ////////////////////////
76
77 typedef Size_MatType ExpFixture;
78
79 OCL_PERF_TEST_P(ExpFixture, Exp, ::testing::Combine(
80                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
81 {
82     const Size_MatType_t params = GetParam();
83     const Size srcSize = get<0>(params);
84     const int type = get<1>(params);
85
86     checkDeviceMaxMemoryAllocSize(srcSize, type);
87
88     UMat src(srcSize, type), dst(srcSize, type);
89     declare.in(src).out(dst);
90     randu(src, 5, 16);
91
92     OCL_TEST_CYCLE() cv::exp(src, dst);
93
94     SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
95 }
96
97 ///////////// Log ////////////////////////
98
99 typedef Size_MatType LogFixture;
100
101 OCL_PERF_TEST_P(LogFixture, Log, ::testing::Combine(
102                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
103 {
104     const Size_MatType_t params = GetParam();
105     const Size srcSize = get<0>(params);
106     const int type = get<1>(params);
107
108     checkDeviceMaxMemoryAllocSize(srcSize, type);
109
110     UMat src(srcSize, type), dst(srcSize, type);
111     randu(src, 1, 10000);
112     declare.in(src).out(dst);
113
114     OCL_TEST_CYCLE() cv::log(src, dst);
115
116     SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
117 }
118
119 ///////////// Add ////////////////////////
120
121 typedef Size_MatType AddFixture;
122
123 OCL_PERF_TEST_P(AddFixture, Add,
124                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
125 {
126     const Size srcSize = GET_PARAM(0);
127     const int type = GET_PARAM(1);
128
129     checkDeviceMaxMemoryAllocSize(srcSize, type);
130
131     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
132     declare.in(src1, src2, WARMUP_RNG).out(dst);
133
134     OCL_TEST_CYCLE() cv::add(src1, src2, dst);
135
136     SANITY_CHECK(dst);
137 }
138
139 ///////////// Subtract ////////////////////////
140
141 typedef Size_MatType SubtractFixture;
142
143 OCL_PERF_TEST_P(SubtractFixture, Subtract,
144             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
145 {
146     const Size_MatType_t params = GetParam();
147     const Size srcSize = get<0>(params);
148     const int type = get<1>(params);
149
150     checkDeviceMaxMemoryAllocSize(srcSize, type);
151
152     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
153     declare.in(src1, src2, WARMUP_RNG).out(dst);
154
155     OCL_TEST_CYCLE() cv::subtract(src1, src2, dst);
156
157     SANITY_CHECK(dst);
158 }
159
160 ///////////// Mul ////////////////////////
161
162 typedef Size_MatType MulFixture;
163
164 OCL_PERF_TEST_P(MulFixture, Multiply, ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
165 {
166     const Size_MatType_t params = GetParam();
167     const Size srcSize = get<0>(params);
168     const int type = get<1>(params);
169
170     checkDeviceMaxMemoryAllocSize(srcSize, type);
171
172     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
173     declare.in(src1, src2, WARMUP_RNG).out(dst);
174
175     OCL_TEST_CYCLE() cv::multiply(src1, src2, dst);
176
177     SANITY_CHECK(dst);
178 }
179
180 ///////////// Div ////////////////////////
181
182 typedef Size_MatType DivFixture;
183
184 OCL_PERF_TEST_P(DivFixture, Divide,
185             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
186 {
187     const Size_MatType_t params = GetParam();
188     const Size srcSize = get<0>(params);
189     const int type = get<1>(params);
190
191     checkDeviceMaxMemoryAllocSize(srcSize, type);
192
193     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
194     declare.in(src1, src2, WARMUP_RNG).out(dst);
195
196     OCL_TEST_CYCLE() cv::divide(src1, src2, dst);
197
198     SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
199 }
200
201 ///////////// Absdiff ////////////////////////
202
203 typedef Size_MatType AbsDiffFixture;
204
205 OCL_PERF_TEST_P(AbsDiffFixture, Absdiff,
206             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
207 {
208     const Size_MatType_t params = GetParam();
209     const Size srcSize = get<0>(params);
210     const int type = get<1>(params);
211
212     checkDeviceMaxMemoryAllocSize(srcSize, type);
213
214     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
215     declare.in(src1, src2, WARMUP_RNG).in(dst);
216
217     OCL_TEST_CYCLE() cv::absdiff(src1, src2, dst);
218
219     SANITY_CHECK(dst);
220 }
221
222 ///////////// CartToPolar ////////////////////////
223
224 typedef Size_MatType CartToPolarFixture;
225
226 OCL_PERF_TEST_P(CartToPolarFixture, CartToPolar, ::testing::Combine(
227                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
228 {
229     const Size_MatType_t params = GetParam();
230     const Size srcSize = get<0>(params);
231     const int type = get<1>(params);
232
233     checkDeviceMaxMemoryAllocSize(srcSize, type);
234
235     UMat src1(srcSize, type), src2(srcSize, type),
236             dst1(srcSize, type), dst2(srcSize, type);
237     declare.in(src1, src2, WARMUP_RNG).out(dst1, dst2);
238
239     OCL_TEST_CYCLE() cv::cartToPolar(src1, src2, dst1, dst2);
240
241     SANITY_CHECK(dst1, 8e-3);
242     SANITY_CHECK(dst2, 8e-3);
243 }
244
245 ///////////// PolarToCart ////////////////////////
246
247 typedef Size_MatType PolarToCartFixture;
248
249 OCL_PERF_TEST_P(PolarToCartFixture, PolarToCart, ::testing::Combine(
250                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
251 {
252     const Size_MatType_t params = GetParam();
253     const Size srcSize = get<0>(params);
254     const int type = get<1>(params);
255
256     checkDeviceMaxMemoryAllocSize(srcSize, type);
257
258     UMat src1(srcSize, type), src2(srcSize, type),
259             dst1(srcSize, type), dst2(srcSize, type);
260     declare.in(src1, src2, WARMUP_RNG).out(dst1, dst2);
261
262     OCL_TEST_CYCLE() cv::polarToCart(src1, src2, dst1, dst2);
263
264     SANITY_CHECK(dst1, 5e-5);
265     SANITY_CHECK(dst2, 5e-5);
266 }
267
268 ///////////// Magnitude ////////////////////////
269
270 typedef Size_MatType MagnitudeFixture;
271
272 OCL_PERF_TEST_P(MagnitudeFixture, Magnitude, ::testing::Combine(
273                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
274 {
275     const Size_MatType_t params = GetParam();
276     const Size srcSize = get<0>(params);
277     const int type = get<1>(params);
278
279     checkDeviceMaxMemoryAllocSize(srcSize, type);
280
281     UMat src1(srcSize, type), src2(srcSize, type),
282             dst(srcSize, type);
283     declare.in(src1, src2, WARMUP_RNG).out(dst);
284
285     OCL_TEST_CYCLE() cv::magnitude(src1, src2, dst);
286
287     SANITY_CHECK(dst, 1e-6);
288 }
289
290 ///////////// Transpose ////////////////////////
291
292 typedef Size_MatType TransposeFixture;
293
294 OCL_PERF_TEST_P(TransposeFixture, Transpose, ::testing::Combine(
295                 OCL_TEST_SIZES, OCL_TEST_TYPES_134))
296 {
297     const Size_MatType_t params = GetParam();
298     const Size srcSize = get<0>(params);
299     const int type = get<1>(params);
300
301     checkDeviceMaxMemoryAllocSize(srcSize, type);
302
303     UMat src(srcSize, type), dst(srcSize, type);
304     declare.in(src, WARMUP_RNG).out(dst);
305
306     OCL_TEST_CYCLE() cv::transpose(src, dst);
307
308     SANITY_CHECK(dst);
309 }
310
311 ///////////// Flip ////////////////////////
312
313 enum
314 {
315     FLIP_BOTH = 0, FLIP_ROWS, FLIP_COLS
316 };
317
318 CV_ENUM(FlipType, FLIP_BOTH, FLIP_ROWS, FLIP_COLS)
319
320 typedef std::tr1::tuple<Size, MatType, FlipType> FlipParams;
321 typedef TestBaseWithParam<FlipParams> FlipFixture;
322
323 OCL_PERF_TEST_P(FlipFixture, Flip,
324             ::testing::Combine(OCL_TEST_SIZES,
325                                OCL_TEST_TYPES, FlipType::all()))
326 {
327     const FlipParams params = GetParam();
328     const Size srcSize = get<0>(params);
329     const int type = get<1>(params);
330     const int flipType = get<2>(params);
331
332     checkDeviceMaxMemoryAllocSize(srcSize, type);
333
334     UMat src(srcSize, type), dst(srcSize, type);
335     declare.in(src, WARMUP_RNG).out(dst);
336
337     OCL_TEST_CYCLE() cv::flip(src, dst, flipType - 1);
338
339     SANITY_CHECK(dst);
340 }
341
342 ///////////// minMaxLoc ////////////////////////
343
344 typedef Size_MatType MinMaxLocFixture;
345
346 OCL_PERF_TEST_P(MinMaxLocFixture, MinMaxLoc,
347             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
348 {
349     const Size_MatType_t params = GetParam();
350     const Size srcSize = get<0>(params);
351     const int type = get<1>(params);
352     bool onecn = CV_MAT_CN(type) == 1;
353
354     checkDeviceMaxMemoryAllocSize(srcSize, type);
355
356     UMat src(srcSize, type);;
357     declare.in(src, WARMUP_RNG);
358
359     double min_val = 0.0, max_val = 0.0;
360     Point min_loc, max_loc;
361
362     OCL_TEST_CYCLE() cv::minMaxLoc(src, &min_val, &max_val, onecn ? &min_loc : NULL,
363                                    onecn ? &max_loc : NULL);
364
365     ASSERT_GE(max_val, min_val);
366     SANITY_CHECK(min_val);
367     SANITY_CHECK(max_val);
368
369     int min_loc_x = min_loc.x, min_loc_y = min_loc.y, max_loc_x = max_loc.x,
370             max_loc_y = max_loc.y;
371     SANITY_CHECK(min_loc_x);
372     SANITY_CHECK(min_loc_y);
373     SANITY_CHECK(max_loc_x);
374     SANITY_CHECK(max_loc_y);
375 }
376
377 ///////////// Sum ////////////////////////
378
379 typedef Size_MatType SumFixture;
380
381 OCL_PERF_TEST_P(SumFixture, Sum,
382             ::testing::Combine(OCL_TEST_SIZES,
383                                OCL_TEST_TYPES_134))
384 {
385     const Size_MatType_t params = GetParam();
386     const Size srcSize = get<0>(params);
387     const int type = get<1>(params), depth = CV_MAT_DEPTH(type);
388
389     checkDeviceMaxMemoryAllocSize(srcSize, type);
390
391     UMat src(srcSize, type);
392     Scalar result;
393     randu(src, 0, 60);
394     declare.in(src);
395
396     OCL_TEST_CYCLE() result = cv::sum(src);
397
398     if (depth >= CV_32F)
399         SANITY_CHECK(result, 1e-6, ERROR_RELATIVE);
400     else
401         SANITY_CHECK(result);
402 }
403
404 ///////////// countNonZero ////////////////////////
405
406 typedef Size_MatType CountNonZeroFixture;
407
408 OCL_PERF_TEST_P(CountNonZeroFixture, CountNonZero,
409                 ::testing::Combine(OCL_TEST_SIZES,
410                                OCL_PERF_ENUM(CV_8UC1, CV_32FC1)))
411 {
412     const Size_MatType_t params = GetParam();
413     const Size srcSize = get<0>(params);
414     const int type = get<1>(params);
415
416     checkDeviceMaxMemoryAllocSize(srcSize, type);
417
418     UMat src(srcSize, type);
419     int result = 0;
420     randu(src, 0, 10);
421     declare.in(src);
422
423     OCL_TEST_CYCLE() result = cv::countNonZero(src);
424
425     SANITY_CHECK(result);
426 }
427
428 ///////////// Phase ////////////////////////
429
430 typedef Size_MatType PhaseFixture;
431
432 OCL_PERF_TEST_P(PhaseFixture, Phase, ::testing::Combine(
433                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
434 {
435     const Size_MatType_t params = GetParam();
436     const Size srcSize = get<0>(params);
437     const int type = get<1>(params);
438
439     checkDeviceMaxMemoryAllocSize(srcSize, type);
440
441     UMat src1(srcSize, type), src2(srcSize, type),
442             dst(srcSize, type);
443     declare.in(src1, src2, WARMUP_RNG).out(dst);
444
445     OCL_TEST_CYCLE() cv::phase(src1, src2, dst, 1);
446
447     SANITY_CHECK(dst, 1e-2);
448 }
449
450 ///////////// bitwise_and ////////////////////////
451
452 typedef Size_MatType BitwiseAndFixture;
453
454 OCL_PERF_TEST_P(BitwiseAndFixture, Bitwise_and,
455             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
456 {
457     const Size_MatType_t params = GetParam();
458     const Size srcSize = get<0>(params);
459     const int type = get<1>(params);
460
461     checkDeviceMaxMemoryAllocSize(srcSize, type);
462
463     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
464     declare.in(src1, src2, WARMUP_RNG).out(dst);
465
466     OCL_TEST_CYCLE() cv::bitwise_and(src1, src2, dst);
467
468     SANITY_CHECK(dst);
469 }
470
471 ///////////// bitwise_xor ////////////////////////
472
473 typedef Size_MatType BitwiseXorFixture;
474
475 OCL_PERF_TEST_P(BitwiseXorFixture, Bitwise_xor,
476             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
477 {
478     const Size_MatType_t params = GetParam();
479     const Size srcSize = get<0>(params);
480     const int type = get<1>(params);
481
482     checkDeviceMaxMemoryAllocSize(srcSize, type);
483
484     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
485     declare.in(src1, src2, WARMUP_RNG).out(dst);
486
487     OCL_TEST_CYCLE() cv::bitwise_xor(src1, src2, dst);
488
489     SANITY_CHECK(dst);
490 }
491
492 ///////////// bitwise_or ////////////////////////
493
494 typedef Size_MatType BitwiseOrFixture;
495
496 OCL_PERF_TEST_P(BitwiseOrFixture, Bitwise_or,
497             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
498 {
499     const Size_MatType_t params = GetParam();
500     const Size srcSize = get<0>(params);
501     const int type = get<1>(params);
502
503     checkDeviceMaxMemoryAllocSize(srcSize, type);
504
505     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
506     declare.in(src1, src2, WARMUP_RNG).out(dst);
507
508     OCL_TEST_CYCLE() cv::bitwise_or(src1, src2, dst);
509
510     SANITY_CHECK(dst);
511 }
512
513 ///////////// bitwise_not ////////////////////////
514
515 typedef Size_MatType BitwiseNotFixture;
516
517 OCL_PERF_TEST_P(BitwiseNotFixture, Bitwise_not,
518             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
519 {
520     const Size_MatType_t params = GetParam();
521     const Size srcSize = get<0>(params);
522     const int type = get<1>(params);
523
524     checkDeviceMaxMemoryAllocSize(srcSize, type);
525
526     UMat src(srcSize, type), dst(srcSize, type);
527     declare.in(src, WARMUP_RNG).out(dst);
528
529     OCL_TEST_CYCLE() cv::bitwise_not(src, dst);
530
531     SANITY_CHECK(dst);
532 }
533
534 ///////////// compare ////////////////////////
535
536 CV_ENUM(CmpCode, CMP_LT, CMP_LE, CMP_EQ, CMP_NE, CMP_GE, CMP_GT)
537
538 typedef std::tr1::tuple<Size, MatType, CmpCode> CompareParams;
539 typedef TestBaseWithParam<CompareParams> CompareFixture;
540
541 OCL_PERF_TEST_P(CompareFixture, Compare,
542             ::testing::Combine(OCL_TEST_SIZES,
543                                OCL_TEST_TYPES_134, CmpCode::all()))
544 {
545     const CompareParams params = GetParam();
546     const Size srcSize = get<0>(params);
547     const int type = get<1>(params);
548     const int cmpCode = get<2>(params);
549
550     checkDeviceMaxMemoryAllocSize(srcSize, type);
551
552     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, CV_8UC(CV_MAT_CN(type)));
553     declare.in(src1, src2, WARMUP_RNG).out(dst);
554
555     OCL_TEST_CYCLE() cv::compare(src1, src2, dst, cmpCode);
556
557     SANITY_CHECK(dst);
558 }
559
560 OCL_PERF_TEST_P(CompareFixture, CompareScalar,
561             ::testing::Combine(OCL_TEST_SIZES,
562                                OCL_TEST_TYPES_134, CmpCode::all()))
563 {
564     const CompareParams params = GetParam();
565     const Size srcSize = get<0>(params);
566     const int type = get<1>(params);
567     const int cmpCode = get<2>(params);
568
569     checkDeviceMaxMemoryAllocSize(srcSize, type);
570
571     UMat src1(srcSize, type), dst(srcSize, CV_8UC(CV_MAT_CN(type)));
572     declare.in(src1, WARMUP_RNG).out(dst);
573
574     OCL_TEST_CYCLE() cv::compare(src1, 32, dst, cmpCode);
575
576     SANITY_CHECK(dst);
577 }
578
579 ///////////// pow ////////////////////////
580
581 typedef Size_MatType PowFixture;
582
583 OCL_PERF_TEST_P(PowFixture, Pow, ::testing::Combine(
584                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
585 {
586     const Size_MatType_t params = GetParam();
587     const Size srcSize = get<0>(params);
588     const int type = get<1>(params);
589
590     checkDeviceMaxMemoryAllocSize(srcSize, type);
591
592     UMat src(srcSize, type), dst(srcSize, type);
593     randu(src, -100, 100);
594     declare.in(src).out(dst);
595
596     OCL_TEST_CYCLE() cv::pow(src, -2.0, dst);
597
598     SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
599 }
600
601 ///////////// AddWeighted////////////////////////
602
603 typedef Size_MatType AddWeightedFixture;
604
605 OCL_PERF_TEST_P(AddWeightedFixture, AddWeighted,
606             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134))
607 {
608     const Size_MatType_t params = GetParam();
609     const Size srcSize = get<0>(params);
610     const int type = get<1>(params), depth = CV_MAT_DEPTH(type);
611
612     checkDeviceMaxMemoryAllocSize(srcSize, type);
613
614     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
615     declare.in(src1, src2, WARMUP_RNG).out(dst);
616     double alpha = 2.0, beta = 1.0, gama = 3.0;
617
618     OCL_TEST_CYCLE() cv::addWeighted(src1, alpha, src2, beta, gama, dst);
619
620     if (depth >= CV_32F)
621         SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
622     else
623         SANITY_CHECK(dst);
624 }
625
626 ///////////// Sqrt ///////////////////////
627
628 typedef Size_MatType SqrtFixture;
629
630 OCL_PERF_TEST_P(SqrtFixture, Sqrt, ::testing::Combine(
631                 OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
632 {
633     const Size_MatType_t params = GetParam();
634     const Size srcSize = get<0>(params);
635     const int type = get<1>(params);
636
637     checkDeviceMaxMemoryAllocSize(srcSize, type);
638
639     UMat src(srcSize, type), dst(srcSize, type);
640     randu(src, 0, 1000);
641     declare.in(src).out(dst);
642
643     OCL_TEST_CYCLE() cv::sqrt(src, dst);
644
645     SANITY_CHECK(dst, 1e-6, ERROR_RELATIVE);
646 }
647
648 ///////////// SetIdentity ////////////////////////
649
650 typedef Size_MatType SetIdentityFixture;
651
652 OCL_PERF_TEST_P(SetIdentityFixture, SetIdentity,
653             ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
654 {
655     const Size_MatType_t params = GetParam();
656     const Size srcSize = get<0>(params);
657     const int type = get<1>(params);
658
659     checkDeviceMaxMemoryAllocSize(srcSize, type);
660
661     UMat dst(srcSize, type);
662     declare.out(dst);
663
664     OCL_TEST_CYCLE() cv::setIdentity(dst, cv::Scalar::all(181));
665
666     SANITY_CHECK(dst);
667 }
668
669 ///////////// MeanStdDev ////////////////////////
670
671 typedef Size_MatType MeanStdDevFixture;
672
673 OCL_PERF_TEST_P(MeanStdDevFixture, MeanStdDev,
674                 ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3),
675                                    OCL_TEST_TYPES_134))
676 {
677     const Size_MatType_t params = GetParam();
678     const Size srcSize = get<0>(params);
679     const int type = get<1>(params);
680     const double eps = 2e-5;
681
682     checkDeviceMaxMemoryAllocSize(srcSize, type);
683
684     UMat src(srcSize, type);
685     Scalar mean, stddev;
686     declare.in(src, WARMUP_RNG);
687
688     OCL_TEST_CYCLE() cv::meanStdDev(src, mean, stddev);
689
690     double mean0 = mean[0], mean1 = mean[1], mean2 = mean[2], mean3 = mean[3];
691     double stddev0 = stddev[0], stddev1 = stddev[1], stddev2 = stddev[2], stddev3 = stddev[3];
692
693     SANITY_CHECK(mean0, eps, ERROR_RELATIVE);
694     SANITY_CHECK(mean1, eps, ERROR_RELATIVE);
695     SANITY_CHECK(mean2, eps, ERROR_RELATIVE);
696     SANITY_CHECK(mean3, eps, ERROR_RELATIVE);
697     SANITY_CHECK(stddev0, eps, ERROR_RELATIVE);
698     SANITY_CHECK(stddev1, eps, ERROR_RELATIVE);
699     SANITY_CHECK(stddev2, eps, ERROR_RELATIVE);
700     SANITY_CHECK(stddev3, eps, ERROR_RELATIVE);
701 }
702
703 ///////////// Norm ////////////////////////
704
705 CV_ENUM(NormType, NORM_INF, NORM_L1, NORM_L2)
706
707 typedef std::tr1::tuple<Size, MatType, NormType> NormParams;
708 typedef TestBaseWithParam<NormParams> NormFixture;
709
710 OCL_PERF_TEST_P(NormFixture, Norm,
711                 ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3),
712                                    OCL_TEST_TYPES_134, NormType::all()))
713 {
714     const NormParams params = GetParam();
715     const Size srcSize = get<0>(params);
716     const int type = get<1>(params);
717     const int normType = get<2>(params);
718
719     checkDeviceMaxMemoryAllocSize(srcSize, type);
720
721     UMat src1(srcSize, type), src2(srcSize, type);
722     double res;
723     declare.in(src1, src2, WARMUP_RNG);
724
725     OCL_TEST_CYCLE() res = cv::norm(src1, src2, normType);
726
727     SANITY_CHECK(res, 1e-5, ERROR_RELATIVE);
728 }
729
730 ///////////// UMat::dot ////////////////////////
731
732 typedef Size_MatType UMatDotFixture;
733
734 OCL_PERF_TEST_P(UMatDotFixture, UMatDot,
735             ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3),
736                                OCL_TEST_TYPES_134))
737 {
738     const Size_MatType_t params = GetParam();
739     const Size srcSize = get<0>(params);
740     const int type = get<1>(params);
741     double r = 0.0;
742
743     checkDeviceMaxMemoryAllocSize(srcSize, type);
744
745     UMat src1(srcSize, type), src2(srcSize, type);
746     declare.in(src1, src2, WARMUP_RNG);
747
748     OCL_TEST_CYCLE() r = src1.dot(src2);
749
750     SANITY_CHECK(r, 1e-5, ERROR_RELATIVE);
751 }
752
753 ///////////// Repeat ////////////////////////
754
755 typedef Size_MatType RepeatFixture;
756
757 OCL_PERF_TEST_P(RepeatFixture, Repeat,
758             ::testing::Combine(OCL_PERF_ENUM(OCL_SIZE_1, OCL_SIZE_2, OCL_SIZE_3), OCL_TEST_TYPES))
759 {
760     const Size_MatType_t params = GetParam();
761     const Size srcSize = get<0>(params);
762     const int type = get<1>(params), nx = 2, ny = 2;
763
764     checkDeviceMaxMemoryAllocSize(srcSize, type);
765
766     UMat src(srcSize, type), dst(Size(srcSize.width * nx, srcSize.height * ny), type);
767     declare.in(src, WARMUP_RNG).out(dst);
768
769     OCL_TEST_CYCLE() cv::repeat(src, nx, ny, dst);
770
771     SANITY_CHECK(dst);
772 }
773
774 ///////////// Min ////////////////////////
775
776 typedef Size_MatType MinFixture;
777
778 OCL_PERF_TEST_P(MinFixture, Min,
779                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
780 {
781     const Size_MatType_t params = GetParam();
782     const Size srcSize = get<0>(params);
783     const int type = get<1>(params);
784
785     checkDeviceMaxMemoryAllocSize(srcSize, type);
786
787     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
788     declare.in(src1, src2, WARMUP_RNG).out(dst);
789
790     OCL_TEST_CYCLE() cv::min(src1, src2, dst);
791
792     SANITY_CHECK(dst);
793 }
794
795 ///////////// Max ////////////////////////
796
797 typedef Size_MatType MaxFixture;
798
799 OCL_PERF_TEST_P(MaxFixture, Max,
800                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
801 {
802     const Size_MatType_t params = GetParam();
803     const Size srcSize = get<0>(params);
804     const int type = get<1>(params);
805
806     checkDeviceMaxMemoryAllocSize(srcSize, type);
807
808     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
809     declare.in(src1, src2, WARMUP_RNG).out(dst);
810
811     OCL_TEST_CYCLE() cv::max(src1, src2, dst);
812
813     SANITY_CHECK(dst);
814 }
815
816 ///////////// InRange ////////////////////////
817
818 typedef Size_MatType InRangeFixture;
819
820 OCL_PERF_TEST_P(InRangeFixture, InRange,
821                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
822 {
823     const Size_MatType_t params = GetParam();
824     const Size srcSize = get<0>(params);
825     const int type = get<1>(params);
826
827     checkDeviceMaxMemoryAllocSize(srcSize, type);
828
829     UMat src(srcSize, type), lb(srcSize, type), ub(srcSize, type), dst(srcSize, CV_8UC1);
830     declare.in(src, lb, ub, WARMUP_RNG).out(dst);
831
832     OCL_TEST_CYCLE() cv::inRange(src, lb, ub, dst);
833
834     SANITY_CHECK(dst);
835 }
836
837 ///////////// Normalize ////////////////////////
838
839 CV_ENUM(NormalizeModes, CV_MINMAX, CV_L2, CV_L1, CV_C)
840
841 typedef tuple<Size, MatType, NormalizeModes> NormalizeParams;
842 typedef TestBaseWithParam<NormalizeParams> NormalizeFixture;
843
844 OCL_PERF_TEST_P(NormalizeFixture, Normalize,
845                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES_134,
846                                    NormalizeModes::all()))
847 {
848     const NormalizeParams params = GetParam();
849     const Size srcSize = get<0>(params);
850     const int type = get<1>(params), mode = get<2>(params);
851
852     checkDeviceMaxMemoryAllocSize(srcSize, type);
853
854     UMat src(srcSize, type), dst(srcSize, type);
855     declare.in(src, WARMUP_RNG).out(dst);
856
857     OCL_TEST_CYCLE() cv::normalize(src, dst, 10, 110, mode);
858
859     SANITY_CHECK(dst, 5e-2);
860 }
861
862 ///////////// ConvertScaleAbs ////////////////////////
863
864 typedef Size_MatType ConvertScaleAbsFixture;
865
866 OCL_PERF_TEST_P(ConvertScaleAbsFixture, ConvertScaleAbs,
867                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
868 {
869     const Size_MatType_t params = GetParam();
870     const Size srcSize = get<0>(params);
871     const int type = get<1>(params), cn = CV_MAT_CN(type);
872
873     checkDeviceMaxMemoryAllocSize(srcSize, type);
874
875     UMat src(srcSize, type), dst(srcSize, CV_8UC(cn));
876     declare.in(src, WARMUP_RNG).out(dst);
877
878     OCL_TEST_CYCLE() cv::convertScaleAbs(src, dst, 0.5, 2);
879
880     SANITY_CHECK(dst);
881 }
882
883 ///////////// PatchNaNs ////////////////////////
884
885 typedef Size_MatType PatchNaNsFixture;
886
887 OCL_PERF_TEST_P(PatchNaNsFixture, PatchNaNs,
888                 ::testing::Combine(OCL_TEST_SIZES, OCL_PERF_ENUM(CV_32FC1, CV_32FC4)))
889 {
890     const Size_MatType_t params = GetParam();
891     Size srcSize = get<0>(params);
892     const int type = get<1>(params), cn = CV_MAT_CN(type);
893
894     checkDeviceMaxMemoryAllocSize(srcSize, type);
895
896     UMat src(srcSize, type);
897     declare.in(src, WARMUP_RNG).out(src);
898
899     // generating NaNs
900     {
901         Mat src_ = src.getMat(ACCESS_RW);
902         srcSize.width *= cn;
903         for (int y = 0; y < srcSize.height; ++y)
904         {
905             float * const ptr = src_.ptr<float>(y);
906             for (int x = 0; x < srcSize.width; ++x)
907                 ptr[x] = (x + y) % 2 == 0 ? std::numeric_limits<float>::quiet_NaN() : ptr[x];
908         }
909     }
910
911     OCL_TEST_CYCLE() cv::patchNaNs(src, 17.7);
912
913     SANITY_CHECK(src);
914 }
915
916
917 ///////////// ScaleAdd ////////////////////////
918
919 typedef Size_MatType ScaleAddFixture;
920
921 OCL_PERF_TEST_P(ScaleAddFixture, ScaleAdd,
922                 ::testing::Combine(OCL_TEST_SIZES, OCL_TEST_TYPES))
923 {
924     const Size_MatType_t params = GetParam();
925     const Size srcSize = get<0>(params);
926     const int type = get<1>(params);
927
928     checkDeviceMaxMemoryAllocSize(srcSize, type);
929
930     UMat src1(srcSize, type), src2(srcSize, type), dst(srcSize, type);
931     declare.in(src1, src2, WARMUP_RNG).out(dst);
932
933     OCL_TEST_CYCLE() cv::scaleAdd(src1, 0.6, src2, dst);
934
935     SANITY_CHECK(dst, 1e-6);
936 }
937
938 ///////////// PSNR ////////////////////////
939
940 typedef Size_MatType PSNRFixture;
941
942 OCL_PERF_TEST_P(PSNRFixture, PSNR,
943                 ::testing::Combine(OCL_TEST_SIZES, OCL_PERF_ENUM(CV_8UC1, CV_8UC4)))
944 {
945     const Size_MatType_t params = GetParam();
946     const Size srcSize = get<0>(params);
947     const int type = get<1>(params);
948
949     checkDeviceMaxMemoryAllocSize(srcSize, type);
950
951     double psnr = 0;
952     UMat src1(srcSize, type), src2(srcSize, type);
953     declare.in(src1, src2, WARMUP_RNG);
954
955     OCL_TEST_CYCLE() psnr = cv::PSNR(src1, src2);
956
957     SANITY_CHECK(psnr, 1e-4, ERROR_RELATIVE);
958 }
959
960 ///////////// Reduce ////////////////////////
961
962 CV_ENUM(ReduceMinMaxOp, CV_REDUCE_MIN, CV_REDUCE_MAX)
963
964 typedef tuple<Size, std::pair<MatType, MatType>, int, ReduceMinMaxOp> ReduceMinMaxParams;
965 typedef TestBaseWithParam<ReduceMinMaxParams> ReduceMinMaxFixture;
966
967 OCL_PERF_TEST_P(ReduceMinMaxFixture, Reduce,
968                 ::testing::Combine(OCL_TEST_SIZES,
969                                    OCL_PERF_ENUM(std::make_pair<MatType, MatType>(CV_8UC1, CV_8UC1),
970                                                  std::make_pair<MatType, MatType>(CV_32FC4, CV_32FC4)),
971                                    OCL_PERF_ENUM(0, 1),
972                                    ReduceMinMaxOp::all()))
973 {
974     const ReduceMinMaxParams params = GetParam();
975     const std::pair<MatType, MatType> types = get<1>(params);
976     const int stype = types.first, dtype = types.second,
977             dim = get<2>(params), op = get<3>(params);
978     const Size srcSize = get<0>(params),
979             dstSize(dim == 0 ? srcSize.width : 1, dim == 0 ? 1 : srcSize.height);
980     const double eps = CV_MAT_DEPTH(dtype) <= CV_32S ? 1 : 1e-5;
981
982     checkDeviceMaxMemoryAllocSize(srcSize, stype);
983     checkDeviceMaxMemoryAllocSize(srcSize, dtype);
984
985     UMat src(srcSize, stype), dst(dstSize, dtype);
986     declare.in(src, WARMUP_RNG).out(dst);
987
988     OCL_TEST_CYCLE() cv::reduce(src, dst, dim, op, dtype);
989
990     SANITY_CHECK(dst, eps);
991 }
992
993 CV_ENUM(ReduceAccOp, CV_REDUCE_SUM, CV_REDUCE_AVG)
994
995 typedef tuple<Size, std::pair<MatType, MatType>, int, ReduceAccOp> ReduceAccParams;
996 typedef TestBaseWithParam<ReduceAccParams> ReduceAccFixture;
997
998 OCL_PERF_TEST_P(ReduceAccFixture, Reduce,
999                 ::testing::Combine(OCL_TEST_SIZES,
1000                                    OCL_PERF_ENUM(std::make_pair<MatType, MatType>(CV_8UC4, CV_32SC4),
1001                                                  std::make_pair<MatType, MatType>(CV_32FC1, CV_32FC1)),
1002                                    OCL_PERF_ENUM(0, 1),
1003                                    ReduceAccOp::all()))
1004 {
1005     const ReduceAccParams params = GetParam();
1006     const std::pair<MatType, MatType> types = get<1>(params);
1007     const int stype = types.first, dtype = types.second,
1008             dim = get<2>(params), op = get<3>(params);
1009     const Size srcSize = get<0>(params),
1010             dstSize(dim == 0 ? srcSize.width : 1, dim == 0 ? 1 : srcSize.height);
1011     const double eps = CV_MAT_DEPTH(dtype) <= CV_32S ? 1 : 3e-4;
1012
1013     checkDeviceMaxMemoryAllocSize(srcSize, stype);
1014     checkDeviceMaxMemoryAllocSize(srcSize, dtype);
1015
1016     UMat src(srcSize, stype), dst(dstSize, dtype);
1017     declare.in(src, WARMUP_RNG).out(dst);
1018
1019     OCL_TEST_CYCLE() cv::reduce(src, dst, dim, op, dtype);
1020
1021     SANITY_CHECK(dst, eps);
1022 }
1023
1024 } } // namespace cvtest::ocl
1025
1026 #endif // HAVE_OPENCL