Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_GpuBuffer.H
Go to the documentation of this file.
1#ifndef AMREX_GPU_DEVICE_BUFFER_H_
2#define AMREX_GPU_DEVICE_BUFFER_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Arena.H>
6#include <AMReX_TypeTraits.H>
7#include <AMReX_GpuDevice.H>
9#include <cstring>
10#include <cstdlib>
11#include <initializer_list>
12#include <memory>
13
14namespace amrex::Gpu {
15
21template <typename T, std::enable_if_t<std::is_trivially_copyable_v<T>,int> = 0>
22class Buffer
23{
24public:
25
26 Buffer (std::initializer_list<T> init) {
27 resize(init.size());
28
29 if (init.size() > 0) {
30 std::memcpy(h_vect.data(), init.begin(), init.size()*sizeof(T));
32 }
33 }
34
35 Buffer (T const* h_p, const std::size_t n) {
36 resize(n);
37
38 if (n > 0 && h_p != nullptr) {
39 std::memcpy(h_vect.data(), h_p, n*sizeof(T));
41 }
42 }
43
44 Buffer (const std::size_t n) {
45 resize(n);
46 }
47
48 Buffer () = default;
49
50 [[nodiscard]] T const* data () const noexcept {
51 return (useDVect() && !d_vect.empty()) ? d_vect.data() : h_vect.data();
52 }
53 [[nodiscard]] T* data () noexcept {
54 return (useDVect() && !d_vect.empty()) ? d_vect.data() : h_vect.data();
55 }
56
57 [[nodiscard]] T const* hostData () const noexcept { return h_vect.data(); }
58 [[nodiscard]] T* hostData () noexcept { return h_vect.data(); }
59
79 [[nodiscard]] T& operator[] (const std::size_t i) noexcept {
80 return h_vect[i];
81 }
82
83 [[nodiscard]] const T& operator[] (const std::size_t i) const noexcept {
84 return h_vect[i];
85 }
86
87 [[nodiscard]] std::size_t size () const noexcept { return h_vect.size(); }
88
89 [[nodiscard]] bool empty () const noexcept { return h_vect.size() == 0; }
90
91 void resize (const std::size_t n) {
92 h_vect.resize(n);
93 if (useDVect()) {
94 d_vect.resize(n);
95 }
96 }
97
98 void clear () noexcept {
99 h_vect.clear();
100 d_vect.clear();
101 }
102
104 h_vect.shrink_to_fit();
105 d_vect.shrink_to_fit();
106 }
107
108 void reserve (const std::size_t n) {
109 h_vect.reserve(n);
110 if (useDVect()) {
111 d_vect.reserve(n);
112 }
113 }
114
134 void push_back (const T& value) {
135 h_vect.push_back(value);
136 }
137
139#ifdef AMREX_USE_GPU
140 if (useDVect() && !h_vect.empty())
141 {
142 d_vect.resize(h_vect.size());
143 Gpu::htod_memcpy_async(d_vect.data(), h_vect.data(), h_vect.size()*sizeof(T));
144 return d_vect.data();
145 }
146#endif
147 return h_vect.data();
148 }
149
151#ifdef AMREX_USE_GPU
152 if (useDVect() && !d_vect.empty())
153 {
154 h_vect.resize(d_vect.size());
155 Gpu::dtoh_memcpy_async(h_vect.data(), d_vect.data(), d_vect.size()*sizeof(T));
157 }
158#endif
159 return h_vect.data();
160 }
161
162private:
163
164 [[nodiscard]] bool useDVect () const noexcept {
165 return Gpu::inLaunchRegion() /* && !use_unified_gpu_memory */;
166 }
167
168 DeviceVector<T> d_vect;
169 PinnedVector<T> h_vect;
170};
171
172}
173
174#endif
Definition AMReX_GpuBuffer.H:23
T const * data() const noexcept
Definition AMReX_GpuBuffer.H:50
Buffer(std::initializer_list< T > init)
Definition AMReX_GpuBuffer.H:26
T * copyToDeviceAsync()
Definition AMReX_GpuBuffer.H:138
T const * hostData() const noexcept
Definition AMReX_GpuBuffer.H:57
T * data() noexcept
Definition AMReX_GpuBuffer.H:53
Buffer(T const *h_p, const std::size_t n)
Definition AMReX_GpuBuffer.H:35
T & operator[](const std::size_t i) noexcept
Changes the value of an element of the host (CPU) vector. Does not update the device (GPU) vector,...
Definition AMReX_GpuBuffer.H:79
void reserve(const std::size_t n)
Definition AMReX_GpuBuffer.H:108
void shrink_to_fit()
Definition AMReX_GpuBuffer.H:103
bool empty() const noexcept
Definition AMReX_GpuBuffer.H:89
void push_back(const T &value)
Adds an element to the back of the host (CPU) vector. Does not update the device (GPU) vector,...
Definition AMReX_GpuBuffer.H:134
std::size_t size() const noexcept
Definition AMReX_GpuBuffer.H:87
void clear() noexcept
Definition AMReX_GpuBuffer.H:98
void resize(const std::size_t n)
Definition AMReX_GpuBuffer.H:91
Buffer(const std::size_t n)
Definition AMReX_GpuBuffer.H:44
T * copyToHost()
Definition AMReX_GpuBuffer.H:150
T * hostData() noexcept
Definition AMReX_GpuBuffer.H:58
Definition AMReX_BaseFwd.H:55
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:435
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:92
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:421