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.
1 /*======================================================================
2 
3  This file is part of the elastix software.
4 
5  Copyright (c) University Medical Center Utrecht. All rights reserved.
6  See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
7  details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notices for more information.
12 
13 ======================================================================*/
14 #ifndef __cudaInlineFunctions_h
15 #define __cudaInlineFunctions_h
16 
17 #include <cuda_runtime.h>
18 #include <stdio.h>
19 #include <string>
20 #include <assert.h>
21 #include "cudaMacro.h"
22 
23 namespace cuda
24 {
25 
26 #define cudaCheckMsg( msg ) __cudaCheckMsg( msg, __FILE__, __LINE__ )
27 
28 inline void __cudaCheckMsg( const char *msg, const char *file, const int line )
29 {
30  cudaError_t err = ::cudaGetLastError();
31  if ( err != cudaSuccess )
32  {
33  const char* errcmsg = ::cudaGetErrorString( err );
34  fprintf( stderr, "CUDA error: %s in file <%s>, line %i : %s.\n",
35  msg, file, line, errcmsg );
36  //assert( false );
37 
38  std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
39  throw errmsg;
40  }
41 
42 #ifndef NDEBUG
43  err = ::cudaThreadSynchronize();
44  if ( err != cudaSuccess )
45  {
46  const char* errcmsg = ::cudaGetErrorString( err );
47  fprintf( stderr, "cudaThreadSynchronize error: %s in file <%s>, line %i : %s.\n",
48  msg, file, line, errcmsg );
49  assert( false );
50 
51  std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
52  throw errmsg;
53  }
54 #endif /* NDEBUG */
55 }
56 
57 
58 template <class T>
59 inline T* cudaMalloc( size_t nof_elems )
60 {
61  T* dst;
62  size_t size = nof_elems * sizeof( T );
63  ::cudaMalloc( (void**)&dst, size );
64  cudaCheckMsg( "cudaMalloc failed!" );
65 
66  return dst;
67 }
68 
69 
70 template <class T>
71 inline T* cudaHostAlloc( size_t nof_elems, unsigned int flags = cudaHostAllocDefault )
72 {
73  T* dst;
74  size_t size = nof_elems * sizeof( T );
75  ::cudaHostAlloc( (void**)&dst, size, flags );
76  cudaCheckMsg( "cudaHostAlloc failed!" );
77 
78  return dst;
79 }
80 
81 
82 inline cudaError_t cudaMemcpy( void* dst, const void* src,
83  size_t nof_elems, size_t sizeof_elem, cudaMemcpyKind direction )
84 {
85  cudaError err = ::cudaMemcpy( dst, src, nof_elems * sizeof_elem, direction );
86  cudaCheckMsg( "cudaMemcpy failed!" );
87  return err;
88 }
89 
90 
91 template <class T>
92 inline void cudaMemcpy( T* dst, const T* src,
93  size_t nof_elems, cudaMemcpyKind direction )
94 {
95  size_t size = nof_elems * sizeof( T );
96  ::cudaMemcpy( dst, src, size, direction );
97  cudaCheckMsg( "cudaMemcpy failed!" );
98 }
99 
100 
101 template <class T>
102 inline void cudaMemset( T* dst, int value, size_t nof_elems )
103 {
104  size_t size = nof_elems * sizeof( T );
105  ::cudaMemset( dst, value, size );
106  cudaCheckMsg( "cudaMemset failed!" );
107 }
108 
109 
110 template <typename T, typename Q>
111 inline cudaError_t cudaMemcpyToSymbol( T& dst, const Q& src,
112  cudaMemcpyKind direction )
113 {
114  cudaError err = ::cudaMemcpyToSymbol( dst, &src, sizeof(dst), 0, direction );
115  cudaCheckMsg( "cudaMemcpyToSymbol failed!" );
116  return err;
117 }
118 
119 
120 template <typename T>
121 inline cudaError_t cudaBindTextureToArray( T& tex, cudaArray* array,
122  const cudaChannelFormatDesc desc )
123 {
124  cudaError_t err = ::cudaBindTextureToArray( tex, array, desc );
125  cudaCheckMsg( "cudaBindTextureToArray failed!" );
126  return err;
127 }
128 
129 
130 template <typename T>
131 inline cudaError_t cudaUnbindTexture( T& tex )
132 {
133  cudaError_t err = ::cudaUnbindTexture( tex );
134  cudaCheckMsg( "cudaUnbindTexture failed!" );
135  return err;
136 }
137 
138 
139 /* Simple wrappers around functions we use to check return type.
140  * In the future we might wrap the Driver-API around this so we can keep
141  * using the high-level API.
142  */
143 DBG_FUNC( cudaFreeArray, (struct cudaArray *array), (array) );
144 DBG_FUNC( cudaFree, (void *devPtr), (devPtr) );
145 DBG_FUNC( cudaMalloc3DArray, (struct cudaArray** arrayPtr,
146  const struct cudaChannelFormatDesc* desc, struct cudaExtent extent),
147  (arrayPtr, desc, extent) );
148 DBG_FUNC( cudaMemcpy3D, (const struct cudaMemcpy3DParms *p), (p) );
149 DBG_FUNC( cudaSetDevice, (int device), (device) );
150 DBG_FUNC( cudaGetDeviceProperties, (struct cudaDeviceProp *prop, int device),
151  (prop, device) );
152 
153 }; /* cuda */
154 
155 #endif // end #ifndef __cudaInlineFunctions_h


Generated on 21-03-2014 for elastix by doxygen 1.8.1.2 elastix logo