ocl: change processing of OpenCL failures
[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         if (!device.available())
132             CV_ErrorNoReturn(CV_OpenCLInitError, "OpenCL device is not available");
133
134         DUMP_MESSAGE_STDOUT("Current OpenCL device: ");
135
136 #if 0
137         DUMP_MESSAGE_STDOUT("    Platform = "<< device.getPlatform().name());
138         DUMP_PROPERTY_XML("cv_ocl_current_platformName", device.getPlatform().name());
139 #endif
140
141         const char* deviceTypeStr = device.type() == Device::TYPE_CPU
142             ? ("CPU") : (device.type() == Device::TYPE_GPU ? device.hostUnifiedMemory() ? "iGPU" : "dGPU" : "unknown");
143         DUMP_MESSAGE_STDOUT("    Type = "<< deviceTypeStr);
144         DUMP_PROPERTY_XML("cv_ocl_current_deviceType", deviceTypeStr);
145
146         DUMP_MESSAGE_STDOUT("    Name = "<< device.name());
147         DUMP_PROPERTY_XML("cv_ocl_current_deviceName", device.name());
148
149         DUMP_MESSAGE_STDOUT("    Version = " << device.version());
150         DUMP_PROPERTY_XML("cv_ocl_current_deviceVersion", device.version());
151
152         DUMP_MESSAGE_STDOUT("    Compute units = "<< device.maxComputeUnits());
153         DUMP_PROPERTY_XML("cv_ocl_current_maxComputeUnits", device.maxComputeUnits());
154
155         DUMP_MESSAGE_STDOUT("    Max work group size = "<< device.maxWorkGroupSize());
156         DUMP_PROPERTY_XML("cv_ocl_current_maxWorkGroupSize", device.maxWorkGroupSize());
157
158         std::string localMemorySizeStr = bytesToStringRepr(device.localMemSize());
159         DUMP_MESSAGE_STDOUT("    Local memory size = " << localMemorySizeStr);
160         DUMP_PROPERTY_XML("cv_ocl_current_localMemSize", device.localMemSize());
161
162         std::string maxMemAllocSizeStr = bytesToStringRepr(device.maxMemAllocSize());
163         DUMP_MESSAGE_STDOUT("    Max memory allocation size = "<< maxMemAllocSizeStr);
164         DUMP_PROPERTY_XML("cv_ocl_current_maxMemAllocSize", device.maxMemAllocSize());
165
166         const char* doubleSupportStr = device.doubleFPConfig() > 0 ? "Yes" : "No";
167         DUMP_MESSAGE_STDOUT("    Double support = "<< doubleSupportStr);
168         DUMP_PROPERTY_XML("cv_ocl_current_haveDoubleSupport", device.doubleFPConfig() > 0);
169
170         const char* isUnifiedMemoryStr = device.hostUnifiedMemory() ? "Yes" : "No";
171         DUMP_MESSAGE_STDOUT("    Host unified memory = "<< isUnifiedMemoryStr);
172         DUMP_PROPERTY_XML("cv_ocl_current_hostUnifiedMemory", device.hostUnifiedMemory());
173
174         const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No";
175         DUMP_MESSAGE_STDOUT("    Has AMD Blas = "<< haveAmdBlasStr);
176         DUMP_PROPERTY_XML("cv_ocl_current_AmdBlas", haveAmdBlas());
177
178         const char* haveAmdFftStr = haveAmdFft() ? "Yes" : "No";
179         DUMP_MESSAGE_STDOUT("    Has AMD Fft = "<< haveAmdFftStr);
180         DUMP_PROPERTY_XML("cv_ocl_current_AmdFft", haveAmdFft());
181
182
183         DUMP_MESSAGE_STDOUT("    Preferred vector width char = "<< device.preferredVectorWidthChar());
184         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthChar", device.preferredVectorWidthChar());
185
186         DUMP_MESSAGE_STDOUT("    Preferred vector width short = "<< device.preferredVectorWidthShort());
187         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthShort", device.preferredVectorWidthShort());
188
189         DUMP_MESSAGE_STDOUT("    Preferred vector width int = "<< device.preferredVectorWidthInt());
190         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthInt", device.preferredVectorWidthInt());
191
192         DUMP_MESSAGE_STDOUT("    Preferred vector width long = "<< device.preferredVectorWidthLong());
193         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthLong", device.preferredVectorWidthLong());
194
195         DUMP_MESSAGE_STDOUT("    Preferred vector width float = "<< device.preferredVectorWidthFloat());
196         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthFloat", device.preferredVectorWidthFloat());
197
198         DUMP_MESSAGE_STDOUT("    Preferred vector width double = "<< device.preferredVectorWidthDouble());
199         DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthDouble", device.preferredVectorWidthDouble());
200     }
201     catch (...)
202     {
203         DUMP_MESSAGE_STDOUT("Exception. Can't dump OpenCL info");
204         DUMP_MESSAGE_STDOUT("OpenCL device not available");
205         DUMP_PROPERTY_XML("cv_ocl", "not available");
206     }
207 }
208 #undef DUMP_MESSAGE_STDOUT
209 #undef DUMP_PROPERTY_XML
210
211 #endif
212
213 Mat TestUtils::readImage(const String &fileName, int flags)
214 {
215     return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
216 }
217
218 Mat TestUtils::readImageType(const String &fname, int type)
219 {
220     Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
221     if (CV_MAT_CN(type) == 4)
222     {
223         Mat temp;
224         cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
225         swap(src, temp);
226     }
227     src.convertTo(src, CV_MAT_DEPTH(type));
228     return src;
229 }
230
231 double TestUtils::checkNorm1(InputArray m, InputArray mask)
232 {
233     return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
234 }
235
236 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
237 {
238     return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
239 }
240
241 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
242 {
243     Mat diff;
244     matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
245     return std::abs(diff.at<float>(0, 0) - 1.f);
246 }
247
248 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
249 {
250     double final_test_result = 0.0;
251     size_t sz1 = ob1.size();
252     size_t sz2 = ob2.size();
253
254     if (sz1 != sz2)
255         return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
256     else
257     {
258         if (sz1 == 0 && sz2 == 0)
259             return 0;
260         cv::Mat cpu_result(sz, CV_8UC1);
261         cpu_result.setTo(0);
262
263         for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
264         {
265             cv::Mat cpu_result_roi(cpu_result, *r);
266             cpu_result_roi.setTo(1);
267             cpu_result.copyTo(cpu_result);
268         }
269         int cpu_area = cv::countNonZero(cpu_result > 0);
270
271         cv::Mat gpu_result(sz, CV_8UC1);
272         gpu_result.setTo(0);
273         for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
274         {
275             cv::Mat gpu_result_roi(gpu_result, *r2);
276             gpu_result_roi.setTo(1);
277             gpu_result.copyTo(gpu_result);
278         }
279
280         cv::Mat result_;
281         multiply(cpu_result, gpu_result, result_);
282         int result = cv::countNonZero(result_ > 0);
283         if (cpu_area!=0 && result!=0)
284             final_test_result = 1.0 - (double)result/(double)cpu_area;
285         else if(cpu_area==0 && result!=0)
286             final_test_result = -1;
287     }
288     return final_test_result;
289 }
290
291 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
292 {
293     Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
294
295     Mat diff, diff_thresh;
296     absdiff(gold, actual, diff);
297     diff.convertTo(diff, CV_32F);
298     threshold(diff, diff_thresh, eps, 255.0, cv::THRESH_BINARY);
299
300     if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 0)
301     {
302 #if 0
303         std::cout << "Source: " << std::endl << src << std::endl;
304         std::cout << "Expected: " << std::endl << gold << std::endl;
305         std::cout << "Actual: " << std::endl << actual << std::endl;
306 #endif
307
308         namedWindow("src", WINDOW_NORMAL);
309         namedWindow("gold", WINDOW_NORMAL);
310         namedWindow("actual", WINDOW_NORMAL);
311         namedWindow("diff", WINDOW_NORMAL);
312
313         imshow("src", src);
314         imshow("gold", gold);
315         imshow("actual", actual);
316         imshow("diff", diff);
317
318         cv::waitKey();
319     }
320 }
321
322 } } // namespace cvtest::ocl