Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

cuda_defs.h

Go to the documentation of this file.
00001 /*
00002 * Copyright 1993-2006 NVIDIA Corporation.  All rights reserved.
00003 *
00004 * NOTICE TO USER:   
00005 *
00006 * This source code is subject to NVIDIA ownership rights under U.S. and 
00007 * international Copyright laws.  
00008 *
00009 * NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE 
00010 * CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR 
00011 * IMPLIED WARRANTY OF ANY KIND.  NVIDIA DISCLAIMS ALL WARRANTIES WITH 
00012 * REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF 
00013 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.   
00014 * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, 
00015 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 
00016 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
00017 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE 
00018 * OR PERFORMANCE OF THIS SOURCE CODE.  
00019 *
00020 * U.S. Government End Users.  This source code is a "commercial item" as 
00021 * that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting  of 
00022 * "commercial computer software" and "commercial computer software 
00023 * documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995) 
00024 * and is provided to the U.S. Government only as a commercial end item.  
00025 * Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through 
00026 * 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the 
00027 * source code with only those rights set forth herein.
00028 */
00029 
00030 
00031 /* CUda UTility Library */
00032 
00033 #ifndef _CUTIL_H_
00034 #define _CUTIL_H_
00035 
00036 #ifdef _WIN32
00037 #   pragma warning( disable : 4996 ) // disable deprecated warning 
00038 #endif
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044     // helper typedefs for building DLL
00045 #ifdef _WIN32
00046 #  ifdef BUILD_DLL
00047 #    define DLL_MAPPING  __declspec(dllexport)
00048 #  else
00049 #    define DLL_MAPPING  __declspec(dllimport)
00050 #  endif
00051 #else 
00052 #  define DLL_MAPPING 
00053 #endif
00054 
00055 #ifdef _WIN32
00056     #define CUTIL_API __stdcall
00057 #else
00058     #define CUTIL_API
00059 #endif
00060 
00061 
00065     enum CUTBoolean 
00066     {
00067         CUTFalse = 0,
00068         CUTTrue = 1
00069     };
00070 
00075     DLL_MAPPING
00076     void CUTIL_API
00077         cutFree( void* ptr);
00078 
00093     DLL_MAPPING
00094     void CUTIL_API
00095     cutCheckBankAccess( unsigned int tidx, unsigned int tidy, unsigned int tidz,
00096                         unsigned int bdimx, unsigned int bdimy, 
00097                         unsigned int bdimz, const char* file, const int line,
00098                         const char* aname, const int index);
00099 
00106     DLL_MAPPING
00107     char* CUTIL_API
00108     cutFindFilePath(const char* filename, const char* executablePath);
00109 
00121     DLL_MAPPING
00122     CUTBoolean CUTIL_API 
00123     cutReadFilef( const char* filename, float** data, unsigned int* len, 
00124                   bool verbose = false);
00125 
00137     DLL_MAPPING
00138     CUTBoolean CUTIL_API 
00139     cutReadFiled( const char* filename, double** data, unsigned int* len, 
00140                   bool verbose = false);
00141 
00153     DLL_MAPPING
00154     CUTBoolean CUTIL_API 
00155     cutReadFilei( const char* filename, int** data, unsigned int* len, bool verbose = false);
00156 
00168     DLL_MAPPING
00169     CUTBoolean CUTIL_API 
00170     cutReadFileui( const char* filename, unsigned int** data, 
00171                    unsigned int* len, bool verbose = false);
00172 
00184     DLL_MAPPING
00185     CUTBoolean CUTIL_API 
00186     cutReadFileb( const char* filename, char** data, unsigned int* len, 
00187                   bool verbose = false);
00188 
00200     DLL_MAPPING
00201     CUTBoolean CUTIL_API 
00202     cutReadFileub( const char* filename, unsigned char** data, 
00203                    unsigned int* len, bool verbose = false);
00204 
00214     DLL_MAPPING
00215     CUTBoolean CUTIL_API 
00216     cutWriteFilef( const char* filename, const float* data, unsigned int len,
00217                    const float epsilon, bool verbose = false);
00218 
00228     DLL_MAPPING
00229     CUTBoolean CUTIL_API 
00230     cutWriteFiled( const char* filename, const float* data, unsigned int len,
00231                    const double epsilon, bool verbose = false);
00232 
00240     DLL_MAPPING
00241     CUTBoolean CUTIL_API 
00242     cutWriteFilei( const char* filename, const int* data, unsigned int len,
00243                    bool verbose = false);
00244 
00252     DLL_MAPPING
00253     CUTBoolean CUTIL_API 
00254     cutWriteFileui( const char* filename,const unsigned int* data, 
00255                     unsigned int len, bool verbose = false);
00256 
00264     DLL_MAPPING
00265     CUTBoolean CUTIL_API 
00266     cutWriteFileb( const char* filename, const char* data, unsigned int len, 
00267                    bool verbose = false);
00268 
00276     DLL_MAPPING
00277     CUTBoolean CUTIL_API 
00278     cutWriteFileub( const char* filename,const unsigned char* data,
00279                     unsigned int len, bool verbose = false);
00280 
00292     DLL_MAPPING
00293     CUTBoolean CUTIL_API
00294     cutLoadPGMub( const char* file, unsigned char** data,
00295                   unsigned int *w,unsigned int *h);
00296 
00305     DLL_MAPPING
00306     CUTBoolean CUTIL_API
00307     cutLoadPPMub( const char* file, unsigned char** data, 
00308                   unsigned int *w,unsigned int *h);
00309 
00319     DLL_MAPPING
00320     CUTBoolean CUTIL_API
00321     cutLoadPPM4ub( const char* file, unsigned char** data, 
00322                    unsigned int *w,unsigned int *h);
00323 
00335     DLL_MAPPING
00336     CUTBoolean CUTIL_API
00337         cutLoadPGMi( const char* file, unsigned int** data, 
00338                      unsigned int* w, unsigned int* h);
00339 
00351     DLL_MAPPING
00352     CUTBoolean CUTIL_API
00353         cutLoadPGMs( const char* file, unsigned short** data, 
00354                      unsigned int* w, unsigned int* h);
00355 
00366     DLL_MAPPING
00367     CUTBoolean CUTIL_API
00368         cutLoadPGMf( const char* file, float** data,
00369                      unsigned int* w, unsigned int* h);
00370 
00378     DLL_MAPPING
00379     CUTBoolean CUTIL_API
00380         cutSavePGMub( const char* file, unsigned char* data, 
00381                       unsigned int w, unsigned int h);
00382 
00390     DLL_MAPPING
00391     CUTBoolean CUTIL_API
00392     cutSavePPMub( const char* file, unsigned char *data, 
00393                 unsigned int w, unsigned int h);
00394 
00403     DLL_MAPPING
00404     CUTBoolean CUTIL_API
00405     cutSavePPM4ub( const char* file, unsigned char *data, 
00406                    unsigned int w, unsigned int h);
00407 
00415     DLL_MAPPING
00416     CUTBoolean CUTIL_API
00417     cutSavePGMi( const char* file, unsigned int* data,
00418                  unsigned int w, unsigned int h);
00419 
00427     DLL_MAPPING
00428     CUTBoolean CUTIL_API
00429     cutSavePGMs( const char* file, unsigned short* data,
00430                  unsigned int w, unsigned int h);
00431 
00439     DLL_MAPPING
00440     CUTBoolean CUTIL_API
00441     cutSavePGMf( const char* file, float* data,
00442                  unsigned int w, unsigned int h);
00443 
00445     // Command line arguments: General notes
00446     // * All command line arguments begin with '--' followed by the token; 
00447     //   token and value are seperated by '='; example --samples=50
00448     // * Arrays have the form --model=[one.obj,two.obj,three.obj] 
00449     //   (without whitespaces)
00451 
00460     DLL_MAPPING
00461     CUTBoolean CUTIL_API
00462     cutCheckCmdLineFlag( const int argc, const char** argv, 
00463                          const char* flag_name);
00464 
00474     DLL_MAPPING
00475     CUTBoolean CUTIL_API
00476     cutGetCmdLineArgumenti( const int argc, const char** argv, 
00477                             const char* arg_name, int* val);
00478 
00488     DLL_MAPPING
00489     CUTBoolean CUTIL_API
00490     cutGetCmdLineArgumentf( const int argc, const char** argv, 
00491                             const char* arg_name, float* val);
00492 
00502     DLL_MAPPING
00503     CUTBoolean CUTIL_API
00504     cutGetCmdLineArgumentstr( const int argc, const char** argv, 
00505                               const char* arg_name, char** val);
00506 
00517     DLL_MAPPING
00518     CUTBoolean CUTIL_API
00519     cutGetCmdLineArgumentListstr( const int argc, const char** argv, 
00520                                   const char* arg_name, char** val, 
00521                                   unsigned int* len);
00522 
00531     DLL_MAPPING
00532     CUTBoolean CUTIL_API
00533     cutCheckCondition( int val, const char* file, const int line);
00534 
00543     DLL_MAPPING
00544     CUTBoolean CUTIL_API 
00545     cutComparef( const float* reference, const float* data,
00546                  const unsigned int len);
00547 
00556     DLL_MAPPING
00557     CUTBoolean CUTIL_API 
00558     cutComparei( const int* reference, const int* data, 
00559                  const unsigned int len ); 
00560 
00569     DLL_MAPPING
00570     CUTBoolean CUTIL_API 
00571     cutCompareub( const unsigned char* reference, const unsigned char* data,
00572                   const unsigned int len ); 
00573 
00583     DLL_MAPPING
00584     CUTBoolean CUTIL_API
00585     cutCompareube( const unsigned char* reference, const unsigned char* data,
00586                  const unsigned int len, const int epsilon );
00587 
00597     DLL_MAPPING
00598     CUTBoolean CUTIL_API 
00599     cutComparefe( const float* reference, const float* data,
00600                   const unsigned int len, const float epsilon );
00601 
00612     DLL_MAPPING
00613     CUTBoolean CUTIL_API 
00614     cutCompareL2fe( const float* reference, const float* data,
00615                     const unsigned int len, const float epsilon );
00616 
00619 
00625     DLL_MAPPING
00626     CUTBoolean CUTIL_API 
00627     cutCreateTimer( unsigned int* name);
00628 
00634     DLL_MAPPING
00635     CUTBoolean CUTIL_API 
00636     cutDeleteTimer( unsigned int name);
00637 
00642     DLL_MAPPING
00643     CUTBoolean CUTIL_API 
00644     cutStartTimer( const unsigned int name);
00645 
00650     DLL_MAPPING
00651     CUTBoolean CUTIL_API 
00652     cutStopTimer( const unsigned int name);
00653 
00658     DLL_MAPPING
00659     CUTBoolean CUTIL_API 
00660     cutResetTimer( const unsigned int name);
00661 
00667     DLL_MAPPING
00668     float CUTIL_API 
00669     cutGetTimerValue( const unsigned int name);
00670 
00678     DLL_MAPPING
00679     float CUTIL_API 
00680     cutGetAverageTimerValue( const unsigned int name);
00681 
00684 
00685 #ifdef _DEBUG
00686 
00687 #if __DEVICE_EMULATION__
00688     // Interface for bank conflict checker
00689 #define CUT_BANK_CHECKER( array, index)                                      \
00690     (cutCheckBankAccess( threadIdx.x, threadIdx.y, threadIdx.z, blockDim.x,  \
00691     blockDim.y, blockDim.z,                                                  \
00692     __FILE__, __LINE__, #array, index ),                                     \
00693     array[index])
00694 #else
00695 #define CUT_BANK_CHECKER( array, index)  array[index]
00696 #endif
00697 
00698 #  define CU_SAFE_CALL_NO_SYNC( call ) do {                                  \
00699     CUresult err = call;                                                     \
00700     if( CUDA_SUCCESS != err) {                                               \
00701         fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n",   \
00702                 err, __FILE__, __LINE__ );                                   \
00703         exit(EXIT_FAILURE);                                                  \
00704     } } while (0)
00705 
00706 #  define CU_SAFE_CALL( call ) do {                                          \
00707     CU_SAFE_CALL_NO_SYNC(call);                                              \
00708     CUresult err = cuCtxSynchronize();                                       \
00709     if( CUDA_SUCCESS != err) {                                               \
00710         fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n",   \
00711                 err, __FILE__, __LINE__ );                                   \
00712         exit(EXIT_FAILURE);                                                  \
00713     } } while (0)
00714 
00715 #  define CUDA_SAFE_CALL_NO_SYNC( call) do {                                 \
00716     cudaError err = call;                                                    \
00717     if( cudaSuccess != err) {                                                \
00718         fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n",        \
00719                 __FILE__, __LINE__, cudaGetErrorString( err) );              \
00720         exit(EXIT_FAILURE);                                                  \
00721     } } while (0)
00722 
00723 #  define CUDA_SAFE_CALL( call) do {                                         \
00724     CUDA_SAFE_CALL_NO_SYNC(call);                                            \
00725     cudaError err = cudaThreadSynchronize();                                 \
00726     if( cudaSuccess != err) {                                                \
00727         fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n",        \
00728                 __FILE__, __LINE__, cudaGetErrorString( err) );              \
00729         exit(EXIT_FAILURE);                                                  \
00730     } } while (0)
00731 
00732 #  define CUFFT_SAFE_CALL( call) do {                                        \
00733     cufftResult err = call;                                                  \
00734     if( CUFFT_SUCCESS != err) {                                              \
00735         fprintf(stderr, "CUFFT error in file '%s' in line %i.\n",            \
00736                 __FILE__, __LINE__);                                         \
00737         exit(EXIT_FAILURE);                                                  \
00738     } } while (0)
00739 
00740 #  define CUT_SAFE_CALL( call)                                               \
00741     if( CUTTrue != call) {                                                   \
00742         fprintf(stderr, "Cut error in file '%s' in line %i.\n",              \
00743                 __FILE__, __LINE__);                                         \
00744         exit(EXIT_FAILURE);                                                  \
00745     } 
00746 
00748 #  define CUT_CHECK_ERROR(errorMessage) do {                                 \
00749     cudaError_t err = cudaGetLastError();                                    \
00750     if( cudaSuccess != err) {                                                \
00751         fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n",    \
00752                 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00753         exit(EXIT_FAILURE);                                                  \
00754     }                                                                        \
00755     err = cudaThreadSynchronize();                                           \
00756     if( cudaSuccess != err) {                                                \
00757         fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n",    \
00758                 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00759         exit(EXIT_FAILURE);                                                  \
00760     } } while (0)
00761 
00763 #  define CUT_SAFE_MALLOC( mallocCall ) do{                                  \
00764     if( !(mallocCall)) {                                                     \
00765         fprintf(stderr, "Host malloc failure in file '%s' in line %i\n",     \
00766                 __FILE__, __LINE__);                                         \
00767         exit(EXIT_FAILURE);                                                  \
00768     } } while(0);
00769 
00771 #  define CUT_CONDITION( val)                                                \
00772     if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) {           \
00773         exit(EXIT_FAILURE);                                                  \
00774     }
00775 
00776 #else  // not DEBUG
00777 
00778 #define CUT_BANK_CHECKER( array, index)  array[index]
00779 
00780     // void macros for performance reasons
00781 #  define CUT_CHECK_ERROR(errorMessage)
00782 #  define CUT_CHECK_ERROR_GL()
00783 #  define CUT_CONDITION( val) 
00784 #  define CU_SAFE_CALL_NO_SYNC( call) call
00785 #  define CU_SAFE_CALL( call) call
00786 #  define CUDA_SAFE_CALL_NO_SYNC( call) call
00787 #  define CUDA_SAFE_CALL( call) call
00788 #  define CUT_SAFE_CALL( call) call
00789 #  define CUFFT_SAFE_CALL( call) call
00790 #  define CUT_SAFE_MALLOC( mallocCall ) mallocCall
00791 
00792 #endif
00793 
00794 #if __DEVICE_EMULATION__
00795 
00796 #  define CUT_DEVICE_INIT(ARGC, ARGV)
00797 
00798 #else
00799 
00800 #  define CUT_DEVICE_INIT(ARGC, ARGV) {                                      \
00801     int deviceCount;                                                         \
00802     CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount));                \
00803     if (deviceCount == 0) {                                                  \
00804         fprintf(stderr, "cutil error: no devices supporting CUDA.\n");       \
00805         exit(EXIT_FAILURE);                                                  \
00806     }                                                                        \
00807     int dev = 0;                                                             \
00808     cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev);      \
00809     if (dev > deviceCount-1) dev = deviceCount - 1;                          \
00810     cudaDeviceProp deviceProp;                                               \
00811     CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev));       \
00812     if (deviceProp.major < 1) {                                              \
00813         fprintf(stderr, "cutil error: device does not support CUDA.\n");     \
00814         exit(EXIT_FAILURE);                                                  \
00815     }                                                                        \
00816     if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
00817         fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name);       \
00818     CUDA_SAFE_CALL(cudaSetDevice(dev));                                      \
00819 }
00820 
00821 #endif
00822 
00823 #  define CUT_DEVICE_INIT_DRV(cuDevice, ARGC, ARGV) {                        \
00824     cuDevice = 0;                                                            \
00825     int deviceCount = 0;                                                     \
00826     CUresult err = cuInit(0);                                                \
00827     if (CUDA_SUCCESS == err)                                                 \
00828         CU_SAFE_CALL_NO_SYNC(cuDeviceGetCount(&deviceCount));                \
00829     if (deviceCount == 0) {                                                  \
00830         fprintf(stderr, "cutil error: no devices supporting CUDA\n");        \
00831         exit(EXIT_FAILURE);                                                  \
00832     }                                                                        \
00833     int dev = 0;                                                             \
00834     cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev);      \
00835     if (dev > deviceCount-1) dev = deviceCount - 1;                          \
00836     CU_SAFE_CALL_NO_SYNC(cuDeviceGet(&cuDevice, dev));                       \
00837     char name[100];                                                          \
00838     cuDeviceGetName(name, 100, cuDevice);                                    \
00839     if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
00840         fprintf(stderr, "Using device %d: %s\n", dev, name);                  \
00841 }
00842 
00843 #define CUT_EXIT(argc, argv)                                                 \
00844     if (!cutCheckCmdLineFlag(argc, (const char**)argv, "noprompt")) {        \
00845         printf("\nPress ENTER to exit...\n");                                \
00846         fflush( stdout);                                                     \
00847         fflush( stderr);                                                     \
00848         getchar();                                                           \
00849     }                                                                        \
00850     exit(EXIT_SUCCESS);
00851 
00852 
00853 #ifdef __cplusplus
00854 }
00855 #endif  // #ifdef _DEBUG (else branch)
00856 
00857 #endif  // #ifndef _CUTIL_H_

Generated on Tue Jun 11 13:46:13 2013 for EMAN2 by  doxygen 1.3.9.1