My Project
BlockedMatrix.hpp
1 /*
2  Copyright 2019 Equinor ASA
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef BLOCKED_MATRIX_HPP
21 #define BLOCKED_MATRIX_HPP
22 
23 #if HAVE_FPGA
24 #include <vector>
25 #endif
26 
27 #include <opm/simulators/linalg/bda/FPGAMatrix.hpp>
28 
29 namespace bda
30 {
31 
34 template<unsigned int block_size>
36 {
37 
38 public:
39 
43  BlockedMatrix(int Nb_, int nnzbs_)
44  : nnzValues(new double[nnzbs_*block_size*block_size]),
45  colIndices(new int[nnzbs_*block_size*block_size]),
46  rowPointers(new int[Nb_+1]),
47  Nb(Nb_),
48  nnzbs(nnzbs_),
49  deleteNnzs(true),
50  deleteSparsity(true)
51  {}
52 
56  : nnzValues(new double[M.nnzbs*block_size*block_size]),
57  colIndices(M.colIndices),
58  rowPointers(M.rowPointers),
59  Nb(M.Nb),
60  nnzbs(M.nnzbs),
61  deleteNnzs(true),
62  deleteSparsity(false)
63  {}
64 
71  BlockedMatrix(int Nb_, int nnzbs_, double *nnzValues_, int *colIndices_, int *rowPointers_)
72  : nnzValues(nnzValues_),
73  colIndices(colIndices_),
74  rowPointers(rowPointers_),
75  Nb(Nb_),
76  nnzbs(nnzbs_),
77  deleteNnzs(false),
78  deleteSparsity(false)
79  {}
80 
81  ~BlockedMatrix(){
82  if (deleteNnzs) {
83  delete[] nnzValues;
84  }
85  if (deleteSparsity) {
86  delete[] colIndices;
87  delete[] rowPointers;
88  }
89  }
90 
91 #if HAVE_FPGA
92  constexpr static double nnzThreshold = 1e-80; // for unblocking, a nonzero must be bigger than this threshold to be considered a nonzero
93 
94  int countUnblockedNnzs();
95 
96  void unblock(Matrix *mat, bool isUMatrix);
97 
100  int toRDF(int numColors, int *nodesPerColor, bool isUMatrix,
101  std::vector<std::vector<int> >& colIndicesInColor, int nnzsPerRowLimit, int *nnzValsSizes,
102  std::vector<std::vector<double> >& nnzValues, short int *colIndices, unsigned char *NROffsets, int *colorSizes, int *valSize);
103 
106  int findPartitionColumns(int numColors, int *nodesPerColor,
107  int rowsPerColorLimit, int columnsPerColorLimit,
108  std::vector<std::vector<int> >& colIndicesInColor, int *PIndicesAddr, int *colorSizes,
109  std::vector<std::vector<int> >& LColIndicesInColor, int *LPIndicesAddr, int *LColorSizes,
110  std::vector<std::vector<int> >& UColIndicesInColor, int *UPIndicesAddr, int *UColorSizes);
111 #endif
112 
113  double *nnzValues;
114  int *colIndices;
115  int *rowPointers;
116  int Nb;
117  int nnzbs;
118  bool deleteNnzs;
119  bool deleteSparsity;
120 };
121 
122 
128 template <unsigned int block_size>
129 void sortBlockedRow(int *colIndices, double *data, int left, int right);
130 
136 template <unsigned int block_size>
137 void blockMultSub(double *a, double *b, double *c);
138 
143 template <unsigned int block_size>
144 void blockMult(double *mat1, double *mat2, double *resMat);
145 
146 
147 #if HAVE_FPGA
148 template <unsigned int block_size>
149 void blockSub(double *mat1, double *mat2, double *resMat);
150 
151 template <unsigned int block_size>
152 void blockVectMult(double *mat, double *vect, double scale, double *resVect, bool resetRes);
153 
164 void blockedDiagtoRDF(double *blockedDiagVals, int rowSize, int numColors, std::vector<int>& rowsPerColor, double *RDFDiag);
165 #endif
166 
167 } // end namespace bda
168 
169 #endif
This struct resembles a blocked csr matrix, like Dune::BCRSMatrix.
Definition: BlockedMatrix.hpp:36
BlockedMatrix(const BlockedMatrix &M)
Allocate BlockedMatrix, but copy sparsity pattern instead of allocating new memory.
Definition: BlockedMatrix.hpp:55
BlockedMatrix(int Nb_, int nnzbs_)
Allocate BlockedMatrix and data arrays with given sizes.
Definition: BlockedMatrix.hpp:43
BlockedMatrix(int Nb_, int nnzbs_, double *nnzValues_, int *colIndices_, int *rowPointers_)
Allocate BlockedMatrix, but let data arrays point to existing arrays.
Definition: BlockedMatrix.hpp:71