5#ifndef RUN_LOCALSTIFFNESSMATRICES_H
6#define RUN_LOCALSTIFFNESSMATRICES_H
10#include <unordered_set>
12#include "../stencils/Stencil.h"
13#include "../cells/CellStructure.h"
16double bytesToGigabytes(
unsigned long long bytes);
24 bool operator>(
const Container &other)
const {
25 return totalSize > other.totalSize;
27 std::vector<std::pair<Depth,int>> boxes;
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);
47 bool operator()(
const Depth& lhs,
const Depth& rhs)
const {
49 for(
int d=0; d<DimensionSparseGrid; d++){
50 if(lhs.at(d)!=rhs.at(d))
return false;
57class DistributedDepthsHashtable{
58 std::unordered_map<Depth,int, DepthHash, DepthEqual> _map;
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});};
66 std::unordered_map<Depth,int, DepthHash, DepthEqual>* getMap(){
74void distribute_LocalStiffnessMatrices(
int numberofprocesses,
AdaptiveSparseGrid &sg, Container* containers_sorted);
81class LocalStiffnessMatrices {
84 class LocalStiffnessMatrixFixedDepthSymmetric;
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;};
101 virtual void applyLocalStiffnessMatricesDepth(VectorSparseG& input, VectorSparseG& output, Depth& depth);
103 void applyLocalStiffnessMatricesIndex(VectorSparseG& input, VectorSparseG& output, Depth& depth,IndexDimension& Index){
107 void applyLocalStiffnessMatricesFixedDepth(VectorSparseG& input, VectorSparseG& output, Depth& depth);
108 void applyLocalStiffnessMatricesFixedDepth_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D);
110 double applyLocalStiffnessMatricesFixedDepthIndex_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D, IndexDimension& Index);
112 void applyLocalStiffnessMatricesOnIndices_onNode(VectorSparseG& input, VectorSparseG& output, Depth& D, std::vector<IndexDimension>& Indices);
114 void receiveApplySend(
int n);
115 virtual void receiveApplySendOnActiveWorkers();
117 void resetActiveWorkers();
119 bool distribute=
true;
122 std::vector<int> active_worker;
123 int getNumberProcesses(){
return number_processes;};
125 TypeMatrixVectorMultiplication getTypeMatrixVectorMultiplication(){
return typeMatrixVectorMultiplication;};
127 int getNodeForDepth(Depth& T){
return distributedDepthsHashtable.getNodeForDepth(T);};
131 pairedDepthsLocalStiffnessMatrices.clear();
134 int size(){
return pairedDepthsLocalStiffnessMatrices.size();};
139 double local_value = time;
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);
145 if(abs(time-global_max)<1e-5){
147 cout << numbercells_max <<
" " << time <<
" " << global_max << endl;
150 local_value = time_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);
156 if(abs(time_min-global_min)<1e-5){
158 cout << numbercells_min <<
" " << time_min <<
" " << global_min << endl;
169 double time_min =1e+10;
170 int numbercells_max=0;
171 int numbercells_min=0;
174 std::vector<std::pair<Depth,std::vector<LocalStiffnessMatrixFixedDepthSymmetric>>>* getPairedDepthsLocalStiffnessMatrices(){
175 return &pairedDepthsLocalStiffnessMatrices;
179 StencilTemplate& stencilClass;
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_){
190 int number_processes=1;
191 void printEstimatedStorage();
192 TypeMatrixVectorMultiplication typeMatrixVectorMultiplication = StoreLocalStiffnessMatrix;
198 DistributedDepthsHashtable distributedDepthsHashtable;
200 VectorSparseG input_node,output_node;
204 class LocalStiffnessMatrixFixedDepthSymmetric{
205 friend LocalStiffnessMatrices;
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_);
215 LocalStiffnessMatrixFixedDepthSymmetric& operator=(
const LocalStiffnessMatrixFixedDepthSymmetric& other) {
216 if (
this == &other) {
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];
230 void operator+=(
const LocalStiffnessMatrixFixedDepthSymmetric& other) {
232 for(
int i=0; i <TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; i++ ){
234 entries[i] += other.entries[i];
236 if (storage_q[i] != other.storage_q[i]) {
237 cout <<
" LocalStiffnessMatrixFixedDepthSymmetric : added wrong values " << storage_q[i] <<
" "
238 << other.storage_q[i] << endl;
246 bool operator==(
const LocalStiffnessMatrixFixedDepthSymmetric& other) {
249 for(
int i=0; i <TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value; i++ ){
251 if(entries[i] != other.entries[i]){
253 cellDimension.PrintCoordinates();
264 CellDimension* getCell(){
return &cellDimension;};
267 int array_size=TriangularNumber<PowerOfTwo<DimensionSparseGrid>::value>::value;
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];
278 StencilTemplate& stencilTemplate;
279 CellDimension cellDimension;
283 void setValues(CellDimension& cell);
285 void setValues(CellDimension& cell, StencilTemplate& stencil);
287 void applyLocalMatrix(VectorSparseG& input, VectorSparseG& output);
289 void applyLocalMatrixIndex(VectorSparseG& input, VectorSparseG& output,IndexDimension& Index);
291 bool indexInMatrix(IndexDimension& Index);;
304std::vector<std::pair<Depth,std::vector<LocalStiffnessMatrixFixedDepthSymmetric>>> pairedDepthsLocalStiffnessMatrices;
Definition sparseGrid.h:277