LoAdSG
LocalStiffnessMatricesFixedDistribution.h
1//
2// Created by to35jepo on 7/12/24.
3//
4
5#ifndef RUN_LOCALSTIFFNESSMATRICESFIXEDDISTRIBUTION_H
6#define RUN_LOCALSTIFFNESSMATRICESFIXEDDISTRIBUTION_H
7
8
9#include <utility>
10
11#include "LocalStiffnessMatrices.h"
12
13class LocalStiffnessMatricesFixedDistribution : public LocalStiffnessMatrices{
14
15
16public:
17 LocalStiffnessMatricesFixedDistribution(AdaptiveSparseGrid &sg, StencilTemplate& stencilClass, int number_processes_, DistributedDepthsHashtable distributedDepthsHashtable_ )
18 :LocalStiffnessMatrices(sg, stencilClass, number_processes_){
19 distributedDepthsHashtable = distributedDepthsHashtable_;
20 for (auto it = depthList.begin_all(); it != depthList.end_all(); ++it) {
21 Depth T = *it;
22
23 int node = distributedDepthsHashtable.getNodeForDepth(T);
24 int node_vergleich = distributedDepthsHashtable_.getNodeForDepth(T);
25 if (node != node_vergleich) {
26 cout << " err " << endl;
27 exit(1);
28 }
29 }
30
31 int num_tasks = 1;
32 int rank = 0;
33
34#ifdef MY_MPI_ON
35 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
36 MPI_Comm_size(MPI_COMM_WORLD, &num_tasks);
37#else
38 cout << "USE LOCALSTIFFNESSMATRICES ONLY WITH MPI" << endl;
39 //exit(1);
40#endif
41
42 for (auto it = depthList.begin_all(); it != depthList.end_all(); ++it){
43 Depth T = *it;
44
45 int node = distributedDepthsHashtable.getNodeForDepth(T);
46 int node_vergleich = distributedDepthsHashtable_.getNodeForDepth(T);
47 if (node != node_vergleich) {
48 cout << " err " << endl;
49 exit(1);
50 }
51
52 if (rank == node) {
53
54 stencilClass.initialize(T);
55
56 Depth Tcell = T;
57 ++Tcell;
58
59
60
61
62 const SingleDepthHashCellStructure &depthGrid = cellData.getGridForDepth(Tcell);
63 const auto &map = depthGrid._map;
64 const auto end = depthGrid.getNumberOfEntries();
65
66
67
68 std::vector<LocalStiffnessMatrixFixedDepthSymmetric> vector;
69
70#pragma omp parallel for schedule(dynamic)
71 for (size_t i = 0; i < end; i++) {
72 CellDimension cellDimension = map.getIndexOfTable(i);
73 LocalStiffnessMatrixFixedDepthSymmetric localStiffnessMatrixFixedDepthSymmetric(cellDimension, sg, stencilClass);
74 numbercells++;
75
76
77#pragma omp critical
78 vector.push_back(localStiffnessMatrixFixedDepthSymmetric);
79 }
80 pairedDepthsLocalStiffnessMatrices.push_back(std::make_pair(T, vector));
81
82
83 }
84
85 }
86
87 }
88
89 void operator+=(LocalStiffnessMatrices& localStiffnessMatrices){
90 for(auto& item : pairedDepthsLocalStiffnessMatrices){
91 Depth T = item.first;
92
93 auto it = std::find_if(
94 localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->begin(),
95 localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->end(),
96 [&T](const std::pair<Depth,std::vector<LocalStiffnessMatrices::LocalStiffnessMatrixFixedDepthSymmetric>>& pair) {
97 return pair.first == T;
98 }
99 );
100
101 if (it != localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->end()) {
102 // Found the matching entry
103 std::vector<LocalStiffnessMatrices::LocalStiffnessMatrixFixedDepthSymmetric> &matrices_add = it->second;
104
105 for(auto& matrix : item.second){
106 for (auto &matrix_add: matrices_add){
107 CellDimension cellCompare=*matrix.getCell();
108 if(cellCompare==*matrix_add.getCell()){
109 matrix+=matrix_add;
110 }
111 }
112 }
113 }
114 }
115 }
116
117 LocalStiffnessMatrices& operator+(LocalStiffnessMatrices& localStiffnessMatrices){
118 for(auto& item : pairedDepthsLocalStiffnessMatrices){
119 Depth T = item.first;
120
121 auto it = std::find_if(
122 localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->begin(),
123 localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->end(),
124 [&T](const std::pair<Depth,std::vector<LocalStiffnessMatrices::LocalStiffnessMatrixFixedDepthSymmetric>>& pair) {
125 return pair.first == T;
126 }
127 );
128
129 if (it != localStiffnessMatrices.getPairedDepthsLocalStiffnessMatrices()->end()) {
130 // Found the matching entry
131 std::vector<LocalStiffnessMatrices::LocalStiffnessMatrixFixedDepthSymmetric> &matrices_add = it->second;
132
133 for(auto& matrix : item.second) {
134 for (auto &matrix_add: matrices_add){
135 CellDimension cellCompare=*matrix.getCell();
136 if(cellCompare==*matrix_add.getCell()){
137 matrix+=matrix_add;
138 }
139 }
140 }
141 }
142 }
143 return *this;
144 }
145};
146
147
148#endif //RUN_LOCALSTIFFNESSMATRICESFIXEDDISTRIBUTION_H
Definition sparseGrid.h:277