Move opencl variables to Preconditioner

This commit is contained in:
Tong Dong Qiu 2021-12-01 13:23:46 +01:00
parent 94ea2dcd30
commit 374f8276dc
7 changed files with 42 additions and 36 deletions

View File

@ -48,14 +48,6 @@ BILU0<block_size>::BILU0(ILUReorder opencl_ilu_reorder_, int verbosity_) :
}
template <unsigned int block_size>
void BILU0<block_size>::init(int Nb, int nnzb, std::shared_ptr<cl::Context>& context_, std::shared_ptr<cl::CommandQueue>& queue_)
{
context = context_.get();
queue = queue_.get();
}
template <unsigned int block_size>
bool BILU0<block_size>::analyze_matrix(BlockedMatrix *mat)
{

View File

@ -48,6 +48,10 @@ class BILU0 : public Preconditioner<block_size>
using Base::nnz;
using Base::nnzb;
using Base::verbosity;
using Base::context;
using Base::queue;
using Base::events;
using Base::err;
private:
std::unique_ptr<BlockedMatrix> LUmat = nullptr;
@ -78,10 +82,6 @@ private:
} GPU_storage;
GPU_storage s;
cl::Context *context;
cl::CommandQueue *queue;
std::vector<cl::Event> events;
cl_int err;
#if CHOW_PATEL
ChowPatelIlu<block_size> chowPatelIlu;
@ -91,8 +91,6 @@ public:
BILU0(ILUReorder opencl_ilu_reorder, int verbosity);
void init(int Nb, int nnzb, std::shared_ptr<cl::Context>& context, std::shared_ptr<cl::CommandQueue>& queue) override;
// analysis, find reordering if specified
bool analyze_matrix(BlockedMatrix *mat) override;

View File

@ -47,31 +47,28 @@ CPR<block_size>::CPR(int verbosity_, ILUReorder opencl_ilu_reorder_) :
Preconditioner<block_size>(verbosity_), opencl_ilu_reorder(opencl_ilu_reorder_)
{
bilu0 = std::make_unique<BILU0<block_size> >(opencl_ilu_reorder, verbosity_);
diagIndices.resize(1);
}
template <unsigned int block_size>
void CPR<block_size>::init(int Nb_, int nnzb_, std::shared_ptr<cl::Context>& context_, std::shared_ptr<cl::CommandQueue>& queue_) {
this->Nb = Nb_;
this->nnzb = nnzb_;
this->N = Nb_ * block_size;
this->nnz = nnzb_ * block_size * block_size;
void CPR<block_size>::setOpencl(std::shared_ptr<cl::Context>& context_, std::shared_ptr<cl::CommandQueue>& queue_) {
context = context_;
queue = queue_;
coarse_vals.resize(nnzb);
coarse_x.resize(Nb);
coarse_y.resize(Nb);
weights.resize(N);
diagIndices.resize(1);
bilu0->init(Nb, nnzb, context, queue);
bilu0->setOpencl(context, queue);
}
template <unsigned int block_size>
bool CPR<block_size>::analyze_matrix(BlockedMatrix *mat_) {
this->Nb = mat_->Nb;
this->nnzb = mat_->nnzbs;
this->N = Nb * block_size;
this->nnz = nnzb * block_size * block_size;
bool success = bilu0->analyze_matrix(mat_);
if (opencl_ilu_reorder == ILUReorder::NONE) {
@ -191,6 +188,11 @@ template <unsigned int block_size>
void CPR<block_size>::create_preconditioner_amg(BlockedMatrix *mat_) {
this->mat = mat_;
coarse_vals.resize(nnzb);
coarse_x.resize(Nb);
coarse_y.resize(Nb);
weights.resize(N);
try{
double rhs[] = {0, 0, 0};
rhs[pressure_idx] = 1;

View File

@ -57,6 +57,10 @@ class CPR : public Preconditioner<block_size>
using Base::nnz;
using Base::nnzb;
using Base::verbosity;
using Base::context;
using Base::queue;
using Base::events;
using Base::err;
private:
int num_levels;
@ -96,11 +100,6 @@ private:
std::unique_ptr<openclSolverBackend<1> > coarse_solver; // coarse solver is scalar
ILUReorder opencl_ilu_reorder; // reordering strategy for ILU0 in coarse solver
std::shared_ptr<cl::Context> context;
std::shared_ptr<cl::CommandQueue> queue;
std::vector<cl::Event> events;
cl_int err;
// Analyze the AMG hierarchy build by Dune
void analyzeHierarchy();
@ -125,10 +124,11 @@ public:
CPR(int verbosity, ILUReorder opencl_ilu_reorder);
void init(int Nb, int nnzb, std::shared_ptr<cl::Context>& context, std::shared_ptr<cl::CommandQueue>& queue) override;
bool analyze_matrix(BlockedMatrix *mat) override;
// set own Opencl variables, but also that of the bilu0 preconditioner
void setOpencl(std::shared_ptr<cl::Context>& context, std::shared_ptr<cl::CommandQueue>& queue) override;
// applies blocked ilu0
// also applies amg for pressure component
void apply(const cl::Buffer& y, cl::Buffer& x) override;

View File

@ -31,6 +31,13 @@ namespace Opm
namespace Accelerator
{
template <unsigned int block_size>
void Preconditioner<block_size>::setOpencl(std::shared_ptr<cl::Context>& context_, std::shared_ptr<cl::CommandQueue>& queue_) {
context = context_;
queue = queue_;
}
template <unsigned int block_size>
std::unique_ptr<Preconditioner<block_size> > Preconditioner<block_size>::create(PreconditionerType type, int verbosity, ILUReorder opencl_ilu_reorder) {
if (type == PreconditionerType::BILU0) {
@ -44,7 +51,8 @@ std::unique_ptr<Preconditioner<block_size> > Preconditioner<block_size>::create(
#define INSTANTIATE_BDA_FUNCTIONS(n) \
template std::unique_ptr<Preconditioner<n> > Preconditioner<n>::create(PreconditionerType, int, ILUReorder);
template std::unique_ptr<Preconditioner<n> > Preconditioner<n>::create(PreconditionerType, int, ILUReorder); \
template void Preconditioner<n>::setOpencl(std::shared_ptr<cl::Context>&, std::shared_ptr<cl::CommandQueue>&);
INSTANTIATE_BDA_FUNCTIONS(1);
INSTANTIATE_BDA_FUNCTIONS(2);

View File

@ -42,6 +42,11 @@ protected:
int nnzb = 0; // number of blocks of the matrix
int verbosity = 0;
std::shared_ptr<cl::Context> context;
std::shared_ptr<cl::CommandQueue> queue;
std::vector<cl::Event> events;
cl_int err;
Preconditioner(int verbosity_) :
verbosity(verbosity_)
{};
@ -54,7 +59,8 @@ public:
static std::unique_ptr<Preconditioner> create(PreconditionerType type, int verbosity, ILUReorder opencl_ilu_reorder);
virtual void init(int Nb, int nnzb, std::shared_ptr<cl::Context>& context, std::shared_ptr<cl::CommandQueue>& queue);
// 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;

View File

@ -388,7 +388,7 @@ void openclSolverBackend<block_size>::initialize(int N_, int nnz_, int dim, doub
out.clear();
try {
prec->init(Nb, nnzb, context, queue);
prec->setOpencl(context, queue);
#if COPY_ROW_BY_ROW
vals_contiguous.resize(nnz);