opm-simulators
GpuBridge.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 GPUBRIDGE_HEADER_INCLUDED
21 #define GPUBRIDGE_HEADER_INCLUDED
22 
23 #include "dune/istl/solver.hh" // for struct InverseOperatorResult
24 
25 #include <opm/simulators/linalg/gpubridge/GpuSolver.hpp>
26 
27 namespace Opm
28 {
29 
30 template<class Scalar> class WellContributions;
31 
32 typedef Dune::InverseOperatorResult InverseOperatorResult;
33 
35 template <class BridgeMatrix, class BridgeVector, int block_size>
36 class GpuBridge
37 {
38 private:
39  using Scalar = typename BridgeVector::field_type;
40  int verbosity = 0;
41  bool use_gpu = false;
42  std::string accelerator_mode;
43  std::unique_ptr<Accelerator::GpuSolver<Scalar,block_size>> backend;
44  std::shared_ptr<Accelerator::BlockedMatrix<Scalar>> matrix; // 'stores' matrix, actually points to h_rows, h_cols and the received BridgeMatrix for the nonzeroes
45  std::shared_ptr<Accelerator::BlockedMatrix<Scalar>> jacMatrix; // 'stores' preconditioner matrix, actually points to h_rows, h_cols and the received BridgeMatrix for the nonzeroes
46  std::vector<int> h_rows, h_cols; // store the sparsity pattern of the matrix
47  std::vector<int> h_jacRows, h_jacCols; // store the sparsity pattern of the jacMatrix
48  std::vector<typename BridgeMatrix::size_type> diagIndices; // contains offsets of the diagonal blocks wrt start of the row, used for replaceZeroDiagonal()
49  std::vector<typename BridgeMatrix::size_type> jacDiagIndices; // same but for jacMatrix
50 
51 public:
61  GpuBridge(std::string accelerator_mode,
62  int linear_solver_verbosity,
63  int maxit,
64  Scalar tolerance,
65  unsigned int platformID,
66  unsigned int deviceID,
67  bool opencl_ilu_parallel,
68  std::string linsolver);
69 
70 
79  void solve_system(BridgeMatrix* bridgeMat,
80  BridgeMatrix* jacMat,
81  int numJacobiBlocks,
82  BridgeVector& b,
83  WellContributions<Scalar>& wellContribs,
84  InverseOperatorResult &result);
85 
88  void get_result(BridgeVector &x);
89 
92  bool getUseGpu()
93  {
94  return use_gpu;
95  }
96 
101  static void copySparsityPatternFromISTL(const BridgeMatrix& mat,
102  std::vector<int>& h_rows,
103  std::vector<int>& h_cols);
104 
109  void initWellContributions(WellContributions<Scalar>& wellContribs, unsigned N);
110 
112  std::string getAccleratorName()
113  {
114  return accelerator_mode;
115  }
116 }; // end class GpuBridge
117 
118 }
119 
120 #endif
GpuBridge(std::string accelerator_mode, int linear_solver_verbosity, int maxit, Scalar tolerance, unsigned int platformID, unsigned int deviceID, bool opencl_ilu_parallel, std::string linsolver)
Construct a GpuBridge.
Definition: GpuBridge.cpp:66
bool getUseGpu()
Return whether the GpuBridge will use the GPU or not return whether the GpuBridge will use the GPU or...
Definition: GpuBridge.hpp:92
void initWellContributions(WellContributions< Scalar > &wellContribs, unsigned N)
Initialize the WellContributions object with opencl context and queue those must be set before callin...
Definition: GpuBridge.cpp:343
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
static void copySparsityPatternFromISTL(const BridgeMatrix &mat, std::vector< int > &h_rows, std::vector< int > &h_cols)
Store sparsity pattern into vectors.
Definition: GpuBridge.cpp:182
This class serves to eliminate the need to include the WellContributions into the matrix (with –matr...
Definition: GpuBridge.hpp:30
void get_result(BridgeVector &x)
Get the resulting x vector.
Definition: GpuBridge.cpp:334
GpuBridge acts as interface between opm-simulators with the GpuSolvers.
Definition: GpuBridge.hpp:36
std::string getAccleratorName()
Return the selected accelerator mode, this is input via the command-line.
Definition: GpuBridge.hpp:112
void solve_system(BridgeMatrix *bridgeMat, BridgeMatrix *jacMat, int numJacobiBlocks, BridgeVector &b, WellContributions< Scalar > &wellContribs, InverseOperatorResult &result)
Solve linear system, A*x = b.
Definition: GpuBridge.cpp:231