Skip to content

Commit

Permalink
Rename to coarse_skip
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Apr 26, 2024
1 parent 5ce8123 commit fc23624
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 87 deletions.
15 changes: 8 additions & 7 deletions common/unified/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void fill_restrict_op(std::shared_ptr<const DefaultExecutor> exec,
restrict_col_idxs[coarse_data[tidx]] = tidx;
}
},
coarse_rows->get_num_elems(), coarse_rows->get_const_data(),
coarse_rows->get_size(), coarse_rows->get_const_data(),
restrict_op->get_col_idxs());
}

Expand All @@ -47,18 +47,19 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(

template <typename IndexType>
void fill_incremental_indices(std::shared_ptr<const DefaultExecutor> exec,
size_type num_jumps,
size_type coarse_skip,
array<IndexType>* coarse_rows)
{
IndexType num_elems = (coarse_rows->get_num_elems());
IndexType num_elems = coarse_rows->get_size();
run_kernel(
exec,
[] GKO_KERNEL(auto tidx, auto num_jumps, auto coarse_data, auto size) {
if (tidx % num_jumps == 0 && tidx < size) {
coarse_data[tidx] = tidx / num_jumps;
[] GKO_KERNEL(auto tidx, auto coarse_skip, auto coarse_data,
auto size) {
if (tidx % coarse_skip == 0 && tidx < size) {
coarse_data[tidx] = tidx / coarse_skip;
}
},
num_elems, num_jumps, coarse_rows->get_data(), num_elems);
num_elems, coarse_skip, coarse_rows->get_data(), num_elems);
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
Expand Down
7 changes: 4 additions & 3 deletions core/multigrid/uniform_coarsening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ void UniformCoarsening<ValueType, IndexType>::generate()
this->set_fine_op(uniform_coarsening_op_shared_ptr);
}
// Use -1 as sentinel value
coarse_rows_ = Array<IndexType>(exec, num_rows);
coarse_rows_ = array<IndexType>(exec, num_rows);
coarse_rows_.fill(-one<IndexType>());

// Fill with incremental local indices.
exec->run(uniform_coarsening::make_fill_incremental_indices(
parameters_.num_jumps, &coarse_rows_));
parameters_.coarse_skip, &coarse_rows_));

