-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.h
More file actions
38 lines (33 loc) · 1.86 KB
/
helpers.h
File metadata and controls
38 lines (33 loc) · 1.86 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
#ifndef CUDA_HELPERS_H
#define CUDA_HELPERS_H
// include guards: prevent header from being included multiple times during compilation
#include <stdio.h>
#include <cuda_runtime.h>
// macro definition. takes one argument: call
// do while loop makes a multi statement macro and makes it behave like a single line statement
// executes the cuda function "call" and stores the return value in error
// if it is not a success, display the error and details in the standard error output stream
// EXIT_FAILURE terminates the program.
#define CUDA_CHECK(call) \
do { \
cudaError_t error = call; \
if (error != cudaSuccess){ \
fprintf(stderr, "CUDA Error at %s: %d\n", __FILE__, __LINE__); \
fprintf(stderr, "Error code: %d, %s\n", error, cudaGetErrorString(error)); \
exit(EXIT_FAILURE); \
} \
} while (0)
#endif
inline void checkKernelError(const char* file, int line) {
cudaError_t err = cudaGetLastError(); // Get launch error
if (err != cudaSuccess) {
printf("Kernel launch error: %s in %s at line %d\n", cudaGetErrorString(err), file, line);
exit(EXIT_FAILURE);
}
err = cudaDeviceSynchronize(); // Wait and check for execution error
if (err != cudaSuccess) {
printf("Kernel execution error: %s in %s at line %d\n", cudaGetErrorString(err), file, line);
exit(EXIT_FAILURE);
}
}
#define CHECK_KERNEL_ERROR() checkKernelError(__FILE__, __LINE__)