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 #if CHOW_PATEL
59  std::unique_ptr<BlockedMatrix> Lmat = nullptr, Umat = nullptr;
60 #endif
61  std::vector<double> invDiagVals;
62  std::vector<int> diagIndex;
63  std::vector<int> rowsPerColor; // color i contains rowsPerColor[i] rows, which are processed in parallel
64  std::vector<int> rowsPerColorPrefix; // the prefix sum of rowsPerColor
65  std::vector<int> toOrder, fromOrder;
66  int numColors;
67  std::once_flag pattern_uploaded;
68 
69  ILUReorder opencl_ilu_reorder;
70 
71  typedef struct {
72  cl::Buffer invDiagVals;
73  cl::Buffer diagIndex;
74  cl::Buffer rowsPerColor;
75 #if CHOW_PATEL
76  cl::Buffer Lvals, Lcols, Lrows;
77  cl::Buffer Uvals, Ucols, Urows;
78 #else
79  cl::Buffer LUvals, LUcols, LUrows;
80 #endif
81  } GPU_storage;
82 
83  GPU_storage s;
84 
85 #if CHOW_PATEL
86  ChowPatelIlu<block_size> chowPatelIlu;
87 #endif
88 
89 public:
90 
91  BILU0(ILUReorder opencl_ilu_reorder, int verbosity);
92 
93  // analysis, find reordering if specified
94  bool analyze_matrix(BlockedMatrix *mat) override;
95 
96  // ilu_decomposition
97  bool create_preconditioner(BlockedMatrix *mat) override;
98 
99  // apply preconditioner, x = prec(y)
100  void apply(const cl::Buffer& y, cl::Buffer& x) override;
101 
102  int* getToOrder() override
103  {
104  return toOrder.data();
105  }
106 
107  int* getFromOrder() override
108  {
109  return fromOrder.data();
110  }
111 
112  BlockedMatrix* getRMat() override
113  {
114  return rmat.get();
115  }
116 
117  std::tuple<std::vector<int>, std::vector<int>, std::vector<int>> get_preconditioner_structure()
118  {
119  return {{LUmat->rowPointers, LUmat->rowPointers + (Nb + 1)}, {LUmat->colIndices, LUmat->colIndices + nnzb}, diagIndex};
120  }
121 
122  std::pair<cl::Buffer, cl::Buffer> get_preconditioner_data()
123  {
124 #if CHOW_PATEL
125  return std::make_pair(s.Lvals, s.invDiagVals); // send dummy, BISAI is disabled when ChowPatel is selected
126 #else
127  return std::make_pair(s.LUvals, s.invDiagVals);
128 #endif
129  }
130 
131 };
132 
133 } // namespace Accelerator
134 } // namespace Opm
135 
136 #endif
137 
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