LoAdSG
LocalStiffnessMatrices.h
1//
2// Created by to35jepo on 6/6/24.
3//
4
5#ifndef RUN_LOCALSTIFFNESSMATRICES_H
6#define RUN_LOCALSTIFFNESSMATRICES_H
7
8
9#include <queue>
10#include <unordered_set>
11#include "../mympi.h"
12#include "../stencils/Stencil.h"
13#include "../cells/CellStructure.h"
14
15
16double bytesToGigabytes(unsigned long long bytes);
17
18
19struct Container {
20 int id;
21 int totalSize;
22
23 // This operator will allow the priority_queue to compare containers based on their totalSize.
24 bool operator>(const Container &other) const {
25 return totalSize > other.totalSize;
26 }
27 std::vector<std::pair<Depth,int>> boxes;
28
29 bool full=false;
30};
31
32
33
34// Custom hash function for Depth
35struct DepthHash {
36 std::size_t operator()(const Depth& D) const {
37 unsigned long value = D.at(0);
38 for (int d = 1; d < DimensionSparseGrid; ++d) {
39 value = value + D.at(d) * PrimeNumbers::getPrimeForHash(d);
40 }
41 return value;
42 }
43};
44
45// Custom equality comparison function for Depth
46struct DepthEqual {
47 bool operator()(const Depth& lhs, const Depth& rhs) const {
48 // Implement an equality comparison function for Depth here
49 for(int d=0; d<DimensionSparseGrid; d++){
50 if(lhs.at(d)!=rhs.at(d)) return false;
51 }
52 return true;
53 }
54};
55
56
57class DistributedDepthsHashtable{
58 std::unordered_map<Depth,int, DepthHash, DepthEqual> _map;
59
60public:
61 DistributedDepthsHashtable()=default;
62 DistributedDepthsHashtable(Container* containers_sorted, int n);
63 int getNodeForDepth(const Depth &D);
64 void setNodeDepth(int i, Depth& D){_map.insert({D,i});};
65
66 std::unordered_map<Depth,int, DepthHash, DepthEqual>* getMap(){
67 return &_map;
68 }
69
70};
71
72
73
74void distribute_LocalStiffnessMatrices(int numberofprocesses, AdaptiveSparseGrid &sg, Container* containers_sorted);
75
76
77
78
79
80
81class LocalStiffnessMatrices {
82
83protected:
84 class LocalStiffnessMatrixFixedDepthSymmetric;
85
86public:
87 //ghost functions
88 virtual inline void initialize(Depth &T_){};
89 virtual inline void applyStencilOnCell(CellDimension& cell,VectorSparseG& input, VectorSparseG& output){};
90 virtual inline double returnValue(const IndexDimension &Index, const MultiDimCompass &mc){return 0.0;};
91
92
101 virtual void applyLocalStiffnessMatricesDepth(VectorSparseG& input, VectorSparseG& output, Depth& depth);
102
103 void applyLocalStiffnessMatricesIndex(VectorSparseG& input, VectorSparseG& output, Depth& depth,IndexDimension& Index){
104 //TODO
105 };
106
107 void applyLocalStiffnessMatricesFixedDepth(VectorSparseG& input, VectorSparseG& output, Depth& depth);
108 void applyLocalStiffnessMatricesFixedDepth_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D);
109
110 double applyLocalStiffnessMatricesFixedDepthIndex_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D, IndexDimension& Index);
111
112 void applyLocalStiffnessMatricesOnIndices_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D, std::vector<IndexDimension>& Indices);
113
114 void receiveApplySend(int n);
115 virtual void receiveApplySendOnActiveWorkers();
116
117 void resetActiveWorkers();
118
119 bool distribute=true;
120
121
122 std::vector<int> active_worker;
123 int getNumberProcesses(){return number_processes;};
124
125 TypeMatrixVectorMultiplication getTypeMatrixVectorMultiplication(){return typeMatrixVectorMultiplication;};
126
127 int getNodeForDepth(Depth& T){return distributedDepthsHashtable.getNodeForDepth(T);};
128 int numbercells=0;
129
130 void releaseData(){
131 pairedDepthsLocalStiffnessMatrices.clear();
132 }
133
134 int size(){ return pairedDepthsLocalStiffnessMatrices.size();};
135
136
137
138 double getTime(){
139 double local_value = time;
140 double global_max;
141 MPI_Reduce(&local_value, &global_max, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
142 MPI_Bcast(&global_max, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
143
144
145 if(abs(time-global_max)<1e-5){
146 Tmax.Print();
147 cout << numbercells_max << " " << time << " " << global_max << endl;
148 }
149
150 local_value = time_min;
151 double global_min;
152 MPI_Reduce(&local_value, &global_min, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD);
153 MPI_Bcast(&global_min, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
154
155
156 if(abs(time_min-global_min)<1e-5){
157
158 cout << numbercells_min << " " << time_min << " " << global_min << endl;
159 }
160
161 return global_max;
162 }
163
164 Depth getDepth(){
165
166 return Tmax;};
167
168 double time = 0.0;
169 double time_min =1e+10;
170 int numbercells_max=0;
171 int numbercells_min=0;
172 Depth Tmax;
173
174 std::vector<std::pair<Depth,std::vector<LocalStiffnessMatrixFixedDepthSymmetric>>>* getPairedDepthsLocalStiffnessMatrices(){
175 return &pairedDepthsLocalStiffnessMatrices;
176 }
177protected:
178
179 StencilTemplate& stencilClass;
180 AdaptiveSparseGrid& grid;
181 LocalStiffnessMatrices(AdaptiveSparseGrid& sg, StencilTemplate& stencilClass_, int number_processes_):grid(sg), depthList(sg),stencilClass(stencilClass_), cellData(sg),input_node(sg),output_node(sg),number_processes(number_processes_){
182/* int rank;
183 int size;
184 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
185 MPI_Comm_size(MPI_COMM_WORLD, &size);*/
186
187
188 } ;
189
190 int number_processes=1;
191 void printEstimatedStorage();
192 TypeMatrixVectorMultiplication typeMatrixVectorMultiplication = StoreLocalStiffnessMatrix;
193
194
195
196 DepthList depthList;
197 CellData cellData;
198 DistributedDepthsHashtable distributedDepthsHashtable;
199
200 VectorSparseG input_node,output_node;
201
202
203
204 class LocalStiffnessMatrixFixedDepthSymmetric{
205 friend LocalStiffnessMatrices;
206 public:
207 LocalStiffnessMatrixFixedDepthSymmetric( AdaptiveSparseGrid& grid_,StencilTemplate& stencilTemplate1):grid(grid_),stencilTemplate(stencilTemplate1){ };
208 LocalStiffnessMatrixFixedDepthSymmetric(CellDimension& cell, AdaptiveSparseGrid& grid_, StencilTemplate& stencilTemplate_) :stencilTemplate(stencilTemplate_),grid(grid_){
209 setValues(cell, stencilTemplate_);
210 cellDimension=cell;
211
212 }
213
214 // Copy assignment operator
215 LocalStiffnessMatrixFixedDepthSymmetric& operator=(const LocalStiffnessMatrixFixedDepthSymmetric& other) {
216 if (this == &other) {
217 return *this; // Handle self-assignment
218 }
219 for(int i=0; i <TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; i++ ){
220 occupied[i]=other.occupied[i];
221 entries[i]=other.entries[i];
222 storage_q[i]=other.storage_q[i];
223 storage_p[i]=other.storage_p[i];
224 }
225 return *this;
226 }
227
228
229 // add operator
230 void operator+=(const LocalStiffnessMatrixFixedDepthSymmetric& other) {
231
232 for(int i=0; i <TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; i++ ){
233 if(occupied[i]) {
234 entries[i] += other.entries[i];
235
236 if (storage_q[i] != other.storage_q[i]) {
237 cout << " LocalStiffnessMatrixFixedDepthSymmetric : added wrong values " << storage_q[i] << " "
238 << other.storage_q[i] << endl;
239 std::terminate();
240 }
241 }
242 }
243
244 }
245
246 bool operator==(const LocalStiffnessMatrixFixedDepthSymmetric& other) {
247
248 bool retbool=true;
249 for(int i=0; i <TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; i++ ){
250 if(occupied[i]) {
251 if(entries[i] != other.entries[i]){
252
253 cellDimension.PrintCoordinates();
254
255 retbool=false;
256 break;
257 }
258 }
259 }
260 return retbool;
261
262 }
263
264 CellDimension* getCell(){return &cellDimension;};
265
266
267 int array_size=TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; // Declaration of static member
268
269 bool occupied[TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value];
270 double entries[TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value];
271 unsigned long storage_p[TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value];
272 unsigned long storage_q[TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value];
273
274
275
276
277 AdaptiveSparseGrid& grid;
278 StencilTemplate& stencilTemplate;
279 CellDimension cellDimension;
280
281
282
283 void setValues(CellDimension& cell);
284
285 void setValues(CellDimension& cell, StencilTemplate& stencil);
286
287 void applyLocalMatrix(VectorSparseG& input, VectorSparseG& output);
288
289 void applyLocalMatrixIndex(VectorSparseG& input, VectorSparseG& output,IndexDimension& Index);
290
291 bool indexInMatrix(IndexDimension& Index);;
292
293
294
295 };
296
297
304std::vector<std::pair<Depth,std::vector<LocalStiffnessMatrixFixedDepthSymmetric>>> pairedDepthsLocalStiffnessMatrices;
305};
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321#endif //RUN_LOCALSTIFFNESSMATRICES_H
Definition sparseGrid.h:277
Definition index.h:356