NCVTest.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifndef _ncvtest_hpp_
  43. #define _ncvtest_hpp_
  44. #if defined _MSC_VER
  45. # pragma warning( disable : 4201 4408 4100)
  46. #endif
  47. #include <string>
  48. #include <vector>
  49. #include <map>
  50. #include <memory>
  51. #include <algorithm>
  52. #include <fstream>
  53. #include <cuda_runtime.h>
  54. #include "opencv2/cudalegacy.hpp"
  55. struct NCVTestReport
  56. {
  57. std::map<std::string, Ncv32u> statsNums;
  58. std::map<std::string, std::string> statsText;
  59. };
  60. class INCVTest
  61. {
  62. public:
  63. virtual bool executeTest(NCVTestReport &report) = 0;
  64. virtual std::string getName() const = 0;
  65. virtual ~INCVTest(){}
  66. };
  67. class NCVTestProvider : public INCVTest
  68. {
  69. public:
  70. NCVTestProvider(std::string testName_)
  71. :
  72. testName(testName_)
  73. {
  74. int devId;
  75. ncvAssertPrintReturn(cudaSuccess == cudaGetDevice(&devId), "Error returned from cudaGetDevice", );
  76. ncvAssertPrintReturn(cudaSuccess == cudaGetDeviceProperties(&this->devProp, devId), "Error returned from cudaGetDeviceProperties", );
  77. }
  78. virtual bool init() = 0;
  79. virtual bool process() = 0;
  80. virtual bool deinit() = 0;
  81. virtual bool toString(std::ofstream &strOut) = 0;
  82. virtual std::string getName() const
  83. {
  84. return this->testName;
  85. }
  86. virtual ~NCVTestProvider()
  87. {
  88. deinitMemory();
  89. }
  90. virtual bool executeTest(NCVTestReport &report)
  91. {
  92. bool res;
  93. report.statsText["rcode"] = "FAILED";
  94. res = initMemory(report);
  95. if (!res)
  96. {
  97. dumpToFile(report);
  98. deinitMemory();
  99. return false;
  100. }
  101. res = init();
  102. if (!res)
  103. {
  104. dumpToFile(report);
  105. deinit();
  106. deinitMemory();
  107. return false;
  108. }
  109. res = process();
  110. if (!res)
  111. {
  112. dumpToFile(report);
  113. deinit();
  114. deinitMemory();
  115. return false;
  116. }
  117. res = deinit();
  118. if (!res)
  119. {
  120. dumpToFile(report);
  121. deinitMemory();
  122. return false;
  123. }
  124. deinitMemory();
  125. report.statsText["rcode"] = "Passed";
  126. return true;
  127. }
  128. protected:
  129. cudaDeviceProp devProp;
  130. std::unique_ptr<INCVMemAllocator> allocatorGPU;
  131. std::unique_ptr<INCVMemAllocator> allocatorCPU;
  132. private:
  133. std::string testName;
  134. bool initMemory(NCVTestReport &report)
  135. {
  136. this->allocatorGPU.reset(new NCVMemStackAllocator(static_cast<Ncv32u>(devProp.textureAlignment)));
  137. this->allocatorCPU.reset(new NCVMemStackAllocator(static_cast<Ncv32u>(devProp.textureAlignment)));
  138. if (!this->allocatorGPU.get()->isInitialized() ||
  139. !this->allocatorCPU.get()->isInitialized())
  140. {
  141. report.statsText["rcode"] = "Memory FAILED";
  142. return false;
  143. }
  144. if (!this->process())
  145. {
  146. report.statsText["rcode"] = "Memory FAILED";
  147. return false;
  148. }
  149. Ncv32u maxGPUsize = (Ncv32u)this->allocatorGPU.get()->maxSize();
  150. Ncv32u maxCPUsize = (Ncv32u)this->allocatorCPU.get()->maxSize();
  151. report.statsNums["MemGPU"] = maxGPUsize;
  152. report.statsNums["MemCPU"] = maxCPUsize;
  153. this->allocatorGPU.reset(new NCVMemStackAllocator(NCVMemoryTypeDevice, maxGPUsize, static_cast<Ncv32u>(devProp.textureAlignment)));
  154. this->allocatorCPU.reset(new NCVMemStackAllocator(NCVMemoryTypeHostPinned, maxCPUsize, static_cast<Ncv32u>(devProp.textureAlignment)));
  155. if (!this->allocatorGPU.get()->isInitialized() ||
  156. !this->allocatorCPU.get()->isInitialized())
  157. {
  158. report.statsText["rcode"] = "Memory FAILED";
  159. return false;
  160. }
  161. return true;
  162. }
  163. void deinitMemory()
  164. {
  165. this->allocatorGPU.reset();
  166. this->allocatorCPU.reset();
  167. }
  168. void dumpToFile(NCVTestReport &report)
  169. {
  170. bool bReasonMem = (0 == report.statsText["rcode"].compare("Memory FAILED"));
  171. std::string fname = "TestDump_";
  172. fname += (bReasonMem ? "m_" : "") + this->testName + ".log";
  173. std::ofstream stream(fname.c_str(), std::ios::trunc | std::ios::out);
  174. if (!stream.is_open()) return;
  175. stream << "NCV Test Failure Log: " << this->testName << std::endl;
  176. stream << "====================================================" << std::endl << std::endl;
  177. stream << "Test initialization report: " << std::endl;
  178. for (std::map<std::string,std::string>::iterator it=report.statsText.begin();
  179. it != report.statsText.end(); ++it)
  180. {
  181. stream << it->first << "=" << it->second << std::endl;
  182. }
  183. for (std::map<std::string,Ncv32u>::iterator it=report.statsNums.begin();
  184. it != report.statsNums.end(); ++it)
  185. {
  186. stream << it->first << "=" << it->second << std::endl;
  187. }
  188. stream << std::endl;
  189. stream << "Test initialization parameters: " << std::endl;
  190. bool bSerializeRes = false;
  191. try
  192. {
  193. bSerializeRes = this->toString(stream);
  194. }
  195. catch (...)
  196. {
  197. }
  198. if (!bSerializeRes)
  199. {
  200. stream << "Couldn't retrieve object dump" << std::endl;
  201. }
  202. stream.flush();
  203. }
  204. };
  205. #endif // _ncvtest_hpp_