-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.hpp
More file actions
49 lines (45 loc) · 1.39 KB
/
error.hpp
File metadata and controls
49 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include <cuda_runtime.h>
#include <cstdio>
#include <cstdlib>
namespace tinycuda {
/**
* @brief Check a CUDA error code and exit if there is an error.
*
* @param err The cudaError_t returned by a CUDA runtime call.
* @param file The source file where the error occurred.
* @param line The line number in the source file.
*
* This function prints an error message and aborts if `err` indicates a failure.
* Consider wrapping in try-catch for exception-based alternatives in production.
*
* Example usage:
* cudaError_t status = cudaMalloc(&ptr, N * sizeof(float));
* tinycuda::cuda_check(status, __FILE__, __LINE__);
*/
inline void cuda_check(cudaError_t err, const char* file, int line) {
if (err != cudaSuccess) {
fprintf(stderr,
"[CUDA ERROR] %s:%d: %s\n",
file,
line,
cudaGetErrorString(err));
std::exit(EXIT_FAILURE);
}
// Clear any pending errors (best practice)
(void) cudaGetLastError();
}
/**
* @brief Convenience macro to automatically pass file and line number.
*
* Evaluates the expression, checks its return value, and aborts on error.
*
* Example:
* CUDA_CHECK(cudaMalloc(&ptr, N * sizeof(float)));
*/
#define CUDA_CHECK(expr) \
do { \
cudaError_t status = (expr); \
tinycuda::cuda_check(status, __FILE__, __LINE__); \
} while(0)
} // namespace tinycuda