diff --git a/common/unified/multigrid/uniform_coarsening_kernels.cpp b/common/unified/multigrid/uniform_coarsening_kernels.cpp index f09c37283d2..e5661d82792 100644 --- a/common/unified/multigrid/uniform_coarsening_kernels.cpp +++ b/common/unified/multigrid/uniform_coarsening_kernels.cpp @@ -37,7 +37,7 @@ void fill_restrict_op(std::shared_ptr 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()); } @@ -47,18 +47,19 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE( template void fill_incremental_indices(std::shared_ptr exec, - size_type num_jumps, + size_type coarse_skip, array* 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( diff --git a/core/multigrid/uniform_coarsening.cpp b/core/multigrid/uniform_coarsening.cpp index d61d2d060e6..15fd9056895 100644 --- a/core/multigrid/uniform_coarsening.cpp +++ b/core/multigrid/uniform_coarsening.cpp @@ -64,15 +64,16 @@ void UniformCoarsening::generate() this->set_fine_op(uniform_coarsening_op_shared_ptr); } // Use -1 as sentinel value - coarse_rows_ = Array(exec, num_rows); + coarse_rows_ = array(exec, num_rows); coarse_rows_.fill(-one()); // 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, diff --git a/core/multigrid/uniform_coarsening_kernels.hpp b/core/multigrid/uniform_coarsening_kernels.hpp index 6026703345e..319966626b6 100644 --- a/core/multigrid/uniform_coarsening_kernels.hpp +++ b/core/multigrid/uniform_coarsening_kernels.hpp @@ -31,7 +31,7 @@ namespace uniform_coarsening { #define GKO_DECLARE_UNIFORM_COARSENING_FILL_INCREMENTAL_INDICES(IndexType) \ void fill_incremental_indices(std::shared_ptr exec, \ - size_type num_jumps, \ + size_type coarse_skip, \ array* coarse_rows) diff --git a/core/test/multigrid/uniform_coarsening.cpp b/core/test/multigrid/uniform_coarsening.cpp index 2a87b81a737..fa480cfa81d 100644 --- a/core/test/multigrid/uniform_coarsening.cpp +++ b/core/test/multigrid/uniform_coarsening.cpp @@ -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)) {} @@ -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); } diff --git a/examples/multigrid-preconditioned-solver/multigrid-preconditioned-solver.cpp b/examples/multigrid-preconditioned-solver/multigrid-preconditioned-solver.cpp index 130e72569d2..636fc0ec567 100644 --- a/examples/multigrid-preconditioned-solver/multigrid-preconditioned-solver.cpp +++ b/examples/multigrid-preconditioned-solver/multigrid-preconditioned-solver.cpp @@ -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 @@ -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( diff --git a/include/ginkgo/core/multigrid/uniform_coarsening.hpp b/include/ginkgo/core/multigrid/uniform_coarsening.hpp index 643f1fa47d7..1decd1e6053 100644 --- a/include/ginkgo/core/multigrid/uniform_coarsening.hpp +++ b/include/ginkgo/core/multigrid/uniform_coarsening.hpp @@ -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 diff --git a/reference/multigrid/uniform_coarsening_kernels.cpp b/reference/multigrid/uniform_coarsening_kernels.cpp index acc54535e5d..04f006b68cd 100644 --- a/reference/multigrid/uniform_coarsening_kernels.cpp +++ b/reference/multigrid/uniform_coarsening_kernels.cpp @@ -57,11 +57,11 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE( template void fill_incremental_indices(std::shared_ptr exec, - size_type num_jumps, + size_type coarse_skip, array* 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; } } diff --git a/reference/test/multigrid/uniform_coarsening_kernels.cpp b/reference/test/multigrid/uniform_coarsening_kernels.cpp index 2ef53a41ca8..dfbbddd81a7 100644 --- a/reference/test/multigrid/uniform_coarsening_kernels.cpp +++ b/reference/test/multigrid/uniform_coarsening_kernels.cpp @@ -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( {I({2.0, -1.0}), I({-1.0, 2.0}), I({0.0, -1.0}), @@ -83,7 +83,7 @@ class UniformCoarsening : public ::testing::Test { mg_level = uniform_coarsening_factory->generate(mtx); } - void create_mtx(Mtx* fine, gko::Array* coarse_rows, Mtx* coarse) + void create_mtx(Mtx* fine, gko::array* coarse_rows, Mtx* coarse) { auto coarse_rows_val = coarse_rows->get_data(); coarse_rows_val[0] = 0; @@ -158,7 +158,7 @@ class UniformCoarsening : public ::testing::Test { std::shared_ptr exec; std::shared_ptr mtx; std::shared_ptr coarse; - gko::Array coarse_rows; + gko::array coarse_rows; std::shared_ptr coarse_b; std::shared_ptr fine_b; std::shared_ptr restrict_ans; @@ -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(copy_coarse.get()), this->coarse.get()); } @@ -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(); @@ -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(copy_coarse.get()), this->coarse.get()); } @@ -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(clone_coarse.get()), this->coarse.get()); } @@ -269,10 +269,14 @@ TYPED_TEST(UniformCoarsening, FillIncrementalIndicesWorks) { using index_type = typename TestFixture::index_type; auto c2_rows = - gko::Array(this->exec, {0, -1, 1, -1, 2, -1, 3, -1, 4, -1}); - auto c3_rows = gko::Array(this->exec, + gko::array(this->exec, {0, -1, 1, -1, 2, -1, 3, -1, 4, -1}); + auto c3_rows = gko::array(this->exec, {0, -1, -1, 1, -1, -1, 2, -1, -1, 3}); - auto c_rows = gko::Array(this->exec, 10); + auto c4_rows = gko::array( + this->exec, {0, -1, -1, -1, 1, -1, -1, -1, 2, -1}); + auto c5_rows = gko::array( + this->exec, {0, -1, -1, -1, -1, 1, -1, -1, -1, -1}); + auto c_rows = gko::array(this->exec, 10); c_rows.fill(-gko::one()); gko::kernels::reference::uniform_coarsening::fill_incremental_indices( @@ -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()); + 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()); + gko::kernels::reference::uniform_coarsening::fill_incremental_indices( + this->exec, 5, &c_rows); + GKO_ASSERT_ARRAY_EQ(c_rows, c5_rows); } @@ -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()); @@ -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 diff --git a/test/multigrid/CMakeLists.txt b/test/multigrid/CMakeLists.txt index bd0ea3b984a..6025e07af30 100644 --- a/test/multigrid/CMakeLists.txt +++ b/test/multigrid/CMakeLists.txt @@ -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) diff --git a/test/multigrid/uniform_coarsening_kernels.cpp b/test/multigrid/uniform_coarsening_kernels.cpp index bf52a2cd551..3132ecc47f1 100644 --- a/test/multigrid/uniform_coarsening_kernels.cpp +++ b/test/multigrid/uniform_coarsening_kernels.cpp @@ -5,7 +5,7 @@ #include "core/multigrid/uniform_coarsening_kernels.hpp" -#include +#include #include #include #include @@ -33,45 +33,22 @@ #include "test/utils/executor.hpp" -namespace { - - -class UniformCoarsening : public ::testing::Test { +class UniformCoarsening : public CommonTestFixture { protected: -#if GINKGO_COMMON_SINGLE_MODE - using value_type = float; -#else - using value_type = double; -#endif // GINKGO_COMMON_SINGLE_MODE - using index_type = gko::int32; using Mtx = gko::matrix::Dense; using Csr = gko::matrix::Csr; - UniformCoarsening() : rand_engine(30) {} - - void SetUp() - { - ref = gko::ReferenceExecutor::create(); - init_executor(ref, exec); - m = 597; - } - - void TearDown() - { - if (exec != nullptr) { - ASSERT_NO_THROW(exec->synchronize()); - } - } + UniformCoarsening() : rand_engine(30), m{597} {} gko::array gen_coarse_array(gko::size_type num, - gko::size_type num_jumps) + gko::size_type coarse_skip) { gko::array coarse_array(ref, num); coarse_array.fill(-1); // the aggregated group must contain the identifier-th element // agg_val[i] == i holds in the aggregated group whose identifier is i - for (gko::size_type i = 0; i < num; i += num_jumps) { - coarse_array.get_data()[i] = i / num_jumps; + for (gko::size_type i = 0; i < num; i += coarse_skip) { + coarse_array.get_data()[i] = i / coarse_skip; } return coarse_array; } @@ -84,10 +61,10 @@ class UniformCoarsening : public ::testing::Test { std::normal_distribution(-1.0, 1.0), rand_engine, ref); } - void initialize_data(gko::size_type num_jumps = 2) + void initialize_data(gko::size_type coarse_skip = 2) { - coarse_rows = gen_coarse_array(m, num_jumps); - c_dim = (coarse_rows.get_num_elems() + 1) / num_jumps; + coarse_rows = gen_coarse_array(m, coarse_skip); + c_dim = (coarse_rows.get_size() + 1) / coarse_skip; d_coarse_rows = gko::array(exec); d_coarse_rows = coarse_rows; @@ -102,9 +79,6 @@ class UniformCoarsening : public ::testing::Test { d_system_mtx = gko::clone(exec, system_mtx); } - std::shared_ptr ref; - std::shared_ptr exec; - std::default_random_engine rand_engine; gko::array coarse_rows; @@ -129,31 +103,31 @@ TEST_F(UniformCoarsening, FillIncrementalIndicesIsEquivalentToRef) auto d_c_rows = gko::array(exec, c_rows); { - gko::size_type num_jumps = 2; + gko::size_type coarse_skip = 2; gko::kernels::reference::uniform_coarsening::fill_incremental_indices( - ref, num_jumps, &c_rows); + ref, coarse_skip, &c_rows); gko::kernels::EXEC_NAMESPACE::uniform_coarsening:: - fill_incremental_indices(exec, num_jumps, &d_c_rows); + fill_incremental_indices(exec, coarse_skip, &d_c_rows); GKO_ASSERT_ARRAY_EQ(c_rows, d_c_rows); } { - gko::size_type num_jumps = 3; + gko::size_type coarse_skip = 3; c_rows.fill(-gko::one()); d_c_rows.fill(-gko::one()); gko::kernels::reference::uniform_coarsening::fill_incremental_indices( - ref, num_jumps, &c_rows); + ref, coarse_skip, &c_rows); gko::kernels::EXEC_NAMESPACE::uniform_coarsening:: - fill_incremental_indices(exec, num_jumps, &d_c_rows); + fill_incremental_indices(exec, coarse_skip, &d_c_rows); GKO_ASSERT_ARRAY_EQ(c_rows, d_c_rows); } { - gko::size_type num_jumps = 47; + gko::size_type coarse_skip = 47; c_rows.fill(-gko::one()); d_c_rows.fill(-gko::one()); gko::kernels::reference::uniform_coarsening::fill_incremental_indices( - ref, num_jumps, &c_rows); + ref, coarse_skip, &c_rows); gko::kernels::EXEC_NAMESPACE::uniform_coarsening:: - fill_incremental_indices(exec, num_jumps, &d_c_rows); + fill_incremental_indices(exec, coarse_skip, &d_c_rows); GKO_ASSERT_ARRAY_EQ(c_rows, d_c_rows); } } @@ -182,16 +156,16 @@ TEST_F(UniformCoarsening, FillRestrictOpIsEquivalentToRef) TEST_F(UniformCoarsening, GenerateMgLevelIsEquivalentToRef) { - gko::size_type num_jumps = 2; - initialize_data(num_jumps); + gko::size_type coarse_skip = 2; + initialize_data(coarse_skip); auto mg_level_factory = gko::multigrid::UniformCoarsening::build() - .with_num_jumps(static_cast(num_jumps)) + .with_coarse_skip(static_cast(coarse_skip)) .with_skip_sorting(true) .on(ref); auto d_mg_level_factory = gko::multigrid::UniformCoarsening::build() - .with_num_jumps(static_cast(num_jumps)) + .with_coarse_skip(static_cast(coarse_skip)) .with_skip_sorting(true) .on(exec); @@ -213,7 +187,7 @@ TEST_F(UniformCoarsening, GenerateMgLevelIsEquivalentToRef) TEST_F(UniformCoarsening, GenerateMgLevelIsEquivalentToRefOnUnsortedMatrix) { initialize_data(); - gko::test::unsort_matrix(gko::lend(system_mtx), rand_engine); + gko::test::unsort_matrix(system_mtx, rand_engine); d_system_mtx = gko::clone(exec, system_mtx); auto mg_level_factory = gko::multigrid::UniformCoarsening::build().on(ref); @@ -233,6 +207,3 @@ TEST_F(UniformCoarsening, GenerateMgLevelIsEquivalentToRefOnUnsortedMatrix) gko::as(mg_level->get_coarse_op()), r::value); } - - -} // namespace