bnmf-algs
device_memory_2d.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "defs.hpp"
4 #include <cstddef>
5 #include <cuda_runtime.h>
6 
7 namespace bnmf_algs {
8 namespace cuda {
30 template <typename T> class DeviceMemory2D {
31  public:
35  using value_type = T;
36 
50  explicit DeviceMemory2D(size_t rows, size_t cols)
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);
54  BNMF_ASSERT(
55  err == cudaSuccess,
56  "Error allocating memory in cuda::DeviceMemory2D::DeviceMemory2D");
57  };
58 
65  DeviceMemory2D(const DeviceMemory2D&) = delete;
66 
73  DeviceMemory2D& operator=(const DeviceMemory2D&) = delete;
74 
85  : m_data(other.m_data), m_pitch(other.m_pitch), m_dims(other.m_dims) {
86  other.reset_members();
87  }
88 
101  this->free_cuda_mem();
102 
103  this->m_data = other.m_data;
104  this->m_pitch = other.m_pitch;
105  this->m_dims = other.m_dims;
106 
107  other.reset_members();
108 
109  return *this;
110  }
111 
120  ~DeviceMemory2D() { free_cuda_mem(); }
121 
128  T* data() const { return m_data; }
129 
138  size_t pitch() const { return m_pitch; }
139 
150  size_t width() const { return m_dims[1] * sizeof(T); }
151 
160  size_t height() const { return m_dims[0]; }
161 
167  shape<2> dims() const { return m_dims; }
168 
169  private:
173  void free_cuda_mem() {
174  auto err = cudaFree(m_data);
175  BNMF_ASSERT(
176  err == cudaSuccess,
177  "Error deallocating memory in cuda::DeviceMemory2D::free_cuda_mem");
178  }
179 
183  void reset_members() {
184  this->m_data = nullptr;
185  this->m_pitch = 0;
186  this->m_dims = {0, 0};
187  }
188 
189  private:
194  T* m_data;
195 
200  size_t m_pitch;
201 
205  shape<2> m_dims;
206 };
207 } // namespace cuda
208 } // namespace bnmf_algs
~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