Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
DeviceBuffer.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
13
14#include <cstddef>
15
17
27template <typename T> class DeviceBuffer {
28public:
29 DeviceBuffer() = default;
30
36 explicit DeviceBuffer(std::size_t count) : count_(count) {
37 if (count_ > 0) {
38 hipCheck(hipMalloc(&ptr_, count_ * sizeof(T)));
39 }
40 }
41
43
44 // Move-only semantics
45 DeviceBuffer(DeviceBuffer &&other) noexcept : ptr_(other.ptr_), count_(other.count_) {
46 other.ptr_ = nullptr;
47 other.count_ = 0;
48 }
49
50 DeviceBuffer &operator=(DeviceBuffer &&other) noexcept {
51 if (this != &other) {
52 release();
53 ptr_ = other.ptr_;
54 count_ = other.count_;
55 other.ptr_ = nullptr;
56 other.count_ = 0;
57 }
58 return *this;
59 }
60
61 DeviceBuffer(const DeviceBuffer &) = delete;
63
70 void copyFromHost(const T *host_data, std::size_t num_elements) {
71 hipCheck(hipMemcpy(ptr_, host_data, num_elements * sizeof(T), hipMemcpyHostToDevice));
72 }
73
80 void copyToHost(T *host_data, std::size_t num_elements) const {
81 hipCheck(hipMemcpy(host_data, ptr_, num_elements * sizeof(T), hipMemcpyDeviceToHost));
82 }
83
88 void setScalar(const T &value) {
89 hipCheck(hipMemcpy(ptr_, &value, sizeof(T), hipMemcpyHostToDevice));
90 }
91
96 [[nodiscard]] T *get() noexcept { return ptr_; }
97
102 [[nodiscard]] const T *get() const noexcept { return ptr_; }
103
108 [[nodiscard]] std::size_t size() const noexcept { return count_; }
109
110private:
111 void release() noexcept {
112 if (ptr_ != nullptr) {
113 hipFree(ptr_);
114 ptr_ = nullptr;
115 count_ = 0;
116 }
117 }
118
119 T *ptr_{nullptr};
120 std::size_t count_{0};
121};
122
123} // namespace correlation::core::gpu
Throwing error-check function for HIP/CUDA GPU API calls.
Unified GPU portability layer between CUDA and HIP with host fallback stubs.
hipError_t hipFree(void *ptr) noexcept
Host fallback deallocation stub.
Definition GPUPortability.hpp:94
constexpr int hipMemcpyDeviceToHost
Device-to-host memory copy transfer direction flag.
Definition GPUPortability.hpp:135
hipError_t hipMemcpy(MemcpyParams params) noexcept
Host fallback memory copy stub.
Definition GPUPortability.hpp:111
hipError_t hipMalloc(T **ptr, std::size_t size) noexcept
Host fallback allocation stub returning failure when GPU support is disabled.
Definition GPUPortability.hpp:82
constexpr int hipMemcpyHostToDevice
Host-to-device memory copy transfer direction flag.
Definition GPUPortability.hpp:133
~DeviceBuffer()
Definition DeviceBuffer.hpp:42
std::size_t count_
Definition DeviceBuffer.hpp:120
DeviceBuffer(std::size_t count)
Allocates device memory for count elements of type T.
Definition DeviceBuffer.hpp:36
T * get() noexcept
Returns the raw device pointer.
Definition DeviceBuffer.hpp:96
DeviceBuffer(const DeviceBuffer &)=delete
void copyToHost(T *host_data, std::size_t num_elements) const
Copies device data back to a host buffer.
Definition DeviceBuffer.hpp:80
void setScalar(const T &value)
Sets the entire buffer to a value via host-to-device copy of a single element.
Definition DeviceBuffer.hpp:88
std::size_t size() const noexcept
Returns the number of allocated elements.
Definition DeviceBuffer.hpp:108
T * ptr_
Definition DeviceBuffer.hpp:119
DeviceBuffer(DeviceBuffer &&other) noexcept
Definition DeviceBuffer.hpp:45
const T * get() const noexcept
Returns the raw device pointer (const overload).
Definition DeviceBuffer.hpp:102
void release() noexcept
Definition DeviceBuffer.hpp:111
DeviceBuffer & operator=(const DeviceBuffer &)=delete
void copyFromHost(const T *host_data, std::size_t num_elements)
Copies host data to this device buffer.
Definition DeviceBuffer.hpp:70
DeviceBuffer & operator=(DeviceBuffer &&other) noexcept
Definition DeviceBuffer.hpp:50
Definition DeviceBuffer.hpp:16
void hipCheck(hipError_t result, std::source_location loc=std::source_location::current())
Validates a HIP/CUDA runtime call result and throws GPUError on failure.
Definition GPUErrorCheck.hpp:33