#include #include "cuda.h" #include "cuda_util.h" // CUDA utilities // Jan Mandel, January 2009 // adapted from online sources as noted void checkCUDAError(const char *msg) // check for error on a previous CUDA call // useful for kernel launch which has no return code // or operations that can fail later after the call // adapted from Dr Dobb's journal { // block until the device has completed - added jm cudaThreadSynchronize(); // check error code cudaError_t err = cudaGetLastError(); if( cudaSuccess != err) { fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString( err) ); exit(1); } } void cudasafe( cudaError_t err, char* str) // adapted from FortranCUDA by NVIDA // check return code from a CUDA routine { if (err != cudaSuccess) { printf("%s failed with error code %i\n",str,err); exit(1); } // added jm just in case there is a delayed error checkCUDAError(str); } void cuda_device_init( int dev) { // adapted from FortranCUDA by NVIDA // initialize CUDA devide number dev (0 if system has only one) int deviceCount; cudasafe(cudaGetDeviceCount(&deviceCount),"cudaGetDeviceCount"); if (deviceCount == 0) { fprintf(stderr, "No devices supporting CUDA.\n"); exit(1); } cudaDeviceProp deviceProp; cudasafe(cudaGetDeviceProperties(&deviceProp, dev),"cudaGetDeviceProperties"); if (deviceProp.major < 1) { fprintf(stderr, "Device does not support CUDA.\n"); exit(1); } fprintf(stdout, " Device %d %s\n", dev, deviceProp.name); // after all that checking finally initialize cudasafe(cudaSetDevice(dev),"cudaSetDevice"); } // Fortran stubs extern "C" void cuda_device_init__( int* Dev) { // adapted from FortranCUDA by NVIDA // fortran callable stub to the initialization routine cuda_device_init( *Dev); }