1 /*M///////////////////////////////////////////////////////////////////////////////////////
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-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
44 #include "opencv2/ts/ocl_test.hpp"
51 int test_loop_times = 1; // TODO Read from command line / environment
55 #define DUMP_PROPERTY_XML(propertyName, propertyValue) \
57 std::stringstream ssName, ssValue;\
58 ssName << propertyName;\
59 ssValue << (propertyValue); \
60 ::testing::Test::RecordProperty(ssName.str(), ssValue.str()); \
63 #define DUMP_MESSAGE_STDOUT(msg) \
65 std::cout << msg << std::endl; \
68 static std::string bytesToStringRepr(size_t value)
70 size_t b = value % 1024;
73 size_t kb = value % 1024;
76 size_t mb = value % 1024;
81 std::ostringstream stream;
84 stream << gb << " GB ";
86 stream << mb << " MB ";
88 stream << kb << " kB ";
95 void dumpOpenCLDevice()
97 using namespace cv::ocl;
101 std::vector<PlatformInfo> platforms;
102 cv::ocl::getPlatfomsInfo(platforms);
103 if (platforms.size() > 0)
105 DUMP_MESSAGE_STDOUT("OpenCL Platforms: ");
106 for (size_t i = 0; i < platforms.size(); i++)
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++)
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()) );
125 DUMP_MESSAGE_STDOUT("OpenCL is not available");
126 DUMP_PROPERTY_XML("cv_ocl", "not available");
130 const Device& device = Device::getDefault();
131 if (!device.available())
132 CV_ErrorNoReturn(CV_OpenCLInitError, "OpenCL device is not available");
134 DUMP_MESSAGE_STDOUT("Current OpenCL device: ");
137 DUMP_MESSAGE_STDOUT(" Platform = "<< device.getPlatform().name());
138 DUMP_PROPERTY_XML("cv_ocl_current_platformName", device.getPlatform().name());
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);
146 DUMP_MESSAGE_STDOUT(" Name = "<< device.name());
147 DUMP_PROPERTY_XML("cv_ocl_current_deviceName", device.name());
149 DUMP_MESSAGE_STDOUT(" Version = " << device.version());
150 DUMP_PROPERTY_XML("cv_ocl_current_deviceVersion", device.version());
152 DUMP_MESSAGE_STDOUT(" Compute units = "<< device.maxComputeUnits());
153 DUMP_PROPERTY_XML("cv_ocl_current_maxComputeUnits", device.maxComputeUnits());
155 DUMP_MESSAGE_STDOUT(" Max work group size = "<< device.maxWorkGroupSize());
156 DUMP_PROPERTY_XML("cv_ocl_current_maxWorkGroupSize", device.maxWorkGroupSize());
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());
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());
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);
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());
174 const char* haveAmdBlasStr = haveAmdBlas() ? "Yes" : "No";
175 DUMP_MESSAGE_STDOUT(" Has AMD Blas = "<< haveAmdBlasStr);
176 DUMP_PROPERTY_XML("cv_ocl_current_AmdBlas", haveAmdBlas());
178 const char* haveAmdFftStr = haveAmdFft() ? "Yes" : "No";
179 DUMP_MESSAGE_STDOUT(" Has AMD Fft = "<< haveAmdFftStr);
180 DUMP_PROPERTY_XML("cv_ocl_current_AmdFft", haveAmdFft());
183 DUMP_MESSAGE_STDOUT(" Preferred vector width char = "<< device.preferredVectorWidthChar());
184 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthChar", device.preferredVectorWidthChar());
186 DUMP_MESSAGE_STDOUT(" Preferred vector width short = "<< device.preferredVectorWidthShort());
187 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthShort", device.preferredVectorWidthShort());
189 DUMP_MESSAGE_STDOUT(" Preferred vector width int = "<< device.preferredVectorWidthInt());
190 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthInt", device.preferredVectorWidthInt());
192 DUMP_MESSAGE_STDOUT(" Preferred vector width long = "<< device.preferredVectorWidthLong());
193 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthLong", device.preferredVectorWidthLong());
195 DUMP_MESSAGE_STDOUT(" Preferred vector width float = "<< device.preferredVectorWidthFloat());
196 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthFloat", device.preferredVectorWidthFloat());
198 DUMP_MESSAGE_STDOUT(" Preferred vector width double = "<< device.preferredVectorWidthDouble());
199 DUMP_PROPERTY_XML("cv_ocl_current_preferredVectorWidthDouble", device.preferredVectorWidthDouble());
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");
208 #undef DUMP_MESSAGE_STDOUT
209 #undef DUMP_PROPERTY_XML
213 Mat TestUtils::readImage(const String &fileName, int flags)
215 return cv::imread(cvtest::TS::ptr()->get_data_path() + fileName, flags);
218 Mat TestUtils::readImageType(const String &fname, int type)
220 Mat src = readImage(fname, CV_MAT_CN(type) == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);
221 if (CV_MAT_CN(type) == 4)
224 cv::cvtColor(src, temp, cv::COLOR_BGR2BGRA);
227 src.convertTo(src, CV_MAT_DEPTH(type));
231 double TestUtils::checkNorm1(InputArray m, InputArray mask)
233 return cvtest::norm(m.getMat(), NORM_INF, mask.getMat());
236 double TestUtils::checkNorm2(InputArray m1, InputArray m2, InputArray mask)
238 return cvtest::norm(m1.getMat(), m2.getMat(), NORM_INF, mask.getMat());
241 double TestUtils::checkSimilarity(InputArray m1, InputArray m2)
244 matchTemplate(m1.getMat(), m2.getMat(), diff, CV_TM_CCORR_NORMED);
245 return std::abs(diff.at<float>(0, 0) - 1.f);
248 double TestUtils::checkRectSimilarity(const Size & sz, std::vector<Rect>& ob1, std::vector<Rect>& ob2)
250 double final_test_result = 0.0;
251 size_t sz1 = ob1.size();
252 size_t sz2 = ob2.size();
255 return sz1 > sz2 ? (double)(sz1 - sz2) : (double)(sz2 - sz1);
258 if (sz1 == 0 && sz2 == 0)
260 cv::Mat cpu_result(sz, CV_8UC1);
263 for (vector<Rect>::const_iterator r = ob1.begin(); r != ob1.end(); r++)
265 cv::Mat cpu_result_roi(cpu_result, *r);
266 cpu_result_roi.setTo(1);
267 cpu_result.copyTo(cpu_result);
269 int cpu_area = cv::countNonZero(cpu_result > 0);
271 cv::Mat gpu_result(sz, CV_8UC1);
273 for(vector<Rect>::const_iterator r2 = ob2.begin(); r2 != ob2.end(); r2++)
275 cv::Mat gpu_result_roi(gpu_result, *r2);
276 gpu_result_roi.setTo(1);
277 gpu_result.copyTo(gpu_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;
288 return final_test_result;
291 void TestUtils::showDiff(InputArray _src, InputArray _gold, InputArray _actual, double eps, bool alwaysShow)
293 Mat src = _src.getMat(), actual = _actual.getMat(), gold = _gold.getMat();
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);
300 if (alwaysShow || cv::countNonZero(diff_thresh.reshape(1)) > 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;
308 namedWindow("src", WINDOW_NORMAL);
309 namedWindow("gold", WINDOW_NORMAL);
310 namedWindow("actual", WINDOW_NORMAL);
311 namedWindow("diff", WINDOW_NORMAL);
314 imshow("gold", gold);
315 imshow("actual", actual);
316 imshow("diff", diff);
322 } } // namespace cvtest::ocl