00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _CUTIL_H_
00039 #define _CUTIL_H_
00040
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044
00045 #define _CUDA_DEBUG
00046
00047
00048
00049
00050
00051 #ifdef _CUDA_DEBUG
00052 #include <stdio.h>
00053 #if __DEVICE_EMULATION__
00054
00055 #define CUT_BANK_CHECKER( array, index) \
00056 (cutCheckBankAccess( threadIdx.x, threadIdx.y, threadIdx.z, blockDim.x, \
00057 blockDim.y, blockDim.z, \
00058 __FILE__, __LINE__, #array, index ), \
00059 array[index])
00060 #else
00061 #define CUT_BANK_CHECKER( array, index) array[index]
00062 #endif
00063
00064 # define CU_SAFE_CALL_NO_SYNC( call ) do { \
00065 CUresult err = call; \
00066 if( CUDA_SUCCESS != err) { \
00067 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
00068 err, __FILE__, __LINE__ ); \
00069 exit(EXIT_FAILURE); \
00070 } } while (0)
00071
00072 # define CU_SAFE_CALL( call ) do { \
00073 CU_SAFE_CALL_NO_SYNC(call); \
00074 CUresult err = cuCtxSynchronize(); \
00075 if( CUDA_SUCCESS != err) { \
00076 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
00077 err, __FILE__, __LINE__ ); \
00078 abort(); \
00079 } } while (0)
00080
00081 # define CUDA_SAFE_CALL_NO_SYNC( call) do { \
00082 cudaError err = call; \
00083 if( cudaSuccess != err) { \
00084 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
00085 __FILE__, __LINE__, cudaGetErrorString( err) ); \
00086 abort(); \
00087 } } while (0)
00088
00089 # define CUDA_SAFE_CALL( call) do { \
00090 CUDA_SAFE_CALL_NO_SYNC(call); \
00091 cudaError err = cudaThreadSynchronize(); \
00092 if( cudaSuccess != err) { \
00093 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
00094 __FILE__, __LINE__, cudaGetErrorString( err) ); \
00095 abort(); \
00096 } } while (0)
00097
00098 # define CUFFT_SAFE_CALL( call) do { \
00099 cufftResult err = call; \
00100 if( CUFFT_SUCCESS != err) { \
00101 fprintf(stderr, "CUFFT error in file '%s' in line %i.\n", \
00102 __FILE__, __LINE__); \
00103 exit(EXIT_FAILURE); \
00104 } } while (0)
00105
00106 # define CUT_SAFE_CALL( call) \
00107 if( CUTTrue != call) { \
00108 fprintf(stderr, "Cut error in file '%s' in line %i.\n", \
00109 __FILE__, __LINE__); \
00110 exit(EXIT_FAILURE); \
00111 }
00112
00113
00114 # define CUT_CHECK_ERROR(errorMessage) do { \
00115 cudaError_t err = cudaGetLastError(); \
00116 if( cudaSuccess != err) { \
00117 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00118 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00119 abort(); \
00120 } \
00121 err = cudaThreadSynchronize(); \
00122 if( cudaSuccess != err) { \
00123 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00124 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00125 abort(); \
00126 } } while (0)
00127
00128
00129 # define CUT_SAFE_MALLOC( mallocCall ) do{ \
00130 if( !(mallocCall)) { \
00131 fprintf(stderr, "Host malloc failure in file '%s' in line %i\n", \
00132 __FILE__, __LINE__); \
00133 exit(EXIT_FAILURE); \
00134 } } while(0);
00135
00136
00137 # define CUT_CONDITION( val) \
00138 if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) { \
00139 exit(EXIT_FAILURE); \
00140 }
00141
00142 #else // not DEBUG
00143
00144 #define CUT_BANK_CHECKER( array, index) array[index]
00145
00146
00147 # define CUT_CHECK_ERROR(errorMessage)
00148 # define CUT_CHECK_ERROR_GL()
00149 # define CUT_CONDITION( val)
00150 # define CU_SAFE_CALL_NO_SYNC( call) call
00151 # define CU_SAFE_CALL( call) call
00152 # define CUDA_SAFE_CALL_NO_SYNC( call) call
00153 # define CUDA_SAFE_CALL( call) call
00154 # define CUT_SAFE_CALL( call) call
00155 # define CUFFT_SAFE_CALL( call) call
00156 # define CUT_SAFE_MALLOC( mallocCall ) mallocCall
00157
00158 #endif
00159
00160 #if __DEVICE_EMULATION__
00161
00162 # define CUT_DEVICE_INIT(DEV)
00163
00164 #else
00165
00166 # define CUT_DEVICE_INIT(DEV) { \
00167 int deviceCount; \
00168 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount)); \
00169 if (deviceCount == 0) { \
00170 fprintf(stderr, "cutil error: no devices supporting CUDA.\n"); \
00171 exit(EXIT_FAILURE); \
00172 } \
00173 int dev = DEV; \
00174 if (dev > deviceCount-1) dev = deviceCount - 1; \
00175 struct cudaDeviceProp deviceProp; \
00176 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev)); \
00177 if (deviceProp.major < 1) { \
00178 fprintf(stderr, "cutil error: device does not support CUDA.\n"); \
00179 exit(EXIT_FAILURE); \
00180 } \
00181 fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name); \
00182 CUDA_SAFE_CALL(cudaSetDevice(dev)); \
00183 }
00184
00185 #endif
00186
00187
00188 #ifdef __cplusplus
00189 }
00190 #endif // #ifdef _DEBUG (else branch)
00191
00192 #endif // #ifndef _CUTIL_H_