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) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Third party copyrights are property of their respective owners. 16 // 17 // Redistribution and use in source and binary forms, with or without modification, 18 // are permitted provided that the following conditions are met: 19 // 20 // * Redistribution's of source code must retain the above copyright notice, 21 // this list of conditions and the following disclaimer. 22 // 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 materials provided with the distribution. 26 // 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. 29 // 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. 40 // 41 //M*/ 42 43 #ifndef _ncvtest_hpp_ 44 #define _ncvtest_hpp_ 45 46 #if defined _MSC_VER 47 # pragma warning( disable : 4201 4408 4127 4100) 48 #endif 49 50 #include <string> 51 #include <vector> 52 #include <map> 53 #include <memory> 54 #include <algorithm> 55 #include <fstream> 56 57 #include <cuda_runtime.h> 58 59 #include "opencv2/cudalegacy.hpp" 60 61 62 struct NCVTestReport 63 { 64 std::map<std::string, Ncv32u> statsNums; 65 std::map<std::string, std::string> statsText; 66 }; 67 68 69 class INCVTest 70 { 71 public: 72 virtual bool executeTest(NCVTestReport &report) = 0; 73 virtual std::string getName() const = 0; ~INCVTest()74 virtual ~INCVTest(){} 75 }; 76 77 78 class NCVTestProvider : public INCVTest 79 { 80 public: 81 NCVTestProvider(std::string testName_)82 NCVTestProvider(std::string testName_) 83 : 84 testName(testName_) 85 { 86 int devId; 87 ncvAssertPrintReturn(cudaSuccess == cudaGetDevice(&devId), "Error returned from cudaGetDevice", ); 88 ncvAssertPrintReturn(cudaSuccess == cudaGetDeviceProperties(&this->devProp, devId), "Error returned from cudaGetDeviceProperties", ); 89 } 90 91 virtual bool init() = 0; 92 virtual bool process() = 0; 93 virtual bool deinit() = 0; 94 virtual bool toString(std::ofstream &strOut) = 0; 95 getName() const96 virtual std::string getName() const 97 { 98 return this->testName; 99 } 100 ~NCVTestProvider()101 virtual ~NCVTestProvider() 102 { 103 deinitMemory(); 104 } 105 executeTest(NCVTestReport & report)106 virtual bool executeTest(NCVTestReport &report) 107 { 108 bool res; 109 report.statsText["rcode"] = "FAILED"; 110 111 res = initMemory(report); 112 if (!res) 113 { 114 dumpToFile(report); 115 deinitMemory(); 116 return false; 117 } 118 119 res = init(); 120 if (!res) 121 { 122 dumpToFile(report); 123 deinit(); 124 deinitMemory(); 125 return false; 126 } 127 128 res = process(); 129 if (!res) 130 { 131 dumpToFile(report); 132 deinit(); 133 deinitMemory(); 134 return false; 135 } 136 137 res = deinit(); 138 if (!res) 139 { 140 dumpToFile(report); 141 deinitMemory(); 142 return false; 143 } 144 145 deinitMemory(); 146 147 report.statsText["rcode"] = "Passed"; 148 return true; 149 } 150 151 protected: 152 153 cudaDeviceProp devProp; 154 std::auto_ptr<INCVMemAllocator> allocatorGPU; 155 std::auto_ptr<INCVMemAllocator> allocatorCPU; 156 157 private: 158 159 std::string testName; 160 initMemory(NCVTestReport & report)161 bool initMemory(NCVTestReport &report) 162 { 163 this->allocatorGPU.reset(new NCVMemStackAllocator(static_cast<Ncv32u>(devProp.textureAlignment))); 164 this->allocatorCPU.reset(new NCVMemStackAllocator(static_cast<Ncv32u>(devProp.textureAlignment))); 165 166 if (!this->allocatorGPU.get()->isInitialized() || 167 !this->allocatorCPU.get()->isInitialized()) 168 { 169 report.statsText["rcode"] = "Memory FAILED"; 170 return false; 171 } 172 173 if (!this->process()) 174 { 175 report.statsText["rcode"] = "Memory FAILED"; 176 return false; 177 } 178 179 Ncv32u maxGPUsize = (Ncv32u)this->allocatorGPU.get()->maxSize(); 180 Ncv32u maxCPUsize = (Ncv32u)this->allocatorCPU.get()->maxSize(); 181 182 report.statsNums["MemGPU"] = maxGPUsize; 183 report.statsNums["MemCPU"] = maxCPUsize; 184 185 this->allocatorGPU.reset(new NCVMemStackAllocator(NCVMemoryTypeDevice, maxGPUsize, static_cast<Ncv32u>(devProp.textureAlignment))); 186 187 this->allocatorCPU.reset(new NCVMemStackAllocator(NCVMemoryTypeHostPinned, maxCPUsize, static_cast<Ncv32u>(devProp.textureAlignment))); 188 189 if (!this->allocatorGPU.get()->isInitialized() || 190 !this->allocatorCPU.get()->isInitialized()) 191 { 192 report.statsText["rcode"] = "Memory FAILED"; 193 return false; 194 } 195 196 return true; 197 } 198 deinitMemory()199 void deinitMemory() 200 { 201 this->allocatorGPU.reset(); 202 this->allocatorCPU.reset(); 203 } 204 dumpToFile(NCVTestReport & report)205 void dumpToFile(NCVTestReport &report) 206 { 207 bool bReasonMem = (0 == report.statsText["rcode"].compare("Memory FAILED")); 208 std::string fname = "TestDump_"; 209 fname += (bReasonMem ? "m_" : "") + this->testName + ".log"; 210 std::ofstream stream(fname.c_str(), std::ios::trunc | std::ios::out); 211 if (!stream.is_open()) return; 212 213 stream << "NCV Test Failure Log: " << this->testName << std::endl; 214 stream << "====================================================" << std::endl << std::endl; 215 stream << "Test initialization report: " << std::endl; 216 for (std::map<std::string,std::string>::iterator it=report.statsText.begin(); 217 it != report.statsText.end(); it++) 218 { 219 stream << it->first << "=" << it->second << std::endl; 220 } 221 for (std::map<std::string,Ncv32u>::iterator it=report.statsNums.begin(); 222 it != report.statsNums.end(); it++) 223 { 224 stream << it->first << "=" << it->second << std::endl; 225 } 226 stream << std::endl; 227 228 stream << "Test initialization parameters: " << std::endl; 229 bool bSerializeRes = false; 230 try 231 { 232 bSerializeRes = this->toString(stream); 233 } 234 catch (...) 235 { 236 } 237 238 if (!bSerializeRes) 239 { 240 stream << "Couldn't retrieve object dump" << std::endl; 241 } 242 243 stream.flush(); 244 } 245 }; 246 247 #endif // _ncvtest_hpp_ 248