opm-simulators
GpuSolver.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 OPM_GPUSOLVER_BACKEND_HEADER_INCLUDED
21 #define OPM_GPUSOLVER_BACKEND_HEADER_INCLUDED
22 
23 
24 #include <opm/simulators/linalg/gpubridge/GpuResult.hpp>
25 #include <opm/simulators/linalg/gpubridge/BlockedMatrix.hpp>
26 
27 #include <memory>
28 
29 namespace Opm {
30 
31 template<class Scalar> class WellContributions;
32 
33 namespace Accelerator {
34 
35 enum class SolverStatus {
36  GPU_SOLVER_SUCCESS,
37  GPU_SOLVER_ANALYSIS_FAILED,
38  GPU_SOLVER_CREATE_PRECONDITIONER_FAILED,
39  GPU_SOLVER_UNKNOWN_ERROR
40 };
41 
44 template<class Scalar, unsigned int block_size>
45 class GpuSolver
46 {
47 protected:
48  // verbosity
49  // 0: print nothing during solves, only when initializing
50  // 1: print number of iterations and final norm
51  // 2: also print norm each iteration
52  // 3: also print timings of different backend functions
53  int verbosity = 0;
54 
55  int maxit = 200;
56  Scalar tolerance = 1e-2;
57 
58  int N; // number of rows
59  int Nb; // number of blocked rows (Nb*block_size == N)
60  int nnz; // number of nonzeroes (scalars)
61  int nnzb; // number of nonzero blocks (nnzb*block_size*block_size == nnz)
62 
63  unsigned int platformID = 0; // ID of OpenCL platform to be used, only used by openclSolver now
64  unsigned int deviceID = 0; // ID of the device to be used
65 
66  bool initialized = false;
67 
68 public:
73  GpuSolver(int linear_solver_verbosity, int max_it, Scalar tolerance_)
74  : verbosity(linear_solver_verbosity)
75  , maxit(max_it)
76  , tolerance(tolerance_)
77  {}
78  GpuSolver(int linear_solver_verbosity, int max_it,
79  Scalar tolerance_, unsigned int deviceID_)
80  : verbosity(linear_solver_verbosity)
81  , maxit(max_it)
82  , tolerance(tolerance_)
83  , deviceID(deviceID_) {};
84  GpuSolver(int linear_solver_verbosity, int max_it,
85  double tolerance_, unsigned int platformID_,
86  unsigned int deviceID_)
87  : verbosity(linear_solver_verbosity)
88  , maxit(max_it)
89  , tolerance(tolerance_)
90  , platformID(platformID_)
91  , deviceID(deviceID_)
92  {}
93 
95  virtual ~GpuSolver() = default;
96 
98  virtual SolverStatus solve_system(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
99  Scalar* b,
100  std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
101  WellContributions<Scalar>& wellContribs,
102  GpuResult& res) = 0;
103 
104  virtual void get_result(Scalar* x) = 0;
105 }; // end class GpuSolver
106 
107 } // namespace Accelerator
108 } // namespace Opm
109 
110 #endif
virtual ~GpuSolver()=default
Define virtual destructor, so that the derivedclass destructor will be called.
virtual SolverStatus solve_system(std::shared_ptr< BlockedMatrix< Scalar >> matrix, Scalar *b, std::shared_ptr< BlockedMatrix< Scalar >> jacMatrix, WellContributions< Scalar > &wellContribs, GpuResult &res)=0
Define as pure virtual functions, so derivedclass must implement them.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
GpuSolver(int linear_solver_verbosity, int max_it, Scalar tolerance_)
Construct a GpuSolver.
Definition: GpuSolver.hpp:73
This class serves to simplify choosing between different backend solvers, such as cusparseSolver and ...
Definition: GpuSolver.hpp:45