Skip to content

Commit

Permalink
Review update.
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Ribizel <ribizel@kit.edu>
Co-authored-by: Yu-Hsiang Tsai <yhmtsai@gmail.com>
  • Loading branch information
3 people committed Aug 6, 2021
1 parent 0c45763 commit 2757b2a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
29 changes: 12 additions & 17 deletions common/preconditioner/jacobi_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void scalar_conj(std::shared_ptr<const DefaultExecutor> exec,
[] GKO_KERNEL(auto elem, auto diag, auto conj_diag) {
conj_diag[elem] = conj(diag[elem]);
},
diag.get_num_elems(), diag.get_const_data(), conj_diag.get_data());
diag.get_num_elems(), diag, conj_diag);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_JACOBI_SCALAR_CONJ_KERNEL);
Expand All @@ -69,14 +69,12 @@ template <typename ValueType>
void invert_diagonal(std::shared_ptr<const DefaultExecutor> exec,
const Array<ValueType> &diag, Array<ValueType> &inv_diag)
{
auto one_val = one<ValueType>();
run_kernel(
exec,
[] GKO_KERNEL(auto elem, auto diag, auto inv_diag, auto one_val) {
inv_diag[elem] = one_val / diag[elem];
[] GKO_KERNEL(auto elem, auto diag, auto inv_diag) {
inv_diag[elem] = safe_divide(one(diag[elem]), diag[elem]);
},
diag.get_num_elems(), diag.get_const_data(), inv_diag.get_data(),
one_val);
diag.get_num_elems(), diag, inv_diag);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_JACOBI_INVERT_DIAGONAL_KERNEL);
Expand All @@ -98,7 +96,7 @@ void scalar_apply(std::shared_ptr<const DefaultExecutor> exec,
x(row, col) = beta[col] * x(row, col) +
alpha[col] * b(row, col) * diag[row];
},
x->get_size(), diag.get_const_data(), alpha->get_const_values(), b,
x->get_size(), diag, alpha->get_const_values(), b,
beta->get_const_values(), x);
} else {
run_kernel(
Expand All @@ -108,7 +106,7 @@ void scalar_apply(std::shared_ptr<const DefaultExecutor> exec,
x(row, col) =
beta[0] * x(row, col) + alpha[0] * b(row, col) * diag[row];
},
x->get_size(), diag.get_const_data(), alpha->get_const_values(), b,
x->get_size(), diag, alpha->get_const_values(), b,
beta->get_const_values(), x);
}
}
Expand All @@ -127,7 +125,7 @@ void simple_scalar_apply(std::shared_ptr<const DefaultExecutor> exec,
[] GKO_KERNEL(auto row, auto col, auto diag, auto b, auto x) {
x(row, col) = b(row, col) * diag[row];
},
x->get_size(), diag.get_const_data(), b, x);
x->get_size(), diag, b, x);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
Expand All @@ -137,20 +135,17 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
template <typename ValueType>
void scalar_convert_to_dense(std::shared_ptr<const DefaultExecutor> exec,
const Array<ValueType> &blocks,
ValueType *result_values,
const gko::dim<2> &matrix_size,
size_type result_stride)
matrix::Dense<ValueType> *result)
{
run_kernel(
exec,
[] GKO_KERNEL(auto row, auto col, auto stride, auto diag,
auto result_values) {
result_values[row * stride + col] = zero<ValueType>();
[] GKO_KERNEL(auto row, auto col, auto diag, auto result) {
result(row, col) = zero(diag[row]);
if (row == col) {
result_values[row * stride + col] = diag[row];
result(row, col) = diag[row];
}
},
matrix_size, result_stride, blocks.get_const_data(), result_values);
result->get_size(), blocks, result);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(
Expand Down
3 changes: 1 addition & 2 deletions core/preconditioner/jacobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ void Jacobi<ValueType, IndexType>::convert_to(
auto exec = this->get_executor();
auto tmp = matrix::Dense<ValueType>::create(exec, this->get_size());
if (parameters_.max_block_size == 1) {
exec->run(jacobi::make_scalar_convert_to_dense(
blocks_, tmp->get_values(), tmp->get_size(), tmp->get_stride()));
exec->run(jacobi::make_scalar_convert_to_dense(blocks_, tmp.get()));
} else {
exec->run(jacobi::make_convert_to_dense(
num_blocks_, parameters_.storage_optimization.block_wise,
Expand Down
9 changes: 4 additions & 5 deletions core/preconditioner/jacobi_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ namespace kernels {
&storage_scheme, \
Array<ValueType> &out_blocks)

#define GKO_DECLARE_JACOBI_SCALAR_CONVERT_TO_DENSE_KERNEL(ValueType) \
void scalar_convert_to_dense( \
std::shared_ptr<const DefaultExecutor> exec, \
const Array<ValueType> &blocks, ValueType *result_values, \
const gko::dim<2> &mat_size, size_type result_stride)
#define GKO_DECLARE_JACOBI_SCALAR_CONVERT_TO_DENSE_KERNEL(ValueType) \
void scalar_convert_to_dense(std::shared_ptr<const DefaultExecutor> exec, \
const Array<ValueType> &blocks, \
matrix::Dense<ValueType> *result)

#define GKO_DECLARE_JACOBI_CONVERT_TO_DENSE_KERNEL(ValueType, IndexType) \
void convert_to_dense( \
Expand Down
16 changes: 8 additions & 8 deletions reference/preconditioner/jacobi_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,10 @@ void invert_diagonal(std::shared_ptr<const DefaultExecutor> exec,
const Array<ValueType> &diag, Array<ValueType> &inv_diag)
{
for (size_type i = 0; i < diag.get_num_elems(); ++i) {
inv_diag.get_data()[i] =
static_cast<ValueType>(1.0) / diag.get_const_data()[i];
auto diag_val = diag.get_const_data()[i] == zero<ValueType>()
? one<ValueType>()
: diag.get_const_data()[i];
inv_diag.get_data()[i] = one<ValueType>() / diag_val;
}
}

Expand Down Expand Up @@ -696,16 +698,14 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
template <typename ValueType>
void scalar_convert_to_dense(std::shared_ptr<const DefaultExecutor> exec,
const Array<ValueType> &blocks,
ValueType *result_values,
const gko::dim<2> &matrix_size,
size_type result_stride)
matrix::Dense<ValueType> *result)
{
auto matrix_size = result->get_size();
for (size_type i = 0; i < matrix_size[0]; ++i) {
for (size_type j = 0; j < matrix_size[1]; ++j) {
result_values[i * result_stride + j] = zero<ValueType>();
result->at(i, j) = zero<ValueType>();
if (i == j) {
result_values[i * result_stride + j] =
blocks.get_const_data()[i];
result->at(i, j) = blocks.get_const_data()[i];
}
}
}
Expand Down
26 changes: 21 additions & 5 deletions reference/test/preconditioner/jacobi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,15 @@ TYPED_TEST(Jacobi, ScalarJacobiConvertsToDense)
dense_j->copy_from(scalar_j.get());
auto j_val = scalar_j->get_blocks();

EXPECT_EQ(dense_j->at(0, 0), j_val[0]);
EXPECT_EQ(dense_j->at(1, 1), j_val[1]);
EXPECT_EQ(dense_j->at(2, 2), j_val[2]);
EXPECT_EQ(dense_j->at(3, 3), j_val[3]);
EXPECT_EQ(dense_j->at(4, 4), j_val[4]);
for (auto i = 0; i < dense_j->get_size()[0]; ++i) {
for (auto j = 0; j < dense_j->get_size()[1]; ++j) {
if (i == j) {
EXPECT_EQ(dense_j->at(i, j), j_val[j]);
} else {
EXPECT_EQ(dense_j->at(i, j), value_type{0.0});
}
}
}
}


Expand All @@ -349,6 +353,12 @@ TYPED_TEST(Jacobi, ScalarJacobiCanBeTransposed)
auto trans_j = gko::as<Bj>(t_j.get())->get_blocks();
auto scal_j = scalar_j->get_blocks();

ASSERT_EQ(scalar_j->get_size(), gko::dim<2>(5, 5));
ASSERT_EQ(scalar_j->get_num_stored_elements(), 5);
ASSERT_EQ(scalar_j->get_parameters().max_block_size, 1);
ASSERT_EQ(scalar_j->get_num_blocks(), 5);
ASSERT_EQ(scalar_j->get_parameters().block_pointers.get_const_data(),
nullptr);
EXPECT_EQ(trans_j[0], scal_j[0]);
EXPECT_EQ(trans_j[1], scal_j[1]);
EXPECT_EQ(trans_j[2], scal_j[2]);
Expand Down Expand Up @@ -390,6 +400,12 @@ TEST(Jacobi, ScalarJacobiCanBeConjTransposed)
auto trans_j = gko::as<Bj>(t_j.get())->get_blocks();
auto scal_j = scalar_j->get_blocks();

ASSERT_EQ(scalar_j->get_size(), gko::dim<2>(5, 5));
ASSERT_EQ(scalar_j->get_num_stored_elements(), 5);
ASSERT_EQ(scalar_j->get_parameters().max_block_size, 1);
ASSERT_EQ(scalar_j->get_num_blocks(), 5);
ASSERT_EQ(scalar_j->get_parameters().block_pointers.get_const_data(),
nullptr);
EXPECT_EQ(trans_j[0], gko::conj(scal_j[0]));
EXPECT_EQ(trans_j[1], gko::conj(scal_j[1]));
EXPECT_EQ(trans_j[2], gko::conj(scal_j[2]));
Expand Down

0 comments on commit 2757b2a

Please sign in to comment.