Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preconditioner: add another template parameter for the apply constants #5899

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions opm/simulators/linalg/gpubridge/Preconditioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum PreconditionerType {

template<class Scalar> class BlockedMatrix;

template<class Scalar, unsigned int block_size>
template<class Scalar, unsigned int block_size, class ApplyScalar = Scalar>
class Preconditioner
{
protected:
Expand All @@ -46,25 +46,19 @@ class Preconditioner
int nnzb = 0; // number of blocks of the matrix
int verbosity = 0;

Preconditioner(int verbosity_) :
verbosity(verbosity_)
{};
Preconditioner(int verbosity_)
: verbosity(verbosity_)
{}

public:

virtual ~Preconditioner() = default;

static std::unique_ptr<Preconditioner> create(PreconditionerType type,
bool opencl_ilu_parallel,
int verbosity);

#if HAVE_OPENCL
// apply preconditioner, x = prec(y)
virtual void apply(const cl::Buffer& y, cl::Buffer& x) = 0;
#endif

// apply preconditioner, x = prec(y)
virtual void apply(Scalar& y, Scalar& x) = 0;
virtual void apply(const ApplyScalar& y, ApplyScalar& x) = 0;

// analyze matrix, e.g. the sparsity pattern
// probably only called once
Expand Down
1 change: 0 additions & 1 deletion opm/simulators/linalg/gpubridge/opencl/openclBILU0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class openclBILU0 : public openclPreconditioner<Scalar,block_size>
// via Lz = y
// and Ux = z
void apply(const cl::Buffer& y, cl::Buffer& x) override;
void apply(Scalar&, Scalar&) override {}

std::tuple<std::vector<int>, std::vector<int>, std::vector<int>>
get_preconditioner_structure()
Expand Down
1 change: 0 additions & 1 deletion opm/simulators/linalg/gpubridge/opencl/openclBISAI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class openclBISAI : public openclPreconditioner<Scalar,block_size>

// apply preconditioner, x = prec(y)
void apply(const cl::Buffer& y, cl::Buffer& x) override;
void apply(Scalar&, Scalar&) override {}
};

/// Similar function to csrPatternToCsc. It gives an offset map from CSR to CSC instead of the full CSR to CSC conversion.
Expand Down
1 change: 0 additions & 1 deletion opm/simulators/linalg/gpubridge/opencl/openclCPR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class openclCPR : public openclPreconditioner<Scalar,block_size>, public CprCrea
// applies blocked ilu0
// also applies amg for pressure component
void apply(const cl::Buffer& y, cl::Buffer& x) override;
void apply(Scalar&, Scalar&) override {}

bool create_preconditioner(BlockedMatrix<Scalar>* mat) override;
bool create_preconditioner(BlockedMatrix<Scalar>* mat,
Expand Down
16 changes: 4 additions & 12 deletions opm/simulators/linalg/gpubridge/opencl/openclPreconditioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Opm::Accelerator {
template<class Scalar> class BlockedMatrix;

template <class Scalar, unsigned int block_size>
class openclPreconditioner : public Preconditioner<Scalar, block_size>
class openclPreconditioner : public Preconditioner<Scalar, block_size, cl::Buffer>
{

protected:
Expand All @@ -37,9 +37,9 @@ class openclPreconditioner : public Preconditioner<Scalar, block_size>
std::vector<cl::Event> events;
cl_int err;

openclPreconditioner(int verbosity_) :
Preconditioner<Scalar, block_size>(verbosity_)
{};
openclPreconditioner(int verbosity_)
: Preconditioner<Scalar, block_size, cl::Buffer>(verbosity_)
{}

public:
virtual ~openclPreconditioner() = default;
Expand All @@ -48,14 +48,6 @@ class openclPreconditioner : public Preconditioner<Scalar, block_size>

// nested Preconditioners might need to override this
virtual void setOpencl(std::shared_ptr<cl::Context>& context, std::shared_ptr<cl::CommandQueue>& queue);

// apply preconditioner, x = prec(y)
virtual void apply(const cl::Buffer& y, cl::Buffer& x) = 0;

// create/update preconditioner, probably used every linear solve
// the version with two params can be overloaded, if not, it will default to using the one param version
virtual bool create_preconditioner(BlockedMatrix<Scalar> *mat) = 0;
virtual bool create_preconditioner(BlockedMatrix<Scalar> *mat, BlockedMatrix<Scalar> *jacMat) = 0;
};
} //namespace Opm

Expand Down
3 changes: 2 additions & 1 deletion opm/simulators/linalg/gpubridge/rocm/rocsparseBILU0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ update_system_on_gpu(Scalar *d_Avals) {

template <class Scalar, unsigned int block_size>
void rocsparseBILU0<Scalar, block_size>::
apply(Scalar& y, Scalar& x) {
apply(const Scalar& y, Scalar& x)
{
Scalar one = 1.0;

Timer t_apply;
Expand Down
7 changes: 1 addition & 6 deletions opm/simulators/linalg/gpubridge/rocm/rocsparseBILU0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,9 @@ class rocsparseBILU0 : public rocsparsePreconditioner<Scalar, block_size>
/// and Ux = z
/// \param[in] y Input y vector
/// \param[out] x Output x vector
void apply(Scalar& y,
void apply(const Scalar& y,
Scalar& x) override;

#if HAVE_OPENCL
// apply preconditioner, x = prec(y)
void apply(const cl::Buffer&, cl::Buffer&) override {}
#endif

/// Copy matrix A values to GPU
/// \param[in] mVals Input values
void copy_system_to_gpu(Scalar *mVals) override;
Expand Down
2 changes: 1 addition & 1 deletion opm/simulators/linalg/gpubridge/rocm/rocsparseCPR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ apply_amg(const Scalar& y,

template <class Scalar, unsigned int block_size>
void rocsparseCPR<Scalar, block_size>::
apply(Scalar& y,
apply(const Scalar &y,
Scalar& x)
{
Dune::Timer t_bilu0;
Expand Down
7 changes: 1 addition & 6 deletions opm/simulators/linalg/gpubridge/rocm/rocsparseCPR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,9 @@ class rocsparseCPR : public rocsparsePreconditioner<Scalar, block_size>, public
/// also applies amg for pressure component
/// \param[in] y Input y vector
/// \param[out] x Output x vector
void apply(Scalar& y,
void apply(const Scalar& y,
Scalar& x) override;

#if HAVE_OPENCL
// apply preconditioner, x = prec(y)
void apply(const cl::Buffer&, cl::Buffer&) override {}
#endif

/// Copy matrix A values to GPU
/// \param[in] mVals Input values
void copy_system_to_gpu(Scalar *b) override;
Expand Down
18 changes: 4 additions & 14 deletions opm/simulators/linalg/gpubridge/rocm/rocsparsePreconditioner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,16 @@ class rocsparsePreconditioner : public Preconditioner<Scalar, block_size>
static std::unique_ptr<rocsparsePreconditioner<Scalar, block_size>> create(PreconditionerType type,
int verbosity);

// apply preconditioner, x = prec(y)
virtual void apply(Scalar& y, Scalar& x) = 0;

// create/update preconditioner, probably used every linear solve
// the version with two params can be overloaded, if not, it will default to using the one param version
virtual bool create_preconditioner(BlockedMatrix<Scalar> *mat) = 0;

virtual bool create_preconditioner(BlockedMatrix<Scalar> *mat,
BlockedMatrix<Scalar> *jacMat) = 0;

virtual bool initialize(std::shared_ptr<BlockedMatrix<Scalar>> matrix,
std::shared_ptr<BlockedMatrix<Scalar>> jacMatrix,
rocsparse_int *d_Arows,
rocsparse_int *d_Acols) = 0;
rocsparse_int* d_Arows,
rocsparse_int* d_Acols) = 0;

virtual void copy_system_to_gpu(Scalar *b)=0;
virtual void copy_system_to_gpu(Scalar* b) = 0;

/// Update linear system to GPU
/// \param[in] b input vector, contains N values
virtual void update_system_on_gpu(Scalar *b)=0;
virtual void update_system_on_gpu(Scalar* b) = 0;

void set_matrix_analysis(rocsparse_mat_descr descr_L,
rocsparse_mat_descr descr_U);
Expand Down