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 #ifndef _CUTIL_H_
00027 #define _CUTIL_H_
00028
00029 #ifdef _WIN32
00030 # pragma warning( disable : 4996 ) // disable deprecated warning
00031 #endif
00032
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039
00040
00041 #ifdef _WIN32
00042 # ifdef BUILD_DLL
00043 # define DLL_MAPPING __declspec(dllexport)
00044 # else
00045 # define DLL_MAPPING __declspec(dllimport)
00046 # endif
00047 #else
00048 # define DLL_MAPPING
00049 #endif
00050
00051 #ifdef _WIN32
00052 #define CUTIL_API __stdcall
00053 #else
00054 #define CUTIL_API
00055 #endif
00056
00060 enum CUTBoolean
00061 {
00062 CUTFalse = 0,
00063 CUTTrue = 1
00064 };
00065
00070 DLL_MAPPING
00071 void CUTIL_API
00072 cutFree( void* ptr);
00073
00088 DLL_MAPPING
00089 void CUTIL_API
00090 cutCheckBankAccess( unsigned int tidx, unsigned int tidy, unsigned int tidz,
00091 unsigned int bdimx, unsigned int bdimy,
00092 unsigned int bdimz, const char* file, const int line,
00093 const char* aname, const int index);
00094
00101 DLL_MAPPING
00102 char* CUTIL_API
00103 cutFindFilePath(const char* filename, const char* executablePath);
00104
00116 DLL_MAPPING
00117 CUTBoolean CUTIL_API
00118 cutReadFilef( const char* filename, float** data, unsigned int* len,
00119 bool verbose = false);
00120
00132 DLL_MAPPING
00133 CUTBoolean CUTIL_API
00134 cutReadFiled( const char* filename, double** data, unsigned int* len,
00135 bool verbose = false);
00136
00148 DLL_MAPPING
00149 CUTBoolean CUTIL_API
00150 cutReadFilei( const char* filename, int** data, unsigned int* len, bool verbose = false);
00151
00163 DLL_MAPPING
00164 CUTBoolean CUTIL_API
00165 cutReadFileui( const char* filename, unsigned int** data,
00166 unsigned int* len, bool verbose = false);
00167
00179 DLL_MAPPING
00180 CUTBoolean CUTIL_API
00181 cutReadFileb( const char* filename, char** data, unsigned int* len,
00182 bool verbose = false);
00183
00195 DLL_MAPPING
00196 CUTBoolean CUTIL_API
00197 cutReadFileub( const char* filename, unsigned char** data,
00198 unsigned int* len, bool verbose = false);
00199
00209 DLL_MAPPING
00210 CUTBoolean CUTIL_API
00211 cutWriteFilef( const char* filename, const float* data, unsigned int len,
00212 const float epsilon, bool verbose = false);
00213
00223 DLL_MAPPING
00224 CUTBoolean CUTIL_API
00225 cutWriteFiled( const char* filename, const float* data, unsigned int len,
00226 const double epsilon, bool verbose = false);
00227
00235 DLL_MAPPING
00236 CUTBoolean CUTIL_API
00237 cutWriteFilei( const char* filename, const int* data, unsigned int len,
00238 bool verbose = false);
00239
00247 DLL_MAPPING
00248 CUTBoolean CUTIL_API
00249 cutWriteFileui( const char* filename,const unsigned int* data,
00250 unsigned int len, bool verbose = false);
00251
00259 DLL_MAPPING
00260 CUTBoolean CUTIL_API
00261 cutWriteFileb( const char* filename, const char* data, unsigned int len,
00262 bool verbose = false);
00263
00271 DLL_MAPPING
00272 CUTBoolean CUTIL_API
00273 cutWriteFileub( const char* filename,const unsigned char* data,
00274 unsigned int len, bool verbose = false);
00275
00287 DLL_MAPPING
00288 CUTBoolean CUTIL_API
00289 cutLoadPGMub( const char* file, unsigned char** data,
00290 unsigned int *w,unsigned int *h);
00291
00300 DLL_MAPPING
00301 CUTBoolean CUTIL_API
00302 cutLoadPPMub( const char* file, unsigned char** data,
00303 unsigned int *w,unsigned int *h);
00304
00314 DLL_MAPPING
00315 CUTBoolean CUTIL_API
00316 cutLoadPPM4ub( const char* file, unsigned char** data,
00317 unsigned int *w,unsigned int *h);
00318
00330 DLL_MAPPING
00331 CUTBoolean CUTIL_API
00332 cutLoadPGMi( const char* file, unsigned int** data,
00333 unsigned int* w, unsigned int* h);
00334
00346 DLL_MAPPING
00347 CUTBoolean CUTIL_API
00348 cutLoadPGMs( const char* file, unsigned short** data,
00349 unsigned int* w, unsigned int* h);
00350
00361 DLL_MAPPING
00362 CUTBoolean CUTIL_API
00363 cutLoadPGMf( const char* file, float** data,
00364 unsigned int* w, unsigned int* h);
00365
00373 DLL_MAPPING
00374 CUTBoolean CUTIL_API
00375 cutSavePGMub( const char* file, unsigned char* data,
00376 unsigned int w, unsigned int h);
00377
00385 DLL_MAPPING
00386 CUTBoolean CUTIL_API
00387 cutSavePPMub( const char* file, unsigned char *data,
00388 unsigned int w, unsigned int h);
00389
00398 DLL_MAPPING
00399 CUTBoolean CUTIL_API
00400 cutSavePPM4ub( const char* file, unsigned char *data,
00401 unsigned int w, unsigned int h);
00402
00410 DLL_MAPPING
00411 CUTBoolean CUTIL_API
00412 cutSavePGMi( const char* file, unsigned int* data,
00413 unsigned int w, unsigned int h);
00414
00422 DLL_MAPPING
00423 CUTBoolean CUTIL_API
00424 cutSavePGMs( const char* file, unsigned short* data,
00425 unsigned int w, unsigned int h);
00426
00434 DLL_MAPPING
00435 CUTBoolean CUTIL_API
00436 cutSavePGMf( const char* file, float* data,
00437 unsigned int w, unsigned int h);
00438
00440
00441
00442
00443
00444
00446
00455 DLL_MAPPING
00456 CUTBoolean CUTIL_API
00457 cutCheckCmdLineFlag( const int argc, const char** argv,
00458 const char* flag_name);
00459
00469 DLL_MAPPING
00470 CUTBoolean CUTIL_API
00471 cutGetCmdLineArgumenti( const int argc, const char** argv,
00472 const char* arg_name, int* val);
00473
00483 DLL_MAPPING
00484 CUTBoolean CUTIL_API
00485 cutGetCmdLineArgumentf( const int argc, const char** argv,
00486 const char* arg_name, float* val);
00487
00497 DLL_MAPPING
00498 CUTBoolean CUTIL_API
00499 cutGetCmdLineArgumentstr( const int argc, const char** argv,
00500 const char* arg_name, char** val);
00501
00512 DLL_MAPPING
00513 CUTBoolean CUTIL_API
00514 cutGetCmdLineArgumentListstr( const int argc, const char** argv,
00515 const char* arg_name, char** val,
00516 unsigned int* len);
00517
00526 DLL_MAPPING
00527 CUTBoolean CUTIL_API
00528 cutCheckCondition( int val, const char* file, const int line);
00529
00538 DLL_MAPPING
00539 CUTBoolean CUTIL_API
00540 cutComparef( const float* reference, const float* data,
00541 const unsigned int len);
00542
00551 DLL_MAPPING
00552 CUTBoolean CUTIL_API
00553 cutComparei( const int* reference, const int* data,
00554 const unsigned int len );
00555
00565 DLL_MAPPING
00566 CUTBoolean CUTIL_API
00567 cutCompareuit( const unsigned int* reference, const unsigned int* data,
00568 const unsigned int len, const float epsilon, const float threshold );
00569
00578 DLL_MAPPING
00579 CUTBoolean CUTIL_API
00580 cutCompareub( const unsigned char* reference, const unsigned char* data,
00581 const unsigned int len );
00582
00593 DLL_MAPPING
00594 CUTBoolean CUTIL_API
00595 cutCompareubt( const unsigned char* reference, const unsigned char* data,
00596 const unsigned int len, const float epsilon, const float threshold );
00597
00607 DLL_MAPPING
00608 CUTBoolean CUTIL_API
00609 cutCompareube( const unsigned char* reference, const unsigned char* data,
00610 const unsigned int len, const float epsilon );
00611
00621 DLL_MAPPING
00622 CUTBoolean CUTIL_API
00623 cutComparefe( const float* reference, const float* data,
00624 const unsigned int len, const float epsilon );
00625
00636 DLL_MAPPING
00637 CUTBoolean CUTIL_API
00638 cutComparefet( const float* reference, const float* data,
00639 const unsigned int len, const float epsilon, const float threshold );
00640
00651 DLL_MAPPING
00652 CUTBoolean CUTIL_API
00653 cutCompareL2fe( const float* reference, const float* data,
00654 const unsigned int len, const float epsilon );
00655
00666 DLL_MAPPING
00667 CUTBoolean CUTIL_API
00668 cutComparePPM( const char *src_file, const char *ref_file, const float epsilon, const float threshold, bool verboseErrors = false );
00669
00670
00673
00679 DLL_MAPPING
00680 CUTBoolean CUTIL_API
00681 cutCreateTimer( unsigned int* name);
00682
00688 DLL_MAPPING
00689 CUTBoolean CUTIL_API
00690 cutDeleteTimer( unsigned int name);
00691
00696 DLL_MAPPING
00697 CUTBoolean CUTIL_API
00698 cutStartTimer( const unsigned int name);
00699
00704 DLL_MAPPING
00705 CUTBoolean CUTIL_API
00706 cutStopTimer( const unsigned int name);
00707
00712 DLL_MAPPING
00713 CUTBoolean CUTIL_API
00714 cutResetTimer( const unsigned int name);
00715
00721 DLL_MAPPING
00722 float CUTIL_API
00723 cutGetTimerValue( const unsigned int name);
00724
00732 DLL_MAPPING
00733 float CUTIL_API
00734 cutGetAverageTimerValue( const unsigned int name);
00735
00738
00739 #if CUDART_VERSION >= 4000
00740 #define CUT_DEVICE_SYNCHRONIZE( ) cudaDeviceSynchronize();
00741 #else
00742 #define CUT_DEVICE_SYNCHRONIZE( ) cudaThreadSynchronize();
00743 #endif
00744
00745 #if CUDART_VERSION >= 4000
00746 #define CUT_DEVICE_RESET( ) cudaDeviceReset();
00747 #else
00748 #define CUT_DEVICE_RESET( ) cudaThreadExit();
00749 #endif
00750
00751
00752 #define CUT_BANK_CHECKER( array, index) array[index]
00753
00754 # define CU_SAFE_CALL_NO_SYNC( call ) { \
00755 CUresult err = call; \
00756 if( CUDA_SUCCESS != err) { \
00757 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
00758 err, __FILE__, __LINE__ ); \
00759 exit(EXIT_FAILURE); \
00760 } }
00761
00762 # define CU_SAFE_CALL( call ) CU_SAFE_CALL_NO_SYNC(call);
00763
00764 # define CU_SAFE_CTX_SYNC( ) { \
00765 CUresult err = cuCtxSynchronize(); \
00766 if( CUDA_SUCCESS != err) { \
00767 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
00768 err, __FILE__, __LINE__ ); \
00769 exit(EXIT_FAILURE); \
00770 } }
00771
00772 # define CUDA_SAFE_CALL_NO_SYNC( call) { \
00773 cudaError err = call; \
00774 if( cudaSuccess != err) { \
00775 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
00776 __FILE__, __LINE__, cudaGetErrorString( err) ); \
00777 exit(EXIT_FAILURE); \
00778 } }
00779
00780 # define CUDA_SAFE_CALL( call) CUDA_SAFE_CALL_NO_SYNC(call); \
00781
00782 # define CUDA_SAFE_THREAD_SYNC( ) { \
00783 cudaError err = CUT_DEVICE_SYNCHRONIZE(); \
00784 if ( cudaSuccess != err) { \
00785 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
00786 __FILE__, __LINE__, cudaGetErrorString( err) ); \
00787 } }
00788
00789 # define CUFFT_SAFE_CALL( call) { \
00790 cufftResult err = call; \
00791 if( CUFFT_SUCCESS != err) { \
00792 fprintf(stderr, "CUFFT error in file '%s' in line %i.\n", \
00793 __FILE__, __LINE__); \
00794 exit(EXIT_FAILURE); \
00795 } }
00796
00797 # define CUT_SAFE_CALL( call) \
00798 if( CUTTrue != call) { \
00799 fprintf(stderr, "Cut error in file '%s' in line %i.\n", \
00800 __FILE__, __LINE__); \
00801 exit(EXIT_FAILURE); \
00802 }
00803
00805 #ifdef _DEBUG
00806 # define CUT_CHECK_ERROR(errorMessage) { \
00807 cudaError_t err = cudaGetLastError(); \
00808 if( cudaSuccess != err) { \
00809 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00810 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00811 exit(EXIT_FAILURE); \
00812 } \
00813 err = CUT_DEVICE_SYNCHRONIZE(); \
00814 if( cudaSuccess != err) { \
00815 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00816 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00817 exit(EXIT_FAILURE); \
00818 } \
00819 }
00820 #else
00821 # define CUT_CHECK_ERROR(errorMessage) { \
00822 cudaError_t err = cudaGetLastError(); \
00823 if( cudaSuccess != err) { \
00824 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00825 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00826 exit(EXIT_FAILURE); \
00827 } \
00828 }
00829 #endif
00830
00832 # define CUT_SAFE_MALLOC( mallocCall ) { \
00833 if( !(mallocCall)) { \
00834 fprintf(stderr, "Host malloc failure in file '%s' in line %i\n", \
00835 __FILE__, __LINE__); \
00836 exit(EXIT_FAILURE); \
00837 } } while(0);
00838
00840 # define CUT_CONDITION( val) \
00841 if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) { \
00842 exit(EXIT_FAILURE); \
00843 }
00844
00845 # define CUT_DEVICE_INIT(ARGC, ARGV) { \
00846 int deviceCount; \
00847 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount)); \
00848 if (deviceCount == 0) { \
00849 fprintf(stderr, "cutil error: no devices supporting CUDA.\n"); \
00850 exit(EXIT_FAILURE); \
00851 } \
00852 int dev = 0; \
00853 cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
00854 if (dev < 0) dev = 0; \
00855 if (dev > deviceCount-1) dev = deviceCount - 1; \
00856 cudaDeviceProp deviceProp; \
00857 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev)); \
00858 if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
00859 fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name); \
00860 CUDA_SAFE_CALL(cudaSetDevice(dev)); \
00861 }
00862
00863
00865 # define CUDA_CHECK_CTX_LOST(errorMessage) { \
00866 cudaError_t err = cudaGetLastError(); \
00867 if( cudaSuccess != err) { \
00868 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00869 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00870 exit(EXIT_FAILURE); \
00871 } \
00872 err = CUT_DEVICE_SYNCHRONIZE(); \
00873 if( cudaSuccess != err) { \
00874 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00875 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00876 exit(EXIT_FAILURE); \
00877 } }
00878
00880 # define CU_CHECK_CTX_LOST(errorMessage) { \
00881 cudaError_t err = cudaGetLastError(); \
00882 if( CUDA_ERROR_INVALID_CONTEXT != err) { \
00883 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00884 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00885 exit(EXIT_FAILURE); \
00886 } \
00887 err = CUT_DEVICE_SYNCHRONIZE(); \
00888 if( cudaSuccess != err) { \
00889 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
00890 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00891 exit(EXIT_FAILURE); \
00892 } }
00893
00894 # define CUT_DEVICE_INIT_DRV(cuDevice, ARGC, ARGV) { \
00895 cuDevice = 0; \
00896 int deviceCount = 0; \
00897 CUresult err = cuInit(0); \
00898 if (CUDA_SUCCESS == err) \
00899 CU_SAFE_CALL_NO_SYNC(cuDeviceGetCount(&deviceCount)); \
00900 if (deviceCount == 0) { \
00901 fprintf(stderr, "cutil error: no devices supporting CUDA\n"); \
00902 exit(EXIT_FAILURE); \
00903 } \
00904 int dev = 0; \
00905 cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
00906 if (dev < 0) dev = 0; \
00907 if (dev > deviceCount-1) dev = deviceCount - 1; \
00908 CU_SAFE_CALL_NO_SYNC(cuDeviceGet(&cuDevice, dev)); \
00909 char name[100]; \
00910 cuDeviceGetName(name, 100, cuDevice); \
00911 if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
00912 fprintf(stderr, "Using device %d: %s\n", dev, name); \
00913 }
00914
00915 #define CUT_EXIT(argc, argv) \
00916 if (!cutCheckCmdLineFlag(argc, (const char**)argv, "noprompt")) { \
00917 printf("\nPress ENTER to exit...\n"); \
00918 fflush( stdout); \
00919 fflush( stderr); \
00920 getchar(); \
00921 } \
00922 exit(EXIT_SUCCESS);
00923
00924
00925 #ifdef __cplusplus
00926 }
00927 #endif // #ifdef _DEBUG (else branch)
00928
00929 #endif // #ifndef _CUTIL_H_