OpenMPCD
DeviceBuffer.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Defines the `OpenMPCD::CUDA::DeviceBuffer` class.
4  */
5 
6 #ifndef OPENMPCD_CUDA_DEVICEBUFFER_HPP
7 #define OPENMPCD_CUDA_DEVICEBUFFER_HPP
8 
9 #include <cstddef>
10 
11 namespace OpenMPCD
12 {
13 namespace CUDA
14 {
15 
16 /**
17  * Represents a memory buffer on the Device.
18  *
19  * The memory for this buffer will be allocated and freed automatically.
20  *
21  * @tparam T The underlying data type.
22  */
23 template<typename T>
25 {
26 public:
27  /**
28  * The constructor.
29  *
30  * @throw OpenMPCD::InvalidArgumentException
31  * If `OPENMPCD_DEBUG` is defined, throws if `elemtCount_ == 0`.
32  *
33  * @param[in] elementCount_
34  * The number of elements to allocate memory for. No constructors
35  * will be called. Must not be `0`.
36  */
37  DeviceBuffer(const std::size_t elementCount_);
38 
39 private:
40  DeviceBuffer(const DeviceBuffer&); ///< The copy constructor.
41 
42 public:
43  /**
44  * The destructor.
45  *
46  * This will free the allocated memory, but will not call any destructors.
47  */
48  ~DeviceBuffer();
49 
50 public:
51  /**
52  * Returns the Device pointer.
53  */
54  T* getPointer();
55 
56  /**
57  * Returns the Device pointer.
58  */
59  const T* getPointer() const;
60 
61  /**
62  * Returns the number of elements the buffer can store.
63  */
64  std::size_t getElementCount() const;
65 
66  /**
67  * Copies `getElementCount()` elements from the given Device pointer to this
68  * buffer.
69  *
70  * @throw OpenMPCD::NULLPointerException
71  * If `OPENMPCD_DEBUG` is defined, throws if `src == nullptr`.
72  *
73  * @param[in] src
74  * The Device pointer to copy from, which must not be `nullptr`
75  * and must point to at least `getElementCount()` elements.
76  */
77  void copyFromDevice(const T* const src);
78 
79  /**
80  * Writes zero-bytes to the entirety of this buffer.
81  */
82  void zeroMemory();
83 
84 private:
85  const DeviceBuffer& operator=(const DeviceBuffer&);
86  ///< The assignment operator.
87 
88 private:
89  T* pointer; ///< The Device pointer.
90  std::size_t elementCount; ///< The number of elements that can be stored.
91 
92 }; //class DeviceBuffer
93 } //namespace CUDA
94 } //namespace OpenMPCD
95 
96 
98 
99 #endif //OPENMPCD_CUDA_DEVICEBUFFER_HPP
DeviceBuffer.hpp
OpenMPCD::CUDA::DeviceBuffer::~DeviceBuffer
~DeviceBuffer()
The destructor.
Definition: ImplementationDetails/DeviceBuffer.hpp:31
OpenMPCD::CUDA::DeviceBuffer::copyFromDevice
void copyFromDevice(const T *const src)
Copies getElementCount() elements from the given Device pointer to this buffer.
Definition: ImplementationDetails/DeviceBuffer.hpp:55
OpenMPCD::CUDA::DeviceBuffer::getElementCount
std::size_t getElementCount() const
Returns the number of elements the buffer can store.
Definition: ImplementationDetails/DeviceBuffer.hpp:49
OpenMPCD::CUDA::DeviceBuffer
Represents a memory buffer on the Device.
Definition: DeviceBuffer.hpp:24
OpenMPCD::CUDA::DeviceBuffer::getPointer
T * getPointer()
Returns the Device pointer.
Definition: ImplementationDetails/DeviceBuffer.hpp:37
OpenMPCD::CUDA::DeviceBuffer::DeviceBuffer
DeviceBuffer(const std::size_t elementCount_)
The constructor.
Definition: ImplementationDetails/DeviceBuffer.hpp:19
OpenMPCD::CUDA::DeviceBuffer::zeroMemory
void zeroMemory()
Writes zero-bytes to the entirety of this buffer.
Definition: ImplementationDetails/DeviceBuffer.hpp:64