2019-12-03 07:10:21 -06:00
|
|
|
/*
|
2019-12-05 07:24:37 -06:00
|
|
|
Copyright 2019 Equinor ASA
|
2019-12-03 07:10:21 -06:00
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-12-18 08:47:35 -06:00
|
|
|
#ifndef OPM_CUSPARSESOLVER_BACKEND_HEADER_INCLUDED
|
|
|
|
#define OPM_CUSPARSESOLVER_BACKEND_HEADER_INCLUDED
|
2019-12-03 07:10:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
#include "cublas_v2.h"
|
|
|
|
#include "cusparse_v2.h"
|
|
|
|
|
2020-06-22 11:26:49 -05:00
|
|
|
#include <opm/simulators/linalg/bda/BdaResult.hpp>
|
|
|
|
#include <opm/simulators/linalg/bda/BdaSolver.hpp>
|
2020-03-13 08:21:59 -05:00
|
|
|
#include <opm/simulators/linalg/bda/WellContributions.hpp>
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2021-10-25 04:08:06 -05:00
|
|
|
namespace Opm
|
|
|
|
{
|
|
|
|
namespace Accelerator
|
2019-12-03 07:10:21 -06:00
|
|
|
{
|
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// This class implements a cusparse-based ilu0-bicgstab solver on GPU
|
2020-06-24 13:09:14 -05:00
|
|
|
template <unsigned int block_size>
|
|
|
|
class cusparseSolverBackend : public BdaSolver<block_size> {
|
|
|
|
|
|
|
|
typedef BdaSolver<block_size> Base;
|
|
|
|
|
|
|
|
using Base::N;
|
|
|
|
using Base::Nb;
|
|
|
|
using Base::nnz;
|
|
|
|
using Base::nnzb;
|
|
|
|
using Base::verbosity;
|
2020-07-01 12:43:22 -05:00
|
|
|
using Base::deviceID;
|
2020-06-24 13:09:14 -05:00
|
|
|
using Base::maxit;
|
|
|
|
using Base::tolerance;
|
|
|
|
using Base::initialized;
|
2019-12-03 07:10:21 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
cublasHandle_t cublasHandle;
|
|
|
|
cusparseHandle_t cusparseHandle;
|
|
|
|
cudaStream_t stream;
|
|
|
|
cusparseMatDescr_t descr_B, descr_M, descr_L, descr_U;
|
|
|
|
bsrilu02Info_t info_M;
|
|
|
|
bsrsv2Info_t info_L, info_U;
|
|
|
|
// b: bsr matrix, m: preconditioner
|
|
|
|
double *d_bVals, *d_mVals;
|
|
|
|
int *d_bCols, *d_mCols;
|
|
|
|
int *d_bRows, *d_mRows;
|
2020-06-22 11:26:49 -05:00
|
|
|
double *d_x, *d_b, *d_r, *d_rw, *d_p; // vectors, used during linear solve
|
2019-12-03 07:10:21 -06:00
|
|
|
double *d_pw, *d_s, *d_t, *d_v;
|
|
|
|
void *d_buffer;
|
2020-06-22 11:26:49 -05:00
|
|
|
double *vals_contiguous; // only used if COPY_ROW_BY_ROW is true in cusparseSolverBackend.cpp
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-06 04:05:41 -06:00
|
|
|
bool analysis_done = false;
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2022-03-10 04:07:03 -06:00
|
|
|
bool useJacMatrix = false;
|
|
|
|
int nnzbs_prec; // number of nonzero blocks in the matrix for preconditioner
|
|
|
|
// could be jacMatrix or matrix
|
2019-12-05 02:44:55 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Solve linear system using ilu0-bicgstab
|
2020-03-13 08:21:59 -05:00
|
|
|
/// \param[in] wellContribs contains all WellContributions, to apply them separately, instead of adding them to matrix A
|
|
|
|
/// \param[inout] res summary of solver result
|
|
|
|
void gpu_pbicgstab(WellContributions& wellContribs, BdaResult& res);
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Initialize GPU and allocate memory
|
2022-03-10 04:07:03 -06:00
|
|
|
/// \param[in] matrix matrix for spmv
|
|
|
|
/// \param[in] jacMatrix matrix for preconditioner
|
|
|
|
void initialize(std::shared_ptr<BlockedMatrix> matrix, std::shared_ptr<BlockedMatrix> jacMatrix);
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Clean memory
|
2019-12-03 07:10:21 -06:00
|
|
|
void finalize();
|
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Copy linear system to GPU
|
2022-03-10 04:07:03 -06:00
|
|
|
/// also copy matrix for preconditioner if needed
|
|
|
|
/// \param[in] matrix matrix for spmv
|
|
|
|
/// \param[in] b input vector, contains N values
|
|
|
|
/// \param[in] jacMatrix matrix for preconditioner
|
|
|
|
void copy_system_to_gpu(std::shared_ptr<BlockedMatrix> matrix, double *b, std::shared_ptr<BlockedMatrix> jacMatrix);
|
|
|
|
|
|
|
|
/// Update linear system on GPU, don't copy rowpointers and colindices, they stay the same
|
|
|
|
/// also copy matrix for preconditioner if needed
|
|
|
|
/// \param[in] matrix matrix for spmv
|
|
|
|
/// \param[in] b input vector, contains N values
|
|
|
|
/// \param[in] jacMatrix matrix for preconditioner
|
|
|
|
void update_system_on_gpu(std::shared_ptr<BlockedMatrix> matrix, double *b, std::shared_ptr<BlockedMatrix> jacMatrix);
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Analyse sparsity pattern to extract parallelism
|
2019-12-18 10:09:33 -06:00
|
|
|
/// \return true iff analysis was successful
|
2019-12-06 04:05:41 -06:00
|
|
|
bool analyse_matrix();
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Perform ilu0-decomposition
|
2019-12-18 10:09:33 -06:00
|
|
|
/// \return true iff decomposition was successful
|
2019-12-03 07:10:21 -06:00
|
|
|
bool create_preconditioner();
|
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Solve linear system
|
2020-03-13 08:21:59 -05:00
|
|
|
/// \param[in] wellContribs contains all WellContributions, to apply them separately, instead of adding them to matrix A
|
|
|
|
/// \param[inout] res summary of solver result
|
|
|
|
void solve_system(WellContributions& wellContribs, BdaResult &res);
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-06 04:05:41 -06:00
|
|
|
public:
|
2019-12-03 07:10:21 -06:00
|
|
|
|
2019-12-06 04:05:41 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Construct a cusparseSolver
|
|
|
|
/// \param[in] linear_solver_verbosity verbosity of cusparseSolver
|
|
|
|
/// \param[in] maxit maximum number of iterations for cusparseSolver
|
|
|
|
/// \param[in] tolerance required relative tolerance for cusparseSolver
|
2020-07-01 12:43:22 -05:00
|
|
|
/// \param[in] deviceID the device to be used
|
|
|
|
cusparseSolverBackend(int linear_solver_verbosity, int maxit, double tolerance, unsigned int deviceID);
|
2019-12-06 04:05:41 -06:00
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Destroy a cusparseSolver, and free memory
|
2019-12-06 04:05:41 -06:00
|
|
|
~cusparseSolverBackend();
|
|
|
|
|
2019-12-18 10:05:33 -06:00
|
|
|
/// Solve linear system, A*x = b, matrix A must be in blocked-CSR format
|
2022-02-07 06:39:38 -06:00
|
|
|
/// \param[in] matrix matrix A
|
2020-03-13 08:21:59 -05:00
|
|
|
/// \param[in] b input vector, contains N values
|
2022-02-23 09:28:37 -06:00
|
|
|
/// \param[in] jacMatrix matrix for preconditioner
|
2020-03-13 08:21:59 -05:00
|
|
|
/// \param[in] wellContribs contains all WellContributions, to apply them separately, instead of adding them to matrix A
|
|
|
|
/// \param[inout] res summary of solver result
|
|
|
|
/// \return status code
|
2022-02-23 09:28:37 -06:00
|
|
|
SolverStatus solve_system(std::shared_ptr<BlockedMatrix> matrix, double *b,
|
|
|
|
std::shared_ptr<BlockedMatrix> jacMatrix, WellContributions& wellContribs, BdaResult &res) override;
|
2022-04-21 10:18:32 -05:00
|
|
|
|
2020-06-22 11:26:49 -05:00
|
|
|
/// Get resulting vector x after linear solve, also includes post processing if necessary
|
2019-12-18 10:05:33 -06:00
|
|
|
/// \param[inout] x resulting x vector, caller must guarantee that x points to a valid array
|
2020-06-22 11:26:49 -05:00
|
|
|
void get_result(double *x) override;
|
2019-12-03 07:10:21 -06:00
|
|
|
|
|
|
|
}; // end class cusparseSolverBackend
|
|
|
|
|
2021-10-25 04:08:06 -05:00
|
|
|
} // namespace Accelerator
|
|
|
|
} // namespace Opm
|
2019-12-03 07:10:21 -06:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|