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-2012, Multicoreware, Inc., 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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other oclMaterials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors as is and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "perf_precomp.hpp"
51 // This program test most of the functions in ocl module and generate data metrix of x-factor in .csv files
52 // All images needed in this test are in samples/gpu folder.
53 // For haar template, haarcascade_frontalface_alt.xml shouold be in working directory
54 void TestSystem::run()
58 for (vector<Runnable *>::iterator it = tests_.begin(); it != tests_.end(); ++it)
60 cout << (*it)->name() << endl;
66 // Run test initializers
67 for (vector<Runnable *>::iterator it = inits_.begin(); it != inits_.end(); ++it)
69 if ((*it)->name().find(test_filter_, 0) != string::npos)
79 for (vector<Runnable *>::iterator it = tests_.begin(); it != tests_.end(); ++it)
83 if ((*it)->name().find(test_filter_, 0) != string::npos)
85 cout << endl << (*it)->name() << ":\n";
87 setCurrentTest((*it)->name());
88 //fprintf(record_,"%s\n",(*it)->name().c_str());
91 finishCurrentSubtest();
94 catch (const Exception &)
96 // Message is printed via callback
97 resetCurrentSubtest();
99 catch (const runtime_error &e)
101 printError(e.what());
102 resetCurrentSubtest();
111 void TestSystem::finishCurrentSubtest()
113 if (cur_subtest_is_empty_)
114 // There is no need to print subtest statistics
119 double cpu_time = cpu_elapsed_ / getTickFrequency() * 1000.0;
120 double gpu_time = gpu_elapsed_ / getTickFrequency() * 1000.0;
121 double gpu_full_time = gpu_full_elapsed_ / getTickFrequency() * 1000.0;
123 double speedup = static_cast<double>(cpu_elapsed_) / std::max(1.0, gpu_elapsed_);
124 speedup_total_ += speedup;
126 double fullspeedup = static_cast<double>(cpu_elapsed_) / std::max(1.0, gpu_full_elapsed_);
127 speedup_full_total_ += fullspeedup;
131 speedup_faster_count_++;
133 else if (speedup < bottom_)
135 speedup_slower_count_++;
139 speedup_equal_count_++;
142 if (fullspeedup > top_)
144 speedup_full_faster_count_++;
146 else if (fullspeedup < bottom_)
148 speedup_full_slower_count_++;
152 speedup_full_equal_count_++;
155 // compute min, max and
156 std::sort(gpu_times_.begin(), gpu_times_.end());
157 double gpu_min = gpu_times_.front() / getTickFrequency() * 1000.0;
158 double gpu_max = gpu_times_.back() / getTickFrequency() * 1000.0;
159 double deviation = 0;
161 if (gpu_times_.size() > 1)
165 for (size_t i = 0; i < gpu_times_.size(); i++)
167 int64 diff = gpu_times_[i] - static_cast<int64>(gpu_elapsed_);
168 double diff_time = diff * 1000 / getTickFrequency();
169 sum += diff_time * diff_time;
172 deviation = std::sqrt(sum / gpu_times_.size());
175 printMetrics(is_accurate_, cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup);
176 writeMetrics(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, gpu_min, gpu_max, deviation);
178 num_subtests_called_++;
179 resetCurrentSubtest();
183 double TestSystem::meanTime(const vector<int64> &samples)
185 double sum = accumulate(samples.begin(), samples.end(), 0.);
186 return sum / samples.size();
190 void TestSystem::printHeading()
193 cout<< setiosflags(ios_base::left);
196 cout<<TAB<<setw(7)<< "Accu." << setw(10) << "CPU (ms)" << setw(10) << "GPU, ms"
197 << setw(8) << "Speedup"<< setw(10)<<"GPUTotal" << setw(10) << "Total"
199 cout<<TAB<<setw(7)<<""<<setw(10)<<""<<setw(10)<<""<<setw(8)<<""<<setw(10)<<"(ms)"<<setw(10)<<"Speedup\n";
202 cout<<TAB<< setw(10) << "CPU (ms)" << setw(10) << "GPU, ms"
203 << setw(8) << "Speedup"<< setw(10)<<"GPUTotal" << setw(10) << "Total"
205 cout<<TAB<<setw(10)<<""<<setw(10)<<""<<setw(8)<<""<<setw(10)<<"(ms)"<<setw(10)<<"Speedup\n";
207 cout << resetiosflags(ios_base::left);
210 void TestSystem::writeHeading()
214 recordname_ += "_OCL.csv";
215 record_ = fopen(recordname_.c_str(), "w");
218 cout<<".csv file open failed.\n";
223 fprintf(record_, "NAME,DESCRIPTION,ACCURACY,DIFFERENCE,CPU (ms),GPU (ms),SPEEDUP,GPUTOTAL (ms),TOTALSPEEDUP,GPU Min (ms),GPU Max (ms), Standard deviation (ms)\n");
228 void TestSystem::printSummary()
230 cout << setiosflags(ios_base::fixed);
231 cout << "\naverage GPU speedup: x"
232 << setprecision(3) << speedup_total_ / std::max(1, num_subtests_called_)
234 cout << "\nGPU exceeded: "
235 << setprecision(3) << speedup_faster_count_
237 << setprecision(3) << speedup_equal_count_
239 << setprecision(3) << speedup_slower_count_
241 cout << "\nGPU exceeded rate: "
242 << setprecision(3) << (float)speedup_faster_count_ / std::max(1, num_subtests_called_) * 100
244 << "\nGPU passed rate: "
245 << setprecision(3) << (float)speedup_equal_count_ / std::max(1, num_subtests_called_) * 100
247 << "\nGPU failed rate: "
248 << setprecision(3) << (float)speedup_slower_count_ / std::max(1, num_subtests_called_) * 100
251 cout << "\naverage GPUTOTAL speedup: x"
252 << setprecision(3) << speedup_full_total_ / std::max(1, num_subtests_called_)
254 cout << "\nGPUTOTAL exceeded: "
255 << setprecision(3) << speedup_full_faster_count_
256 << "\nGPUTOTAL passed: "
257 << setprecision(3) << speedup_full_equal_count_
258 << "\nGPUTOTAL failed: "
259 << setprecision(3) << speedup_full_slower_count_
261 cout << "\nGPUTOTAL exceeded rate: "
262 << setprecision(3) << (float)speedup_full_faster_count_ / std::max(1, num_subtests_called_) * 100
264 << "\nGPUTOTAL passed rate: "
265 << setprecision(3) << (float)speedup_full_equal_count_ / std::max(1, num_subtests_called_) * 100
267 << "\nGPUTOTAL failed rate: "
268 << setprecision(3) << (float)speedup_full_slower_count_ / std::max(1, num_subtests_called_) * 100
271 cout << resetiosflags(ios_base::fixed);
281 #if GTEST_OS_WINDOWS&&!GTEST_OS_WINDOWS_MOBILE
282 // Returns the character attribute for the given color.
283 static WORD GetColorAttribute(GTestColor color) {
285 case COLOR_RED: return FOREGROUND_RED;
286 case COLOR_GREEN: return FOREGROUND_GREEN;
287 case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN;
292 static const char* GetAnsiColorCode(GTestColor color) {
294 case COLOR_RED: return "1";
295 case COLOR_GREEN: return "2";
296 case COLOR_YELLOW: return "3";
297 default: return NULL;
302 static void printMetricsUti(double cpu_time, double gpu_time, double gpu_full_time, double speedup, double fullspeedup, std::stringstream& stream, std::stringstream& cur_subtest_description)
304 //cout <<TAB<< setw(7) << stream.str();
309 cout << setw(10) << stream.str();
313 cout << setw(10) << stream.str();
316 stream << "x" << setprecision(3) << speedup;
317 cout << setw(8) << stream.str();
320 stream << gpu_full_time;
321 cout << setw(10) << stream.str();
324 stream << "x" << setprecision(3) << fullspeedup;
325 cout << setw(10) << stream.str();
327 cout << cur_subtest_description.str();
328 cout << resetiosflags(ios_base::left) << endl;
331 void TestSystem::printMetrics(int is_accurate, double cpu_time, double gpu_time, double gpu_full_time, double speedup, double fullspeedup)
333 cout << setiosflags(ios_base::left);
336 std::stringstream &cur_subtest_description = getCurSubtestDescription();
338 #if GTEST_OS_WINDOWS&&!GTEST_OS_WINDOWS_MOBILE
341 const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
342 // Gets the current text color.
343 CONSOLE_SCREEN_BUFFER_INFO buffer_info;
344 GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
345 const WORD old_color_attrs = buffer_info.wAttributes;
346 // We need to flush the stream buffers into the console before each
347 // SetConsoleTextAttribute call lest it affect the text that is already
348 // printed but has not yet reached the console.
351 if(is_accurate == 1||is_accurate == -1)
353 color = old_color_attrs;
354 printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description);
358 color = GetColorAttribute(COLOR_RED);
359 SetConsoleTextAttribute(stdout_handle,
360 color| FOREGROUND_INTENSITY);
362 printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description);
364 // Restores the text color.
365 SetConsoleTextAttribute(stdout_handle, old_color_attrs);
368 GTestColor color = COLOR_RED;
369 if(is_accurate == 1|| is_accurate == -1)
371 printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description);
375 printf("\033[0;3%sm", GetAnsiColorCode(color));
376 printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description);
377 printf("\033[m"); // Resets the terminal to default.
382 void TestSystem::writeMetrics(double cpu_time, double gpu_time, double gpu_full_time, double speedup, double fullspeedup, double gpu_min, double gpu_max, double std_dev)
386 recordname_ += ".csv";
387 record_ = fopen(recordname_.c_str(), "w");
390 string _is_accurate_;
392 if(is_accurate_ == 1)
393 _is_accurate_ = "Pass";
394 else if(is_accurate_ == 0)
395 _is_accurate_ = "Fail";
396 else if(is_accurate_ == -1)
400 std::cout<<"is_accurate errer: "<<is_accurate_<<"\n";
404 fprintf(record_, "%s,%s,%s,%.2f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\n",
405 itname_changed_ ? itname_.c_str() : "",
406 cur_subtest_description_.str().c_str(),
407 _is_accurate_.c_str(),
409 cpu_time, gpu_time, speedup, gpu_full_time, fullspeedup,
410 gpu_min, gpu_max, std_dev);
414 itname_changed_ = false;
420 void TestSystem::writeSummary()
424 recordname_ += ".csv";
425 record_ = fopen(recordname_.c_str(), "w");
428 fprintf(record_, "\nAverage GPU speedup: %.3f\n"
429 "exceeded: %d (%.3f%%)\n"
430 "passed: %d (%.3f%%)\n"
431 "failed: %d (%.3f%%)\n"
432 "\nAverage GPUTOTAL speedup: %.3f\n"
433 "exceeded: %d (%.3f%%)\n"
434 "passed: %d (%.3f%%)\n"
435 "failed: %d (%.3f%%)\n",
436 speedup_total_ / std::max(1, num_subtests_called_),
437 speedup_faster_count_, (float)speedup_faster_count_ / std::max(1, num_subtests_called_) * 100,
438 speedup_equal_count_, (float)speedup_equal_count_ / std::max(1, num_subtests_called_) * 100,
439 speedup_slower_count_, (float)speedup_slower_count_ / std::max(1, num_subtests_called_) * 100,
440 speedup_full_total_ / std::max(1, num_subtests_called_),
441 speedup_full_faster_count_, (float)speedup_full_faster_count_ / std::max(1, num_subtests_called_) * 100,
442 speedup_full_equal_count_, (float)speedup_full_equal_count_ / std::max(1, num_subtests_called_) * 100,
443 speedup_full_slower_count_, (float)speedup_full_slower_count_ / std::max(1, num_subtests_called_) * 100
448 void TestSystem::printError(const std::string &msg)
450 if(msg != "CL_INVALID_BUFFER_SIZE")
452 cout << TAB << "[error: " << msg << "] " << cur_subtest_description_.str() << endl;
456 void gen(Mat &mat, int rows, int cols, int type, Scalar low, Scalar high)
458 mat.create(rows, cols, type);
460 rng.fill(mat, RNG::UNIFORM, low, high);
463 string abspath(const string &relpath)
465 return TestSystem::instance().workingDir() + relpath;
469 int CV_CDECL cvErrorCallback(int /*status*/, const char * /*func_name*/,
470 const char *err_msg, const char * /*file_name*/,
471 int /*line*/, void * /*userdata*/)
473 TestSystem::instance().printError(err_msg);
477 double checkNorm(const Mat &m)
479 return norm(m, NORM_INF);
482 double checkNorm(const Mat &m1, const Mat &m2)
484 return norm(m1, m2, NORM_INF);
487 double checkSimilarity(const Mat &m1, const Mat &m2)
490 matchTemplate(m1, m2, diff, CV_TM_CCORR_NORMED);
491 return std::abs(diff.at<float>(0, 0) - 1.f);