8000 Fix inconsistent behavior of scalar Jacobi between reference and the others by yhmtsai · Pull Request #1642 · ginkgo-project/ginkgo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix inconsistent behavior of scalar Jacobi between reference and the others #1642

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

Merged
merged 3 commits into from
Jul 6, 2024
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
4 changes: 3 additions & 1 deletion common/unified/preconditioner/jacobi_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ void invert_diagonal(std::shared_ptr<const DefaultExecutor> exec,
run_kernel(
exec,
[] GKO_KERNEL(auto elem, auto diag, auto inv_diag) {
inv_diag[elem] = safe_divide(one(diag[elem]), diag[elem]);
// if the diagonal is zero, we use 1 for in the inverted result.
inv_diag[elem] = is_zero(diag[elem]) ? one(diag[elem])
: one(diag[elem]) / diag[elem];
},
diag.get_size(), diag, inv_diag);
}
Expand Down
23 changes: 23 additions & 0 deletions dpcpp/test/preconditioner/jacobi_kernels.dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,4 +905,27 @@ TEST_F(
}


TEST_F(Jacobi, ScalarJacobiHandleZero)
{
auto mtx = gko::share(
gko::initialize<Vec>({{0, 0, 0}, {0, 2, 0}, {0, 0, 0}}, ref));
auto b = gko::initialize<Vec>({1, 2, 3}, ref);
auto x = Vec::create(ref, gko::dim<2>(3, 1));
auto d_b = b->clone(dpcpp);
auto d_x = x->clone(dpcpp);
auto d_mtx = gko::share(mtx->clone(dpcpp));

auto jacobi = Bj::build().with_max_block_size(1u).on(ref)->generate(mtx);
// Must generate from scratch because the clone copies the inverted
// information.
auto d_jacobi =
Bj::build().with_max_block_size(1u).on(dpcpp)->generate(d_mtx);

// Jacobi uses 1 as the result when diagonal value is zero.
jacobi->apply(b, x);
d_jacobi->apply(d_b, d_x);
GKO_ASSERT_MTX_NEAR(d_x, x, 0.0);
}


} // namespace
20 changes: 20 additions & 0 deletions reference/test/preconditioner/jacobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,24 @@ TYPED_TEST(Jacobi, ScalarJacobiGeneratesOnDifferentPrecision)
}


TYPED_TEST(Jacobi, ScalarJacobiHandleZero)
{
using value_type = typename TestFixture::value_type;
using Vec = typename TestFixture::Vec;
using Bj = typename TestFixture::Bj;
auto mtx = gko::share(
gko::initialize<Vec>({{0, 0, 0}, {0, 2, 0}, {0, 0, 0}}, this->exec));
auto b = gko::initialize<Vec>({1, 2, 3}, this->exec);
auto x = Vec::create(this->exec, gko::dim<2>(3, 1));

auto jacobi = this->scalar_j_factory->generate(mtx);

// Jacobi uses 1 as the result when diagonal value is zero.
jacobi->apply(b, x);
ASSERT_EQ(x->at(0, 0), value_type{1.0});
ASSERT_EQ(x->at(1, 0), value_type{1.0});
ASSERT_EQ(x->at(2, 0), value_type{3.0});
}


} // namespace
23 changes: 23 additions & 0 deletions test/preconditioner/jacobi_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,26 @@ TEST_F(

GKO_ASSERT_MTX_NEAR(d_x, x, 1e-6);
}


TEST_F(Jacobi, ScalarJacobiHandleZero)
{
auto mtx = gko::share(
gko::initialize<Vec>({{0, 0, 0}, {0, 2, 0}, {0, 0, 0}}, ref));
auto b = gko::initialize<Vec>({1, 2, 3}, ref);
auto x = Vec::create(ref, gko::dim<2>(3, 1));
auto d_b = b->clone(exec);
auto d_x = x->clone(exec);
auto d_mtx = gko::share(mtx->clone(exec));

auto jacobi = Bj::build().with_max_block_size(1u).on(ref)->generate(mtx);
// Must generate from scratch because the clone copies the inverted
// information.
auto d_jacobi =
Bj::build().with_max_block_size(1u).on(exec)->generate(d_mtx);

// Jacobi uses 1 as the result when diagonal value is zero.
jacobi->apply(b, x);
d_jacobi->apply(d_b, d_x);
GKO_ASSERT_MTX_NEAR(d_x, x, 0.0);
}
0