Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_GpuAsyncArray.H
Go to the documentation of this file.
1#ifndef AMREX_GPU_ASYNC_ARRAY_H_
2#define AMREX_GPU_ASYNC_ARRAY_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Arena.H>
6#include <AMReX_TypeTraits.H>
7#include <AMReX_GpuDevice.H>
8
9#include <cstddef>
10#include <cstring>
11#include <cstdlib>
12#include <memory>
13
14namespace amrex {
15namespace Gpu {
16
17template <typename T>
18requires (std::is_trivially_copyable_v<T>)
20{
21public:
22
23 AsyncArray (T const* h_p, const std::size_t n)
24 {
25 if (n == 0) { return; }
26 h_data = static_cast<T*>(The_Pinned_Arena()->alloc(n*sizeof(T)));
27 std::memcpy(h_data, h_p, n*sizeof(T));
28#ifdef AMREX_USE_GPU
30 {
31 d_data = static_cast<T*>(The_Arena()->alloc(n*sizeof(T)));
32 Gpu::htod_memcpy_async(d_data, h_data, n*sizeof(T));
33 }
34#endif
35 }
36
37 explicit AsyncArray (const std::size_t n)
38 requires (std::is_standard_layout_v<T> && std::is_trivial_v<T>)
39
40 {
41 if (n == 0) { return; }
42#ifdef AMREX_USE_GPU
44 {
45 d_data = static_cast<T*>(The_Arena()->alloc(n*sizeof(T)));
46 }
47 else
48#endif
49 {
50 h_data = static_cast<T*>(The_Pinned_Arena()->alloc(n*sizeof(T)));
51 }
52 }
53
55
56 AsyncArray (AsyncArray const&) = delete;
57 AsyncArray (AsyncArray &&) = delete;
58 void operator= (AsyncArray const&) = delete;
59 void operator= (AsyncArray &&) = delete;
60
61 [[nodiscard]] T const* data () const noexcept { return (d_data != nullptr) ? d_data : h_data; }
62 [[nodiscard]] T* data () noexcept { return (d_data != nullptr) ? d_data : h_data; }
63 void clear ()
64 {
65 if (d_data != nullptr) { Gpu::freeAsync(The_Arena(), d_data); }
66 if (h_data != nullptr) { Gpu::freeAsync(The_Pinned_Arena(), h_data); }
67 d_data = nullptr;
68 h_data = nullptr;
69 }
70
71 void copyToHost (T* h_p, std::size_t n) const
72 {
73 if (n == 0) { return; }
74#ifdef AMREX_USE_GPU
75 if (d_data)
76 {
77 Gpu::dtoh_memcpy(h_p, d_data, n*sizeof(T));
78 }
79 else
80#endif
81 if (h_data)
82 {
83 std::memcpy(h_p, h_data, n*sizeof(T));
84 }
85 }
86
87private:
88 T* d_data = nullptr;
89 T* h_data = nullptr;
90};
91
92}
93
94using Gpu::AsyncArray;
95}
96
97#endif
Memory arena base class and global arena accessors.
virtual void * alloc(std::size_t sz)=0
Allocate sz bytes from this arena.
AsyncArray(T const *h_p, const std::size_t n)
Definition AMReX_GpuAsyncArray.H:23
void copyToHost(T *h_p, std::size_t n) const
Definition AMReX_GpuAsyncArray.H:71
AsyncArray(AsyncArray const &)=delete
AsyncArray(AsyncArray &&)=delete
void clear()
Definition AMReX_GpuAsyncArray.H:63
T * data() noexcept
Definition AMReX_GpuAsyncArray.H:62
~AsyncArray()
Definition AMReX_GpuAsyncArray.H:54
T const * data() const noexcept
Definition AMReX_GpuAsyncArray.H:61
AsyncArray(T const *h_p, const std::size_t n)
Definition AMReX_GpuAsyncArray.H:23
AsyncArray(const std::size_t n)
Definition AMReX_GpuAsyncArray.H:37
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:879
Arena * The_Arena()
Definition AMReX_Arena.cpp:839
Definition AMReX_BaseFwd.H:60
void freeAsync(Arena *arena, void *mem) noexcept
Definition AMReX_GpuDevice.H:364
bool inLaunchRegion() noexcept
Definition AMReX_GpuControl.H:88
void dtoh_memcpy(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:515
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:440
Definition AMReX_Amr.cpp:50