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 #ifndef CUDAUTILS_H
00033 #define CUDAUTILS_H
00034
00037 #include <cstdio>
00038 #include <iostream>
00039
00040 #define safeCall(err) __safeCall(err, __FILE__, __LINE__)
00041 #define safeThreadSync() __safeThreadSync(__FILE__, __LINE__)
00042 #define checkMsg(msg) __checkMsg(msg, __FILE__, __LINE__)
00043
00045
00050 inline void __safeCall(cudaError err, const char *file, const int line)
00051 {
00052 if (cudaSuccess != err) {
00053 fprintf(stderr, "safeCall() Runtime API error in file <%s>, line %i : %s.\n", file, line, cudaGetErrorString(err));
00054 exit(-1);
00055 }
00056 }
00057
00059
00063 inline void __safeThreadSync(const char *file, const int line)
00064 {
00065 cudaError err = cudaThreadSynchronize();
00066 if (cudaSuccess != err) {
00067 fprintf(stderr, "threadSynchronize() Driver API error in file '%s' in line %i : %s.\n", file, line, cudaGetErrorString(err));
00068 exit(-1);
00069 }
00070 }
00071
00073
00078 inline void __checkMsg(const char *errorMessage, const char *file, const int line)
00079 {
00080 cudaError_t err = cudaGetLastError();
00081 if (cudaSuccess != err) {
00082 fprintf(stderr, "checkMsg() CUDA error: %s in file <%s>, line %i : %s.\n", errorMessage, file, line, cudaGetErrorString(err));
00083 exit(-1);
00084 }
00085 }
00086
00088 inline bool deviceInit(int dev)
00089 {
00090 int deviceCount;
00091 safeCall(cudaGetDeviceCount(&deviceCount));
00092 if (deviceCount == 0) {
00093 fprintf(stderr, "CUDA error: no devices supporting CUDA.\n");
00094 return false;
00095 }
00096 if (dev < 0) dev = 0;
00097 if (dev > deviceCount-1) dev = deviceCount - 1;
00098 cudaDeviceProp deviceProp;
00099 safeCall(cudaGetDeviceProperties(&deviceProp, dev));
00100 if (deviceProp.major < 1) {
00101 fprintf(stderr, "error: device does not support CUDA.\n");
00102 return false;
00103 }
00104 safeCall(cudaSetDevice(dev));
00105 return true;
00106 }
00107
00108
00110 class TimerGPU {
00111 public:
00113 cudaEvent_t start;
00115 cudaEvent_t stop;
00117 cudaStream_t stream;
00119
00122 TimerGPU(cudaStream_t stream_ = 0) : stream(stream_) {
00123 cudaEventCreate(&start);
00124 cudaEventCreate(&stop);
00125 cudaEventRecord(start, stream);
00126 }
00127 ~TimerGPU() {
00128 cudaEventDestroy(start);
00129 cudaEventDestroy(stop);
00130 }
00132
00134 float read() {
00135 cudaEventRecord(stop, stream);
00136 cudaEventSynchronize(stop);
00137 float time;
00138 cudaEventElapsedTime(&time, start, stop);
00139 return time;
00140 }
00141 };
00142
00143 #endif
00144