LoAdSG
multilevelvector.h
1//
2// Created by scherner on 31.05.21.
3//
4
5#ifndef GRUN_MULTILEVELVECTOR_H
6#define GRUN_MULTILEVELVECTOR_H
7
8#include "vector.h"
9#include "../sgrid/multilevelSparseGrid.h"
10
11
12class GetNextSmallerDepthsIterator{
13 private:
14 Depth startDepth;
15 Depth lastDepth;
16 public:
17 GetNextSmallerDepthsIterator(const Depth& T):startDepth(T),lastDepth(T){
18 // next();
19 }
20 Depth operator*() const{
21 return lastDepth;
22 };
23 bool next(){
24 for (size_t i = 0; i < DimensionSparseGrid; i++)
25 {
26 if(lastDepth.at(i)==0)lastDepth.set(startDepth.at(i),i);
27 else {
28 lastDepth.set(lastDepth.at(i)-1,i);
29 return true;
30 }
31 }
32 return false;
33 }
34 void Print(){
35 do{
36 lastDepth.Print();
37 }while (next());
38 }
39};
40
41class GetNextSmallerDepthsIteratorInner{
42private:
43 Depth startDepth;
44 Depth lastDepth;
45public:
46 GetNextSmallerDepthsIteratorInner(const Depth& T):startDepth(T),lastDepth(T){
47 // next();
48 }
49 Depth operator*() const{
50 return lastDepth;
51 };
52 bool next(){
53 for (size_t i = 0; i < DimensionSparseGrid; i++)
54 {
55 if(lastDepth.at(i)==1)lastDepth.set(startDepth.at(i),i);
56 else {
57 lastDepth.set(lastDepth.at(i)-1,i);
58 return true;
59 }
60 }
61 return false;
62 }
63 void Print(){
64 do{
65 lastDepth.Print();
66 }while (next());
67 }
68};
69
70
71
72class MultiLevelVector {
73
74
75public:
76 MultiLevelVector(MultiLevelAdaptiveSparseGrid &grid);
77
78 ~MultiLevelVector();
79
80
81 double getValue(unsigned long i) {
82 return dataTableVector[i];
83 }
84
85 void setValue(unsigned long i, double val) {
86 dataTableVector[i]=val;
87 }
88
89
90 inline void setMultiLevelValues(VectorSparseG &vector, Depth &T);
91
92 inline void setMultiLevelValues2(VectorSparseG &vector, Depth &T);
93
94 inline void setMultiLevelValuesOMP(VectorSparseG &vector, Depth &T, MultiDepthHashGrid& multiDepthHashGrid);
95
96 inline void setMultiLevelValues(VectorSparseG &vector, Depth &T, ListOfDepthOrderedSubgrids& list);
97
98 inline void addMultiLevelValues(VectorSparseG &vector, Depth &T);
99
100 inline void setMultiLevelValuesInVector(VectorSparseG &vector, Depth &T, ListOfDepthOrderedSubgrids &list);
101
102 MultiLevelAdaptiveSparseGrid *getSparseGrid() { return sparseGrid; };
103
104
106
107 inline void operator=(double x) {
108 for (unsigned long i = 0; i < sparseGrid->getMaximalOccupiedSecondTable(); i++)
109 dataTableVector[i] = x;
110 };
111
112 void operator+=(MultiLevelVector &vector) {
113 for (unsigned long i = 0; i < sparseGrid->getMaximalOccupiedSecondTable(); i++) {
114 dataTableVector[i] = vector.getValue(i);
115 }
116 }
117
118
119
121
122 void Broadcast(int rank);
123
124 bool mpi_doit();
125
126 void sendTo(int torank);
127
128 void ReduceSum(int rank);
129
130
132 void PrintDoubleTwoD(int level);
133
134 void PrintDoubleTwoD(int level, Depth T);
135
137 bool workonindex(unsigned long i);
138
139private:
140 MultiLevelAdaptiveSparseGrid *sparseGrid;
141 double *dataTableVector;
142
143
144};
145
146inline void MultiLevelVector::setMultiLevelValues(VectorSparseG &vector, Depth &T) {
147
148 double *data2 = vector.getDatatableVector();
149 AdaptiveSparseGrid_Base *grid = vector.getSparseGrid();
150 // cout <<endl;
151 // cout << grid->getMaximalOccupiedSecondTable() << endl;
152 auto iter = GetNextSmallerDepthsIterator(T);
153 do{
154 Depth Tlocal = *iter;
155 SingleDepthHashGrid& depthGrid = grid->getMultiDepthHashGrid()->getGridForDepth(Tlocal);
156 const auto& mapping = depthGrid._mapPosToGridPos;
157 // cout << mapping.size() << ", " << depthGrid.getNumberOfEntries() <<endl;
158 for (size_t i = 0; i < mapping.size(); i++)
159 {
160 unsigned long k;
161 IndexDimension I = depthGrid._map.getIndexOfTable(i);
162 if (sparseGrid->occupied(k, I, T))
163 dataTableVector[k] = data2[mapping[i]];
164 }
165 // cout <<endl;
166
167 }while(iter.next());
168
169
170};
171
172inline void MultiLevelVector::setMultiLevelValues2(VectorSparseG &vector, Depth &T) {
173
174double *data2 = vector.getDatatableVector();
175 AdaptiveSparseGrid_Base *grid = vector.getSparseGrid();
176 // cout <<endl;
177 // cout << grid->getMaximalOccupiedSecondTable() << endl;
178 auto iter = GetNextSmallerDepthsIterator(T);
179 do{
180 Depth Tlocal = *iter;
181 SingleDepthHashGrid& depthGrid = grid->getMultiDepthHashGrid()->getGridForDepth(Tlocal);
182 const auto& mapping = depthGrid._mapPosToGridPos;
183
184 if(depthGrid.getNumberOfEntries()>0) {
185 int end = mapping.size();
186 for (size_t i = 0; i < end; i++) {
187 unsigned long k;
188 IndexDimension I = depthGrid._map.getIndexOfTable(i);
189 if (sparseGrid->occupied(k, I, T))
190 dataTableVector[k] = data2[mapping[i]];
191 }
192 }
193 // cout <<endl;
194
195 }while(iter.next());
196
197};
198
199
200inline void MultiLevelVector::setMultiLevelValuesOMP(VectorSparseG &vector, Depth &T, MultiDepthHashGrid& multiDepthHashGrid) {
201
202 double *data2 = vector.getDatatableVector();
203
204 auto iter = GetNextSmallerDepthsIterator(T);
205 do{
206 Depth Tlocal = *iter;
207 SingleDepthHashGrid& depthGrid = multiDepthHashGrid.getGridForDepth(Tlocal);
208 const auto& mapping = depthGrid._mapPosToGridPos;
209
210 if(depthGrid.getNumberOfEntries()>0) {
211 auto end = mapping.size();
212 for (size_t i = 0; i < end; i++) {
213 unsigned long k;
214 IndexDimension I = depthGrid._map.getIndexOfTable(i);
215 if (sparseGrid->occupied(k, I, T))
216 dataTableVector[k] = data2[mapping[i]];
217 }
218 }
219
220
221 }while(iter.next());
222
223};
224
225
226inline void MultiLevelVector::setMultiLevelValues(VectorSparseG &vector, Depth &T, ListOfDepthOrderedSubgrids &list) {
227 unsigned long k;
228 double *data2 = vector.getDatatableVector();
229 ListOfDepthOrderedSubgrids::iterator iter(list);
230 do {
231 Depth T_new = iter.getDepth();
232 if(T_new <= T){
233 SubgridFixedDepth::iterator iter_inner(*list.getSubgrid(T_new));
234 do {
235 unsigned long i = iter_inner.geti();
236 IndexDimension I = iter_inner.getPoint();
237 if (sparseGrid->occupied(k, I, T))
238 dataTableVector[k] = data2[i];
239
240 } while (iter_inner.next());
241
242 }
243
244
245 } while (iter.next());
246
247 /* double *data2 = vector.getDatatableVector();
248 unsigned long k;
249 AdaptiveSparseGrid_Base *grid = vector.getSparseGrid();
250 unsigned long maxocc = grid->getMaximalOccupiedSecondTable();
251 unsigned long *secondTable = grid->getSecondTable();
252
253 for (unsigned long i = 0; i < maxocc; i++) {
254 if (secondTable[i] != 0) {
255 IndexDimension I = grid->getIndexOfTable(i);
256 Depth Tlocal(I);
257 if (Tlocal <= T) {
258 if (sparseGrid->occupied(k, I, T))
259 dataTableVector[k] = data2[i];
260 }
261 }
262 }*/
263};
264
265
266inline void MultiLevelVector::addMultiLevelValues(VectorSparseG &vector, Depth &T) {
267
268
269 double *data2 = vector.getDatatableVector();
270 AdaptiveSparseGrid_Base *grid = vector.getSparseGrid();
271 // cout <<endl;
272 // cout << grid->getMaximalOccupiedSecondTable() << endl;
273
274 auto iter = GetNextSmallerDepthsIterator(T);
275 do{
276 Depth Tlocal = *iter;
277
278 SingleDepthHashGrid& depthGrid = grid->getMultiDepthHashGrid()->getGridForDepth(Tlocal);
279 const auto& mapping = depthGrid._mapPosToGridPos;
280 // cout << mapping.size() << ", " << depthGrid.getNumberOfEntries() <<endl;
281 if(depthGrid.getNumberOfEntries()>0) {
282 for (size_t i = 0; i < mapping.size(); i++) {
283 unsigned long k;
284 IndexDimension I = depthGrid._map.getIndexOfTable(i);
285 if (sparseGrid->occupied(k, I, T))
286 dataTableVector[k] = dataTableVector[k] + data2[mapping[i]];
287 }
288 }
289 // cout <<endl;
290
291 }while(iter.next());
292
293
294
295
296
297
298};
299
300void MultiLevelVector::setMultiLevelValuesInVector(VectorSparseG &vector, Depth &T, ListOfDepthOrderedSubgrids &list) {
301 ListOfDepthOrderedSubgrids::iterator iter(list);
302 iter.gotoBegin();
303 do {
304 Depth Tlocal = iter.getDepth();
305 if (Tlocal <= T) {
306 SubgridFixedDepth::iterator inneriter(*iter.getSubgrid());
307 inneriter.gotobegin();
308 do {
309 IndexDimension Index = inneriter.getPoint();
310 unsigned long k;
311 if (sparseGrid->occupied(k, Index, T))
312 vector.setValue(Index, dataTableVector[k]);
313
314 } while (inneriter.next());
315 }
316 } while (iter.next());
317
318/* double *data2 = vector.getDatatableVector();
319 unsigned long k;
320 AdaptiveSparseGrid_Base* grid = vector.getSparseGrid();
321 unsigned long maxocc = grid->getMaximalOccupiedSecondTable();
322 unsigned long* secondTable = grid->getSecondTable();
323
324 for (unsigned long i = 0; i < maxocc; i++) {
325 if(secondTable[i]!=0) {
326 IndexDimension I = grid->getIndexOfTable(i);
327 Depth Tlocal(I);
328 if (Tlocal <= T) {
329 if (sparseGrid->occupied(k, I, T))
330 data2[i]=dataTableVector[k];
331 }
332 }
333 }*/
334}
335
336#endif //GRUN_MULTILEVELVECTOR_H
Definition sparseGrid.h:86
Definition ListOfDepthOrderedGrids.h:115
Definition ListOfDepthOrderedGrids.h:82