diff options
Diffstat (limited to 'testbench.h')
| -rw-r--r-- | testbench.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/testbench.h b/testbench.h new file mode 100644 index 0000000..dff1211 --- /dev/null +++ b/testbench.h | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | /* Copyright 2021-2023 Joshua Bakita | ||
| 2 | * Header for miscellaneous experimental helper functions. | ||
| 3 | */ | ||
| 4 | |||
| 5 | // cudaError_t and CUResult can both safely be cast to an unsigned int | ||
| 6 | static __thread unsigned int __SAFE_err; | ||
| 7 | |||
| 8 | // The very strange cast in these macros is to satisfy two goals at tension: | ||
| 9 | // 1. This file should be able to be included in non-CUDA-using files, and thus | ||
| 10 | // should use no CUDA types outside of this macro. | ||
| 11 | // 2. We want to typecheck uses of these macros. The driver and runtime APIs | ||
| 12 | // do not have identical error numbers and/or meanings, so runtime library | ||
| 13 | // calls should use SAFE, and driver library calls should use SAFE_D. | ||
| 14 | // These macros allow typechecking, but keep a generic global error variable. | ||
| 15 | #define SAFE(x) \ | ||
| 16 | if ((*(cudaError_t*)(&__SAFE_err) = (x)) != 0) { \ | ||
| 17 | printf("(%s:%d) CUDA error %d: %s i.e. \"%s\" returned by %s. Aborting...\n", \ | ||
| 18 | __FILE__, __LINE__, __SAFE_err, cudaGetErrorName((cudaError_t)__SAFE_err), cudaGetErrorString((cudaError_t)__SAFE_err), #x); \ | ||
| 19 | exit(1); \ | ||
| 20 | } | ||
| 21 | |||
| 22 | #define SAFE_D(x) \ | ||
| 23 | if ((*(CUresult*)&(__SAFE_err) = (x)) != 0) { \ | ||
| 24 | const char* name; \ | ||
| 25 | const char* desc; \ | ||
| 26 | cuGetErrorName((CUresult)__SAFE_err, &name); \ | ||
| 27 | cuGetErrorString((CUresult)__SAFE_err, &desc); \ | ||
| 28 | printf("(%s:%d) CUDA error %d: %s i.e. \"%s\" returned by %s. Aborting...\n", \ | ||
| 29 | __FILE__, __LINE__, __SAFE_err, name, desc, #x); \ | ||
| 30 | exit(1); \ | ||
| 31 | } | ||
