Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Arena.H
Go to the documentation of this file.
1#ifndef AMREX_ARENA_H_
2#define AMREX_ARENA_H_
3#include <AMReX_Config.H>
4
10#include <AMReX_BLassert.H>
11#include <AMReX_Exception.H>
12#include <AMReX_INT.H>
13#ifdef AMREX_USE_GPU
14#include <AMReX_GpuControl.H>
15#endif
16
17#ifdef AMREX_TINY_PROFILING
18#include <AMReX_TinyProfiler.H>
19#else
20namespace amrex {
21 struct MemStat {};
22}
23#endif
24
25#include <cstddef>
26#include <cstdlib>
27#include <limits>
28#include <map>
29#include <mutex>
30#include <unordered_map>
31#include <utility>
32
33namespace amrex {
34
39inline std::size_t aligned_size (std::size_t align_requirement, std::size_t size) noexcept
40{
41 return ((size + (align_requirement-1)) / align_requirement) * align_requirement;
42}
43
46inline bool is_aligned (const void* p, std::size_t alignment) noexcept
47{
48 auto* q = const_cast<void*>(p);
49 auto space = alignment;
50 return std::align(alignment, alignment, q, space) == p;
51}
52
53class Arena;
54
57Arena* The_Arena ();
60Arena* The_Async_Arena ();
64Arena* The_Device_Arena ();
68Arena* The_Managed_Arena ();
71Arena* The_Pinned_Arena ();
75Arena* The_Comms_Arena ();
78Arena* The_Cpu_Arena ();
79
83Arena* The_Null_Arena ();
84
92{
93 Long release_threshold = std::numeric_limits<Long>::max();
94 bool use_cpu_memory = false;
96 bool device_set_readonly = false;
97 bool device_set_preferred = false;
98 bool device_use_hostalloc = false;
99 bool defragmentation = false;
103 return *this;
104 }
108 device_use_hostalloc = false;
109 return *this;
110 }
112 ArenaInfo& SetReadOnly () noexcept {
114 device_set_readonly = true;
115 return *this;
116 }
118 ArenaInfo& SetPreferred () noexcept {
121 return *this;
122 }
124 ArenaInfo& SetHostAlloc () noexcept {
127 return *this;
128 }
130 ArenaInfo& SetCpuMemory () noexcept {
131 use_cpu_memory = true;
133 device_set_readonly = false;
134 device_set_preferred = false;
135 device_use_hostalloc = false;
136 return *this;
137 }
139 ArenaInfo& SetDefragmentation (bool b) noexcept {
140 defragmentation = b;
141 return *this;
142 }
143};
144
153class Arena
154{
155public:
156
157 virtual ~Arena () = default;
158 Arena () noexcept = default;
159
160 Arena (const Arena& rhs) = delete;
161 Arena (Arena&& rhs) = delete;
162 Arena& operator= (const Arena& rhs) = delete;
163 Arena& operator= (Arena&& rhs) = delete;
164
170 [[nodiscard]] virtual void* alloc (std::size_t sz) = 0;
171
186 [[nodiscard]] virtual std::pair<void*,std::size_t>
187 alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax)
188 {
189 ignore_unused(pt, szmin);
190 auto* p = alloc(szmax);
191 return std::make_pair(p, szmax);
192 }
193
206 [[nodiscard]] virtual void*
207 shrink_in_place (void* pt, std::size_t sz)
208 {
209 ignore_unused(pt);
210 return alloc(sz);
211 }
212
214 virtual void free (void* pt) = 0;
215
220 virtual std::size_t freeUnused () { return 0; }
221
227 [[nodiscard]] virtual std::size_t largestFreeBlock () const { return 0; }
228
231 [[nodiscard]] virtual bool isDeviceAccessible () const;
232
235 [[nodiscard]] virtual bool isHostAccessible () const;
236
244 [[nodiscard]] virtual bool isManaged () const;
245
253 [[nodiscard]] virtual bool isDevice () const;
254
262 [[nodiscard]] virtual bool isPinned () const;
263
271 [[nodiscard]] virtual bool hasFreeDeviceMemory (std::size_t sz);
272
277 void registerForProfiling (const std::string& memory_name);
278
284
285#ifdef AMREX_USE_GPU
287 [[nodiscard]] virtual bool isStreamOrderedArena () const { return false; }
288 virtual void streamOrderedFree (void* pt, gpuStream_t stream);
289#endif
290
294 static std::size_t align (std::size_t sz);
295
300 static void Initialize (bool minimal);
301
317 static void PrintUsage (bool print_max_usage=false);
318
330 static void PrintUsageToStream (std::ostream& os, std::string const& space);
331
344 static void PrintUsageToFiles (std::string const& filename, std::string const& message);
345
346 static void Finalize ();
347
356 static const std::size_t align_size = 256;
357
361 [[nodiscard]] const ArenaInfo& arenaInfo () const { return arena_info; }
362
365 virtual void ResetMaxUsageCounter () {}
366
368 [[nodiscard]] static bool IsInitialized ();
369
370protected:
371
373
374 virtual std::size_t freeUnused_protected () { return 0; }
375 void* allocate_system (std::size_t nbytes);
376 void deallocate_system (void* p, std::size_t nbytes);
377 [[noreturn]] static void throw_out_of_memory (std::string const& memory_type,
378 std::size_t nbytes,
379 std::string const& error_msg);
380
382 struct ArenaProfiler {
384 bool m_do_profiling = false;
386 std::mutex m_arena_profiler_mutex;
388 std::map<std::string, MemStat> m_profiling_stats;
390 std::unordered_map<void*, std::pair<MemStat*, std::size_t>> m_currently_allocated;
391
392 ~ArenaProfiler ();
393 ArenaProfiler () noexcept = default;
394 ArenaProfiler (const ArenaProfiler& rhs) = delete;
395 ArenaProfiler (ArenaProfiler&& rhs) = delete;
396 ArenaProfiler& operator= (const ArenaProfiler& rhs) = delete;
397 ArenaProfiler& operator= (ArenaProfiler&& rhs) = delete;
398
399 void profile_alloc (void* ptr, std::size_t nbytes);
400
401 void profile_free (void* ptr);
402
403 } m_profiler;
405};
406
407}
408
409#endif /*BL_ARENA_H*/
Assertion macros used across AMReX for runtime consistency checks.
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
AMReX exception types.
Abstract base class for memory arenas.
Definition AMReX_Arena.H:154
virtual std::size_t largestFreeBlock() const
Return the largest free memory block already held by this Arena.
Definition AMReX_Arena.H:227
static void Finalize()
Used internally by amrex.
Definition AMReX_Arena.cpp:758
virtual void free(void *pt)=0
Free a previously allocated block pointed to by pt.
virtual void * alloc(std::size_t sz)=0
Allocate sz bytes from this arena.
virtual std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax)
Allocate memory in-place if possible.
Definition AMReX_Arena.H:187
static std::size_t align(std::size_t sz)
Return the smallest multiple of Arena::align_size that is >= sz bytes.
Definition AMReX_Arena.cpp:178
Arena() noexcept=default
virtual ~Arena()=default
virtual bool isPinned() const
Check whether it is pinned host memory.
Definition AMReX_Arena.cpp:123
virtual void ResetMaxUsageCounter()
Definition AMReX_Arena.H:365
virtual bool isHostAccessible() const
Definition AMReX_Arena.cpp:87
virtual bool isStreamOrderedArena() const
Is this GPU stream ordered memory allocator?
Definition AMReX_Arena.H:287
static const std::size_t align_size
Definition AMReX_Arena.H:356
static bool IsInitialized()
Definition AMReX_Arena.cpp:823
const ArenaInfo & arenaInfo() const
Return the ArenaInfo object for querying.
Definition AMReX_Arena.H:361
virtual bool isManaged() const
Check whether it is managed GPU memory.
Definition AMReX_Arena.cpp:99
void deregisterFromProfiling()
Remove this Arena from the list of Arenas that are profiled by TinyProfiler. This is equivalent to de...
Definition AMReX_Arena.cpp:165
virtual std::size_t freeUnused_protected()
Definition AMReX_Arena.H:374
virtual void * shrink_in_place(void *pt, std::size_t sz)
Shrink allocation size in-place.
Definition AMReX_Arena.H:207
virtual void streamOrderedFree(void *pt, gpuStream_t stream)
Definition AMReX_Arena.cpp:141
static void throw_out_of_memory(std::string const &memory_type, std::size_t nbytes, std::string const &error_msg)
Definition AMReX_Arena.cpp:732
void registerForProfiling(const std::string &memory_name)
Add this Arena to the list of Arenas that are profiled by TinyProfiler.
Definition AMReX_Arena.cpp:155
static void PrintUsageToFiles(std::string const &filename, std::string const &message)
Print memory usage information of all arenas to given file.
Definition AMReX_Arena.cpp:716
virtual bool isDevice() const
Check whether it is non-managed GPU device memory.
Definition AMReX_Arena.cpp:111
virtual bool hasFreeDeviceMemory(std::size_t sz)
Does the device have enough free memory for allocating this much memory? For CPU builds,...
Definition AMReX_Arena.cpp:134
void deallocate_system(void *p, std::size_t nbytes)
Definition AMReX_Arena.cpp:376
static void Initialize(bool minimal)
Initialize the Arena subsystem (used internally by AMReX).
Definition AMReX_Arena.cpp:425
static void PrintUsage(bool print_max_usage=false)
Print memory usage information of all arenas.
Definition AMReX_Arena.cpp:609
virtual std::size_t freeUnused()
Release unused cached memory back to the system.
Definition AMReX_Arena.H:220
ArenaInfo arena_info
Definition AMReX_Arena.H:372
static void PrintUsageToStream(std::ostream &os, std::string const &space)
Print memory usage information of all arenas to a given output stream.
Definition AMReX_Arena.cpp:672
virtual bool isDeviceAccessible() const
Definition AMReX_Arena.cpp:77
void * allocate_system(std::size_t nbytes)
Definition AMReX_Arena.cpp:184
amrex_long Long
Definition AMReX_INT.H:30
bool is_aligned(const void *p, std::size_t alignment) noexcept
Definition AMReX_Arena.H:46
Arena * The_Managed_Arena()
Definition AMReX_Arena.cpp:859
Arena * The_Comms_Arena()
Definition AMReX_Arena.cpp:889
Arena * The_Device_Arena()
Definition AMReX_Arena.cpp:849
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:869
Arena * The_Async_Arena()
Definition AMReX_Arena.cpp:839
std::size_t aligned_size(std::size_t align_requirement, std::size_t size) noexcept
Return the smallest multiple of align_requirement that is >= size.
Definition AMReX_Arena.H:39
Arena * The_Cpu_Arena()
Definition AMReX_Arena.cpp:879
Arena * The_Arena()
Definition AMReX_Arena.cpp:829
Definition AMReX_Amr.cpp:50
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
Arena * The_Null_Arena()
Definition AMReX_Arena.cpp:899
cudaStream_t gpuStream_t
Definition AMReX_GpuControl.H:79
Configuration options for constructing an Arena.
Definition AMReX_Arena.H:92
ArenaInfo & SetReadOnly() noexcept
Advise the runtime that the managed memory will be read-only on the device.
Definition AMReX_Arena.H:112
bool device_set_preferred
Advise the runtime to prefer the device for managed memory.
Definition AMReX_Arena.H:97
bool device_set_readonly
Advise the runtime that the managed memory is read-only on the device.
Definition AMReX_Arena.H:96
bool device_use_hostalloc
Use host-pinned memory (cudaMallocHost/hipHostMalloc).
Definition AMReX_Arena.H:98
ArenaInfo & SetReleaseThreshold(Long rt) noexcept
Set the usage threshold that triggers release of cached unused blocks before growing.
Definition AMReX_Arena.H:101
ArenaInfo & SetDefragmentation(bool b) noexcept
Enable or disable defragmentation in the arena.
Definition AMReX_Arena.H:139
Long release_threshold
When usage would exceed this, cached unused blocks are released first.
Definition AMReX_Arena.H:93
ArenaInfo & SetPreferred() noexcept
Advise the runtime to prefer device residence for the managed memory.
Definition AMReX_Arena.H:118
ArenaInfo & SetCpuMemory() noexcept
Configure the arena to use CPU (host) memory.
Definition AMReX_Arena.H:130
bool device_use_managed_memory
Use CUDA/HIP managed (unified) memory for GPU allocations.
Definition AMReX_Arena.H:95
bool use_cpu_memory
Allocate CPU (host) memory instead of GPU memory.
Definition AMReX_Arena.H:94
ArenaInfo & SetDeviceMemory() noexcept
Configure the arena to use non-managed GPU device memory.
Definition AMReX_Arena.H:106
bool defragmentation
Definition AMReX_Arena.H:99
ArenaInfo & SetHostAlloc() noexcept
Configure the arena to use host-pinned memory.
Definition AMReX_Arena.H:124
Definition AMReX_Arena.H:21