bnmf-algs
device_memory_1d.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 {
9 
29 template <typename T> class DeviceMemory1D {
30  public:
35  using value_type = T;
36 
47  explicit DeviceMemory1D(size_t num_elems)
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);
51  BNMF_ASSERT(
52  err == cudaSuccess,
53  "Error allocating memory in cuda::DeviceMemory1D::DeviceMemory1D");
54  };
55 
62  DeviceMemory1D(const DeviceMemory1D&) = delete;
63 
70  DeviceMemory1D& operator=(const DeviceMemory1D&) = delete;
71 
82  : m_dims(other.m_dims), m_data(other.m_data) {
83  other.reset_members();
84  }
85 
98  this->free_cuda_mem();
99  this->m_dims = other.m_dims;
100  this->m_data = other.m_data;
101 
102  other.reset_members();
103 
104  return *this;
105  }
106 
115  ~DeviceMemory1D() { free_cuda_mem(); }
116 
123  T* data() const { return m_data; }
124 
130  size_t bytes() const { return m_dims[0] * sizeof(T); }
131 
137  shape<1> dims() const { return m_dims; }
138 
139  private:
143  void free_cuda_mem() {
144  auto err = cudaFree(m_data);
145  BNMF_ASSERT(
146  err == cudaSuccess,
147  "Error deallocating memory in cuda::DeviceMemory1D::free_cuda_mem");
148  }
149 
153  void reset_members() {
154  m_dims[0] = 0;
155  m_data = nullptr;
156  }
157 
158  private:
162  shape<1> m_dims;
163 
168  T* m_data;
169 };
170 } // namespace cuda
171 } // namespace bnmf_algs
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