himrep
cudaUtility.h
1 /*
2  * inference-101
3  */
4 
5 #ifndef __CUDA_UTILITY_H_
6 #define __CUDA_UTILITY_H_
7 
8 
9 #include <cuda_runtime.h>
10 #include <cuda.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 
19 #define CUDA(x) cudaCheckError((x), #x, __FILE__, __LINE__)
20 
24 #define CUDA_SUCCESS(x) (CUDA(x) == cudaSuccess)
25 
29 #define CUDA_FAILED(x) (CUDA(x) != cudaSuccess)
30 
34 #define CUDA_VERIFY(x) if(CUDA_FAILED(x)) return false;
35 
39 #define LOG_CUDA "[cuda] "
40 
41 /*
42  * define this if you want all cuda calls to be printed
43  */
44 //#define CUDA_TRACE
45 
46 
47 
51 inline cudaError_t cudaCheckError(cudaError_t retval, const char* txt, const char* file, int line )
52 {
53 #if !defined(CUDA_TRACE)
54  if( retval == cudaSuccess)
55  return cudaSuccess;
56 #endif
57 
58  //int activeDevice = -1;
59  //cudaGetDevice(&activeDevice);
60 
61  //Log("[cuda] device %i - %s\n", activeDevice, txt);
62 
63  printf(LOG_CUDA "%s\n", txt);
64 
65 
66  if( retval != cudaSuccess )
67  {
68  printf(LOG_CUDA " %s (error %u) (hex 0x%02X)\n", cudaGetErrorString(retval), retval, retval);
69  printf(LOG_CUDA " %s:%i\n", file, line);
70  }
71 
72  return retval;
73 }
74 
75 
79 inline __device__ __host__ int iDivUp( int a, int b ) { return (a % b != 0) ? (a / b + 1) : (a / b); }
80 
81 
82 
83 #endif