27 #ifndef math_cudaPtr_hpp
28 #define math_cudaPtr_hpp
32 #include <cuda_runtime.h>
161 return (
typename cpp2cudaType<devicePtrT>::cudaType*)
m_devicePtr;
169 const typename cpp2cudaType<devicePtrT>::cudaType*
operator()()
const
171 return (
typename cpp2cudaType<devicePtrT>::cudaType*)
m_devicePtr;
185 if( m_size == sz )
return 0;
189 cudaError_t rv = cudaMalloc((
void **)&m_devicePtr, sz*
sizeof(
devicePtrT));
191 if(rv != cudaSuccess)
193 std::cerr <<
"Error from cudaMalloc: ";
194 printf(
"[%s] %s\n", cudaGetErrorName(rv), cudaGetErrorString(rv));
206 return resize(x_sz*y_sz);
215 return resize(x_sz*y_sz*z_sz);
221 return ::cudaMemset(m_devicePtr, 0, m_size*
sizeof(
devicePtrT));
229 int rv = cudaFree(m_devicePtr);
231 if(rv != cudaSuccess)
233 std::cerr <<
"Cuda Free Error \n";
248 int rv = cudaMemcpy( m_devicePtr, src, m_size*
sizeof(
devicePtrT), cudaMemcpyHostToDevice);
250 if(rv != cudaSuccess)
252 std::cerr <<
"Cuda Memcpy error \n";
277 int rv = cudaMemcpy( dest, m_devicePtr, m_size*
sizeof(
devicePtrT), cudaMemcpyDeviceToHost);
279 if(rv != cudaSuccess)
281 std::cerr <<
"Cuda Memcpy error \n";
A smart-pointer wrapper for cuda device pointers.
int resize(size_t x_sz, size_t y_sz)
Resize the memory allocation, in 2D.
cpp2cudaType< devicePtrT >::cudaType * operator()()
Conversion operator, accesses the device pointer for use in Cuda functions.
cudaError_t initialize()
Initialize the array bytes to 0.
T devicePtrT
The device data type.
const cpp2cudaType< devicePtrT >::cudaType * operator()() const
Conversion operator, accesses the device pointer for use in Cuda functions.
size_t m_size
The allocated size.
int download(hostPtrT *dest)
Copy from the device to the host.
int upload(const hostPtrT *src)
Copy from the host to the device, after allocation.
devicePtrT * m_devicePtr
The device pointer.
int free()
Free the memory allocation.
T hostPtrT
The host data type.
int resize(size_t x_sz, size_t y_sz, size_t z_sz)
Resize the memory allocation, in 3D.
int upload(const hostPtrT *src, size_t sz)
Copy from the host to the device with allocation.
int resize(size_t sz)
Resize the memory allocation, in 1D.
~cudaPtr()
Destructor, frees memory if allocated.
Utilities for a template interface to cuda.