EMAN2
cuda_defs.h
Go to the documentation of this file.
1/*
2* Copyright 1993-2006 NVIDIA Corporation. All rights reserved.
3*
4* NOTICE TO USER:
5*
6* This source code is subject to NVIDIA ownership rights under U.S. and
7* international Copyright laws.
8*
9* NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
10* CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
11* IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH
12* REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
13* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
14* IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
15* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
17* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
18* OR PERFORMANCE OF THIS SOURCE CODE.
19*
20* U.S. Government End Users. This source code is a "commercial item" as
21* that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting of
22* "commercial computer software" and "commercial computer software
23* documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
24* and is provided to the U.S. Government only as a commercial end item.
25* Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
26* 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
27* source code with only those rights set forth herein.
28*/
29
30
31/* CUda UTility Library */
32
33#ifndef _CUTIL_H_
34#define _CUTIL_H_
35
36#ifdef _WIN32
37# pragma warning( disable : 4996 ) // disable deprecated warning
38#endif
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44 // helper typedefs for building DLL
45#ifdef _WIN32
46# ifdef BUILD_DLL
47# define DLL_MAPPING __declspec(dllexport)
48# else
49# define DLL_MAPPING __declspec(dllimport)
50# endif
51#else
52# define DLL_MAPPING
53#endif
54
55#ifdef _WIN32
56 #define CUTIL_API __stdcall
57#else
58 #define CUTIL_API
59#endif
60
61
66 {
68 CUTTrue = 1
69 };
70
76 void CUTIL_API
77 cutFree( void* ptr);
78
94 void CUTIL_API
95 cutCheckBankAccess( unsigned int tidx, unsigned int tidy, unsigned int tidz,
96 unsigned int bdimx, unsigned int bdimy,
97 unsigned int bdimz, const char* file, const int line,
98 const char* aname, const int index);
99
107 char* CUTIL_API
108 cutFindFilePath(const char* filename, const char* executablePath);
109
123 cutReadFilef( const char* filename, float** data, unsigned int* len,
124 bool verbose = false);
125
139 cutReadFiled( const char* filename, double** data, unsigned int* len,
140 bool verbose = false);
141
155 cutReadFilei( const char* filename, int** data, unsigned int* len, bool verbose = false);
156
170 cutReadFileui( const char* filename, unsigned int** data,
171 unsigned int* len, bool verbose = false);
172
186 cutReadFileb( const char* filename, char** data, unsigned int* len,
187 bool verbose = false);
188
202 cutReadFileub( const char* filename, unsigned char** data,
203 unsigned int* len, bool verbose = false);
204
216 cutWriteFilef( const char* filename, const float* data, unsigned int len,
217 const float epsilon, bool verbose = false);
218
230 cutWriteFiled( const char* filename, const float* data, unsigned int len,
231 const double epsilon, bool verbose = false);
232
242 cutWriteFilei( const char* filename, const int* data, unsigned int len,
243 bool verbose = false);
244
254 cutWriteFileui( const char* filename,const unsigned int* data,
255 unsigned int len, bool verbose = false);
256
266 cutWriteFileb( const char* filename, const char* data, unsigned int len,
267 bool verbose = false);
268
278 cutWriteFileub( const char* filename,const unsigned char* data,
279 unsigned int len, bool verbose = false);
280
294 cutLoadPGMub( const char* file, unsigned char** data,
295 unsigned int *w,unsigned int *h);
296
307 cutLoadPPMub( const char* file, unsigned char** data,
308 unsigned int *w,unsigned int *h);
309
321 cutLoadPPM4ub( const char* file, unsigned char** data,
322 unsigned int *w,unsigned int *h);
323
337 cutLoadPGMi( const char* file, unsigned int** data,
338 unsigned int* w, unsigned int* h);
339
353 cutLoadPGMs( const char* file, unsigned short** data,
354 unsigned int* w, unsigned int* h);
355
368 cutLoadPGMf( const char* file, float** data,
369 unsigned int* w, unsigned int* h);
370
380 cutSavePGMub( const char* file, unsigned char* data,
381 unsigned int w, unsigned int h);
382
392 cutSavePPMub( const char* file, unsigned char *data,
393 unsigned int w, unsigned int h);
394
405 cutSavePPM4ub( const char* file, unsigned char *data,
406 unsigned int w, unsigned int h);
407
417 cutSavePGMi( const char* file, unsigned int* data,
418 unsigned int w, unsigned int h);
419
429 cutSavePGMs( const char* file, unsigned short* data,
430 unsigned int w, unsigned int h);
431
441 cutSavePGMf( const char* file, float* data,
442 unsigned int w, unsigned int h);
443
445 // Command line arguments: General notes
446 // * All command line arguments begin with '--' followed by the token;
447 // token and value are seperated by '='; example --samples=50
448 // * Arrays have the form --model=[one.obj,two.obj,three.obj]
449 // (without whitespaces)
451
462 cutCheckCmdLineFlag( const int argc, const char** argv,
463 const char* flag_name);
464
476 cutGetCmdLineArgumenti( const int argc, const char** argv,
477 const char* arg_name, int* val);
478
490 cutGetCmdLineArgumentf( const int argc, const char** argv,
491 const char* arg_name, float* val);
492
504 cutGetCmdLineArgumentstr( const int argc, const char** argv,
505 const char* arg_name, char** val);
506
519 cutGetCmdLineArgumentListstr( const int argc, const char** argv,
520 const char* arg_name, char** val,
521 unsigned int* len);
522
533 cutCheckCondition( int val, const char* file, const int line);
534
545 cutComparef( const float* reference, const float* data,
546 const unsigned int len);
547
558 cutComparei( const int* reference, const int* data,
559 const unsigned int len );
560
571 cutCompareub( const unsigned char* reference, const unsigned char* data,
572 const unsigned int len );
573
585 cutCompareube( const unsigned char* reference, const unsigned char* data,
586 const unsigned int len, const int epsilon );
587
599 cutComparefe( const float* reference, const float* data,
600 const unsigned int len, const float epsilon );
601
614 cutCompareL2fe( const float* reference, const float* data,
615 const unsigned int len, const float epsilon );
616
619
627 cutCreateTimer( unsigned int* name);
628
636 cutDeleteTimer( unsigned int name);
637
644 cutStartTimer( const unsigned int name);
645
652 cutStopTimer( const unsigned int name);
653
660 cutResetTimer( const unsigned int name);
661
668 float CUTIL_API
669 cutGetTimerValue( const unsigned int name);
670
679 float CUTIL_API
680 cutGetAverageTimerValue( const unsigned int name);
681
684
685#ifdef _DEBUG
686
687#if __DEVICE_EMULATION__
688 // Interface for bank conflict checker
689#define CUT_BANK_CHECKER( array, index) \
690 (cutCheckBankAccess( threadIdx.x, threadIdx.y, threadIdx.z, blockDim.x, \
691 blockDim.y, blockDim.z, \
692 __FILE__, __LINE__, #array, index ), \
693 array[index])
694#else
695#define CUT_BANK_CHECKER( array, index) array[index]
696#endif
697
698# define CU_SAFE_CALL_NO_SYNC( call ) do { \
699 CUresult err = call; \
700 if( CUDA_SUCCESS != err) { \
701 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
702 err, __FILE__, __LINE__ ); \
703 exit(EXIT_FAILURE); \
704 } } while (0)
705
706# define CU_SAFE_CALL( call ) do { \
707 CU_SAFE_CALL_NO_SYNC(call); \
708 CUresult err = cuCtxSynchronize(); \
709 if( CUDA_SUCCESS != err) { \
710 fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
711 err, __FILE__, __LINE__ ); \
712 exit(EXIT_FAILURE); \
713 } } while (0)
714
715# define CUDA_SAFE_CALL_NO_SYNC( call) do { \
716 cudaError err = call; \
717 if( cudaSuccess != err) { \
718 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
719 __FILE__, __LINE__, cudaGetErrorString( err) ); \
720 exit(EXIT_FAILURE); \
721 } } while (0)
722
723# define CUDA_SAFE_CALL( call) do { \
724 CUDA_SAFE_CALL_NO_SYNC(call); \
725 cudaError err = cudaThreadSynchronize(); \
726 if( cudaSuccess != err) { \
727 fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
728 __FILE__, __LINE__, cudaGetErrorString( err) ); \
729 exit(EXIT_FAILURE); \
730 } } while (0)
731
732# define CUFFT_SAFE_CALL( call) do { \
733 cufftResult err = call; \
734 if( CUFFT_SUCCESS != err) { \
735 fprintf(stderr, "CUFFT error in file '%s' in line %i.\n", \
736 __FILE__, __LINE__); \
737 exit(EXIT_FAILURE); \
738 } } while (0)
739
740# define CUT_SAFE_CALL( call) \
741 if( CUTTrue != call) { \
742 fprintf(stderr, "Cut error in file '%s' in line %i.\n", \
743 __FILE__, __LINE__); \
744 exit(EXIT_FAILURE); \
745 }
746
748# define CUT_CHECK_ERROR(errorMessage) do { \
749 cudaError_t err = cudaGetLastError(); \
750 if( cudaSuccess != err) { \
751 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
752 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
753 exit(EXIT_FAILURE); \
754 } \
755 err = cudaThreadSynchronize(); \
756 if( cudaSuccess != err) { \
757 fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
758 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
759 exit(EXIT_FAILURE); \
760 } } while (0)
761
763# define CUT_SAFE_MALLOC( mallocCall ) do{ \
764 if( !(mallocCall)) { \
765 fprintf(stderr, "Host malloc failure in file '%s' in line %i\n", \
766 __FILE__, __LINE__); \
767 exit(EXIT_FAILURE); \
768 } } while(0);
769
771# define CUT_CONDITION( val) \
772 if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) { \
773 exit(EXIT_FAILURE); \
774 }
775
776#else // not DEBUG
777
778#define CUT_BANK_CHECKER( array, index) array[index]
779
780 // void macros for performance reasons
781# define CUT_CHECK_ERROR(errorMessage)
782# define CUT_CHECK_ERROR_GL()
783# define CUT_CONDITION( val)
784# define CU_SAFE_CALL_NO_SYNC( call) call
785# define CU_SAFE_CALL( call) call
786# define CUDA_SAFE_CALL_NO_SYNC( call) call
787# define CUDA_SAFE_CALL( call) call
788# define CUT_SAFE_CALL( call) call
789# define CUFFT_SAFE_CALL( call) call
790# define CUT_SAFE_MALLOC( mallocCall ) mallocCall
791
792#endif
793
794#if __DEVICE_EMULATION__
795
796# define CUT_DEVICE_INIT(ARGC, ARGV)
797
798#else
799
800# define CUT_DEVICE_INIT(ARGC, ARGV) { \
801 int deviceCount; \
802 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount)); \
803 if (deviceCount == 0) { \
804 fprintf(stderr, "cutil error: no devices supporting CUDA.\n"); \
805 exit(EXIT_FAILURE); \
806 } \
807 int dev = 0; \
808 cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
809 if (dev > deviceCount-1) dev = deviceCount - 1; \
810 cudaDeviceProp deviceProp; \
811 CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev)); \
812 if (deviceProp.major < 1) { \
813 fprintf(stderr, "cutil error: device does not support CUDA.\n"); \
814 exit(EXIT_FAILURE); \
815 } \
816 if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
817 fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name); \
818 CUDA_SAFE_CALL(cudaSetDevice(dev)); \
819}
820
821#endif
822
823# define CUT_DEVICE_INIT_DRV(cuDevice, ARGC, ARGV) { \
824 cuDevice = 0; \
825 int deviceCount = 0; \
826 CUresult err = cuInit(0); \
827 if (CUDA_SUCCESS == err) \
828 CU_SAFE_CALL_NO_SYNC(cuDeviceGetCount(&deviceCount)); \
829 if (deviceCount == 0) { \
830 fprintf(stderr, "cutil error: no devices supporting CUDA\n"); \
831 exit(EXIT_FAILURE); \
832 } \
833 int dev = 0; \
834 cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
835 if (dev > deviceCount-1) dev = deviceCount - 1; \
836 CU_SAFE_CALL_NO_SYNC(cuDeviceGet(&cuDevice, dev)); \
837 char name[100]; \
838 cuDeviceGetName(name, 100, cuDevice); \
839 if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
840 fprintf(stderr, "Using device %d: %s\n", dev, name); \
841}
842
843#define CUT_EXIT(argc, argv) \
844 if (!cutCheckCmdLineFlag(argc, (const char**)argv, "noprompt")) { \
845 printf("\nPress ENTER to exit...\n"); \
846 fflush( stdout); \
847 fflush( stderr); \
848 getchar(); \
849 } \
850 exit(EXIT_SUCCESS);
851
852
853#ifdef __cplusplus
854}
855#endif // #ifdef _DEBUG (else branch)
856
857#endif // #ifndef _CUTIL_H_
DLL_MAPPING CUTBoolean CUTIL_API cutDeleteTimer(unsigned int name)
Delete a timer.
DLL_MAPPING CUTBoolean CUTIL_API cutReadFilef(const char *filename, float **data, unsigned int *len, bool verbose=false)
Read file \filename containing single precision floating point data.
DLL_MAPPING CUTBoolean CUTIL_API cutCheckCmdLineFlag(const int argc, const char **argv, const char *flag_name)
Check if command line argument flag-name is given.
DLL_MAPPING void CUTIL_API cutFree(void *ptr)
Deallocate memory allocated within Cutil.
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPPM4ub(const char *file, unsigned char **data, unsigned int *w, unsigned int *h)
Load PPM image file (with unsigned char as data element type), padding 4th component.
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPGMf(const char *file, float **data, unsigned int *w, unsigned int *h)
Load PGM image file (with float as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutGetCmdLineArgumentf(const int argc, const char **argv, const char *arg_name, float *val)
Get the value of a command line argument of type float.
DLL_MAPPING CUTBoolean CUTIL_API cutStartTimer(const unsigned int name)
Start the time with name name.
DLL_MAPPING CUTBoolean CUTIL_API cutComparei(const int *reference, const int *data, const unsigned int len)
Compare two integer arrays.
DLL_MAPPING CUTBoolean CUTIL_API cutCreateTimer(unsigned int *name)
Timer functionality.
DLL_MAPPING CUTBoolean CUTIL_API cutComparef(const float *reference, const float *data, const unsigned int len)
Compare two float arrays.
DLL_MAPPING CUTBoolean CUTIL_API cutSavePGMi(const char *file, unsigned int *data, unsigned int w, unsigned int h)
Save PGM image file (with unsigned int as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutGetCmdLineArgumentListstr(const int argc, const char **argv, const char *arg_name, char **val, unsigned int *len)
Get the value of a command line argument list those element are strings.
DLL_MAPPING void CUTIL_API cutCheckBankAccess(unsigned int tidx, unsigned int tidy, unsigned int tidz, unsigned int bdimx, unsigned int bdimy, unsigned int bdimz, const char *file, const int line, const char *aname, const int index)
Helper for bank conflict checking (should only be used with the CUT_BANK_CHECKER macro)
DLL_MAPPING CUTBoolean CUTIL_API cutSavePGMub(const char *file, unsigned char *data, unsigned int w, unsigned int h)
Save PGM image file (with unsigned char as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutStopTimer(const unsigned int name)
Stop the time with name name.
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFileui(const char *filename, const unsigned int *data, unsigned int len, bool verbose=false)
Write a data file \filename containing unsigned integer data.
DLL_MAPPING CUTBoolean CUTIL_API cutReadFiled(const char *filename, double **data, unsigned int *len, bool verbose=false)
Read file \filename containing double precision floating point data.
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFiled(const char *filename, const float *data, unsigned int len, const double epsilon, bool verbose=false)
Write a data file \filename containing double precision floating point data.
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFilef(const char *filename, const float *data, unsigned int len, const float epsilon, bool verbose=false)
Write a data file \filename containing single precision floating point data.
DLL_MAPPING CUTBoolean CUTIL_API cutCompareube(const unsigned char *reference, const unsigned char *data, const unsigned int len, const int epsilon)
Compare two integer arrays witha n epsilon tolerance for equality.
DLL_MAPPING CUTBoolean CUTIL_API cutComparefe(const float *reference, const float *data, const unsigned int len, const float epsilon)
Compare two float arrays with an epsilon tolerance for equality.
DLL_MAPPING CUTBoolean CUTIL_API cutCompareub(const unsigned char *reference, const unsigned char *data, const unsigned int len)
Compare two unsigned char arrays.
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFilei(const char *filename, const int *data, unsigned int len, bool verbose=false)
Write a data file \filename containing integer data.
DLL_MAPPING char *CUTIL_API cutFindFilePath(const char *filename, const char *executablePath)
Find the path for a filename.
DLL_MAPPING CUTBoolean CUTIL_API cutReadFilei(const char *filename, int **data, unsigned int *len, bool verbose=false)
Read file \filename containing integer data.
#define DLL_MAPPING
Definition: cuda_defs.h:52
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPGMub(const char *file, unsigned char **data, unsigned int *w, unsigned int *h)
Load PGM image file (with unsigned char as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPPMub(const char *file, unsigned char **data, unsigned int *w, unsigned int *h)
Load PPM image file (with unsigned char as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFileub(const char *filename, const unsigned char *data, unsigned int len, bool verbose=false)
Write a data file \filename containing unsigned char / byte data.
DLL_MAPPING CUTBoolean CUTIL_API cutWriteFileb(const char *filename, const char *data, unsigned int len, bool verbose=false)
Write a data file \filename containing char / byte data.
DLL_MAPPING CUTBoolean CUTIL_API cutSavePGMf(const char *file, float *data, unsigned int w, unsigned int h)
Save PGM image file (with float as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutGetCmdLineArgumentstr(const int argc, const char **argv, const char *arg_name, char **val)
Get the value of a command line argument of type string.
DLL_MAPPING CUTBoolean CUTIL_API cutSavePGMs(const char *file, unsigned short *data, unsigned int w, unsigned int h)
Save PGM image file (with unsigned short as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPGMi(const char *file, unsigned int **data, unsigned int *w, unsigned int *h)
Load PGM image file (with unsigned int as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutCheckCondition(int val, const char *file, const int line)
Extended assert.
DLL_MAPPING CUTBoolean CUTIL_API cutSavePPMub(const char *file, unsigned char *data, unsigned int w, unsigned int h)
Save PPM image file (with unsigned char as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutSavePPM4ub(const char *file, unsigned char *data, unsigned int w, unsigned int h)
Save PPM image file (with unsigned char as data element type, padded to 4 bytes)
DLL_MAPPING CUTBoolean CUTIL_API cutGetCmdLineArgumenti(const int argc, const char **argv, const char *arg_name, int *val)
Get the value of a command line argument of type int.
DLL_MAPPING CUTBoolean CUTIL_API cutResetTimer(const unsigned int name)
Resets the timer's counter.
DLL_MAPPING CUTBoolean CUTIL_API cutReadFileui(const char *filename, unsigned int **data, unsigned int *len, bool verbose=false)
Read file \filename containing unsigned integer data.
DLL_MAPPING CUTBoolean CUTIL_API cutReadFileb(const char *filename, char **data, unsigned int *len, bool verbose=false)
Read file \filename containing char / byte data.
DLL_MAPPING CUTBoolean CUTIL_API cutCompareL2fe(const float *reference, const float *data, const unsigned int len, const float epsilon)
Compare two float arrays using L2-norm with an epsilon tolerance for equality.
DLL_MAPPING float CUTIL_API cutGetAverageTimerValue(const unsigned int name)
Return the average time in milliseconds for timer execution as the total time for the timer dividied ...
DLL_MAPPING CUTBoolean CUTIL_API cutLoadPGMs(const char *file, unsigned short **data, unsigned int *w, unsigned int *h)
Load PGM image file (with unsigned short as data element type)
DLL_MAPPING CUTBoolean CUTIL_API cutReadFileub(const char *filename, unsigned char **data, unsigned int *len, bool verbose=false)
Read file \filename containing unsigned char / byte data.
CUTBoolean
CUT bool type.
Definition: cuda_defs.h:66
@ CUTTrue
Definition: cuda_defs.h:68
@ CUTFalse
Definition: cuda_defs.h:67
DLL_MAPPING float CUTIL_API cutGetTimerValue(const unsigned int name)
Returns total execution time in milliseconds for the timer over all runs since the last reset or time...
#define CUTIL_API
Definition: cuda_defs.h:58