Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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>
8#include <cstring>
9#include <cstdlib>
10#include <initializer_list>
11#include <memory>
12
13namespace amrex::Gpu {
14
15template <typename T, std::enable_if_t<std::is_trivially_copyable_v<T>,int> = 0>
16class Buffer
17{
18public:
19
20 Buffer (std::initializer_list<T> init)
21 : m_size(init.size())
22 {
23 if (m_size == 0) { return; }
24#ifdef AMREX_USE_GPU
25 h_data = static_cast<T*>(The_Pinned_Arena()->alloc(m_size*sizeof(T)));
26#else
27 h_data = static_cast<T*>(std::malloc(m_size*sizeof(T)));
28#endif
29 std::memcpy(h_data, init.begin(), m_size*sizeof(T));
30#ifdef AMREX_USE_GPU
32 {
33 d_data = static_cast<T*>(The_Arena()->alloc(m_size*sizeof(T)));
35 }
36#endif
37 }
38
39 Buffer (T const* h_p, const std::size_t n)
40 : m_size(n)
41 {
42 if (m_size == 0) { return; }
43#ifdef AMREX_USE_GPU
44 h_data = static_cast<T*>(The_Pinned_Arena()->alloc(m_size*sizeof(T)));
45#else
46 h_data = static_cast<T*>(std::malloc(m_size*sizeof(T)));
47#endif
48 std::memcpy(h_data, h_p, m_size*sizeof(T));
49#ifdef AMREX_USE_GPU
51 {
52 d_data = static_cast<T*>(The_Arena()->alloc(m_size*sizeof(T)));
54 }
55#endif
56 }
57
58 ~Buffer () { clear(); }
59
60 Buffer (Buffer const&) = delete;
61 Buffer (Buffer &&) = delete;
62 void operator= (Buffer const&) = delete;
63 void operator= (Buffer &&) = delete;
64
65 [[nodiscard]] T const* data () const noexcept { return (d_data != nullptr) ? d_data : h_data; }
66 [[nodiscard]] T* data () noexcept { return (d_data != nullptr) ? d_data : h_data; }
67
68 [[nodiscard]] T const* hostData () const noexcept { return h_data; }
69 [[nodiscard]] T* hostData () noexcept { return h_data; }
70
71 [[nodiscard]] std::size_t size () const noexcept { return m_size; }
72
73 void clear ()
74 {
75#ifdef AMREX_USE_GPU
76 if (d_data) { The_Arena()->free(d_data); }
78#else
79 std::free(h_data);
80#endif
81 d_data = nullptr;
82 h_data = nullptr;
83 }
84
86 {
87#ifdef AMREX_USE_GPU
88 if (d_data)
89 {
92 }
93#endif
94 return h_data;
95 }
96
97private:
98 std::size_t m_size;
99 T* d_data = nullptr;
100 T* h_data = nullptr;
101};
102
103}
104
105#endif
virtual void free(void *pt)=0
A pure virtual function for deleting the arena pointed to by pt.
virtual void * alloc(std::size_t sz)=0
Definition AMReX_GpuBuffer.H:17
void operator=(Buffer const &)=delete
void clear()
Definition AMReX_GpuBuffer.H:73
T const * data() const noexcept
Definition AMReX_GpuBuffer.H:65
~Buffer()
Definition AMReX_GpuBuffer.H:58
Buffer(std::initializer_list< T > init)
Definition AMReX_GpuBuffer.H:20
Buffer(Buffer const &)=delete
T const * hostData() const noexcept
Definition AMReX_GpuBuffer.H:68
T * data() noexcept
Definition AMReX_GpuBuffer.H:66
T * d_data
Definition AMReX_GpuBuffer.H:99
Buffer(T const *h_p, const std::size_t n)
Definition AMReX_GpuBuffer.H:39
Buffer(Buffer &&)=delete
std::size_t size() const noexcept
Definition AMReX_GpuBuffer.H:71
T * copyToHost()
Definition AMReX_GpuBuffer.H:85
std::size_t m_size
Definition AMReX_GpuBuffer.H:98
T * h_data
Definition AMReX_GpuBuffer.H:100
T * hostData() noexcept
Definition AMReX_GpuBuffer.H:69
Definition AMReX_BaseFwd.H:52
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:237
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:265
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:86
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:251
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:656
Arena * The_Arena()
Definition AMReX_Arena.cpp:616