Skip to content

Commit

Permalink
no way to face nullptr preconditioner in batch apply
Browse files Browse the repository at this point in the history
Co-authored-by: Marcel Koch <marcel.koch@kit.edu>
  • Loading branch information
yhmtsai and MarcelKoch committed Nov 21, 2024
1 parent 2fb4434 commit 20b7e8b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 101 deletions.
17 changes: 5 additions & 12 deletions core/solver/batch_bicgstab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,11 @@ void Bicgstab<ValueType>::solver_apply(

run<matrix::Dense<ValueType>, matrix::Csr<ValueType>,
matrix::Ell<ValueType>>(this->system_matrix_.get(), [&](auto matrix) {
if (this->preconditioner_ == nullptr) {
auto identity =
matrix::Identity<ValueType>::create(exec, matrix->get_size());
exec->run(bicgstab::make_apply(settings, matrix, identity.get(), b,
x, *log_data));
} else {
run<matrix::Identity<ValueType>, preconditioner::Jacobi<ValueType>>(
this->preconditioner_.get(), [&](auto preconditioner) {
exec->run(bicgstab::make_apply(
settings, matrix, preconditioner, b, x, *log_data));
});
}
run<matrix::Identity<ValueType>, preconditioner::Jacobi<ValueType>>(
this->preconditioner_.get(), [&](auto preconditioner) {
exec->run(bicgstab::make_apply(settings, matrix, preconditioner,
b, x, *log_data));
});
});
}

Expand Down
19 changes: 6 additions & 13 deletions core/solver/batch_cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,12 @@ void Cg<ValueType>::solver_apply(
run<batch::matrix::Dense<ValueType>, batch::matrix::Csr<ValueType>,
batch::matrix::Ell<ValueType>>(
this->system_matrix_.get(), [&](auto matrix) {
if (this->preconditioner_ == nullptr) {
auto identity = matrix::Identity<ValueType>::create(
exec, matrix->get_size());
exec->run(cg::make_apply(settings, matrix, identity.get(), b, x,
*log_data));
} else {
run<batch::matrix::Identity<ValueType>,
batch::preconditioner::Jacobi<ValueType>>(
this->preconditioner_.get(), [&](auto preconditioner) {
exec->run(cg::make_apply(
settings, matrix, preconditioner, b, x, *log_data));
});
}
run<batch::matrix::Identity<ValueType>,
batch::preconditioner::Jacobi<ValueType>>(
this->preconditioner_.get(), [&](auto preconditioner) {
exec->run(cg::make_apply(settings, matrix, preconditioner,
b, x, *log_data));
});
});
}

Expand Down
4 changes: 3 additions & 1 deletion core/test/utils/batch_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ginkgo/core/base/device_matrix_data.hpp>
#include <ginkgo/core/base/matrix_data.hpp>
#include <ginkgo/core/log/batch_logger.hpp>
#include <ginkgo/core/matrix/batch_identity.hpp>
#include <ginkgo/core/matrix/dense.hpp>

#include "core/test/utils/assertions.hpp"
Expand Down Expand Up @@ -334,7 +335,8 @@ ResultWithLogData<typename MatrixType::value_type> solve_linear_system(
if (precond_factory) {
precond = precond_factory->generate(sys.matrix);
} else {
precond = nullptr;
precond = gko::batch::matrix::Identity<value_type>::create(
exec, sys.matrix->get_size());
}

solve_lambda(settings, precond.get(), sys.matrix.get(), sys.rhs.get(),
Expand Down
21 changes: 6 additions & 15 deletions reference/test/solver/batch_bicgstab_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,12 @@ class BatchBicgstab : public ::testing::Test {
const gko::batch::BatchLinOp* prec,
const Mtx* mtx, const MVec* b, MVec* x,
LogData& log_data) {
if (prec == nullptr) {
auto identity =
gko::batch::matrix::Identity<value_type>::create(
executor, mtx->get_size());
gko::kernels::reference::batch_bicgstab::apply(
executor, opts, mtx, identity.get(), b, x, log_data);
} else {
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::reference::batch_bicgstab::apply(
executor, opts, mtx, preconditioner, b, x,
log_data);
});
}
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::reference::batch_bicgstab::apply(
executor, opts, mtx, preconditioner, b, x, log_data);
});
};
}