gko::dim<2>::dimension_type coarse_dim =
(coarse_rows_.get_num_elems() + 1) / parameters_.num_jumps;
(coarse_rows_.get_size() + parameters_.coarse_skip - 1) /
parameters_.coarse_skip;
auto fine_dim = system_matrix_->get_size()[0];
auto restrict_op = share(
csr_type::create(exec, gko::dim<2>{coarse_dim, fine_dim}, coarse_dim,
Expand Down
2 changes: 1 addition & 1 deletion core/multigrid/uniform_coarsening_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace uniform_coarsening {

#define GKO_DECLARE_UNIFORM_COARSENING_FILL_INCREMENTAL_INDICES(IndexType) \
void fill_incremental_indices(std::shared_ptr<const DefaultExecutor> exec, \
size_type num_jumps, \
size_type coarse_skip, \
array<IndexType>* coarse_rows)


Expand Down
6 changes: 3 additions & 3 deletions core/test/multigrid/uniform_coarsening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UniformCoarseningFactory : public ::testing::Test {
UniformCoarseningFactory()
: exec(gko::ReferenceExecutor::create()),
uniform_coarsening1_factory(
MgLevel::build().with_num_jumps(4u).with_skip_sorting(true).on(
MgLevel::build().with_coarse_skip(4u).with_skip_sorting(true).on(
exec))
{}

Expand All @@ -56,14 +56,14 @@ TYPED_TEST(UniformCoarseningFactory, DefaultSetting)
using MgLevel = typename TestFixture::MgLevel;
auto factory = MgLevel::build().on(this->exec);

ASSERT_EQ(factory->get_parameters().num_jumps, 2u);
ASSERT_EQ(factory->get_parameters().coarse_skip, 2u);
ASSERT_EQ(factory->get_parameters().skip_sorting, false);
}


TYPED_TEST(UniformCoarseningFactory, SetNumJumps)
{
ASSERT_EQ(this->uniform_coarsening1_factory->get_parameters().num_jumps,
ASSERT_EQ(this->uniform_coarsening1_factory->get_parameters().coarse_skip,
4u);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main(int argc, char* argv[])

const auto executor_string = argc >= 2 ? argv[1] : "reference";
const auto coarse_type = argc >= 3 ? argv[2] : "pgm";
const unsigned num_jumps = argc >= 4 ? std::atoi(argv[3]) : 2u;
const unsigned coarse_skip = argc >= 4 ? std::atoi(argv[3]) : 2u;
const unsigned grid_dim = argc >= 5 ? std::atoi(argv[4]) : 20u;

// Figure out where to run the code
Expand Down Expand Up @@ -143,7 +143,7 @@ int main(int argc, char* argv[])
gko::share(pgm::build().with_deterministic(true).on(exec));
// Create MultigridLevel factory
auto coarse_unif_gen = gko::share(
uniform_coarsening::build().with_num_jumps(num_jumps).on(exec));
uniform_coarsening::build().with_coarse_skip(coarse_skip).on(exec));

// Create CoarsestSolver factory
auto coarsest_gen = gko::share(
Expand Down
5 changes: 2 additions & 3 deletions include/ginkgo/core/multigrid/uniform_coarsening.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ class UniformCoarsening
GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
{
/**
* The number of jumps between the rows to be selected. For example if
* set to 2, every second row is selected in the coarse grid matrix.
* The number of rows to skip for the coarse matrix generation
*/
unsigned GKO_FACTORY_PARAMETER_SCALAR(num_jumps, 2u);
unsigned GKO_FACTORY_PARAMETER_SCALAR(coarse_skip, 2u);

/**
* The `system_matrix`, which will be given to this factory, must be
Expand Down
6 changes: 3 additions & 3 deletions reference/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(

template <typename IndexType>
void fill_incremental_indices(std::shared_ptr<const DefaultExecutor> exec,
size_type num_jumps,
size_type coarse_skip,
array<IndexType>* coarse_rows)
{
for (IndexType i = 0; i < coarse_rows->get_num_elems(); i += num_jumps) {
coarse_rows->get_data()[i] = i / num_jumps;
for (IndexType i = 0; i < coarse_rows->get_size(); i += coarse_skip) {
coarse_rows->get_data()[i] = i / coarse_skip;
}
}

Expand Down
38 changes: 26 additions & 12 deletions reference/test/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class UniformCoarsening : public ::testing::Test {
UniformCoarsening()
: exec(gko::ReferenceExecutor::create()),
uniform_coarsening_factory(
MgLevel::build().with_num_jumps(2u).with_skip_sorting(true).on(
MgLevel::build().with_coarse_skip(2u).with_skip_sorting(true).on(
exec)),
fine_b(gko::initialize<Vec>(
{I<VT>({2.0, -1.0}), I<VT>({-1.0, 2.0}), I<VT>({0.0, -1.0}),
Expand Down Expand Up @@ -83,7 +83,7 @@ class UniformCoarsening : public ::testing::Test {
mg_level = uniform_coarsening_factory->generate(mtx);
}

void create_mtx(Mtx* fine, gko::Array<index_type>* coarse_rows, Mtx* coarse)
void create_mtx(Mtx* fine, gko::array<index_type>* coarse_rows, Mtx* coarse)
{
auto coarse_rows_val = coarse_rows->get_data();
coarse_rows_val[0] = 0;
Expand Down Expand Up @@ -158,7 +158,7 @@ class UniformCoarsening : public ::testing::Test {
std::shared_ptr<const gko::ReferenceExecutor> exec;
std::shared_ptr<Mtx> mtx;
std::shared_ptr<Mtx> coarse;
gko::Array<index_type> coarse_rows;
gko::array<index_type> coarse_rows;
std::shared_ptr<Vec> coarse_b;
std::shared_ptr<Vec> fine_b;
std::shared_ptr<Vec> restrict_ans;
Expand Down Expand Up @@ -189,7 +189,7 @@ TYPED_TEST(UniformCoarsening, CanBeCopied)
this->mtx.get());
this->assert_same_coarse_rows(copy_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_num_elems());
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get());
}
Expand All @@ -202,7 +202,7 @@ TYPED_TEST(UniformCoarsening, CanBeMoved)
auto copy =
this->uniform_coarsening_factory->generate(Mtx::create(this->exec));

copy->copy_from(std::move(this->mg_level));
copy->move_from(this->mg_level);
auto copy_mtx = copy->get_system_matrix();
auto copy_coarse_rows = copy->get_const_coarse_rows();
auto copy_coarse = copy->get_coarse_op();
Expand All @@ -211,7 +211,7 @@ TYPED_TEST(UniformCoarsening, CanBeMoved)
this->mtx.get());
this->assert_same_coarse_rows(copy_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_num_elems());
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get());
}
Expand All @@ -230,7 +230,7 @@ TYPED_TEST(UniformCoarsening, CanBeCloned)
this->mtx.get());
this->assert_same_coarse_rows(clone_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_num_elems());
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(clone_coarse.get()),
this->coarse.get());
}
Expand Down Expand Up @@ -269,10 +269,14 @@ TYPED_TEST(UniformCoarsening, FillIncrementalIndicesWorks)
{
using index_type = typename TestFixture::index_type;
auto c2_rows =
gko::Array<index_type>(this->exec, {0, -1, 1, -1, 2, -1, 3, -1, 4, -1});
auto c3_rows = gko::Array<index_type>(this->exec,
gko::array<index_type>(this->exec, {0, -1, 1, -1, 2, -1, 3, -1, 4, -1});
auto c3_rows = gko::array<index_type>(this->exec,
{0, -1, -1, 1, -1, -1, 2, -1, -1, 3});
auto c_rows = gko::Array<index_type>(this->exec, 10);
auto c4_rows = gko::array<index_type>(
this->exec, {0, -1, -1, -1, 1, -1, -1, -1, 2, -1});
auto c5_rows = gko::array<index_type>(
this->exec, {0, -1, -1, -1, -1, 1, -1, -1, -1, -1});
auto c_rows = gko::array<index_type>(this->exec, 10);
c_rows.fill(-gko::one<index_type>());

gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
Expand All @@ -283,6 +287,16 @@ TYPED_TEST(UniformCoarsening, FillIncrementalIndicesWorks)
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
this->exec, 3, &c_rows);
GKO_ASSERT_ARRAY_EQ(c_rows, c3_rows);

c_rows.fill(-gko::one<index_type>());
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
this->exec, 4, &c_rows);
GKO_ASSERT_ARRAY_EQ(c_rows, c4_rows);

c_rows.fill(-gko::one<index_type>());
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
this->exec, 5, &c_rows);
GKO_ASSERT_ARRAY_EQ(c_rows, c5_rows);
}


Expand All @@ -292,7 +306,7 @@ TYPED_TEST(UniformCoarsening, CoarseFineRestrictApply)
this->uniform_coarsening_factory->generate(this->mtx);
using Vec = typename TestFixture::Vec;
using value_type = typename TestFixture::value_type;
auto x = Vec::create_with_config_of(gko::lend(this->coarse_b));
auto x = Vec::create_with_config_of(this->coarse_b);

uniform_coarsening->get_restrict_op()->apply(this->fine_b.get(), x.get());

Expand Down Expand Up @@ -398,7 +412,7 @@ TYPED_TEST(UniformCoarsening, GenerateMgLevelOnUnsortedMatrix)
using index_type = typename TestFixture::index_type;
using Mtx = typename TestFixture::Mtx;
using MgLevel = typename TestFixture::MgLevel;
auto mglevel_sort = MgLevel::build().with_num_jumps(2u).on(this->exec);
auto mglevel_sort = MgLevel::build().with_coarse_skip(2u).on(this->exec);
/* this unsorted matrix is stored as this->fine:
* 5 -3 -3 0 0
* -3 5 0 -2 -1
Expand Down
2 changes: 1 addition & 1 deletion test/multigrid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ginkgo_create_common_test(pgm_kernels)
ginkgo_create_common_test(fixed_coarsening_kernels)
ginkgo_create_common_test(pgm_kernels)
ginkgo_create_common_test(uniform_coarsening_kernels)
Loading

0 comments on commit fc23624

Please sign in to comment.