Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_CArena.H
Go to the documentation of this file.
1#ifndef BL_CARENA_H
2#define BL_CARENA_H
3#include <AMReX_Config.H>
4
10#include <AMReX_Arena.H>
11
12#include <atomic>
13#include <cstddef>
14#include <functional>
15#include <iosfwd>
16#include <map>
17#include <mutex>
18#include <set>
19#include <string>
20#include <unordered_set>
21#include <vector>
22
23namespace amrex {
24
34class CArena
35 :
36 public Arena
37{
38public:
45 CArena (std::size_t hunk_size = 0, ArenaInfo info = ArenaInfo());
46
47 CArena (const CArena& rhs) = delete;
48 CArena (CArena&& rhs) = delete;
49 CArena& operator= (const CArena& rhs) = delete;
50 CArena& operator= (CArena&& rhs) = delete;
51
53 ~CArena () override;
54
56 [[nodiscard]] void* alloc (std::size_t nbytes) final;
57
79 [[nodiscard]] std::pair<void*,std::size_t>
80 alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax) final;
81
95 [[nodiscard]] void*
96 shrink_in_place (void* pt, std::size_t new_size) final;
97
104 void free (void* vp) final;
105
110 std::size_t freeUnused () final;
111
115 [[nodiscard]] std::size_t freeableMemory () const;
116
118 [[nodiscard]] std::size_t largestFreeBlock () const final;
119
128 [[nodiscard]] bool hasFreeDeviceMemory (std::size_t sz) final;
129
131 std::size_t heap_space_used () const noexcept;
132
134 std::size_t heap_space_actually_used () const noexcept;
135
141 std::size_t sizeOf (void* p) const noexcept;
142
158 void PrintUsage (std::string const& name, bool print_max_usage) const;
159
172 void PrintUsage (std::ostream& os, std::string const& name, std::string const& space) const;
173
175 void ResetMaxUsageCounter () final {
177 }
178
180 constexpr static std::size_t DefaultHunkSize = 1024*1024*8;
181
182protected:
183
184 void* alloc_protected (std::size_t nbytes);
185
186 std::size_t freeUnused_protected () final;
187
189 class Node
190 {
191 public:
192 Node (void* a_block, void* a_owner, std::size_t a_size, MemStat* a_stat=nullptr) noexcept
193 :
194 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
195
197 bool operator< (const Node& rhs) const noexcept
198 {
199 return std::less<>{}(m_block, rhs.m_block);
200 }
201
203 bool operator== (const Node& rhs) const noexcept
204 {
205 return m_block == rhs.m_block;
206 }
207
209 [[nodiscard]] void* block () const noexcept { return m_block; }
210
212 void block (void* blk) noexcept { m_block = blk; }
213
215 [[nodiscard]] std::size_t size () const noexcept { return m_size; }
216
218 void size (std::size_t sz) noexcept { m_size = sz; }
219
220 [[nodiscard]] void* owner () const noexcept { return m_owner; }
221
222 [[nodiscard]] bool coalescable (const Node& rhs) const noexcept {
223 return m_owner == rhs.m_owner;
224 }
225
227 [[nodiscard]] MemStat* mem_stat () const { return m_stat; }
228
230 void mem_stat (MemStat* a_stat) noexcept { m_stat = a_stat; }
231
232 struct hash {
233 std::size_t operator() (const Node& n) const noexcept {
234 return std::hash<void*>{}(n.m_block);
235 }
236 };
237
238 private:
240 void* m_block;
242 void* m_owner;
244 std::size_t m_size;
246 MemStat* m_stat;
247 };
248
250 std::vector<std::pair<void*,std::size_t> > m_alloc;
251
256 using NL = std::set<Node>;
257
263
268// NL m_busylist;
269 std::unordered_set<Node, Node::hash> m_busylist;
271 std::size_t m_hunk;
274 std::atomic<std::size_t> m_used{0};
276 std::size_t m_max_used{0};
279 std::atomic<std::size_t> m_actually_used{0};
281 std::size_t m_max_actually_used{0};
282
283 mutable std::mutex carena_mutex;
284
286 friend std::ostream& operator<< (std::ostream& os, const CArena& arena);
287};
288
289}
290
291#endif /*BL_CARENA_H*/
Memory arena base class and global arena accessors.
Abstract base class for memory arenas.
Definition AMReX_Arena.H:154
The nodes in our free list and block list.
Definition AMReX_CArena.H:190
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:215
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:212
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:218
void * owner() const noexcept
Definition AMReX_CArena.H:220
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:227
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:230
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:222
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:209
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:192
Coalescing first-fit dynamic memory arena.
Definition AMReX_CArena.H:37
NL m_freelist
The free list of allocated but not currently used blocks. Maintained in lo to hi memory sorted order.
Definition AMReX_CArena.H:262
std::size_t sizeOf(void *p) const noexcept
Return the size of the allocation referenced by p (0 if unknown).
Definition AMReX_CArena.cpp:526
CArena(const CArena &rhs)=delete
std::atomic< std::size_t > m_actually_used
Definition AMReX_CArena.H:279
static constexpr std::size_t DefaultHunkSize
The default memory hunk size to grab from the heap.
Definition AMReX_CArena.H:180
void * shrink_in_place(void *pt, std::size_t new_size) final
Shrink allocation size in-place.
Definition AMReX_CArena.cpp:258
std::mutex carena_mutex
Definition AMReX_CArena.H:283
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:438
std::size_t m_max_actually_used
The max amount of memory given out via alloc().
Definition AMReX_CArena.H:281
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc. Thread-safe.
Definition AMReX_CArena.cpp:520
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object. Thread-safe.
Definition AMReX_CArena.cpp:514
std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax) final
Allocate memory in-place if possible.
Definition AMReX_CArena.cpp:185
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Print a summary of the arena's free and busy lists to os.
Definition AMReX_CArena.cpp:578
void ResetMaxUsageCounter() final
Reset the maximum usage counter.
Definition AMReX_CArena.H:175
std::set< Node > NL
The type of our freelist and blocklist. We use a set sorted from lo to hi memory addresses.
Definition AMReX_CArena.H:256
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:271
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount of memory freed.
Definition AMReX_CArena.cpp:407
std::unordered_set< Node, Node::hash > m_busylist
The list of busy blocks. A block is either on the freelist or on the blocklist, but not on both.
Definition AMReX_CArena.H:269
void * alloc(std::size_t nbytes) final
Allocate some memory.
Definition AMReX_CArena.cpp:49
CArena(CArena &&rhs)=delete
void free(void *vp) final
Free up allocated memory. Merge neighboring free memory chunks into largest possible chunk.
Definition AMReX_CArena.cpp:316
std::size_t largestFreeBlock() const final
Return the largest free memory block already held by this CArena.
Definition AMReX_CArena.cpp:427
~CArena() override
The destructor.
Definition AMReX_CArena.cpp:41
bool hasFreeDeviceMemory(std::size_t sz) final
Does the device have enough free memory for allocating this much memory? For CPU builds,...
Definition AMReX_CArena.cpp:476
void * alloc_protected(std::size_t nbytes)
Definition AMReX_CArena.cpp:57
void PrintUsage(std::string const &name, bool print_max_usage) const
Print memory usage information of this arena.
Definition AMReX_CArena.cpp:541
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated.
Definition AMReX_CArena.H:250
std::atomic< std::size_t > m_used
Definition AMReX_CArena.H:274
std::size_t freeableMemory() const
Return the amount of memory that can be released back to the heap.
Definition AMReX_CArena.cpp:414
std::size_t m_max_used
The max amount of heap space currently allocated.
Definition AMReX_CArena.H:276
Definition AMReX_Amr.cpp:50
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:214
Configuration options for constructing an Arena.
Definition AMReX_Arena.H:92
Definition AMReX_CArena.H:232
Definition AMReX_Arena.H:21