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 "itkExceptionObject.h"
00022 #include "cudaMacro.h"
00023 
00024 namespace cuda
00025 {
00026 
00027 #define cudaCheckMsg( msg ) __cudaCheckMsg( msg, __FILE__, __LINE__ )
00028 
00029 inline void __cudaCheckMsg( const char *msg, const char *file, const int line )
00030 {
00031   cudaError_t err = ::cudaGetLastError();
00032   if ( cudaSuccess != err )
00033   {
00034     const char* errcmsg = ::cudaGetErrorString( err );
00035     fprintf( stderr, "CUDA error: %s in file <%s>, line %i : %s.\n",
00036       msg, file, line, errcmsg );
00037     assert( false );
00038 
00039     std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
00040 //    throw itk::ExceptionObject( file, line, errmsg );
00041   }
00042 
00043 #if defined( _DEBUG )
00044   err = ::cudaThreadSynchronize();
00045   if ( cudaSuccess != err )
00046   {
00047     const char* errcmsg = ::cudaGetErrorString( err );
00048     fprintf( stderr, "cudaThreadSynchronize error: %s in file <%s>, line %i : %s.\n",
00049       msg, file, line, errcmsg );
00050     assert( false );
00051 
00052     std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
00053 //    throw itk::ExceptionObject( file, line, errmsg );
00054   }
00055 #endif /* _DEBUG */
00056 }
00057 
00058 
00059 template <class T>
00060 inline T* cudaMalloc( size_t nof_elems )
00061 {
00062   T* dst;
00063   size_t size = nof_elems * sizeof( T );
00064   ::cudaMalloc( (void**)&dst, size );
00065   cudaCheckMsg( "cudaMalloc failed!" );
00066 
00067   return dst;
00068 }
00069 
00070 
00071 template <class T>
00072 inline T* cudaHostAlloc( size_t nof_elems, unsigned int flags = cudaHostAllocDefault )
00073 {
00074   T* dst;
00075   size_t size = nof_elems * sizeof( T );
00076   ::cudaHostAlloc( (void**)&dst, size, flags );
00077   cudaCheckMsg( "cudaHostAlloc failed!" );
00078 
00079   return dst;
00080 }
00081 
00082 
00083 inline cudaError_t cudaMemcpy( void* dst, const void* src,
00084   size_t nof_elems, size_t sizeof_elem, cudaMemcpyKind direction )
00085 {
00086   cudaError err = ::cudaMemcpy( dst, src, nof_elems * sizeof_elem, direction );
00087   cudaCheckMsg( "cudaMemcpy failed!" );
00088   return err;
00089 }
00090 
00091 
00092 template <class T>
00093 inline void cudaMemcpy( T* dst, const T* src,
00094   size_t nof_elems, cudaMemcpyKind direction )
00095 {
00096   size_t size = nof_elems * sizeof( T );
00097   ::cudaMemcpy( dst, src, size, direction );
00098   cudaCheckMsg( "cudaMemcpy failed!" );
00099 }
00100 
00101 
00102 template <class T>
00103 inline void cudaMemset( T* dst, int value, size_t nof_elems )
00104 {
00105   size_t size = nof_elems * sizeof( T );
00106   ::cudaMemset( dst, value, size );
00107   cudaCheckMsg( "cudaMemset failed!" );
00108 }
00109 
00110 
00111 template <typename T, typename Q>
00112 inline cudaError_t cudaMemcpyToSymbol( T& dst, const Q& src,
00113   cudaMemcpyKind direction )
00114 {
00115   cudaError err = ::cudaMemcpyToSymbol( dst, &src, sizeof(dst), 0, direction );
00116   cudaCheckMsg( "cudaMemcpyToSymbol failed!" );
00117   return err;
00118 }
00119 
00120 
00121 template <typename T>
00122 inline cudaError_t cudaBindTextureToArray( T& tex, cudaArray* array,
00123   const cudaChannelFormatDesc desc )
00124 {
00125   cudaError_t err = ::cudaBindTextureToArray( tex, array, desc );
00126   cudaCheckMsg( "cudaBindTextureToArray failed!" );
00127   return err;
00128 }
00129 
00130 
00131 template <typename T>
00132 inline cudaError_t cudaUnbindTexture( T& tex )
00133 {
00134   cudaError_t err = ::cudaUnbindTexture( tex );
00135   cudaCheckMsg( "cudaUnbindTexture failed!" );
00136   return err;
00137 }
00138 
00139 
00140 /* Simple wrappers around functions we use to check return type.
00141  * In the future we might wrap the Driver-API around this so we can keep
00142  * using the high-level API.
00143  */
00144 DBG_FUNC( cudaFreeArray, (struct cudaArray *array), (array) );
00145 DBG_FUNC( cudaFree, (void *devPtr), (devPtr) );
00146 DBG_FUNC( cudaMalloc3DArray, (struct cudaArray** arrayPtr,
00147   const struct cudaChannelFormatDesc* desc, struct cudaExtent extent),
00148   (arrayPtr, desc, extent) );
00149 DBG_FUNC( cudaMemcpy3D, (const struct cudaMemcpy3DParms *p), (p) );
00150 DBG_FUNC( cudaSetDevice, (int device), (device) );
00151 DBG_FUNC( cudaGetDeviceProperties, (struct cudaDeviceProp *prop, int device),
00152   (prop, device) );
00153 
00154 }; /* cuda */
00155 
00156 #endif // end #ifndef __cudaInlineFunctions_h


Generated on 11-05-2011 for elastix by doxygen 1.7.4 elastix logo