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  * Copyright UMC Utrecht and contributors
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __cudaInlineFunctions_h
19 #define __cudaInlineFunctions_h
20 
21 #include <cuda_runtime.h>
22 #include <stdio.h>
23 #include <string>
24 #include <assert.h>
25 #include "cudaMacro.h"
26 
27 namespace cuda
28 {
29 
30 #define cudaCheckMsg( msg ) __cudaCheckMsg( msg, __FILE__, __LINE__ )
31 
32 inline void __cudaCheckMsg( const char *msg, const char *file, const int line )
33 {
34  cudaError_t err = ::cudaGetLastError();
35  if ( err != cudaSuccess )
36  {
37  const char* errcmsg = ::cudaGetErrorString( err );
38  fprintf( stderr, "CUDA error: %s in file <%s>, line %i : %s.\n",
39  msg, file, line, errcmsg );
40  //assert( false );
41 
42  std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
43  throw errmsg;
44  }
45 
46 #ifndef NDEBUG
47  err = ::cudaThreadSynchronize();
48  if ( err != cudaSuccess )
49  {
50  const char* errcmsg = ::cudaGetErrorString( err );
51  fprintf( stderr, "cudaThreadSynchronize error: %s in file <%s>, line %i : %s.\n",
52  msg, file, line, errcmsg );
53  assert( false );
54 
55  std::string errmsg = std::string( msg ) + ":: " + std::string( errcmsg );
56  throw errmsg;
57  }
58 #endif /* NDEBUG */
59 }
60 
61 
62 template <class T>
63 inline T* cudaMalloc( size_t nof_elems )
64 {
65  T* dst;
66  size_t size = nof_elems * sizeof( T );
67  ::cudaMalloc( (void**)&dst, size );
68  cudaCheckMsg( "cudaMalloc failed!" );
69 
70  return dst;
71 }
72 
73 
74 template <class T>
75 inline T* cudaHostAlloc( size_t nof_elems, unsigned int flags = cudaHostAllocDefault )
76 {
77  T* dst;
78  size_t size = nof_elems * sizeof( T );
79  ::cudaHostAlloc( (void**)&dst, size, flags );
80  cudaCheckMsg( "cudaHostAlloc failed!" );
81 
82  return dst;
83 }
84 
85 
86 inline cudaError_t cudaMemcpy( void* dst, const void* src,
87  size_t nof_elems, size_t sizeof_elem, cudaMemcpyKind direction )
88 {
89  cudaError err = ::cudaMemcpy( dst, src, nof_elems * sizeof_elem, direction );
90  cudaCheckMsg( "cudaMemcpy failed!" );
91  return err;
92 }
93 
94 
95 template <class T>
96 inline void cudaMemcpy( T* dst, const T* src,
97  size_t nof_elems, cudaMemcpyKind direction )
98 {
99  size_t size = nof_elems * sizeof( T );
100  ::cudaMemcpy( dst, src, size, direction );
101  cudaCheckMsg( "cudaMemcpy failed!" );
102 }
103 
104 
105 template <class T>
106 inline void cudaMemset( T* dst, int value, size_t nof_elems )
107 {
108  size_t size = nof_elems * sizeof( T );
109  ::cudaMemset( dst, value, size );
110  cudaCheckMsg( "cudaMemset failed!" );
111 }
112 
113 
114 template <typename T, typename Q>
115 inline cudaError_t cudaMemcpyToSymbol( const T& dst, const Q& src,
116  cudaMemcpyKind direction )
117 {
118  cudaError err = ::cudaMemcpyToSymbol( &dst, &src, sizeof(dst), 0, direction );
119  cudaCheckMsg( "cudaMemcpyToSymbol failed!" );
120  return err;
121 }
122 
123 
124 template <typename T>
125 inline cudaError_t cudaBindTextureToArray( const T& tex, cudaArray* array,
126  const cudaChannelFormatDesc desc )
127 {
128  cudaError_t err = ::cudaBindTextureToArray( &tex, array, &desc );
129  cudaCheckMsg( "cudaBindTextureToArray failed!" );
130  return err;
131 }
132 
133 
134 template <typename T>
135 inline cudaError_t cudaUnbindTexture( const T& tex )
136 {
137  cudaError_t err = ::cudaUnbindTexture( &tex );
138  cudaCheckMsg( "cudaUnbindTexture failed!" );
139  return err;
140 }
141 
142 
143 /* Simple wrappers around functions we use to check return type.
144  * In the future we might wrap the Driver-API around this so we can keep
145  * using the high-level API.
146  */
147 DBG_FUNC( cudaFreeArray, (struct cudaArray *array), (array) );
148 DBG_FUNC( cudaFree, (void *devPtr), (devPtr) );
149 DBG_FUNC( cudaMalloc3DArray, (struct cudaArray** arrayPtr,
150  const struct cudaChannelFormatDesc* desc, struct cudaExtent extent),
151  (arrayPtr, desc, extent) );
152 DBG_FUNC( cudaMemcpy3D, (const struct cudaMemcpy3DParms *p), (p) );
153 DBG_FUNC( cudaSetDevice, (int device), (device) );
154 DBG_FUNC( cudaGetDeviceProperties, (struct cudaDeviceProp *prop, int device),
155  (prop, device) );
156 
157 }; /* cuda */
158 
159 #endif // end #ifndef __cudaInlineFunctions_h
cudaError_t cudaMemcpyToSymbol(const T &dst, const Q &src, cudaMemcpyKind direction)
T * cudaHostAlloc(vcl_size_t nof_elems, unsigned int flags=cudaHostAllocDefault)
DBG_FUNC(cudaFreeArray,(struct cudaArray *array),(array))
T * cudaMalloc(vcl_size_t nof_elems)
cudaError_t cudaUnbindTexture(const T &tex)
cudaError_t cudaMemcpy(void *dst, const void *src, vcl_size_t nof_elems, vcl_size_t sizeof_elem, cudaMemcpyKind direction)
cudaError_t cudaBindTextureToArray(const T &tex, cudaArray *array, const cudaChannelFormatDesc desc)
void cudaMemset(T *dst, int value, vcl_size_t nof_elems)
#define cudaCheckMsg(msg)
void __cudaCheckMsg(const char *msg, const char *file, const int line)


Generated on 04-09-2015 for elastix by doxygen 1.8.9.1 elastix logo