27#ifndef math_cudaPtr_hpp
28#define math_cudaPtr_hpp
32#include <cuda_runtime.h>
161 return (
typename cpp2cudaType<devicePtrT>::cudaType *)
m_devicePtr;
170 const typename cpp2cudaType<devicePtrT>::cudaType *
operator()()
const
172 return (
typename cpp2cudaType<devicePtrT>::cudaType *)
m_devicePtr;
190 cudaError_t rv = cudaMalloc( (
void **)&m_devicePtr, sz *
sizeof(
devicePtrT ) );
192 if( rv != cudaSuccess )
194 std::cerr <<
"Error from cudaMalloc: ";
195 printf(
"[%s] %s\n", cudaGetErrorName( rv ), cudaGetErrorString( rv ) );
204 return resize( x_sz * y_sz );
210 return resize( x_sz * y_sz * z_sz );
216 return ::cudaMemset( m_devicePtr, 0, m_size *
sizeof(
devicePtrT ) );
224 int rv = cudaFree( m_devicePtr );
226 if( rv != cudaSuccess )
228 std::cerr <<
"Cuda Free Error \n";
243 int rv = cudaMemcpy( m_devicePtr, src, m_size *
sizeof(
devicePtrT ), cudaMemcpyHostToDevice );
245 if( rv != cudaSuccess )
247 std::cerr <<
"Cuda Memcpy error \n";
264 return upload( src );
271 int rv = cudaMemcpy( dest, m_devicePtr, m_size *
sizeof(
devicePtrT ), cudaMemcpyDeviceToHost );
273 if( rv != cudaSuccess )
275 std::cerr <<
"Cuda Memcpy error \n";
A smart-pointer wrapper for cuda device pointers.
const cpp2cudaType< devicePtrT >::cudaType * operator()() const
Conversion operator, accesses the device pointer for use in Cuda functions.
int resize(size_t x_sz, size_t y_sz)
Resize the memory allocation, in 2D.
cudaError_t initialize()
Initialize the array bytes to 0.
T devicePtrT
The device data type.
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.
cpp2cudaType< devicePtrT >::cudaType * operator()()
Conversion operator, accesses the device pointer for use in Cuda functions.
int resize(size_t sz)
Resize the memory allocation, in 1D.
~cudaPtr()
Destructor, frees memory if allocated.
Utilities for a template interface to cuda.