Peano 4
Loading...
Searching...
No Matches
HierarchicalStackMap.h
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3#pragma once
4
5
6#include "maps.h"
7
9
11
12#include <map>
13
14
15
16namespace peano4 {
17 namespace maps {
18 template <typename T>
20 }
21}
22
23
24
36template <typename T>
38 private:
42 struct TreeData {
43 std::map< int, T* > _stackNumberToData;
44 };
45
50 std::vector< TreeData > _data;
51
56 void createStack(int localTreeNumber, int stackNumber);
57
58 public:
61
62 bool empty(int treeId, int stackId) const;
63
67 void clear();
68
69 void clear(int spacetree);
70
86 T* getForPush(int treeId, int stackId);
87 T* getForPush(const StackKey& key);
88
92 T* getForPop(int treeId, int stackId);
93 T* getForPop(const StackKey& key);
94
95 std::string toString() const;
96
101 std::set<StackKey> getKeys();
102
106 void garbageCollection(int spacetree);
107
111 bool holdsStack(int treeId, int stackId) const;
112 bool holdsStack(const StackKey& key) const;
113};
114
115
116template <typename T>
117void peano4::maps::HierarchicalStackMap<T>::createStack(int localTreeNumber, int stackNumber) {
118 assertion(localTreeNumber>=0);
119 assertion(localTreeNumber<static_cast<int>(_data.size()));
120 if ( _data[localTreeNumber]._stackNumberToData.count(stackNumber)==0 ) {
121 _data[localTreeNumber]._stackNumberToData.insert(
122 std::pair< int, T* >(
123 stackNumber,
124 new T()
125 )
126 );
127 }
128}
129
130
131template <typename T>
132bool peano4::maps::HierarchicalStackMap<T>::empty(int treeId, int stackId) const {
133 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(treeId);
134 assertion3(localTreeId>=0,localTreeId,treeId,stackId);
135 assertion4(localTreeId<_data.size(),localTreeId,_data.size(),treeId,stackId);
136 return _data[localTreeId]._stackNumberToData.count(stackId)==0
137 or _data[localTreeId]._stackNumberToData.at(stackId)->empty();
138}
139
140
141template <typename T>
143 return getForPush( StackKey(treeId,stackId) );
144}
145
146
147template <typename T>
148bool peano4::maps::HierarchicalStackMap<T>::holdsStack(int treeId, int stackId) const {
149 return holdsStack( StackKey(treeId,stackId) );
150}
151
152
153template <typename T>
155 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(key.first);
156 assertion3(localTreeId>=0,localTreeId,key.first,key.second);
157 assertion4(localTreeId<static_cast<int>(_data.size()),localTreeId,_data.size(),key.first,key.second);
158 return _data[localTreeId]._stackNumberToData.count(key.second)==1;
159}
160
161
162template <typename T>
164 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(key.first);
165 assertion3(localTreeId>=0,localTreeId,key.first,key.second);
166 assertion4(localTreeId<static_cast<int>(_data.size()),localTreeId,_data.size(),key.first,key.second);
167 createStack(localTreeId,key.second);
168 assertion4( _data[localTreeId]._stackNumberToData[key.second] != nullptr, localTreeId, key.first, key.second, tarch::mpi::Rank::getInstance().getRank() );
169 return _data[localTreeId]._stackNumberToData[key.second];
170}
171
172
173template <typename T>
175 return getForPop( StackKey(treeId,stackId) );
176}
177
178
179template <typename T>
181 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(key.first);
182 assertion3(localTreeId>=0,localTreeId,key.first,key.second);
183 assertion4(localTreeId<static_cast<int>(_data.size()),localTreeId,_data.size(),key.first,key.second);
184 assertion4(_data[localTreeId]._stackNumberToData.count(key.second)==1,localTreeId,_data.size(),key.first,key.second);
185 assertion3(key.second>=0,localTreeId,key.first,key.second);
186 return _data[localTreeId]._stackNumberToData.at(key.second);
187}
188
189
190template <typename T>
192 std::string result = "(" + std::to_string( _data.size() );
193/*
194 for (auto& p: _data) {
195 result += ",";
196 result += std::to_string(p.first.first) + "x" + std::to_string(p.first.second) + ":" + std::to_string(p.second->size());
197 }
198*/
199 result += ")";
200 return result;
201}
202
203
204template <typename T>
205std::set<peano4::maps::StackKey> peano4::maps::HierarchicalStackMap<T>::getKeys() {
206 std::set<peano4::maps::StackKey> result;
207 for (int i = 0; i < static_cast<int>(_data.size()); i++) {
208 for (auto& pp: _data[i]._stackNumberToData) {
209 result.insert( peano4::maps::StackKey( peano4::parallel::Node::getInstance().getGlobalTreeId(i),pp.first) );
210 }
211 }
212 return result;
213}
214
215
216template <typename T>
218 for (auto& p: _data) {
219 for (auto& pp: p._stackNumberToData) {
220 delete pp.second;
221 }
222 }
223}
224
225
226template <typename T>
228 _data(peano4::parallel::Node::MaxSpacetreesPerRank) {
229}
230
231
232template <typename T>
234 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(spacetree);
235 if (static_cast<int>(_data.size()) > localTreeId) {
236 for (auto& p: _data[localTreeId]._stackNumberToData) {
237 if (
238 p.second->empty()
239 and
241 ) {
242 delete p.second;
243 p.second = new T();
244 }
245 }
246 }
247}
248
249
250template <typename T>
252 for (auto& p: _data) {
253 for (auto& pp: p._stackNumberToData) {
254 if (pp.second->empty()) {
255 delete pp.second;
256 pp.second = new T();
257 }
258 }
259 }
260}
261
262
263template <typename T>
265 const int localTreeId = peano4::parallel::Node::getInstance().getLocalTreeId(spacetree);
266 if (static_cast<int>(_data.size()) > localTreeId) {
267 for (auto& p: _data[localTreeId]._stackNumberToData) {
268 delete p.second;
269 p.second = new T();
270 }
271 }
272}
#define assertion4(expr, param0, param1, param2, param3)
#define assertion3(expr, param0, param1, param2)
#define assertion(expr)
void createStack(int localTreeNumber, int stackNumber)
This routine is not thread-safe, i.e.
T * getForPush(int treeId, int stackId)
Get the stack belonging to a tree.
std::set< StackKey > getKeys()
This one should be const, but I might need a semaphore, so I have to omit the const qualifier.
bool holdsStack(int treeId, int stackId) const
For debugging/assertions.
std::vector< TreeData > _data
A vector of maps.
bool empty(int treeId, int stackId) const
T * getForPop(int treeId, int stackId)
int getLocalTreeId(int treeId) const
Definition Node.cpp:125
static bool isStorageStackNumber(int number)
There are communication stacks and storage stacks.
Definition Node.cpp:198
static Node & getInstance()
This operation returns the singleton instance.
Definition Node.cpp:108
static Rank & getInstance()
This operation returns the singleton instance.
Definition Rank.cpp:538
int getRank() const
Return rank of this node.
Definition Rank.cpp:528
The parallel namespace is Peano's core abstracts from both MPI and multicore parallelisation.
std::pair< int, int > StackKey
Unique key identifying a stack.
Definition maps.h:32