Expand Down
21 changes: 6 additions & 15 deletions reference/test/solver/batch_cg_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,12 @@ class BatchCg : public ::testing::Test {
const gko::batch::BatchLinOp* prec,
const Mtx* mtx, const MVec* b, MVec* x,
LogData& log_data) {
if (prec == nullptr) {
auto identity =
gko::batch::matrix::Identity<value_type>::create(
executor, mtx->get_size());
gko::kernels::reference::batch_cg::apply(
executor, opts, mtx, identity.get(), b, x, log_data);
} else {
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::reference::batch_cg::apply(
executor, opts, mtx, preconditioner, b, x,
log_data);
});
}
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::reference::batch_cg::apply(
executor, opts, mtx, preconditioner, b, x, log_data);
});
};
}

Expand Down
22 changes: 7 additions & 15 deletions test/preconditioner/batch_jacobi_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,13 @@ class BatchJacobi : public CommonTestFixture {
const gko::batch::BatchLinOp* prec,
const Mtx* mtx, const MVec* b, MVec* x,
LogData& log_data) {
if (prec == nullptr) {
auto identity =
gko::batch::matrix::Identity<value_type>::create(
executor, mtx->get_size());
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::apply(
executor, settings, mtx, identity.get(), b, x, log_data);
} else {
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::
apply(executor, settings, mtx, preconditioner, b, x,
log_data);
});
}
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::apply(
executor, settings, mtx, preconditioner, b, x,
log_data);
});
};
solver_settings = Settings{max_iters, tol,
gko::batch::stop::tolerance_type::relative};
Expand Down
22 changes: 7 additions & 15 deletions test/solver/batch_bicgstab_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,13 @@ class BatchBicgstab : public CommonTestFixture {
const gko::batch::BatchLinOp* prec,
const Mtx* mtx, const MVec* b, MVec* x,
LogData& log_data) {
if (prec == nullptr) {
auto identity =
gko::batch::matrix::Identity<value_type>::create(
executor, mtx->get_size());
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::apply(
executor, settings, mtx, identity.get(), b, x, log_data);
} else {
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::
apply(executor, settings, mtx, preconditioner, b, x,
log_data);
});
}
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_bicgstab::apply(
executor, settings, mtx, preconditioner, b, x,
log_data);
});
};
solver_settings = Settings{max_iters, tol,
gko::batch::stop::tolerance_type::relative};
Expand Down
22 changes: 7 additions & 15 deletions test/solver/batch_cg_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,13 @@ class BatchCg : public CommonTestFixture {
const gko::batch::BatchLinOp* prec,
const Mtx* mtx, const MVec* b, MVec* x,
LogData& log_data) {
if (prec == nullptr) {
auto identity =
gko::batch::matrix::Identity<value_type>::create(
executor, mtx->get_size());
gko::kernels::GKO_DEVICE_NAMESPACE::batch_cg::apply(
executor, settings, mtx, identity.get(), b, x, log_data);
} else {
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_cg::apply(
executor, settings, mtx, preconditioner, b, x,
log_data);
});
}
gko::run<gko::batch::matrix::Identity<value_type>,
gko::batch::preconditioner::Jacobi<value_type>>(
prec, [&](auto preconditioner) {
gko::kernels::GKO_DEVICE_NAMESPACE::batch_cg::apply(
executor, settings, mtx, preconditioner, b, x,
log_data);
});
};
solver_settings = Settings{max_iters, tol,
gko::batch::stop::tolerance_type::relative};
Expand Down

0 comments on commit 20b7e8b

Please sign in to comment.