bnmf-algs
device_memory_3d.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 DeviceMemory3D {
31  public:
35  using value_type = T;
36 
55  explicit DeviceMemory3D(size_t first_dim, size_t second_dim,
56  size_t third_dim)
57  : m_dims(shape<3>{first_dim, second_dim, third_dim}),
58  m_extent(
59  make_cudaExtent(third_dim * sizeof(T), second_dim, first_dim)),
60  m_ptr() {
61  auto err = cudaMalloc3D(&m_ptr, m_extent);
62  BNMF_ASSERT(
63  err == cudaSuccess,
64  "Error allocating memory in cuda::DeviceMemory3D::DeviceMemory3D");
65  }
66 
73  DeviceMemory3D(const DeviceMemory3D&) = delete;
74 
81  DeviceMemory3D& operator=(const DeviceMemory3D&) = delete;
82 
93  : m_dims(other.m_dims), m_extent(other.m_extent), m_ptr(other.m_ptr) {
94  other.reset_members();
95  }
96 
109  this->free_cuda_mem();
110 
111  this->m_dims = other.m_dims;
112  this->m_extent = other.m_extent;
113  this->m_ptr = other.m_ptr;
114 
115  other.reset_members();
116 
117  return *this;
118  }
119 
128  ~DeviceMemory3D() { free_cuda_mem(); }
129 
141  cudaPitchedPtr pitched_ptr() const { return m_ptr; }
142 
152  cudaExtent extent() const { return m_extent; }
153 
160  shape<3> dims() const { return m_dims; }
161 
162  private:
166  void free_cuda_mem() {
167  auto err = cudaFree(m_ptr.ptr);
168  BNMF_ASSERT(
169  err == cudaSuccess,
170  "Error deallocating memory in cuda::DeviceMemory3D::free_cuda_mem");
171  }
172 
176  void reset_members() {
177  this->m_dims = {0, 0, 0};
178  this->m_extent = {0, 0, 0};
179  this->m_ptr = {nullptr, 0, 0, 0};
180  }
181 
182  private:
187  shape<3> m_dims;
188 
192  cudaExtent m_extent;
193 
197  cudaPitchedPtr m_ptr;
198 };
199 } // namespace cuda
200 } // namespace bnmf_algs
cudaExtent extent() const
Get the cudaExtent type storing the extents of the allocation.
Definition: device_memory_3d.hpp:152
A wrapper template class around 3D row-major pitched memory stored in device memory (GPU memory)...
Definition: device_memory_3d.hpp:30
DeviceMemory3D & operator=(DeviceMemory3D &&other)
Move assignment operator.
Definition: device_memory_3d.hpp:108
shape< 3 > dims() const
Get the dimensions of this memory region in terms of elements.
Definition: device_memory_3d.hpp:160
Eigen::array< size_t, N > shape
Shape of vectors, matrices, tensors, etc.
Definition: defs.hpp:66
DeviceMemory3D(size_t first_dim, size_t second_dim, size_t third_dim)
Construct a DeviceMemory3D object responsible from the GPU memory allocated with respect to the given...
Definition: device_memory_3d.hpp:55
~DeviceMemory3D()
Destruct the current DeviceMemory3D object by deallocating the held GPU memory.
Definition: device_memory_3d.hpp:128
cudaPitchedPtr pitched_ptr() const
Get the cudaPitchedPtr type storing allocation parameters and the pointer to the device memory...
Definition: device_memory_3d.hpp:141
T value_type
Type of the values stored on GPU.
Definition: device_memory_3d.hpp:35
DeviceMemory3D & operator=(const DeviceMemory3D &)=delete
Copy assignment operator (deleted).
DeviceMemory3D(DeviceMemory3D &&other)
Move constructor.
Definition: device_memory_3d.hpp:92
Main namespace for bnmf-algs library.
Definition: alloc_model_funcs.hpp:12