diff options
Diffstat (limited to 'testbench.h')
| -rw-r--r-- | testbench.h | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/testbench.h b/testbench.h index 5b47bd4..5e77410 100644 --- a/testbench.h +++ b/testbench.h | |||
| @@ -2,10 +2,34 @@ | |||
| 2 | * Header for miscellaneous experimental helper functions. | 2 | * Header for miscellaneous experimental helper functions. |
| 3 | */ | 3 | */ |
| 4 | 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 | // Our design allows typechecking while keeping a non-CUDA per-thread error var. | ||
| 15 | |||
| 16 | // For CUDA Runtime Library functions; typically those prefixed with `cuda` | ||
| 5 | #define SAFE(x) \ | 17 | #define SAFE(x) \ |
| 6 | if ((err = (cudaError_t)(x)) != 0) { \ | 18 | if ((*(cudaError_t*)(&__SAFE_err) = (x)) != 0) { \ |
| 7 | printf("CUDA error %d! %s\n", err, cudaGetErrorString(err)); \ | 19 | printf("(%s:%d) CUDA error %d: %s i.e. \"%s\" returned by %s. Aborting...\n", \ |
| 8 | printf("Suspect line: %s\n", #x); \ | 20 | __FILE__, __LINE__, __SAFE_err, cudaGetErrorName((cudaError_t)__SAFE_err), cudaGetErrorString((cudaError_t)__SAFE_err), #x); \ |
| 21 | exit(1); \ | ||
| 22 | } | ||
| 23 | |||
| 24 | // For CUDA Driver Library functions; typically those prefixed with just `cu` | ||
| 25 | #define SAFE_D(x) \ | ||
| 26 | if ((*(CUresult*)&(__SAFE_err) = (x)) != 0) { \ | ||
| 27 | const char* name; \ | ||
| 28 | const char* desc; \ | ||
| 29 | cuGetErrorName((CUresult)__SAFE_err, &name); \ | ||
| 30 | cuGetErrorString((CUresult)__SAFE_err, &desc); \ | ||
| 31 | printf("(%s:%d) CUDA error %d: %s i.e. \"%s\" returned by %s. Aborting...\n", \ | ||
| 32 | __FILE__, __LINE__, __SAFE_err, name, desc, #x); \ | ||
| 9 | exit(1); \ | 33 | exit(1); \ |
| 10 | } | 34 | } |
| 11 | 35 | ||
