My Project
BILU0.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 BILU0_HPP
21 #define BILU0_HPP
22 
23 #include <mutex>
24 
25 #include <opm/simulators/linalg/bda/BlockedMatrix.hpp>
26 #include <opm/simulators/linalg/bda/ILUReorder.hpp>
27 
28 #include <opm/simulators/linalg/bda/opencl/opencl.hpp>
29 #include <opm/simulators/linalg/bda/opencl/Preconditioner.hpp>
30 #include <opm/simulators/linalg/bda/opencl/ChowPatelIlu.hpp>
31 
32 
33 namespace Opm
34 {
35 namespace Accelerator
36 {
37 
40 template <unsigned int block_size>
41 class BILU0 : public Preconditioner<block_size>
42 {
44 
45  using Base::N;
46  using Base::Nb;
47  using Base::nnz;
48  using Base::nnzb;
49  using Base::verbosity;
50  using Base::context;
51  using Base::queue;
52  using Base::events;
53  using Base::err;
54 
55 private:
56  std::unique_ptr<BlockedMatrix> LUmat = nullptr;
57  std::shared_ptr<BlockedMatrix> rmat = nullptr; // only used with PAR_SIM
58  std::shared_ptr<BlockedMatrix> rJacMat = nullptr;
59 #if CHOW_PATEL
60  std::unique_ptr<BlockedMatrix> Lmat = nullptr, Umat = nullptr;
61 #endif
62  std::vector<double> invDiagVals;
63  std::vector<int> diagIndex;
64  std::vector<int> rowsPerColor; // color i contains rowsPerColor[i] rows, which are processed in parallel
65  std::vector<int> rowsPerColorPrefix; // the prefix sum of rowsPerColor
66  std::vector<int> toOrder, fromOrder;
67  int numColors;
68  std::once_flag pattern_uploaded;
69 
70  ILUReorder opencl_ilu_reorder;
71 
72  std::vector<int> reordermappingNonzeroes; // maps nonzero blocks to new location in reordered matrix
73  std::vector<int> jacReordermappingNonzeroes; // same but for jacMatrix
74 
75  typedef struct {
76  cl::Buffer invDiagVals;
77  cl::Buffer diagIndex;
78  cl::Buffer rowsPerColor;
79 #if CHOW_PATEL
80  cl::Buffer Lvals, Lcols, Lrows;
81  cl::Buffer Uvals, Ucols, Urows;
82 #else
83  cl::Buffer LUvals, LUcols, LUrows;
84 #endif
85  } GPU_storage;
86 
87  GPU_storage s;
88 
89 #if CHOW_PATEL
90  ChowPatelIlu<block_size> chowPatelIlu;
91 #endif
92 
93 public:
94 
95  BILU0(ILUReorder opencl_ilu_reorder, int verbosity);
96 
97  // analysis, find reordering if specified
98  bool analyze_matrix(BlockedMatrix *mat) override;
99  bool analyze_matrix(BlockedMatrix *mat, BlockedMatrix *jacMat) override;
100 
101  // ilu_decomposition
102  bool create_preconditioner(BlockedMatrix *mat) override;
103  bool create_preconditioner(BlockedMatrix *mat, BlockedMatrix *jacMat) override;
104 
105  // apply preconditioner, x = prec(y)
106  void apply(const cl::Buffer& y, cl::Buffer& x) override;
107 
108  int* getToOrder() override
109  {
110  return toOrder.data();
111  }
112 
113  int* getFromOrder() override
114  {
115  return fromOrder.data();
116  }
117 
118  BlockedMatrix* getRMat() override
119  {
120  return rmat.get();
121  }
122 
123  BlockedMatrix* getRJacMat()
124  {
125  return rJacMat.get();
126  }
127 
128  std::tuple<std::vector<int>, std::vector<int>, std::vector<int>> get_preconditioner_structure()
129  {
130  return {{LUmat->rowPointers, LUmat->rowPointers + (Nb + 1)}, {LUmat->colIndices, LUmat->colIndices + nnzb}, diagIndex};
131  }
132 
133  std::pair<cl::Buffer, cl::Buffer> get_preconditioner_data()
134  {
135 #if CHOW_PATEL
136  return std::make_pair(s.Lvals, s.invDiagVals); // send dummy, BISAI is disabled when ChowPatel is selected
137 #else
138  return std::make_pair(s.LUvals, s.invDiagVals);
139 #endif
140  }
141 };
142 
143 } // namespace Accelerator
144 } // namespace Opm
145 
146 #endif
147 
This class implements a Blocked ILU0 preconditioner The decomposition is done on CPU,...
Definition: BILU0.hpp:42
This struct resembles a blocked csr matrix, like Dune::BCRSMatrix.
Definition: BlockedMatrix.hpp:37
Definition: Preconditioner.hpp:36
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: BlackoilPhases.hpp:27