5 #include <cuda_runtime.h> 51 : m_data(nullptr), m_pitch(), m_dims(
shape<2>{rows, cols}) {
52 auto err = cudaMallocPitch((
void**)(&m_data), &m_pitch,
53 cols *
sizeof(T), rows);
56 "Error allocating memory in cuda::DeviceMemory2D::DeviceMemory2D");
85 : m_data(other.m_data), m_pitch(other.m_pitch), m_dims(other.m_dims) {
86 other.reset_members();
101 this->free_cuda_mem();
103 this->m_data = other.m_data;
104 this->m_pitch = other.m_pitch;
105 this->m_dims = other.m_dims;
107 other.reset_members();
128 T*
data()
const {
return m_data; }
138 size_t pitch()
const {
return m_pitch; }
150 size_t width()
const {
return m_dims[1] *
sizeof(T); }
160 size_t height()
const {
return m_dims[0]; }
173 void free_cuda_mem() {
174 auto err = cudaFree(m_data);
177 "Error deallocating memory in cuda::DeviceMemory2D::free_cuda_mem");
183 void reset_members() {
184 this->m_data =
nullptr;
186 this->m_dims = {0, 0};
~DeviceMemory2D()
Destruct the current DeviceMemory2D object by deallocating the held GPU memory.
Definition: device_memory_2d.hpp:120
DeviceMemory2D & operator=(DeviceMemory2D &&other)
Move assignment operator.
Definition: device_memory_2d.hpp:100
shape< 2 > dims() const
Get the dimensions of this memory region in terms of elements.
Definition: device_memory_2d.hpp:167
size_t pitch() const
Get the pitch of the allocation.
Definition: device_memory_2d.hpp:138
T * data() const
Get a device pointer pointing to the pitched memory allocated by the current DeviceMemory2D object...
Definition: device_memory_2d.hpp:128
Eigen::array< size_t, N > shape
Shape of vectors, matrices, tensors, etc.
Definition: defs.hpp:66
A wrapper template class around 2D row-major pitched memory stored in device memory (GPU memory)...
Definition: device_memory_2d.hpp:30
DeviceMemory2D(DeviceMemory2D &&other)
Move constructor.
Definition: device_memory_2d.hpp:84
DeviceMemory2D & operator=(const DeviceMemory2D &)=delete
Copy assignment operator (deleted).
size_t width() const
Get the width of the allocation in terms of bytes.
Definition: device_memory_2d.hpp:150
size_t height() const
Get the height of the allocation in terms of number of elements.
Definition: device_memory_2d.hpp:160
DeviceMemory2D(size_t rows, size_t cols)
Construct a DeviceMemory2D object responsible from the GPU memory allocated with respect to the given...
Definition: device_memory_2d.hpp:50
Main namespace for bnmf-algs library.
Definition: alloc_model_funcs.hpp:12
T value_type
Type of the values stored on GPU.
Definition: device_memory_2d.hpp:35