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>
22requires (std::is_trivially_copyable_v<T>)
23class Buffer
24{
25public:
26
27 Buffer (std::initializer_list<T> init) {
28 resize(init.size());
29
30 if (init.size() > 0) {
31 std::memcpy(h_vect.data(), init.begin(), init.size()*sizeof(T));
32 copyToDeviceAsync();
33 }
34 }
35
36 Buffer (T const* h_p, const std::size_t n) {
37 resize(n);
38
39 if (n > 0 && h_p != nullptr) {
40 std::memcpy(h_vect.data(), h_p, n*sizeof(T));
41 copyToDeviceAsync();
42 }
43 }
44
45 Buffer (const std::size_t n) {
46 resize(n);
47 }
48
49 Buffer () = default;
50
51 [[nodiscard]] T const* data () const noexcept {
52 return (useDVect() && !d_vect.empty()) ? d_vect.data() : h_vect.data();
53 }
54 [[nodiscard]] T* data () noexcept {
55 return (useDVect() && !d_vect.empty()) ? d_vect.data() : h_vect.data();
56 }
57
58 [[nodiscard]] T const* hostData () const noexcept { return h_vect.data(); }
59 [[nodiscard]] T* hostData () noexcept { return h_vect.data(); }
60
80 [[nodiscard]] T& operator[] (const std::size_t i) noexcept {
81 return h_vect[i];
82 }
83
84 [[nodiscard]] const T& operator[] (const std::size_t i) const noexcept {
85 return h_vect[i];
86 }
87
88 [[nodiscard]] std::size_t size () const noexcept { return h_vect.size(); }
89
90 [[nodiscard]] bool empty () const noexcept { return h_vect.size() == 0; }
91
92 void resize (const std::size_t n) {
93 h_vect.resize(n);
94 if (useDVect()) {
95 d_vect.resize(n);
96 }
97 }
98
99 void clear () noexcept {
100 h_vect.clear();
101 d_vect.clear();
102 }
103
105 h_vect.shrink_to_fit();
106 d_vect.shrink_to_fit();
107 }
108
109 void reserve (const std::size_t n) {
110 h_vect.reserve(n);
111 if (useDVect()) {
112 d_vect.reserve(n);
113 }
114 }
115
135 void push_back (const T& value) {
136 h_vect.push_back(value);
137 }
138
140#ifdef AMREX_USE_GPU
141 if (useDVect() && !h_vect.empty())
142 {
143 d_vect.resize(h_vect.size());
144 Gpu::htod_memcpy_async(d_vect.data(), h_vect.data(), h_vect.size()*sizeof(T));
145 return d_vect.data();
146 }
147#endif
148 return h_vect.data();
149 }
150
152#ifdef AMREX_USE_GPU
153 if (useDVect() && !d_vect.empty())
154 {
155 h_vect.resize(d_vect.size());
156 Gpu::dtoh_memcpy_async(h_vect.data(), d_vect.data(), d_vect.size()*sizeof(T));
158 }
159#endif
160 return h_vect.data();
161 }
162
163private:
164
165 [[nodiscard]] bool useDVect () const noexcept {
166 return Gpu::inLaunchRegion() /* && !use_unified_gpu_memory */;
167 }
168
169 DeviceVector<T> d_vect;
170 PinnedVector<T> h_vect;
171};
172
173}
174
175#endif
Definition AMReX_GpuBuffer.H:24
void shrink_to_fit()
Definition AMReX_GpuBuffer.H:104
Buffer(const std::size_t n)
Definition AMReX_GpuBuffer.H:45
void reserve(const std::size_t n)
Definition AMReX_GpuBuffer.H:109
T const * hostData() const noexcept
Definition AMReX_GpuBuffer.H:58
T * copyToDeviceAsync()
Definition AMReX_GpuBuffer.H:139
T * hostData() noexcept
Definition AMReX_GpuBuffer.H:59
T * data() noexcept
Definition AMReX_GpuBuffer.H:54
std::size_t size() const noexcept
Definition AMReX_GpuBuffer.H:88
void clear() noexcept
Definition AMReX_GpuBuffer.H:99
bool empty() const noexcept
Definition AMReX_GpuBuffer.H:90
Buffer(std::initializer_list< T > init)
Definition AMReX_GpuBuffer.H:27
void resize(const std::size_t n)
Definition AMReX_GpuBuffer.H:92
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:135
T const * data() const noexcept
Definition AMReX_GpuBuffer.H:51
Buffer(T const *h_p, const std::size_t n)
Definition AMReX_GpuBuffer.H:36
T * copyToHost()
Definition AMReX_GpuBuffer.H:151
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:88
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:421