-
Notifications
You must be signed in to change notification settings - Fork 0
/
safecalls.cu
58 lines (49 loc) · 1.7 KB
/
safecalls.cu
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
50
51
52
53
54
55
56
57
58
// ~ Error checks in CUDA code can help catch CUDA errors at their source. There are 2 sources of errors in CUDA source code:
// ~
// ~ Errors from CUDA API calls. For example, a call to cudaMalloc() might fail.
// ~ Errors from CUDA kernel calls. For example, there might be invalid memory access inside a kernel.
// ~
// ~ To use this functions, just include this file und use it like this:
// ~ CudaFunctions:
// ~ CudaSafeCall( cudaMalloc( &fooPtr, fooSize ) );
// ~
// ~ Kernel call
// ~ fooKernel<<< x, y >>>();
// ~ CudaCheckError();
//
// taken from https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/
#include <stdio.h>
#include <stdlib.h>
// Define this to turn on error checking
#define CUDA_ERROR_CHECK
#define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
inline void __cudaSafeCall( cudaError err, const char *file, const int line ){
#ifdef CUDA_ERROR_CHECK
if ( cudaSuccess != err ){
fprintf( stderr, "cudaSafeCall() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}
inline void __cudaCheckError( const char *file, const int line ){
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if ( cudaSuccess != err ){
fprintf( stderr, "cudaCheckError() failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
// More careful checking. However, this will affect performance.
// Comment away if needed.
err = cudaDeviceSynchronize();
if( cudaSuccess != err ){
fprintf( stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
file, line, cudaGetErrorString( err ) );
exit( -1 );
}
#endif
return;
}