cutil.h

00001 
00002 // This file comes from the CUDA SDK examples. It provides a number of
00003 // useful macros. We may get rid of it in the future as we write our own
00004 // macros.
00005 
00006 /*
00007 * Copyright 1993-2006 NVIDIA Corporation.  All rights reserved.
00008 *
00009 * NOTICE TO USER:
00010 *
00011 * This source code is subject to NVIDIA ownership rights under U.S. and
00012 * international Copyright laws.
00013 *
00014 * NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE
00015 * CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR
00016 * IMPLIED WARRANTY OF ANY KIND.  NVIDIA DISCLAIMS ALL WARRANTIES WITH
00017 * REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF
00018 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
00019 * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
00020 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00021 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00022 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
00023 * OR PERFORMANCE OF THIS SOURCE CODE.
00024 *
00025 * U.S. Government End Users.  This source code is a "commercial item" as
00026 * that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting  of
00027 * "commercial computer software" and "commercial computer software
00028 * documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995)
00029 * and is provided to the U.S. Government only as a commercial end item.
00030 * Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through
00031 * 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the
00032 * source code with only those rights set forth herein.
00033 */
00034 
00035 
00036 /* CUda UTility Library */
00037 
00038 #ifndef _CUTIL_H_
00039 #define _CUTIL_H_
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #define _CUDA_DEBUG
00046 
00047 
00048     ////////////////////////////////////////////////////////////////////////////
00049     //! Macros
00050 
00051 #ifdef _CUDA_DEBUG
00052 #include <stdio.h>
00053 #if __DEVICE_EMULATION__
00054     // Interface for bank conflict checker
00055 #define CUT_BANK_CHECKER( array, index)                                      \
00056     (cutCheckBankAccess( threadIdx.x, threadIdx.y, threadIdx.z, blockDim.x,  \
00057     blockDim.y, blockDim.z,                                                  \
00058     __FILE__, __LINE__, #array, index ),                                     \
00059     array[index])
00060 #else
00061 #define CUT_BANK_CHECKER( array, index)  array[index]
00062 #endif
00063 
00064 #  define CU_SAFE_CALL_NO_SYNC( call ) do {                                  \
00065     CUresult err = call;                                                     \
00066     if( CUDA_SUCCESS != err) {                                               \
00067         fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n",   \
00068                 err, __FILE__, __LINE__ );                                   \
00069         exit(EXIT_FAILURE);                                                  \
00070     } } while (0)
00071 
00072 #  define CU_SAFE_CALL( call ) do {                                          \
00073     CU_SAFE_CALL_NO_SYNC(call);                                              \
00074     CUresult err = cuCtxSynchronize();                                       \
00075     if( CUDA_SUCCESS != err) {                                               \
00076         fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n",   \
00077                 err, __FILE__, __LINE__ );                                   \
00078         abort();  \
00079     } } while (0)
00080 
00081 #  define CUDA_SAFE_CALL_NO_SYNC( call) do {                                 \
00082     cudaError err = call;                                                    \
00083     if( cudaSuccess != err) {                                                \
00084         fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n",        \
00085                 __FILE__, __LINE__, cudaGetErrorString( err) );              \
00086         abort();                                                             \
00087     } } while (0)
00088 
00089 #  define CUDA_SAFE_CALL( call) do {                                         \
00090     CUDA_SAFE_CALL_NO_SYNC(call);                                            \
00091     cudaError err = cudaThreadSynchronize();                                 \
00092     if( cudaSuccess != err) {                                                \
00093         fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n",        \
00094                 __FILE__, __LINE__, cudaGetErrorString( err) );              \
00095         abort();                                                             \
00096     } } while (0)
00097 
00098 #  define CUFFT_SAFE_CALL( call) do {                                        \
00099     cufftResult err = call;                                                  \
00100     if( CUFFT_SUCCESS != err) {                                              \
00101         fprintf(stderr, "CUFFT error in file '%s' in line %i.\n",            \
00102                 __FILE__, __LINE__);                                         \
00103         exit(EXIT_FAILURE);                                                  \
00104     } } while (0)
00105 
00106 #  define CUT_SAFE_CALL( call)                                               \
00107     if( CUTTrue != call) {                                                   \
00108         fprintf(stderr, "Cut error in file '%s' in line %i.\n",              \
00109                 __FILE__, __LINE__);                                         \
00110         exit(EXIT_FAILURE);                                                  \
00111     }
00112 
00113     //! Check for CUDA error
00114 #  define CUT_CHECK_ERROR(errorMessage) do {                                 \
00115     cudaError_t err = cudaGetLastError();                                    \
00116     if( cudaSuccess != err) {                                                \
00117         fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n",    \
00118                 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00119         abort();                                                              \
00120     }                                                                        \
00121     err = cudaThreadSynchronize();                                           \
00122     if( cudaSuccess != err) {                                                \
00123         fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n",    \
00124                 errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
00125         abort();                                                             \
00126     } } while (0)
00127 
00128     //! Check for malloc error
00129 #  define CUT_SAFE_MALLOC( mallocCall ) do{                                  \
00130     if( !(mallocCall)) {                                                     \
00131         fprintf(stderr, "Host malloc failure in file '%s' in line %i\n",     \
00132                 __FILE__, __LINE__);                                         \
00133         exit(EXIT_FAILURE);                                                  \
00134     } } while(0);
00135 
00136     //! Check if conditon is true (flexible assert)
00137 #  define CUT_CONDITION( val)                                                \
00138     if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) {           \
00139         exit(EXIT_FAILURE);                                                  \
00140     }
00141 
00142 #else  // not DEBUG
00143 
00144 #define CUT_BANK_CHECKER( array, index)  array[index]
00145 
00146     // void macros for performance reasons
00147 #  define CUT_CHECK_ERROR(errorMessage)
00148 #  define CUT_CHECK_ERROR_GL()
00149 #  define CUT_CONDITION( val)
00150 #  define CU_SAFE_CALL_NO_SYNC( call) call
00151 #  define CU_SAFE_CALL( call) call
00152 #  define CUDA_SAFE_CALL_NO_SYNC( call) call
00153 #  define CUDA_SAFE_CALL( call) call
00154 #  define CUT_SAFE_CALL( call) call
00155 #  define CUFFT_SAFE_CALL( call) call
00156 #  define CUT_SAFE_MALLOC( mallocCall ) mallocCall
00157 
00158 #endif
00159 
00160 #if __DEVICE_EMULATION__
00161 
00162 #  define CUT_DEVICE_INIT(DEV)
00163 
00164 #else
00165 
00166 #  define CUT_DEVICE_INIT(DEV) {                                      \
00167     int deviceCount;                                                         \
00168     CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount));                \
00169     if (deviceCount == 0) {                                                  \
00170         fprintf(stderr, "cutil error: no devices supporting CUDA.\n");       \
00171         exit(EXIT_FAILURE);                                                  \
00172     }                                                                        \
00173     int dev = DEV;                                                             \
00174     if (dev > deviceCount-1) dev = deviceCount - 1;                          \
00175     struct cudaDeviceProp deviceProp;                                               \
00176     CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev));       \
00177     if (deviceProp.major < 1) {                                              \
00178         fprintf(stderr, "cutil error: device does not support CUDA.\n");     \
00179         exit(EXIT_FAILURE);                                                  \
00180     }                                                                        \
00181     fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name);       \
00182     CUDA_SAFE_CALL(cudaSetDevice(dev));                                      \
00183 }
00184 
00185 #endif
00186 
00187 
00188 #ifdef __cplusplus
00189 }
00190 #endif  // #ifdef _DEBUG (else branch)
00191 
00192 #endif  // #ifndef _CUTIL_H_
Generated on Sun May 8 08:04:44 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3