use vectors
[profile/ivi/opencv.git] / modules / ts / src / ocl_test.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 "precomp.hpp"
43
44 #include "opencv2/ts/ocl_test.hpp"
45
46 namespace cvtest {
47 namespace ocl {
48
49 using namespace cv;
50
51 int test_loop_times = 1; // TODO Read from command line / environment
52
53 #ifdef HAVE_OPENCL
54
55 #define DUMP_PROPERTY_XML(propertyName, propertyValue) \
56     do { \
57         std::stringstream ssName, ssValue;\
58         ssName << propertyName;\
59         ssValue << (propertyValue); \
60         ::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
61     } while (false)
62
63 #define DUMP_MESSAGE_STDOUT(msg) \
64     do { \
65         std::cout << msg << std::endl; \
66     } while (false)
67
68 static std::string bytesToStringRepr(size_t value)
69 {
70     size_t b = value % 1024;
71     value /= 1024;
72
73     size_t kb = value % 1024;
74     value /= 1024;
75
76     size_t mb = value % 1024;
77     value /= 1024;
78
79     size_t gb = value;
80
81     std::ostringstream stream;
82
83     if (gb > 0)
84         stream << gb << " GB ";
85     if (mb > 0)
86         stream << mb << " MB ";
87     if (kb > 0)
88         stream << kb << " kB ";
89     if (b > 0)
90         stream << b << " B";
91
92     return stream.str();
93 }
94
95 void dumpOpenCLDevice()
96 {
97     using namespace cv::ocl;
98
99     try
100     {
101         std::vector<PlatformInfo> platforms;
102         cv::ocl::getPlatfomsInfo(platforms);
103         if (platforms.size() > 0)
104         {
105             DUMP_MESSAGE_STDOUT("OpenCL Platforms: ");
106             for (size_t i = 0; i < platforms.size(); i++)
107             {
108                 const PlatformInfo* platform = &platforms[i];
109                 DUMP_MESSAGE_STDOUT("    " << platform->name().c_str());
110                 Device current_device;
111                 for (int j = 0; j < platform->deviceNumber(); j++)
112                 {
113                     platform->getDevice(current_device, j);
114                     const char* deviceTypeStr = current_device.type() == Device::TYPE_CPU
115                         ? ("CPU") : (current_device.type() == Device::TYPE_GPU ? current_device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown");
116                     DUMP_MESSAGE_STDOUT( "        " << deviceTypeStr << ": " << current_device.name().c_str() << " (" << current_device.version().c_str() << ")");
117                     DUMP_PROPERTY_XML( cv::format("cv_ocl_platform_%d_device_%d", (int)i, (int)j ),
118                         cv::format("(Platform=%s)(Type=%s)(Name=%s)(Version=%s)",
119                         platform->name().c_str(), deviceTypeStr, current_device.name().c_str(), current_device.version().c_str()) );
120                 }
121             }
122         }
123         else
124         {
125             DUMP_MESSAGE_STDOUT("OpenCL is not available");
126             DUMP_PROPERTY_XML("cv_ocl", "not available");
127             return;
128         }
129
130         const Device& device = Device::getDefault();
131         DUMP_MESSAGE_STDOUT("Current OpenCL device: ");
132
133 #if 0
134         DUMP_MESSAGE_STDOUT("    Platform = "<< device.getPlatform().name());
135         DUMP_PROPERTY_XML("cv_ocl_current_platformName", device.getPlatform().name());
136 #endif
137
138         const char* deviceTypeStr = device.type() == Device::TYPE_CPU
139             ? ("CPU") : (device.type() == Device::TYPE_GPU ? device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown");
140         DUMP_MESSAGE_STDOUT("    Type = "<< deviceTypeStr);
141         DUMP_PROPERTY_XML("cv_ocl_current_deviceType", deviceTypeStr);
142
143         DUMP_MESSAGE_STDOUT("    Name = "<< device.name());
144         DUMP_PROPERTY_XML("cv_ocl_current_deviceName", device.name());
145
146         DUMP_MESSAGE_STDOUT("    Version = " << device.version());
147         DUMP_PROPERTY_XML("cv_ocl_current_deviceVersion", device.version());
148
149         DUMP_MESSAGE_STDOUT("    Compute units = "<< device.maxComputeUnits());
150         DUMP_PROPERTY_XML("cv_ocl_current_maxComputeUnits", device.maxComputeUnits());
151
152         DUMP_MESSAGE_STDOUT("    Max work group size = "<< device.maxWorkGroupSize());
153         DUMP_PROPERTY_XML("cv_ocl_current_maxWorkGroupSize", device.maxWorkGroupSize());
154
155         std::string localMemorySizeStr = bytesToStringRepr(device.localMemSize());
156         DUMP_MESSAGE_STDOUT("    Local memory size = " << localMemorySizeStr);
157         DUMP_PROPERTY_XML("cv_ocl_current_localMemSize", device.localMemSize());
158
159         std::string maxMemAllocSizeStr = bytesToStringRepr(device.maxMemAllocSize());
160         DUMP_MESSAGE_STDOUT("    Max memory allocation size = "<< maxMemAllocSizeStr);
161         DUMP_PROPERTY_XML("cv_ocl_current_maxMemAllocSize", device.maxMemAllocSize());
162
163         const char* doubleSupportStr = device.doubleFPConfig() > 0 ? "Yes" : "No";
164         DUMP_MESSAGE_STDOUT("    Double support = "<< doubleSupportStr);
165         DUMP_PROPERTY_XML("cv_ocl_current_haveDoubleSupport", device.doubleFPConfig() > 0);
166
167         const char* isUnifiedMemoryStr = device.hostUnifiedMemory() ? "Yes" : "No";
168         DUMP_MESSAGE_STDOUT("    Host unified memory = "<< isUnifiedMemoryStr);
169         DUMP_PROPERTY_XML("cv_ocl_current_hostUnifiedMemory", device.hostUnifiedMemory());
170
171         const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No";
172         DUMP_MESSAGE_STDOUT("    Has AMD Blas = "<< haveAmdBlasStr);
173         DUMP_PROPERTY_XML("cv_ocl_current_AmdBlas", haveAmdBlas());
174
175         const char* haveAmdFftStr = haveAmdFft() ? "Yes" : "No";
176         DUMP_MESSAGE_STDOUT("    Has AMD Fft = "<< haveAmdFftStr);
177         DUMP_PROPERTY_XML("cv_ocl_current_AmdFft", haveAmdFft());
178
179
180         DUMP_MESSAGE_STDOUT("    Preferred vector width char = "<< device.preferredVectorWidthChar());
181         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthChar", device.preferredVectorWidthChar());
182
183         DUMP_MESSAGE_STDOUT("    Preferred vector width short = "<< device.preferredVectorWidthShort());
184         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthShort", device.preferredVectorWidthShort());
185
186         DUMP_MESSAGE_STDOUT("    Preferred vector width int = "<< device.preferredVectorWidthInt());
187         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthInt", device.preferredVectorWidthInt());
188
189         DUMP_MESSAGE_STDOUT("    Preferred vector width long = "<< device.preferredVectorWidthLong());
190         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthLong", device.preferredVectorWidthLong());
191
192         DUMP_MESSAGE_STDOUT("    Preferred vector width float = "<< device.preferredVectorWidthFloat());
193         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthFloat", device.preferredVectorWidthFloat());
194
195         DUMP_MESSAGE_STDOUT("    Preferred vector width double = "<< device.preferredVectorWidthDouble());
196         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthDouble", device.preferredVectorWidthDouble());
197     }
198     catch (...)
199     {
200         DUMP_MESSAGE_STDOUT("Exception. Can't dump OpenCL info");
201         DUMP_MESSAGE_STDOUT("OpenCL device not available");
202         DUMP_PROPERTY_XML("cv_ocl", "not available");
203     }
204 }
205 #undef DUMP_MESSAGE_STDOUT
206 #undef DUMP_PROPERTY_XML
207
208 #endif
209
210 Mat TestUtils::readImage(const String &fileName, int flags)
211 {
212     return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
213 }
214
215 Mat TestUtils::readImageType(const String &fname, int type)
216 {
217     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
218     if (CV_MAT_CN(type) == 4)
219     {
220         Mat temp;
221         cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
222         swap(src, temp);
223     }
224     src.convertTo(src, CV_MAT_DEPTH(type));
225     return src;
226 }
227
228 double TestUtils::checkNorm1(InputArray m, InputArray mask)
229 {
230     return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
231 }
232
233 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
234 {
235     return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
236 }
237
238 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
239 {
240     Mat diff;
241     matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
242     return std::abs(diff.at<float>(0, 0) - 1.f);
243 }
244
245 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
246 {
247     double final_test_result = 0.0;
248     size_t sz1 = ob1.size();
249     size_t sz2 = ob2.size();
250
251     if (sz1 != sz2)
252         return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
253     else
254     {
255         if (sz1 == 0 && sz2 == 0)
256             return 0;
257         cv::Mat cpu_result(sz, CV_8UC1);
258         cpu_result.setTo(0);
259
260         for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
261         {
262             cv::Mat cpu_result_roi(cpu_result, *r);
263             cpu_result_roi.setTo(1);
264             cpu_result.copyTo(cpu_result);
265         }
266         int cpu_area = cv::countNonZero(cpu_result > 0);
267
268         cv::Mat gpu_result(sz, CV_8UC1);
269         gpu_result.setTo(0);
270         for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
271         {
272             cv::Mat gpu_result_roi(gpu_result, *r2);
273             gpu_result_roi.setTo(1);
274             gpu_result.copyTo(gpu_result);
275         }
276
277         cv::Mat result_;
278         multiply(cpu_result, gpu_result, result_);
279         int result = cv::countNonZero(result_ > 0);
280         if (cpu_area!=0 && result!=0)
281             final_test_result = 1.0 - (double)result/(double)cpu_area;
282         else if(cpu_area==0 && result!=0)
283             final_test_result = -1;
284     }
285     return final_test_result;
286 }
287
288 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
289 {
290     Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
291
292     Mat diff, diff_thresh;
293     absdiff(gold, actual, diff);
294     diff.convertTo(diff, CV_32F);
295     threshold(diff, diff_thresh, eps, 255.0, cv::THRESH_BINARY);
296
297     if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 0)
298     {
299 #if 0
300         std::cout << "Source: " << std::endl << src << std::endl;
301         std::cout << "Expected: " << std::endl << gold << std::endl;
302         std::cout << "Actual: " << std::endl << actual << std::endl;
303 #endif
304
305         namedWindow("src", WINDOW_NORMAL);
306         namedWindow("gold", WINDOW_NORMAL);
307         namedWindow("actual", WINDOW_NORMAL);
308         namedWindow("diff", WINDOW_NORMAL);
309
310         imshow("src", src);
311         imshow("gold", gold);
312         imshow("actual", actual);
313         imshow("diff", diff);
314
315         cv::waitKey();
316     }
317 }
318
319 } } // namespace cvtest::ocl