cuda_utils.h
Go to the documentation of this file.
00001 #ifndef CUDA_UTILS_H
00002 #define CUDA_UTILS_H
00003 
00004 #include <cstdlib>
00005 #include <cstdio>
00006 #include <string.h>
00007 #include <vector>
00008 
00009 #include "cuda_common.h"
00010 
00011 #define CUDA_CHECK_ERROR_BASE()          CUDA_CHECK_ERROR("")
00012 
00013 #ifdef dDebug
00014 //Check for CUDA error
00015 #define CUDA_CHECK_ERROR(errorMessage)                                  \
00016   do                                                                    \
00017   {                                                                     \
00018     cudaError_t err = cudaGetLastError();                               \
00019     if(err != cudaSuccess)                                              \
00020     {                                                                   \
00021       fprintf(stderr,"Cuda error: %s in file '%s' in line %i : %s.\n",  \
00022               errorMessage, __FILE__, __LINE__, cudaGetErrorString( err)); \
00023       fprintf(stderr,"Press ENTER key to terminate the program\n");    \
00024       getchar();                                                        \
00025       exit(EXIT_FAILURE);                                               \
00026     }                                                                   \
00027     err = cudaThreadSynchronize();                                      \
00028     if(err != cudaSuccess)                                              \
00029     {                                                                   \
00030       fprintf(stderr,"Cuda error: %s in file '%s' in line %i : %s.\n",  \
00031               errorMessage, __FILE__, __LINE__, cudaGetErrorString( err)); \
00032       fprintf(stderr,"Press ENTER key to terminate the program\n");     \
00033       getchar();                                                        \
00034       exit(EXIT_FAILURE);                                               \
00035     }                                                                   \
00036   }                                                                     \
00037   while(0)
00038 
00039 // Call and check function for CUDA error without synchronization
00040 #define CUDA_SAFE_CALL_NO_SYNC(call)                                    \
00041   do                                                                    \
00042   {                                                                     \
00043     cudaError err = call;                                               \
00044     if(err != cudaSuccess)                                              \
00045     {                                                                   \
00046       fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n",     \
00047               __FILE__, __LINE__, cudaGetErrorString( err) );           \
00048       fprintf(stderr,"Press ENTER key to terminate the program\n");     \
00049       getchar();                                                        \
00050       exit(EXIT_FAILURE);                                               \
00051     }                                                                   \
00052   }                                                                     \
00053   while(0)
00054 
00055 // Call and check function for CUDA error with synchronization
00056 #define CUDA_SAFE_CALL(call)                                            \
00057   do                                                                    \
00058   {                                                                     \
00059     CUDA_SAFE_CALL_NO_SYNC(call);                                       \
00060     cudaError err = cudaThreadSynchronize();                            \
00061     if(err != cudaSuccess)                                              \
00062     {                                                                   \
00063       fprintf(stderr,"Cuda errorSync in file '%s' in line %i : %s.\n",  \
00064               __FILE__, __LINE__, cudaGetErrorString( err) );           \
00065       fprintf(stderr,"Press ENTER key to terminate the program\n");     \
00066       getchar();                                                        \
00067       exit(EXIT_FAILURE);                                               \
00068     }                                                                   \
00069   } while (0)
00070 
00071 #define CUDA_SAFE_CALL(call)                                            \
00072   do                                                                    \
00073   {                                                                     \
00074     CUDA_SAFE_CALL_NO_SYNC(call);                                       \
00075     cudaError err = cudaThreadSynchronize();                            \
00076     if(err != cudaSuccess)                                              \
00077     {                                                                   \
00078       fprintf(stderr,"Cuda errorSync in file '%s' in line %i : %s.\n",  \
00079               __FILE__, __LINE__, cudaGetErrorString( err) );           \
00080       fprintf(stderr,"Press ENTER key to terminate the program\n");     \
00081       getchar();                                                        \
00082       exit(EXIT_FAILURE);                                               \
00083     }                                                                   \
00084   } while (0)
00085 
00086 #else // not DEBUG
00087 
00088 #define CUDA_CHECK_ERROR(errorMessage)
00089 #define CUDA_SAFE_CALL_NO_SYNC(call) call
00090 #define CUDA_SAFE_CALL(call) call
00091 
00092 #endif
00093 
00094 inline void cudaDeviceInit(int dev = 0)
00095 {
00096   int deviceCount;
00097   CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount));
00098   if (deviceCount == 0) {
00099     fprintf(stderr, "CUTIL CUDA error: no devices supporting CUDA.\n");
00100     exit(-1);
00101   }
00102   if (dev < 0) dev = 0;
00103   if (dev > deviceCount-1) dev = deviceCount - 1;
00104   cudaDeviceProp deviceProp;
00105   CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev));
00106   if (deviceProp.major < 1) {
00107     fprintf(stderr, "cutil error: GPU device does not support CUDA.\n");
00108     exit(-1);
00109   }
00110   CUDA_SAFE_CALL(cudaSetDevice(dev));
00111 }
00112 
00113 #endif


parallel_quickstep
Author(s): Jared Duke
autogenerated on Fri Jan 3 2014 11:36:56