|
4 | 4 | #include <cuda_runtime_api.h> |
5 | 5 | #include <stdexcept> |
6 | 6 |
|
7 | | -#ifdef PMPP_CUDA_ERR_CHECK |
8 | | - #error "PMPP_CUDA_ERR_CHECK already defined." |
| 7 | +/** |
| 8 | + * @brief Check the given cuda error. Exit with `EXIT_FAILURE` if not |
| 9 | + * success. |
| 10 | + * The error message is printed to `stderr`. |
| 11 | + */ |
| 12 | +#define PMPP_CUDA_ERR_CHECK(err) \ |
| 13 | + do { \ |
| 14 | + cudaError_t err_ = (err); \ |
| 15 | + if (err_ != cudaSuccess) { \ |
| 16 | + ::fprintf( \ |
| 17 | + stderr, "CUDA error at %s:%d; Error code: %d(%s) \"%s\"", \ |
| 18 | + __FILE__, __LINE__, err, ::cudaGetErrorString(err_), #err); \ |
| 19 | + ::cudaDeviceReset(); \ |
| 20 | + ::std::exit(EXIT_FAILURE); \ |
| 21 | + } \ |
| 22 | + } while (0) |
| 23 | + |
| 24 | +#define PMPP_CUDA_ABORT(msg) \ |
| 25 | + do { \ |
| 26 | + ::fprintf(stderr, "Abort at %s:%d \"%s\"", __FILE__, __LINE__, msg); \ |
| 27 | + ::cudaDeviceReset(); \ |
| 28 | + ::std::abort(); \ |
| 29 | + } while (0) |
| 30 | + |
| 31 | +#ifdef NDEBUG |
| 32 | + /** |
| 33 | + * @brief Cuda error check is turned off on Release mode. |
| 34 | + */ |
| 35 | + #define PMPP_DEBUG_CUDA_ERR_CHECK(err) ((void) 0) |
9 | 36 | #else |
10 | 37 | /** |
11 | 38 | * @brief Check the given cuda error. Exit with `EXIT_FAILURE` if not |
12 | 39 | * success. |
13 | 40 | * The error message is printed to `stderr`. |
14 | 41 | */ |
15 | | - #define PMPP_CUDA_ERR_CHECK(err) \ |
16 | | - do { \ |
17 | | - cudaError_t err_ = (err); \ |
18 | | - if (err_ != cudaSuccess) { \ |
19 | | - ::fprintf(stderr, \ |
20 | | - "CUDA error at %s:%d; Error code: %d(%s) \"%s\"", \ |
21 | | - __FILE__, __LINE__, err, \ |
22 | | - ::cudaGetErrorString(err_), #err); \ |
23 | | - ::cudaDeviceReset(); \ |
24 | | - ::std::abort(); \ |
25 | | - } \ |
26 | | - } while (0) |
27 | | - |
28 | | - #define PMPP_ABORT(msg) \ |
29 | | - do { \ |
30 | | - ::fprintf(stderr, "Abort at %s:%d \"%s\"", __FILE__, __LINE__, \ |
31 | | - msg); \ |
32 | | - ::cudaDeviceReset(); \ |
33 | | - ::std::abort(); \ |
34 | | - } while (0) |
35 | | -#endif |
36 | | - |
37 | | -#ifdef PMPP_DEBUG_CUDA_ERR_CHECK |
38 | | - #error "PMPP_DEBUG_CUDA_ERR_CHECK already defined." |
39 | | -#else |
40 | | - #ifdef NDEBUG |
41 | | - /** |
42 | | - * @brief Cuda error check is turned off on Release mode. |
43 | | - */ |
44 | | - #define PMPP_DEBUG_CUDA_ERR_CHECK(err) ((void) 0) |
45 | | - #else |
46 | | - /** |
47 | | - * @brief Check the given cuda error. Exit with `EXIT_FAILURE` if not |
48 | | - * success. |
49 | | - * The error message is printed to `stderr`. |
50 | | - */ |
51 | | - #define PMPP_DEBUG_CUDA_ERR_CHECK(err) PMPP_CUDA_ERR_CHECK(err) |
52 | | - #endif |
| 42 | + #define PMPP_DEBUG_CUDA_ERR_CHECK(err) PMPP_CUDA_ERR_CHECK(err) |
53 | 43 | #endif |
54 | 44 |
|
55 | 45 | namespace pmpp::cuda |
56 | 46 | { |
| 47 | + |
57 | 48 | template <typename T> |
58 | 49 | __host__ __device__ void initMemory(T* ptr, size_t n, const T& val) |
59 | 50 | { |
60 | 51 | for (size_t i = 0; i < n; ++i) { |
61 | 52 | ptr[i] = val; |
62 | 53 | } |
63 | 54 | } |
64 | | - |
65 | 55 | } // namespace pmpp::cuda |
0 commit comments