Peano
DataRepository.cpp
Go to the documentation of this file.
1 // This file is part of the ExaHyPE2 project. For conditions of distribution and
2 // use, please see the copyright notice at www.peano-framework.org
3 #include "DataRepository.h"
4 
5 #include "FVSolver.h"
6 #include "exahype2/fv/BoundaryConditions.h"
7 #include "peano4/utils/Linearised.h"
8 #include "peano4/utils/Loop.h"
9 
10 #include <algorithm>
11 #include <cmath>
12 #include <iterator>
13 
16  const tarch::la::Vector<DIMENSIONS, int>& cellIndex,
17  const tarch::la::Vector<DIMENSIONS, double>& cellSize
18  ) {
19  const double ratio = DomainSize[0] / cellSize[0];
20  const int depth = static_cast<int>(std::round(std::log(ratio) / std::log(3.0)));
21 
22  int index = 0;
23  int stride = 1;
24  for (int i = 0; i < DIMENSIONS; ++i) {
25  index += cellIndex[i] * stride;
26 
27  int dimSize = (i == 0) ? (std::pow(3, depth) + 1) : std::pow(3, depth);
28  stride *= dimSize;
29  }
30  return index;
31  }
32 
33  tarch::la::Vector<DIMENSIONS, int> DataRepository::Indexing::getFaceIndices(
34  const tarch::la::Vector<DIMENSIONS, double>& cellCentre,
35  const tarch::la::Vector<DIMENSIONS, double>& cellSize
36  ) {
37  tarch::la::Vector<DIMENSIONS, int> faceIndices;
38  tarch::la::Vector<DIMENSIONS, double> cellCentreOffset = cellCentre - DomainOffset; // remove Offset
39  tarch::la::Vector<DIMENSIONS, int> cellIndex = tarch::la::convertScalar<int>(
40  tarch::la::floor(tarch::la::divideComponents(cellCentreOffset, cellSize))
41  );
42  // vector: x_1, x_2, x_3
43  faceIndices[0] = linearise(cellIndex, cellSize);
44  // vector: x_2, x_1, x_3
45  std::swap(cellIndex[0], cellIndex[1]);
46  faceIndices[1] = linearise(cellIndex, cellSize);
47 #if DIMENSIONS == 3
48  // vector: x_3, x_1, x_2
49  std::swap(cellIndex[0], cellIndex[2]);
50  faceIndices[2] = linearise(cellIndex, cellSize);
51 #endif
52  return faceIndices;
53  }
54 
56  const tarch::la::Vector<DIMENSIONS, double>& cellCentre,
57  const tarch::la::Vector<DIMENSIONS, double>& cellSize,
58  int axis
59  ) {
60  tarch::la::Vector<DIMENSIONS, double> cellCentreOffset = cellCentre - DomainOffset; // remove Offset
61  tarch::la::Vector<DIMENSIONS, int> cellIndex = tarch::la::convertScalar<int>(
62  tarch::la::floor(tarch::la::divideComponents(cellCentreOffset, cellSize))
63  );
64  // permute as described in getFaceIndices()
65  for (int i = 1; i <= axis % DIMENSIONS; ++i) {
66  std::swap(cellIndex[0], cellIndex[i]);
67  }
68  int faceIndex = linearise(cellIndex, cellSize);
69  return axis >= DIMENSIONS ? faceIndex + 1 : faceIndex;
70  }
71 
73  int cellCount = std::pow(GridLength, DIMENSIONS);
74  int faceCount = std::pow(GridLength, DIMENSIONS - 1) * (GridLength + 1);
75  _depthLevel.Q.resize(cellCount);
76  for (int axis = 0; axis < DIMENSIONS; ++axis) {
77  _depthLevel.faces[axis].resize(faceCount);
78  _depthLevel.facesDevice[axis] = tarch::allocateMemory<double>(
79  faceCount * getFaceCardinality(),
80  tarch::MemoryLocation::Pinned
81  );
82  }
83  }
85  for (int axis = 0; axis < DIMENSIONS; ++axis) {
86  tarch::freeMemory(_depthLevel.facesDevice[axis], tarch::MemoryLocation::Pinned);
87  }
88  }
89 
91  static DataRepository instance;
92  return instance;
93  }
94 
95  int DataRepository::getFaceCardinality() { return facedata::FVSolverQNew::Cardinality; }
96 
97  int DataRepository::getCellQCardinality() { return celldata::FVSolverQ::Cardinality; }
98 
99  int DataRepository::getCellQReconstructedCardinality() { return celldata::FVSolverQReconstructed::Cardinality; }
100 
101  double* DataRepository::getCellQ(int index) { return _depthLevel.Q[index].data(); }
102 
104  const tarch::la::Vector<DIMENSIONS, double>& cellCentre,
105  const tarch::la::Vector<DIMENSIONS, double>& cellSize
106  ) {
107  tarch::la::Vector<DIMENSIONS, double> cellCentreOffset = cellCentre - DomainOffset; // remove Offset
108  tarch::la::Vector<DIMENSIONS, int> cellIndex = tarch::la::convertScalar<int>(
109  tarch::la::floor(tarch::la::divideComponents(cellCentreOffset, cellSize))
110  );
111  int index = peano4::utils::dLinearised(cellIndex, GridLength);
112  return getCellQ(index);
113  }
114 
115  double* DataRepository::getFaceQNew(int index, int axis) {
116  auto& faceData = _depthLevel.faces[axis % DIMENSIONS];
117  if (axis >= DIMENSIONS) {
118  // high face
119  return faceData[index + 1].data();
120  } else {
121  // low face
122  return faceData[index].data();
123  }
124  }
125 
127  const tarch::la::Vector<DIMENSIONS, double>& cellCentre,
128  const tarch::la::Vector<DIMENSIONS, double>& cellSize,
129  int axis
130  ) {
131  int index = Indexing::getFaceIndex(cellCentre, cellSize, axis);
132  return _depthLevel.faces[axis % DIMENSIONS][index].data();
133  }
134 
136  const tarch::la::Vector<DIMENSIONS, double>& cellCentre,
137  const tarch::la::Vector<DIMENSIONS, double>& cellSize,
138  int axis
139  ) {
140  int index = Indexing::getFaceIndex(cellCentre, cellSize, axis);
141  return _depthLevel.facesDevice[axis % DIMENSIONS] + index * getFaceCardinality();
142  }
143 
145  int faceCount = std::pow(GridLength, DIMENSIONS - 1) * (GridLength + 1);
146  for (int axis = 0; axis < DIMENSIONS; ++axis) {
147  tarch::copyTo<double, double>(
148  _depthLevel.facesDevice[axis],
149  _depthLevel.faces[axis][0].data(),
150  faceCount * getFaceCardinality(),
151  tarch::MemoryLocation::Pinned
152  );
153  }
154  }
155 
157  int faceCount = std::pow(GridLength, DIMENSIONS - 1) * (GridLength + 1);
158  for (int axis = 0; axis < DIMENSIONS; ++axis) {
159  tarch::copyFrom<double, double>(
160  _depthLevel.facesDevice[axis],
161  _depthLevel.faces[axis][0].data(),
162  faceCount * getFaceCardinality(),
163  tarch::MemoryLocation::Pinned
164  );
165  }
166  }
167 } // namespace benchmarks::exahype2::kernelbenchmarks
const tarch::la::Vector< DIMENSIONS, double > cellSize
A singleton repository to manage and provide access to grid cell and face data.
double * getFaceQNewDevice(const tarch::la::Vector< DIMENSIONS, double > &cellCentre, const tarch::la::Vector< DIMENSIONS, double > &cellSize, int axis)
int depth
Definition: acoustic.py:29
std::array< std::vector< FaceQNew >, DIMENSIONS > faces
static tarch::la::Vector< DIMENSIONS, int > getFaceIndices(const tarch::la::Vector< DIMENSIONS, double > &cellCentre, const tarch::la::Vector< DIMENSIONS, double > &cellSize)
Calculates the indices of the lower faces of a cell along along all axis for x axis: x_1 + x_2 * (3^D...
static int linearise(const tarch::la::Vector< DIMENSIONS, int > &cellCentre, const tarch::la::Vector< DIMENSIONS, double > &cellSize)
Calculates the linearised index of the lower face of a cell along one axis (along one axis means the ...
static int getFaceIndex(const tarch::la::Vector< DIMENSIONS, double > &cellCentre, const tarch::la::Vector< DIMENSIONS, double > &cellSize, int axis)