Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
5#include <AMReX_Arena.H>
6
7#include <cstddef>
8#include <functional>
9#include <iosfwd>
10#include <map>
11#include <mutex>
12#include <set>
13#include <string>
14#include <unordered_set>
15#include <vector>
16
17namespace amrex {
18
25class CArena
26 :
27 public Arena
28{
29public:
35 CArena (std::size_t hunk_size = 0, ArenaInfo info = ArenaInfo());
36
37 CArena (const CArena& rhs) = delete;
38 CArena (CArena&& rhs) = delete;
39 CArena& operator= (const CArena& rhs) = delete;
40 CArena& operator= (CArena&& rhs) = delete;
41
43 ~CArena () override;
44
46 [[nodiscard]] void* alloc (std::size_t nbytes) final;
47
51 [[nodiscard]] std::pair<void*,std::size_t>
52 alloc_in_place (void* pt, std::size_t szmin, std::size_t szmax) final;
53
57 [[nodiscard]] void*
58 shrink_in_place (void* pt, std::size_t new_size) final;
59
64 void free (void* vp) final;
65
66 std::size_t freeUnused () final;
67
74 [[nodiscard]] bool hasFreeDeviceMemory (std::size_t sz) final;
75
77 std::size_t heap_space_used () const noexcept;
78
80 std::size_t heap_space_actually_used () const noexcept;
81
83 std::size_t sizeOf (void* p) const noexcept;
84
85 void PrintUsage (std::string const& name) const;
86
87 void PrintUsage (std::ostream& os, std::string const& name, std::string const& space) const;
88
90 constexpr static std::size_t DefaultHunkSize = 1024*1024*8;
91
92protected:
93
94 void* alloc_protected (std::size_t nbytes);
95
96 std::size_t freeUnused_protected () final;
97
99 class Node
100 {
101 public:
102 Node (void* a_block, void* a_owner, std::size_t a_size, MemStat* a_stat=nullptr) noexcept
103 :
104 m_block(a_block), m_owner(a_owner), m_size(a_size), m_stat(a_stat) {}
105
107 bool operator< (const Node& rhs) const noexcept
108 {
109 return std::less<>{}(m_block, rhs.m_block);
110 }
111
113 bool operator== (const Node& rhs) const noexcept
114 {
115 return m_block == rhs.m_block;
116 }
117
119 [[nodiscard]] void* block () const noexcept { return m_block; }
120
122 void block (void* blk) noexcept { m_block = blk; }
123
125 [[nodiscard]] std::size_t size () const noexcept { return m_size; }
126
128 void size (std::size_t sz) noexcept { m_size = sz; }
129
130 [[nodiscard]] void* owner () const noexcept { return m_owner; }
131
132 [[nodiscard]] bool coalescable (const Node& rhs) const noexcept {
133 return m_owner == rhs.m_owner;
134 }
135
137 [[nodiscard]] MemStat* mem_stat () const { return m_stat; }
138
140 void mem_stat (MemStat* a_stat) noexcept { m_stat = a_stat; }
141
142 struct hash {
143 std::size_t operator() (const Node& n) const noexcept {
144 return std::hash<void*>{}(n.m_block);
145 }
146 };
147
148 private:
150 void* m_block;
152 void* m_owner;
154 std::size_t m_size;
157 };
158
160 std::vector<std::pair<void*,std::size_t> > m_alloc;
161
166 using NL = std::set<Node>;
167
173
178// NL m_busylist;
179 std::unordered_set<Node, Node::hash> m_busylist;
181 std::size_t m_hunk;
183 std::size_t m_used{0};
185 std::size_t m_actually_used{0};
186
187
188 std::mutex carena_mutex;
189
190 friend std::ostream& operator<< (std::ostream& os, const CArena& arena);
191};
192
193}
194
195#endif /*BL_CARENA_H*/
A virtual base class for objects that manage their own dynamic memory allocation.
Definition AMReX_Arena.H:100
static void PrintUsage()
Definition AMReX_Arena.cpp:439
The nodes in our free list and block list.
Definition AMReX_CArena.H:100
MemStat * m_stat
Used for profiling if this Node represents a user allocated block of memory.
Definition AMReX_CArena.H:156
std::size_t size() const noexcept
The size of the memory block.
Definition AMReX_CArena.H:125
std::size_t m_size
The size of the block we represent.
Definition AMReX_CArena.H:154
void block(void *blk) noexcept
Set block address.
Definition AMReX_CArena.H:122
void size(std::size_t sz) noexcept
Set size.
Definition AMReX_CArena.H:128
void * owner() const noexcept
Definition AMReX_CArena.H:130
void * m_owner
The starting address of the original allocation.
Definition AMReX_CArena.H:152
MemStat * mem_stat() const
Get the MemStat object of the function where this block was allocated.
Definition AMReX_CArena.H:137
void mem_stat(MemStat *a_stat) noexcept
Set MemStat.
Definition AMReX_CArena.H:140
void * m_block
The block of memory we reference.
Definition AMReX_CArena.H:150
bool coalescable(const Node &rhs) const noexcept
Definition AMReX_CArena.H:132
void * block() const noexcept
The block address.
Definition AMReX_CArena.H:119
Node(void *a_block, void *a_owner, std::size_t a_size, MemStat *a_stat=nullptr) noexcept
Definition AMReX_CArena.H:102
A Concrete Class for Dynamic Memory Management using first fit. This is a coalescing memory manager....
Definition AMReX_CArena.H:28
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:172
std::size_t sizeOf(void *p) const noexcept
Return the amount of memory in this pointer. Return 0 for unknown pointer.
Definition AMReX_CArena.cpp:433
CArena(const CArena &rhs)=delete
static constexpr std::size_t DefaultHunkSize
The default memory hunk size to grab from the heap.
Definition AMReX_CArena.H:90
void * shrink_in_place(void *pt, std::size_t new_size) final
Definition AMReX_CArena.cpp:198
std::mutex carena_mutex
Definition AMReX_CArena.H:188
std::size_t freeUnused_protected() final
Definition AMReX_CArena.cpp:353
std::size_t heap_space_actually_used() const noexcept
Return the total amount of memory given out via alloc.
Definition AMReX_CArena.cpp:427
std::size_t heap_space_used() const noexcept
The current amount of heap space used by the CArena object.
Definition AMReX_CArena.cpp:421
std::pair< void *, std::size_t > alloc_in_place(void *pt, std::size_t szmin, std::size_t szmax) final
Definition AMReX_CArena.cpp:127
CArena & operator=(const CArena &rhs)=delete
friend std::ostream & operator<<(std::ostream &os, const CArena &arena)
Definition AMReX_CArena.cpp:481
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:166
std::size_t m_hunk
The minimal size of hunks to request from system.
Definition AMReX_CArena.H:181
std::size_t freeUnused() final
Free unused memory back to the system. Return value is the amount memory freed.
Definition AMReX_CArena.cpp:346
std::size_t m_used
The amount of heap space currently allocated.
Definition AMReX_CArena.H:183
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:179
void * alloc(std::size_t nbytes) final
Allocate some memory.
Definition AMReX_CArena.cpp:30
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:256
~CArena() override
The destructor.
Definition AMReX_CArena.cpp:22
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:379
void * alloc_protected(std::size_t nbytes)
Definition AMReX_CArena.cpp:38
std::vector< std::pair< void *, std::size_t > > m_alloc
The list of blocks allocated via ::operator new().
Definition AMReX_CArena.H:160
std::size_t m_actually_used
The amount of memory given out via alloc().
Definition AMReX_CArena.H:185
Definition AMReX_Amr.cpp:49
bool operator==(A1 const &a1, A2 const &a2)
Definition AMReX_GpuAllocators.H:203
Definition AMReX_Arena.H:53
Definition AMReX_CArena.H:142
Definition AMReX_Arena.H:12