helper_cuda.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. ////////////////////////////////////////////////////////////////////////////////
  28. // These are CUDA Helper functions for initialization and error checking
  29. #ifndef COMMON_HELPER_CUDA_H_
  30. #define COMMON_HELPER_CUDA_H_
  31. #pragma once
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <helper_string.h>
  37. #ifndef EXIT_WAIVED
  38. #define EXIT_WAIVED 2
  39. #endif
  40. // Note, it is required that your SDK sample to include the proper header
  41. // files, please refer the CUDA examples for examples of the needed CUDA
  42. // headers, which may change depending on which CUDA functions are used.
  43. // CUDA Runtime error messages
  44. #ifdef __DRIVER_TYPES_H__
  45. static const char *_cudaGetErrorEnum(cudaError_t error) {
  46. return cudaGetErrorName(error);
  47. }
  48. #endif
  49. #ifdef CUDA_DRIVER_API
  50. // CUDA Driver API errors
  51. static const char *_cudaGetErrorEnum(CUresult error) {
  52. static char unknown[] = "<unknown>";
  53. const char *ret = NULL;
  54. cuGetErrorName(error, &ret);
  55. return ret ? ret : unknown;
  56. }
  57. #endif
  58. #ifdef CUBLAS_API_H_
  59. // cuBLAS API errors
  60. static const char *_cudaGetErrorEnum(cublasStatus_t error) {
  61. switch (error) {
  62. case CUBLAS_STATUS_SUCCESS:
  63. return "CUBLAS_STATUS_SUCCESS";
  64. case CUBLAS_STATUS_NOT_INITIALIZED:
  65. return "CUBLAS_STATUS_NOT_INITIALIZED";
  66. case CUBLAS_STATUS_ALLOC_FAILED:
  67. return "CUBLAS_STATUS_ALLOC_FAILED";
  68. case CUBLAS_STATUS_INVALID_VALUE:
  69. return "CUBLAS_STATUS_INVALID_VALUE";
  70. case CUBLAS_STATUS_ARCH_MISMATCH:
  71. return "CUBLAS_STATUS_ARCH_MISMATCH";
  72. case CUBLAS_STATUS_MAPPING_ERROR:
  73. return "CUBLAS_STATUS_MAPPING_ERROR";
  74. case CUBLAS_STATUS_EXECUTION_FAILED:
  75. return "CUBLAS_STATUS_EXECUTION_FAILED";
  76. case CUBLAS_STATUS_INTERNAL_ERROR:
  77. return "CUBLAS_STATUS_INTERNAL_ERROR";
  78. case CUBLAS_STATUS_NOT_SUPPORTED:
  79. return "CUBLAS_STATUS_NOT_SUPPORTED";
  80. case CUBLAS_STATUS_LICENSE_ERROR:
  81. return "CUBLAS_STATUS_LICENSE_ERROR";
  82. }
  83. return "<unknown>";
  84. }
  85. #endif
  86. #ifdef _CUFFT_H_
  87. // cuFFT API errors
  88. static const char *_cudaGetErrorEnum(cufftResult error) {
  89. switch (error) {
  90. case CUFFT_SUCCESS:
  91. return "CUFFT_SUCCESS";
  92. case CUFFT_INVALID_PLAN:
  93. return "CUFFT_INVALID_PLAN";
  94. case CUFFT_ALLOC_FAILED:
  95. return "CUFFT_ALLOC_FAILED";
  96. case CUFFT_INVALID_TYPE:
  97. return "CUFFT_INVALID_TYPE";
  98. case CUFFT_INVALID_VALUE:
  99. return "CUFFT_INVALID_VALUE";
  100. case CUFFT_INTERNAL_ERROR:
  101. return "CUFFT_INTERNAL_ERROR";
  102. case CUFFT_EXEC_FAILED:
  103. return "CUFFT_EXEC_FAILED";
  104. case CUFFT_SETUP_FAILED:
  105. return "CUFFT_SETUP_FAILED";
  106. case CUFFT_INVALID_SIZE:
  107. return "CUFFT_INVALID_SIZE";
  108. case CUFFT_UNALIGNED_DATA:
  109. return "CUFFT_UNALIGNED_DATA";
  110. case CUFFT_INCOMPLETE_PARAMETER_LIST:
  111. return "CUFFT_INCOMPLETE_PARAMETER_LIST";
  112. case CUFFT_INVALID_DEVICE:
  113. return "CUFFT_INVALID_DEVICE";
  114. case CUFFT_PARSE_ERROR:
  115. return "CUFFT_PARSE_ERROR";
  116. case CUFFT_NO_WORKSPACE:
  117. return "CUFFT_NO_WORKSPACE";
  118. case CUFFT_NOT_IMPLEMENTED:
  119. return "CUFFT_NOT_IMPLEMENTED";
  120. case CUFFT_LICENSE_ERROR:
  121. return "CUFFT_LICENSE_ERROR";
  122. case CUFFT_NOT_SUPPORTED:
  123. return "CUFFT_NOT_SUPPORTED";
  124. }
  125. return "<unknown>";
  126. }
  127. #endif
  128. #ifdef CUSPARSEAPI
  129. // cuSPARSE API errors
  130. static const char *_cudaGetErrorEnum(cusparseStatus_t error) {
  131. switch (error) {
  132. case CUSPARSE_STATUS_SUCCESS:
  133. return "CUSPARSE_STATUS_SUCCESS";
  134. case CUSPARSE_STATUS_NOT_INITIALIZED:
  135. return "CUSPARSE_STATUS_NOT_INITIALIZED";
  136. case CUSPARSE_STATUS_ALLOC_FAILED:
  137. return "CUSPARSE_STATUS_ALLOC_FAILED";
  138. case CUSPARSE_STATUS_INVALID_VALUE:
  139. return "CUSPARSE_STATUS_INVALID_VALUE";
  140. case CUSPARSE_STATUS_ARCH_MISMATCH:
  141. return "CUSPARSE_STATUS_ARCH_MISMATCH";
  142. case CUSPARSE_STATUS_MAPPING_ERROR:
  143. return "CUSPARSE_STATUS_MAPPING_ERROR";
  144. case CUSPARSE_STATUS_EXECUTION_FAILED:
  145. return "CUSPARSE_STATUS_EXECUTION_FAILED";
  146. case CUSPARSE_STATUS_INTERNAL_ERROR:
  147. return "CUSPARSE_STATUS_INTERNAL_ERROR";
  148. case CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
  149. return "CUSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
  150. }
  151. return "<unknown>";
  152. }
  153. #endif
  154. #ifdef CUSOLVER_COMMON_H_
  155. // cuSOLVER API errors
  156. static const char *_cudaGetErrorEnum(cusolverStatus_t error) {
  157. switch (error) {
  158. case CUSOLVER_STATUS_SUCCESS:
  159. return "CUSOLVER_STATUS_SUCCESS";
  160. case CUSOLVER_STATUS_NOT_INITIALIZED:
  161. return "CUSOLVER_STATUS_NOT_INITIALIZED";
  162. case CUSOLVER_STATUS_ALLOC_FAILED:
  163. return "CUSOLVER_STATUS_ALLOC_FAILED";
  164. case CUSOLVER_STATUS_INVALID_VALUE:
  165. return "CUSOLVER_STATUS_INVALID_VALUE";
  166. case CUSOLVER_STATUS_ARCH_MISMATCH:
  167. return "CUSOLVER_STATUS_ARCH_MISMATCH";
  168. case CUSOLVER_STATUS_MAPPING_ERROR:
  169. return "CUSOLVER_STATUS_MAPPING_ERROR";
  170. case CUSOLVER_STATUS_EXECUTION_FAILED:
  171. return "CUSOLVER_STATUS_EXECUTION_FAILED";
  172. case CUSOLVER_STATUS_INTERNAL_ERROR:
  173. return "CUSOLVER_STATUS_INTERNAL_ERROR";
  174. case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
  175. return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
  176. case CUSOLVER_STATUS_NOT_SUPPORTED:
  177. return "CUSOLVER_STATUS_NOT_SUPPORTED ";
  178. case CUSOLVER_STATUS_ZERO_PIVOT:
  179. return "CUSOLVER_STATUS_ZERO_PIVOT";
  180. case CUSOLVER_STATUS_INVALID_LICENSE:
  181. return "CUSOLVER_STATUS_INVALID_LICENSE";
  182. }
  183. return "<unknown>";
  184. }
  185. #endif
  186. #ifdef CURAND_H_
  187. // cuRAND API errors
  188. static const char *_cudaGetErrorEnum(curandStatus_t error) {
  189. switch (error) {
  190. case CURAND_STATUS_SUCCESS:
  191. return "CURAND_STATUS_SUCCESS";
  192. case CURAND_STATUS_VERSION_MISMATCH:
  193. return "CURAND_STATUS_VERSION_MISMATCH";
  194. case CURAND_STATUS_NOT_INITIALIZED:
  195. return "CURAND_STATUS_NOT_INITIALIZED";
  196. case CURAND_STATUS_ALLOCATION_FAILED:
  197. return "CURAND_STATUS_ALLOCATION_FAILED";
  198. case CURAND_STATUS_TYPE_ERROR:
  199. return "CURAND_STATUS_TYPE_ERROR";
  200. case CURAND_STATUS_OUT_OF_RANGE:
  201. return "CURAND_STATUS_OUT_OF_RANGE";
  202. case CURAND_STATUS_LENGTH_NOT_MULTIPLE:
  203. return "CURAND_STATUS_LENGTH_NOT_MULTIPLE";
  204. case CURAND_STATUS_DOUBLE_PRECISION_REQUIRED:
  205. return "CURAND_STATUS_DOUBLE_PRECISION_REQUIRED";
  206. case CURAND_STATUS_LAUNCH_FAILURE:
  207. return "CURAND_STATUS_LAUNCH_FAILURE";
  208. case CURAND_STATUS_PREEXISTING_FAILURE:
  209. return "CURAND_STATUS_PREEXISTING_FAILURE";
  210. case CURAND_STATUS_INITIALIZATION_FAILED:
  211. return "CURAND_STATUS_INITIALIZATION_FAILED";
  212. case CURAND_STATUS_ARCH_MISMATCH:
  213. return "CURAND_STATUS_ARCH_MISMATCH";
  214. case CURAND_STATUS_INTERNAL_ERROR:
  215. return "CURAND_STATUS_INTERNAL_ERROR";
  216. }
  217. return "<unknown>";
  218. }
  219. #endif
  220. #ifdef NVJPEGAPI
  221. // nvJPEG API errors
  222. static const char *_cudaGetErrorEnum(nvjpegStatus_t error) {
  223. switch (error) {
  224. case NVJPEG_STATUS_SUCCESS:
  225. return "NVJPEG_STATUS_SUCCESS";
  226. case NVJPEG_STATUS_NOT_INITIALIZED:
  227. return "NVJPEG_STATUS_NOT_INITIALIZED";
  228. case NVJPEG_STATUS_INVALID_PARAMETER:
  229. return "NVJPEG_STATUS_INVALID_PARAMETER";
  230. case NVJPEG_STATUS_BAD_JPEG:
  231. return "NVJPEG_STATUS_BAD_JPEG";
  232. case NVJPEG_STATUS_JPEG_NOT_SUPPORTED:
  233. return "NVJPEG_STATUS_JPEG_NOT_SUPPORTED";
  234. case NVJPEG_STATUS_ALLOCATOR_FAILURE:
  235. return "NVJPEG_STATUS_ALLOCATOR_FAILURE";
  236. case NVJPEG_STATUS_EXECUTION_FAILED:
  237. return "NVJPEG_STATUS_EXECUTION_FAILED";
  238. case NVJPEG_STATUS_ARCH_MISMATCH:
  239. return "NVJPEG_STATUS_ARCH_MISMATCH";
  240. case NVJPEG_STATUS_INTERNAL_ERROR:
  241. return "NVJPEG_STATUS_INTERNAL_ERROR";
  242. }
  243. return "<unknown>";
  244. }
  245. #endif
  246. #ifdef NV_NPPIDEFS_H
  247. // NPP API errors
  248. static const char *_cudaGetErrorEnum(NppStatus error) {
  249. switch (error) {
  250. case NPP_NOT_SUPPORTED_MODE_ERROR:
  251. return "NPP_NOT_SUPPORTED_MODE_ERROR";
  252. case NPP_ROUND_MODE_NOT_SUPPORTED_ERROR:
  253. return "NPP_ROUND_MODE_NOT_SUPPORTED_ERROR";
  254. case NPP_RESIZE_NO_OPERATION_ERROR:
  255. return "NPP_RESIZE_NO_OPERATION_ERROR";
  256. case NPP_NOT_SUFFICIENT_COMPUTE_CAPABILITY:
  257. return "NPP_NOT_SUFFICIENT_COMPUTE_CAPABILITY";
  258. #if ((NPP_VERSION_MAJOR << 12) + (NPP_VERSION_MINOR << 4)) <= 0x5000
  259. case NPP_BAD_ARG_ERROR:
  260. return "NPP_BAD_ARGUMENT_ERROR";
  261. case NPP_COEFF_ERROR:
  262. return "NPP_COEFFICIENT_ERROR";
  263. case NPP_RECT_ERROR:
  264. return "NPP_RECTANGLE_ERROR";
  265. case NPP_QUAD_ERROR:
  266. return "NPP_QUADRANGLE_ERROR";
  267. case NPP_MEM_ALLOC_ERR:
  268. return "NPP_MEMORY_ALLOCATION_ERROR";
  269. case NPP_HISTO_NUMBER_OF_LEVELS_ERROR:
  270. return "NPP_HISTOGRAM_NUMBER_OF_LEVELS_ERROR";
  271. case NPP_INVALID_INPUT:
  272. return "NPP_INVALID_INPUT";
  273. case NPP_POINTER_ERROR:
  274. return "NPP_POINTER_ERROR";
  275. case NPP_WARNING:
  276. return "NPP_WARNING";
  277. case NPP_ODD_ROI_WARNING:
  278. return "NPP_ODD_ROI_WARNING";
  279. #else
  280. // These are for CUDA 5.5 or higher
  281. case NPP_BAD_ARGUMENT_ERROR:
  282. return "NPP_BAD_ARGUMENT_ERROR";
  283. case NPP_COEFFICIENT_ERROR:
  284. return "NPP_COEFFICIENT_ERROR";
  285. case NPP_RECTANGLE_ERROR:
  286. return "NPP_RECTANGLE_ERROR";
  287. case NPP_QUADRANGLE_ERROR:
  288. return "NPP_QUADRANGLE_ERROR";
  289. case NPP_MEMORY_ALLOCATION_ERR:
  290. return "NPP_MEMORY_ALLOCATION_ERROR";
  291. case NPP_HISTOGRAM_NUMBER_OF_LEVELS_ERROR:
  292. return "NPP_HISTOGRAM_NUMBER_OF_LEVELS_ERROR";
  293. case NPP_INVALID_HOST_POINTER_ERROR:
  294. return "NPP_INVALID_HOST_POINTER_ERROR";
  295. case NPP_INVALID_DEVICE_POINTER_ERROR:
  296. return "NPP_INVALID_DEVICE_POINTER_ERROR";
  297. #endif
  298. case NPP_LUT_NUMBER_OF_LEVELS_ERROR:
  299. return "NPP_LUT_NUMBER_OF_LEVELS_ERROR";
  300. case NPP_TEXTURE_BIND_ERROR:
  301. return "NPP_TEXTURE_BIND_ERROR";
  302. case NPP_WRONG_INTERSECTION_ROI_ERROR:
  303. return "NPP_WRONG_INTERSECTION_ROI_ERROR";
  304. case NPP_NOT_EVEN_STEP_ERROR:
  305. return "NPP_NOT_EVEN_STEP_ERROR";
  306. case NPP_INTERPOLATION_ERROR:
  307. return "NPP_INTERPOLATION_ERROR";
  308. case NPP_RESIZE_FACTOR_ERROR:
  309. return "NPP_RESIZE_FACTOR_ERROR";
  310. case NPP_HAAR_CLASSIFIER_PIXEL_MATCH_ERROR:
  311. return "NPP_HAAR_CLASSIFIER_PIXEL_MATCH_ERROR";
  312. #if ((NPP_VERSION_MAJOR << 12) + (NPP_VERSION_MINOR << 4)) <= 0x5000
  313. case NPP_MEMFREE_ERR:
  314. return "NPP_MEMFREE_ERR";
  315. case NPP_MEMSET_ERR:
  316. return "NPP_MEMSET_ERR";
  317. case NPP_MEMCPY_ERR:
  318. return "NPP_MEMCPY_ERROR";
  319. case NPP_MIRROR_FLIP_ERR:
  320. return "NPP_MIRROR_FLIP_ERR";
  321. #else
  322. case NPP_MEMFREE_ERROR:
  323. return "NPP_MEMFREE_ERROR";
  324. case NPP_MEMSET_ERROR:
  325. return "NPP_MEMSET_ERROR";
  326. case NPP_MEMCPY_ERROR:
  327. return "NPP_MEMCPY_ERROR";
  328. case NPP_MIRROR_FLIP_ERROR:
  329. return "NPP_MIRROR_FLIP_ERROR";
  330. #endif
  331. case NPP_ALIGNMENT_ERROR:
  332. return "NPP_ALIGNMENT_ERROR";
  333. case NPP_STEP_ERROR:
  334. return "NPP_STEP_ERROR";
  335. case NPP_SIZE_ERROR:
  336. return "NPP_SIZE_ERROR";
  337. case NPP_NULL_POINTER_ERROR:
  338. return "NPP_NULL_POINTER_ERROR";
  339. case NPP_CUDA_KERNEL_EXECUTION_ERROR:
  340. return "NPP_CUDA_KERNEL_EXECUTION_ERROR";
  341. case NPP_NOT_IMPLEMENTED_ERROR:
  342. return "NPP_NOT_IMPLEMENTED_ERROR";
  343. case NPP_ERROR:
  344. return "NPP_ERROR";
  345. case NPP_SUCCESS:
  346. return "NPP_SUCCESS";
  347. case NPP_WRONG_INTERSECTION_QUAD_WARNING:
  348. return "NPP_WRONG_INTERSECTION_QUAD_WARNING";
  349. case NPP_MISALIGNED_DST_ROI_WARNING:
  350. return "NPP_MISALIGNED_DST_ROI_WARNING";
  351. case NPP_AFFINE_QUAD_INCORRECT_WARNING:
  352. return "NPP_AFFINE_QUAD_INCORRECT_WARNING";
  353. case NPP_DOUBLE_SIZE_WARNING:
  354. return "NPP_DOUBLE_SIZE_WARNING";
  355. case NPP_WRONG_INTERSECTION_ROI_WARNING:
  356. return "NPP_WRONG_INTERSECTION_ROI_WARNING";
  357. #if ((NPP_VERSION_MAJOR << 12) + (NPP_VERSION_MINOR << 4)) >= 0x6000
  358. /* These are 6.0 or higher */
  359. case NPP_LUT_PALETTE_BITSIZE_ERROR:
  360. return "NPP_LUT_PALETTE_BITSIZE_ERROR";
  361. case NPP_ZC_MODE_NOT_SUPPORTED_ERROR:
  362. return "NPP_ZC_MODE_NOT_SUPPORTED_ERROR";
  363. case NPP_QUALITY_INDEX_ERROR:
  364. return "NPP_QUALITY_INDEX_ERROR";
  365. case NPP_CHANNEL_ORDER_ERROR:
  366. return "NPP_CHANNEL_ORDER_ERROR";
  367. case NPP_ZERO_MASK_VALUE_ERROR:
  368. return "NPP_ZERO_MASK_VALUE_ERROR";
  369. case NPP_NUMBER_OF_CHANNELS_ERROR:
  370. return "NPP_NUMBER_OF_CHANNELS_ERROR";
  371. case NPP_COI_ERROR:
  372. return "NPP_COI_ERROR";
  373. case NPP_DIVISOR_ERROR:
  374. return "NPP_DIVISOR_ERROR";
  375. case NPP_CHANNEL_ERROR:
  376. return "NPP_CHANNEL_ERROR";
  377. case NPP_STRIDE_ERROR:
  378. return "NPP_STRIDE_ERROR";
  379. case NPP_ANCHOR_ERROR:
  380. return "NPP_ANCHOR_ERROR";
  381. case NPP_MASK_SIZE_ERROR:
  382. return "NPP_MASK_SIZE_ERROR";
  383. case NPP_MOMENT_00_ZERO_ERROR:
  384. return "NPP_MOMENT_00_ZERO_ERROR";
  385. case NPP_THRESHOLD_NEGATIVE_LEVEL_ERROR:
  386. return "NPP_THRESHOLD_NEGATIVE_LEVEL_ERROR";
  387. case NPP_THRESHOLD_ERROR:
  388. return "NPP_THRESHOLD_ERROR";
  389. case NPP_CONTEXT_MATCH_ERROR:
  390. return "NPP_CONTEXT_MATCH_ERROR";
  391. case NPP_FFT_FLAG_ERROR:
  392. return "NPP_FFT_FLAG_ERROR";
  393. case NPP_FFT_ORDER_ERROR:
  394. return "NPP_FFT_ORDER_ERROR";
  395. case NPP_SCALE_RANGE_ERROR:
  396. return "NPP_SCALE_RANGE_ERROR";
  397. case NPP_DATA_TYPE_ERROR:
  398. return "NPP_DATA_TYPE_ERROR";
  399. case NPP_OUT_OFF_RANGE_ERROR:
  400. return "NPP_OUT_OFF_RANGE_ERROR";
  401. case NPP_DIVIDE_BY_ZERO_ERROR:
  402. return "NPP_DIVIDE_BY_ZERO_ERROR";
  403. case NPP_RANGE_ERROR:
  404. return "NPP_RANGE_ERROR";
  405. case NPP_NO_MEMORY_ERROR:
  406. return "NPP_NO_MEMORY_ERROR";
  407. case NPP_ERROR_RESERVED:
  408. return "NPP_ERROR_RESERVED";
  409. case NPP_NO_OPERATION_WARNING:
  410. return "NPP_NO_OPERATION_WARNING";
  411. case NPP_DIVIDE_BY_ZERO_WARNING:
  412. return "NPP_DIVIDE_BY_ZERO_WARNING";
  413. #endif
  414. #if ((NPP_VERSION_MAJOR << 12) + (NPP_VERSION_MINOR << 4)) >= 0x7000
  415. /* These are 7.0 or higher */
  416. case NPP_OVERFLOW_ERROR:
  417. return "NPP_OVERFLOW_ERROR";
  418. case NPP_CORRUPTED_DATA_ERROR:
  419. return "NPP_CORRUPTED_DATA_ERROR";
  420. #endif
  421. }
  422. return "<unknown>";
  423. }
  424. #endif
  425. template <typename T>
  426. void check(T result, char const *const func, const char *const file,
  427. int const line) {
  428. if (result) {
  429. fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", file, line,
  430. static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
  431. exit(EXIT_FAILURE);
  432. }
  433. }
  434. #ifdef __DRIVER_TYPES_H__
  435. // This will output the proper CUDA error strings in the event
  436. // that a CUDA host call returns an error
  437. #define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__)
  438. // This will output the proper error string when calling cudaGetLastError
  439. #define getLastCudaError(msg) __getLastCudaError(msg, __FILE__, __LINE__)
  440. inline void __getLastCudaError(const char *errorMessage, const char *file,
  441. const int line) {
  442. cudaError_t err = cudaGetLastError();
  443. if (cudaSuccess != err) {
  444. fprintf(stderr,
  445. "%s(%i) : getLastCudaError() CUDA error :"
  446. " %s : (%d) %s.\n",
  447. file, line, errorMessage, static_cast<int>(err),
  448. cudaGetErrorString(err));
  449. exit(EXIT_FAILURE);
  450. }
  451. }
  452. // This will only print the proper error string when calling cudaGetLastError
  453. // but not exit program incase error detected.
  454. #define printLastCudaError(msg) __printLastCudaError(msg, __FILE__, __LINE__)
  455. inline void __printLastCudaError(const char *errorMessage, const char *file,
  456. const int line) {
  457. cudaError_t err = cudaGetLastError();
  458. if (cudaSuccess != err) {
  459. fprintf(stderr,
  460. "%s(%i) : getLastCudaError() CUDA error :"
  461. " %s : (%d) %s.\n",
  462. file, line, errorMessage, static_cast<int>(err),
  463. cudaGetErrorString(err));
  464. }
  465. }
  466. #endif
  467. #ifndef MAX
  468. #define MAX(a, b) (a > b ? a : b)
  469. #endif
  470. // Float To Int conversion
  471. inline int ftoi(float value) {
  472. return (value >= 0 ? static_cast<int>(value + 0.5)
  473. : static_cast<int>(value - 0.5));
  474. }
  475. // Beginning of GPU Architecture definitions
  476. inline int _ConvertSMVer2Cores(int major, int minor) {
  477. // Defines for GPU Architecture types (using the SM version to determine
  478. // the # of cores per SM
  479. typedef struct {
  480. int SM; // 0xMm (hexidecimal notation), M = SM Major version,
  481. // and m = SM minor version
  482. int Cores;
  483. } sSMtoCores;
  484. sSMtoCores nGpuArchCoresPerSM[] = {
  485. {0x30, 192},
  486. {0x32, 192},
  487. {0x35, 192},
  488. {0x37, 192},
  489. {0x50, 128},
  490. {0x52, 128},
  491. {0x53, 128},
  492. {0x60, 64},
  493. {0x61, 128},
  494. {0x62, 128},
  495. {0x70, 64},
  496. {0x72, 64},
  497. {0x75, 64},
  498. {0x80, 64},
  499. {0x86, 128},
  500. {-1, -1}};
  501. int index = 0;
  502. while (nGpuArchCoresPerSM[index].SM != -1) {
  503. if (nGpuArchCoresPerSM[index].SM == ((major << 4) + minor)) {
  504. return nGpuArchCoresPerSM[index].Cores;
  505. }
  506. index++;
  507. }
  508. // If we don't find the values, we default use the previous one
  509. // to run properly
  510. printf(
  511. "MapSMtoCores for SM %d.%d is undefined."
  512. " Default to use %d Cores/SM\n",
  513. major, minor, nGpuArchCoresPerSM[index - 1].Cores);
  514. return nGpuArchCoresPerSM[index - 1].Cores;
  515. }
  516. inline const char* _ConvertSMVer2ArchName(int major, int minor) {
  517. // Defines for GPU Architecture types (using the SM version to determine
  518. // the GPU Arch name)
  519. typedef struct {
  520. int SM; // 0xMm (hexidecimal notation), M = SM Major version,
  521. // and m = SM minor version
  522. const char* name;
  523. } sSMtoArchName;
  524. sSMtoArchName nGpuArchNameSM[] = {
  525. {0x30, "Kepler"},
  526. {0x32, "Kepler"},
  527. {0x35, "Kepler"},
  528. {0x37, "Kepler"},
  529. {0x50, "Maxwell"},
  530. {0x52, "Maxwell"},
  531. {0x53, "Maxwell"},
  532. {0x60, "Pascal"},
  533. {0x61, "Pascal"},
  534. {0x62, "Pascal"},
  535. {0x70, "Volta"},
  536. {0x72, "Xavier"},
  537. {0x75, "Turing"},
  538. {0x80, "Ampere"},
  539. {0x86, "Ampere"},
  540. {-1, "Graphics Device"}};
  541. int index = 0;
  542. while (nGpuArchNameSM[index].SM != -1) {
  543. if (nGpuArchNameSM[index].SM == ((major << 4) + minor)) {
  544. return nGpuArchNameSM[index].name;
  545. }
  546. index++;
  547. }
  548. // If we don't find the values, we default use the previous one
  549. // to run properly
  550. printf(
  551. "MapSMtoArchName for SM %d.%d is undefined."
  552. " Default to use %s\n",
  553. major, minor, nGpuArchNameSM[index - 1].name);
  554. return nGpuArchNameSM[index - 1].name;
  555. }
  556. // end of GPU Architecture definitions
  557. #ifdef __CUDA_RUNTIME_H__
  558. // General GPU Device CUDA Initialization
  559. inline int gpuDeviceInit(int devID) {
  560. int device_count;
  561. checkCudaErrors(cudaGetDeviceCount(&device_count));
  562. if (device_count == 0) {
  563. fprintf(stderr,
  564. "gpuDeviceInit() CUDA error: "
  565. "no devices supporting CUDA.\n");
  566. exit(EXIT_FAILURE);
  567. }
  568. if (devID < 0) {
  569. devID = 0;
  570. }
  571. if (devID > device_count - 1) {
  572. fprintf(stderr, "\n");
  573. fprintf(stderr, ">> %d CUDA capable GPU device(s) detected. <<\n",
  574. device_count);
  575. fprintf(stderr,
  576. ">> gpuDeviceInit (-device=%d) is not a valid"
  577. " GPU device. <<\n",
  578. devID);
  579. fprintf(stderr, "\n");
  580. return -devID;
  581. }
  582. int computeMode = -1, major = 0, minor = 0;
  583. checkCudaErrors(cudaDeviceGetAttribute(&computeMode, cudaDevAttrComputeMode, devID));
  584. checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, devID));
  585. checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, devID));
  586. if (computeMode == cudaComputeModeProhibited) {
  587. fprintf(stderr,
  588. "Error: device is running in <Compute Mode "
  589. "Prohibited>, no threads can use cudaSetDevice().\n");
  590. return -1;
  591. }
  592. if (major < 1) {
  593. fprintf(stderr, "gpuDeviceInit(): GPU device does not support CUDA.\n");
  594. exit(EXIT_FAILURE);
  595. }
  596. checkCudaErrors(cudaSetDevice(devID));
  597. printf("gpuDeviceInit() CUDA Device [%d]: \"%s\n", devID, _ConvertSMVer2ArchName(major, minor));
  598. return devID;
  599. }
  600. // This function returns the best GPU (with maximum GFLOPS)
  601. inline int gpuGetMaxGflopsDeviceId() {
  602. int current_device = 0, sm_per_multiproc = 0;
  603. int max_perf_device = 0;
  604. int device_count = 0;
  605. int devices_prohibited = 0;
  606. uint64_t max_compute_perf = 0;
  607. checkCudaErrors(cudaGetDeviceCount(&device_count));
  608. if (device_count == 0) {
  609. fprintf(stderr,
  610. "gpuGetMaxGflopsDeviceId() CUDA error:"
  611. " no devices supporting CUDA.\n");
  612. exit(EXIT_FAILURE);
  613. }
  614. // Find the best CUDA capable GPU device
  615. current_device = 0;
  616. while (current_device < device_count) {
  617. int computeMode = -1, major = 0, minor = 0;
  618. checkCudaErrors(cudaDeviceGetAttribute(&computeMode, cudaDevAttrComputeMode, current_device));
  619. checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, current_device));
  620. checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, current_device));
  621. // If this GPU is not running on Compute Mode prohibited,
  622. // then we can add it to the list
  623. if (computeMode != cudaComputeModeProhibited) {
  624. if (major == 9999 && minor == 9999) {
  625. sm_per_multiproc = 1;
  626. } else {
  627. sm_per_multiproc =
  628. _ConvertSMVer2Cores(major, minor);
  629. }
  630. int multiProcessorCount = 0, clockRate = 0;
  631. checkCudaErrors(cudaDeviceGetAttribute(&multiProcessorCount, cudaDevAttrMultiProcessorCount, current_device));
  632. cudaError_t result = cudaDeviceGetAttribute(&clockRate, cudaDevAttrClockRate, current_device);
  633. if (result != cudaSuccess) {
  634. // If cudaDevAttrClockRate attribute is not supported we
  635. // set clockRate as 1, to consider GPU with most SMs and CUDA Cores.
  636. if(result == cudaErrorInvalidValue) {
  637. clockRate = 1;
  638. }
  639. else {
  640. fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \n", __FILE__, __LINE__,
  641. static_cast<unsigned int>(result), _cudaGetErrorEnum(result));
  642. exit(EXIT_FAILURE);
  643. }
  644. }
  645. uint64_t compute_perf = (uint64_t)multiProcessorCount * sm_per_multiproc * clockRate;
  646. if (compute_perf > max_compute_perf) {
  647. max_compute_perf = compute_perf;
  648. max_perf_device = current_device;
  649. }
  650. } else {
  651. devices_prohibited++;
  652. }
  653. ++current_device;
  654. }
  655. if (devices_prohibited == device_count) {
  656. fprintf(stderr,
  657. "gpuGetMaxGflopsDeviceId() CUDA error:"
  658. " all devices have compute mode prohibited.\n");
  659. exit(EXIT_FAILURE);
  660. }
  661. return max_perf_device;
  662. }
  663. // Initialization code to find the best CUDA Device
  664. inline int findCudaDevice(int argc, const char **argv) {
  665. int devID = 0;
  666. // If the command-line has a device number specified, use it
  667. if (checkCmdLineFlag(argc, argv, "device")) {
  668. devID = getCmdLineArgumentInt(argc, argv, "device=");
  669. if (devID < 0) {
  670. printf("Invalid command line parameter\n ");
  671. exit(EXIT_FAILURE);
  672. } else {
  673. devID = gpuDeviceInit(devID);
  674. if (devID < 0) {
  675. printf("exiting...\n");
  676. exit(EXIT_FAILURE);
  677. }
  678. }
  679. } else {
  680. // Otherwise pick the device with highest Gflops/s
  681. devID = gpuGetMaxGflopsDeviceId();
  682. checkCudaErrors(cudaSetDevice(devID));
  683. int major = 0, minor = 0;
  684. checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, devID));
  685. checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, devID));
  686. printf("GPU Device %d: \"%s\" with compute capability %d.%d\n\n",
  687. devID, _ConvertSMVer2ArchName(major, minor), major, minor);
  688. }
  689. return devID;
  690. }
  691. inline int findIntegratedGPU() {
  692. int current_device = 0;
  693. int device_count = 0;
  694. int devices_prohibited = 0;
  695. checkCudaErrors(cudaGetDeviceCount(&device_count));
  696. if (device_count == 0) {
  697. fprintf(stderr, "CUDA error: no devices supporting CUDA.\n");
  698. exit(EXIT_FAILURE);
  699. }
  700. // Find the integrated GPU which is compute capable
  701. while (current_device < device_count) {
  702. int computeMode = -1, integrated = -1;
  703. checkCudaErrors(cudaDeviceGetAttribute(&computeMode, cudaDevAttrComputeMode, current_device));
  704. checkCudaErrors(cudaDeviceGetAttribute(&integrated, cudaDevAttrIntegrated, current_device));
  705. // If GPU is integrated and is not running on Compute Mode prohibited,
  706. // then cuda can map to GLES resource
  707. if (integrated && (computeMode != cudaComputeModeProhibited)) {
  708. checkCudaErrors(cudaSetDevice(current_device));
  709. int major = 0, minor = 0;
  710. checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, current_device));
  711. checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, current_device));
  712. printf("GPU Device %d: \"%s\" with compute capability %d.%d\n\n",
  713. current_device, _ConvertSMVer2ArchName(major, minor), major, minor);
  714. return current_device;
  715. } else {
  716. devices_prohibited++;
  717. }
  718. current_device++;
  719. }
  720. if (devices_prohibited == device_count) {
  721. fprintf(stderr,
  722. "CUDA error:"
  723. " No GLES-CUDA Interop capable GPU found.\n");
  724. exit(EXIT_FAILURE);
  725. }
  726. return -1;
  727. }
  728. // General check for CUDA GPU SM Capabilities
  729. inline bool checkCudaCapabilities(int major_version, int minor_version) {
  730. int dev;
  731. int major = 0, minor = 0;
  732. checkCudaErrors(cudaGetDevice(&dev));
  733. checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev));
  734. checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev));
  735. if ((major > major_version) ||
  736. (major == major_version &&
  737. minor >= minor_version)) {
  738. printf(" Device %d: <%16s >, Compute SM %d.%d detected\n", dev,
  739. _ConvertSMVer2ArchName(major, minor), major, minor);
  740. return true;
  741. } else {
  742. printf(
  743. " No GPU device was found that can support "
  744. "CUDA compute capability %d.%d.\n",
  745. major_version, minor_version);
  746. return false;
  747. }
  748. }
  749. #endif
  750. // end of CUDA Helper Functions
  751. #endif // COMMON_HELPER_CUDA_H_