go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
cudaInlineFunctions.h
Go to the documentation of this file.
00001 /*======================================================================
00002 
00003   This file is part of the elastix software.
00004 
00005   Copyright (c) University Medical Center Utrecht. All rights reserved.
00006   See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
00007   details.
00008 
00009      This software is distributed WITHOUT ANY WARRANTY; without even
00010      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011      PURPOSE. See the above copyright notices for more information.
00012 
00013 ======================================================================*/
00014 #ifndef __cudaInlineFunctions_h
00015 #define __cudaInlineFunctions_h
00016 
00017 #include <cuda_runtime.h>
00018 #include <stdio.h>
00019 #include <string>
00020 #include <assert.h>
00021 #include "cudaMacro.h"
00022 
00023 namespace cuda
00024 {
00025 
00026 #define cudaCheckMsg( msg ) __cudaCheckMsg( msg, __FILE__, __LINE__ )
00027 
00028 inline void __cudaCheckMsg( const char *msg, const char *file, const int line )
00029 {
00030   cudaError_t err = ::cudaGetLastError();
00031   if ( err != cudaSuccess )
00032   {
00033     const char* errcmsg = ::cudaGetErrorString( err );
00034     fprintf( stderr, "CUDA error: %s in file <%s>, line %i : %s.\n",
00035       msg, file, line, errcmsg );
00036     //assert( false );
00037 
00038     std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
00039     throw errmsg;
00040   }
00041 
00042 #ifndef NDEBUG
00043   err = ::cudaThreadSynchronize();
00044   if ( err != cudaSuccess )
00045   {
00046     const char* errcmsg = ::cudaGetErrorString( err );
00047     fprintf( stderr, "cudaThreadSynchronize error: %s in file <%s>, line %i : %s.\n",
00048       msg, file, line, errcmsg );
00049     assert( false );
00050 
00051     std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
00052     throw errmsg;
00053   }
00054 #endif /* NDEBUG */
00055 }
00056 
00057 
00058 template <class T>
00059 inline T* cudaMalloc( size_t nof_elems )
00060 {
00061   T* dst;
00062   size_t size = nof_elems * sizeof( T );
00063   ::cudaMalloc( (void**)&dst, size );
00064   cudaCheckMsg( "cudaMalloc failed!" );
00065 
00066   return dst;
00067 }
00068 
00069 
00070 template <class T>
00071 inline T* cudaHostAlloc( size_t nof_elems, unsigned int flags = cudaHostAllocDefault )
00072 {
00073   T* dst;
00074   size_t size = nof_elems * sizeof( T );
00075   ::cudaHostAlloc( (void**)&dst, size, flags );
00076   cudaCheckMsg( "cudaHostAlloc failed!" );
00077 
00078   return dst;
00079 }
00080 
00081 
00082 inline cudaError_t cudaMemcpy( void* dst, const void* src,
00083   size_t nof_elems, size_t sizeof_elem, cudaMemcpyKind direction )
00084 {
00085   cudaError err = ::cudaMemcpy( dst, src, nof_elems * sizeof_elem, direction );
00086   cudaCheckMsg( "cudaMemcpy failed!" );
00087   return err;
00088 }
00089 
00090 
00091 template <class T>
00092 inline void cudaMemcpy( T* dst, const T* src,
00093   size_t nof_elems, cudaMemcpyKind direction )
00094 {
00095   size_t size = nof_elems * sizeof( T );
00096   ::cudaMemcpy( dst, src, size, direction );
00097   cudaCheckMsg( "cudaMemcpy failed!" );
00098 }
00099 
00100 
00101 template <class T>
00102 inline void cudaMemset( T* dst, int value, size_t nof_elems )
00103 {
00104   size_t size = nof_elems * sizeof( T );
00105   ::cudaMemset( dst, value, size );
00106   cudaCheckMsg( "cudaMemset failed!" );
00107 }
00108 
00109 
00110 template <typename T, typename Q>
00111 inline cudaError_t cudaMemcpyToSymbol( T& dst, const Q& src,
00112   cudaMemcpyKind direction )
00113 {
00114   cudaError err = ::cudaMemcpyToSymbol( dst, &src, sizeof(dst), 0, direction );
00115   cudaCheckMsg( "cudaMemcpyToSymbol failed!" );
00116   return err;
00117 }
00118 
00119 
00120 template <typename T>
00121 inline cudaError_t cudaBindTextureToArray( T& tex, cudaArray* array,
00122   const cudaChannelFormatDesc desc )
00123 {
00124   cudaError_t err = ::cudaBindTextureToArray( tex, array, desc );
00125   cudaCheckMsg( "cudaBindTextureToArray failed!" );
00126   return err;
00127 }
00128 
00129 
00130 template <typename T>
00131 inline cudaError_t cudaUnbindTexture( T& tex )
00132 {
00133   cudaError_t err = ::cudaUnbindTexture( tex );
00134   cudaCheckMsg( "cudaUnbindTexture failed!" );
00135   return err;
00136 }
00137 
00138 
00139 /* Simple wrappers around functions we use to check return type.
00140  * In the future we might wrap the Driver-API around this so we can keep
00141  * using the high-level API.
00142  */
00143 DBG_FUNC( cudaFreeArray, (struct cudaArray *array), (array) );
00144 DBG_FUNC( cudaFree, (void *devPtr), (devPtr) );
00145 DBG_FUNC( cudaMalloc3DArray, (struct cudaArray** arrayPtr,
00146   const struct cudaChannelFormatDesc* desc, struct cudaExtent extent),
00147   (arrayPtr, desc, extent) );
00148 DBG_FUNC( cudaMemcpy3D, (const struct cudaMemcpy3DParms *p), (p) );
00149 DBG_FUNC( cudaSetDevice, (int device), (device) );
00150 DBG_FUNC( cudaGetDeviceProperties, (struct cudaDeviceProp *prop, int device),
00151   (prop, device) );
00152 
00153 }; /* cuda */
00154 
00155 #endif // end #ifndef __cudaInlineFunctions_h


Generated on 11-05-2012 for elastix by doxygen 1.7.6.1 elastix logo