5 #include <cuda_runtime.h> 48 : m_dims(
shape<1>{num_elems}), m_data(
nullptr) {
49 size_t alloc_size = num_elems *
sizeof(T);
50 auto err = cudaMalloc((
void**)(&m_data), alloc_size);
53 "Error allocating memory in cuda::DeviceMemory1D::DeviceMemory1D");
82 : m_dims(other.m_dims), m_data(other.m_data) {
83 other.reset_members();
98 this->free_cuda_mem();
99 this->m_dims = other.m_dims;
100 this->m_data = other.m_data;
102 other.reset_members();
123 T*
data()
const {
return m_data; }
130 size_t bytes()
const {
return m_dims[0] *
sizeof(T); }
143 void free_cuda_mem() {
144 auto err = cudaFree(m_data);
147 "Error deallocating memory in cuda::DeviceMemory1D::free_cuda_mem");
153 void reset_members() {
A wrapper template class around a contiguous array of T types laid out in device memory (GPU memory)...
Definition: device_memory_1d.hpp:29
DeviceMemory1D & operator=(const DeviceMemory1D &)=delete
Copy assignment operator (deleted).
DeviceMemory1D(DeviceMemory1D &&other)
Move constructor.
Definition: device_memory_1d.hpp:81
T * data() const
Get a device pointer pointing to the memory allocated by the current DeviceMemory1D object...
Definition: device_memory_1d.hpp:123
size_t bytes() const
Get the number of bytes of the allocated memory on the GPU.
Definition: device_memory_1d.hpp:130
~DeviceMemory1D()
Destruct the current DeviceMemory1D object by deallocating the held GPU memory.
Definition: device_memory_1d.hpp:115
Eigen::array< size_t, N > shape
Shape of vectors, matrices, tensors, etc.
Definition: defs.hpp:66
T value_type
Type of the values wrapped around current DeviceMemory1D object.
Definition: device_memory_1d.hpp:35
shape< 1 > dims() const
Get the dimensions of this memory region in terms of elements.
Definition: device_memory_1d.hpp:137
Main namespace for bnmf-algs library.
Definition: alloc_model_funcs.hpp:12
DeviceMemory1D & operator=(DeviceMemory1D &&other)
Move assignment operator.
Definition: device_memory_1d.hpp:97
DeviceMemory1D(size_t num_elems)
Construct a DeviceMemory1D object that allocates num_elems many T values on the GPU device...
Definition: device_memory_1d.hpp:47