![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsCUDA_GlobalFns_Def.cu 00003 00004 CUDA Global Functions Definition (i.e., cpp file). 00005 00006 SUKITTI PUNAK (08/27/2008) 00007 UPDATE (09/17/2009) 00008 ******************************************************************************/ 00009 00010 #include "TAPsCUDA_GlobalFns.cu" 00011 00012 BEGIN_NAMESPACE_TAPs__CUDA 00013 //============================================================================= 00014 //----------------------------------------------------------------------------- 00015 00016 // Initialize CUDA Device 00017 void Host__Init ( int argc, char **argv ) 00018 { 00019 printf( "START: CUT_DEVICE_INIT\n" ); 00020 CUT_DEVICE_INIT(argc, argv); 00021 printf( "END: CUT_DEVICE_INIT\n" ); 00022 } 00023 00024 // Initialize CUDA (with Page-Locked Host Memory) 00025 void Host__Init_withPLHM ( int argc, char **argv ) 00026 { 00027 if ( !SetDevice( 0 ) ) { 00028 printf( "FAILED: setting the device 0! -- File: %s Line: %i\n", __FILE__, __LINE__ ); 00029 exit(-501); 00030 } 00031 int device; 00032 if ( !GetDevice( device ) ) { 00033 printf( "FAILED: getting the current device! -- File: %s Line: %i\n", __FILE__, __LINE__ ); 00034 exit(-501); 00035 } 00036 if ( !CanDeviceMapHostMemory( device ) ) { 00037 printf( "FAILED: the current device cannot map host memory! -- File: %s Line: %i\n", __FILE__, __LINE__ ); 00038 exit(-502); 00039 } 00040 cudaError_t err = cudaSetDeviceFlags( cudaDeviceMapHost ); 00041 if ( err != cudaSuccess ) { 00042 printf( "FAILED: cannot set device's map host memory flag! (%s) -- File: %s Line: %i\n", cudaGetErrorString( err ), __FILE__, __LINE__ ); 00043 exit(-503); 00044 } 00045 } 00046 00047 // Get number of devices 00048 int DeviceCount () 00049 { 00050 int count; 00051 cudaGetDeviceCount( &count ); 00052 return count; 00053 } 00054 00055 // Get the device on which the active host thread executes the device code 00056 bool GetDevice ( int & device ) 00057 { 00058 cudaError_t err = cudaGetDevice( & device ); 00059 if ( err == cudaSuccess ) return true; 00060 printf( "FAILED: GetDevice(%i) -- %s\n", device, GetErrorString( err ) ); 00061 return false; 00062 } 00063 00064 // Set the device on which the active host thread executes the device code 00065 // If the host thread has already initialized the CUDA runtime by calling 00066 // non-device management runtime functions, this call results in 00067 // cudaErrorSetOnActiveProcess. 00068 bool SetDevice ( int device ) 00069 { 00070 cudaError_t err = cudaSetDevice( device ); 00071 if ( err == cudaSuccess ) return true; 00072 printf( "FAILED: SetDevice(%i) -- %s\n", device, GetErrorString( err ) ); 00073 return false; 00074 } 00075 00076 // Get Device's Properties 00077 bool GetDeviceProperties ( cudaDeviceProp * prop, int device ) 00078 { 00079 cudaError_t err = cudaGetDeviceProperties( prop, device ); 00080 if ( err == cudaSuccess ) return true; 00081 printf( "FAILED: GetDeviceProperties( ..., %i) -- %s\n", device, GetErrorString( err ) ); 00082 return false; 00083 } 00084 00085 // Can Map Host Memory (i.e. can utilize CUDA's Page-Locked Host Memory) 00086 bool CanDeviceMapHostMemory ( int device ) 00087 { 00088 //if ( !SetDevice( device ) ) return false; 00089 cudaDeviceProp prop; 00090 if ( !GetDeviceProperties( &prop, device ) ) return false; 00091 return prop.canMapHostMemory == 1; 00092 } 00093 00094 00095 00096 00097 // Check error and exit the program if the action flag is true 00098 bool CheckError ( cudaError_t error, const char * msg, bool bExitProgram ) 00099 { 00100 if ( error == cudaSuccess ) return true; 00101 if ( msg ) { 00102 printf ( "ERROR: %s -- %s\n", msg, cudaGetErrorString( error ) ); 00103 } 00104 else { 00105 printf ( "ERROR: %s\n", cudaGetErrorString( error ) ); 00106 } 00107 if ( bExitProgram ) exit( -555 ); 00108 return false; 00109 } 00110 00111 // Convert cudaError_t to const char * 00112 const char * GetErrorString ( cudaError_t error ) 00113 { 00114 return cudaGetErrorString( error ); 00115 } 00116 00117 //----------------------------------------------------------------------------- 00118 //============================================================================= 00119 END_NAMESPACE_TAPs__CUDA 00120 //----------------------------------------------------------------------------- 00121 //34567890123456789012345678901234567890123456789012345678901234567890123456789 00122 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----