Skip to content

Commit

Permalink
review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Jul 4, 2024
1 parent 4c21ff2 commit 185f551
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 158 deletions.
5 changes: 0 additions & 5 deletions common/unified/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
namespace gko {
namespace kernels {
namespace GKO_DEVICE_NAMESPACE {
/**
* @brief The UniformCoarsening namespace.
*
* @ingroup uniform_coarsening
*/
namespace uniform_coarsening {


Expand Down
4 changes: 2 additions & 2 deletions core/multigrid/uniform_coarsening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "core/components/fill_array_kernels.hpp"
#include "core/matrix/csr_builder.hpp"
#include "core/multigrid/uniform_coarsening_kernels.hpp"
#include "ginkgo/core/base/math.hpp"


namespace gko {
Expand Down Expand Up @@ -69,8 +70,7 @@ void UniformCoarsening<ValueType, IndexType>::generate()
parameters_.coarse_skip, &coarse_rows_));

gko::dim<2>::dimension_type coarse_dim =
(coarse_rows_.get_size() + parameters_.coarse_skip - 1) /
parameters_.coarse_skip;
gko::ceildiv(coarse_rows_.get_size(), 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
6 changes: 3 additions & 3 deletions core/test/multigrid/uniform_coarsening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UniformCoarseningFactory : public ::testing::Test {
UniformCoarseningFactory()
: exec(gko::ReferenceExecutor::create()),
uniform_coarsening1_factory(
MgLevel::build().with_coarse_skip(4u).with_skip_sorting(true).on(
MgLevel::build().with_coarse_skip(4).with_skip_sorting(true).on(
exec))
{}

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

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


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


Expand Down
1 change: 0 additions & 1 deletion examples/multigrid-preconditioned-solver/doc/results.dox
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ This is the expected output:

@code{.cpp}

Using PGM coarsening
Problem size: (8000, 8000)
Initial residual norm sqrt(r^T r):
%%MatrixMarket matrix array real general
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,74 +113,29 @@ int main(int argc, char* argv[])
// copy b again
b->copy_from(b_clone);

// Prepare the stopping criteria
const gko::remove_complex<ValueType> tolerance = 1e-8;
auto iter_stop = gko::share(
gko::stop::Iteration::build().with_max_iters(1000u).on(exec));
auto tol_stop = gko::share(gko::stop::ResidualNorm<ValueType>::build()
.with_reduction_factor(tolerance)
.with_baseline(gko::stop::mode::absolute)
.on(exec));

std::shared_ptr<const gko::log::Convergence<ValueType>> logger =
gko::log::Convergence<ValueType>::create();
iter_stop->add_logger(logger);
tol_stop->add_logger(logger);

// Create smoother factory (ir with bj)
auto inner_solver_gen =
gko::share(bj::build().with_max_block_size(1u).on(exec));
auto smoother_gen = gko::share(
ir::build()
.with_solver(inner_solver_gen)
.with_relaxation_factor(0.9)
.with_criteria(
gko::stop::Iteration::build().with_max_iters(1u).on(exec))
.on(exec));
// Create MultigridLevel factory
auto mg_level_gen =
gko::share(pgm::build().with_deterministic(true).on(exec));
// Create MultigridLevel factory
auto coarse_unif_gen = gko::share(
uniform_coarsening::build().with_coarse_skip(coarse_skip).on(exec));

// Create CoarsestSolver factory
auto coarsest_gen = gko::share(
ir::build()
.with_solver(inner_solver_gen)
.with_relaxation_factor(0.9)
.with_criteria(
gko::stop::Iteration::build().with_max_iters(4u).on(exec))
.on(exec));
// Create multigrid factory
auto multigrid_gen = gko::share(mg::build()
.with_max_levels(10u)
.with_min_coarse_rows(10u)
.with_pre_smoother(smoother_gen)
.with_post_uses_pre(true)
.with_mg_level(mg_level_gen)
.with_coarsest_solver(coarsest_gen)
.with_criteria(iter_stop, tol_stop)
.on(exec));
std::shared_ptr<gko::LinOpFactory> mg_level;
if (coarse_type == std::string("uniform")) {
std::cout << "Using Uniform coarsening" << std::endl;
multigrid_gen = gko::share(mg::build()
.with_max_levels(10u)
.with_min_coarse_rows(10u)
.with_pre_smoother(smoother_gen)
.with_post_uses_pre(true)
.with_mg_level(coarse_unif_gen)
.with_coarsest_solver(coarsest_gen)
.with_criteria(iter_stop, tol_stop)
.on(exec));
mg_level = uniform_coarsening::build().with_coarse_skip(2).on(exec);
} else {
std::cout << "Using PGM coarsening" << std::endl;
mg_level = pgm::build().with_deterministic(true).on(exec);
}
// Create solver factory
auto solver_gen = cg::build()
.with_criteria(iter_stop, tol_stop)
.with_preconditioner(multigrid_gen)
.on(exec);
std::shared_ptr<gko::LinOpFactory> multigrid_gen;
multigrid_gen =
mg::build()
.with_mg_level(mg_level)
.with_criteria(gko::stop::Iteration::build().with_max_iters(1u))
.on(exec);
const gko::remove_complex<ValueType> tolerance = 1e-8;
auto solver_gen =
cg::build()
.with_criteria(gko::stop::Iteration::build().with_max_iters(100u),
gko::stop::ResidualNorm<ValueType>::build()
.with_baseline(gko::stop::mode::absolute)
.with_reduction_factor(tolerance))
.with_preconditioner(multigrid_gen)
.on(exec);

// Create solver
std::chrono::nanoseconds gen_time(0);
auto gen_tic = std::chrono::steady_clock::now();
Expand All @@ -191,8 +146,11 @@ int main(int argc, char* argv[])
std::chrono::duration_cast<std::chrono::nanoseconds>(gen_toc - gen_tic);

// Add logger
std::shared_ptr<const gko::log::Convergence<ValueType>> logger =
gko::log::Convergence<ValueType>::create();
solver->add_logger(logger);


// Solve system
exec->synchronize();
std::chrono::nanoseconds time(0);
Expand Down
2 changes: 1 addition & 1 deletion include/ginkgo/core/multigrid/uniform_coarsening.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class UniformCoarsening
/**
* The number of rows to skip for the coarse matrix generation
*/
unsigned GKO_FACTORY_PARAMETER_SCALAR(coarse_skip, 2u);
int GKO_FACTORY_PARAMETER_SCALAR(coarse_skip, 2);

/**
* The `system_matrix`, which will be given to this factory, must be
Expand Down
5 changes: 0 additions & 5 deletions reference/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
namespace gko {
namespace kernels {
namespace reference {
/**
* @brief The UNIFORM_COARSENING solver namespace.
*
* @ingroup uniform_coarsening
*/
namespace uniform_coarsening {


Expand Down
78 changes: 29 additions & 49 deletions reference/test/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UniformCoarsening : public ::testing::Test {
UniformCoarsening()
: exec(gko::ReferenceExecutor::create()),
uniform_coarsening_factory(
MgLevel::build().with_coarse_skip(2u).with_skip_sorting(true).on(
MgLevel::build().with_coarse_skip(2).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 @@ -128,29 +128,6 @@ class UniformCoarsening : public ::testing::Test {
{2, 2, 5}}});
}

static void assert_same_matrices(const Mtx* m1, const Mtx* m2)
{
ASSERT_EQ(m1->get_size()[0], m2->get_size()[0]);
ASSERT_EQ(m1->get_size()[1], m2->get_size()[1]);
ASSERT_EQ(m1->get_num_stored_elements(), m2->get_num_stored_elements());
for (gko::size_type i = 0; i < m1->get_size()[0] + 1; i++) {
ASSERT_EQ(m1->get_const_row_ptrs()[i], m2->get_const_row_ptrs()[i]);
}
for (gko::size_type i = 0; i < m1->get_num_stored_elements(); ++i) {
EXPECT_EQ(m1->get_const_values()[i], m2->get_const_values()[i]);
EXPECT_EQ(m1->get_const_col_idxs()[i], m2->get_const_col_idxs()[i]);
}
}

static void assert_same_coarse_rows(const index_type* m1,
const index_type* m2,
gko::size_type len)
{
for (gko::size_type i = 0; i < len; ++i) {
EXPECT_EQ(m1[i], m2[i]);
}
}

std::shared_ptr<const gko::ReferenceExecutor> exec;
std::shared_ptr<Mtx> mtx;
std::shared_ptr<Mtx> coarse;
Expand All @@ -172,7 +149,7 @@ TYPED_TEST_SUITE(UniformCoarsening, gko::test::ValueIndexTypes,
TYPED_TEST(UniformCoarsening, CanBeCopied)
{
using Mtx = typename TestFixture::Mtx;
using MgLevel = typename TestFixture::MgLevel;
using IndexType = typename TestFixture::index_type;
auto copy =
this->uniform_coarsening_factory->generate(Mtx::create(this->exec));

Expand All @@ -181,20 +158,21 @@ TYPED_TEST(UniformCoarsening, CanBeCopied)
auto copy_coarse_rows = copy->get_const_coarse_rows();
auto copy_coarse = copy->get_coarse_op();

this->assert_same_matrices(static_cast<const Mtx*>(copy_mtx.get()),
this->mtx.get());
this->assert_same_coarse_rows(copy_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get());
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(copy_mtx.get()),
this->mtx.get(), 0.0);
GKO_ASSERT_ARRAY_EQ(
gko::array<IndexType>::const_view(
this->exec, this->coarse_rows.get_size(), copy_coarse_rows),
this->coarse_rows);
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get(), 0.0);
}


TYPED_TEST(UniformCoarsening, CanBeMoved)
{
using Mtx = typename TestFixture::Mtx;
using MgLevel = typename TestFixture::MgLevel;
using IndexType = typename TestFixture::index_type;
auto copy =
this->uniform_coarsening_factory->generate(Mtx::create(this->exec));

Expand All @@ -203,32 +181,34 @@ TYPED_TEST(UniformCoarsening, CanBeMoved)
auto copy_coarse_rows = copy->get_const_coarse_rows();
auto copy_coarse = copy->get_coarse_op();

this->assert_same_matrices(static_cast<const Mtx*>(copy_mtx.get()),
this->mtx.get());
this->assert_same_coarse_rows(copy_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get());
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(copy_mtx.get()),
this->mtx.get(), 0.0);
GKO_ASSERT_ARRAY_EQ(
gko::array<IndexType>::const_view(
this->exec, this->coarse_rows.get_size(), copy_coarse_rows),
this->coarse_rows);
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(copy_coarse.get()),
this->coarse.get(), 0.0);
}


TYPED_TEST(UniformCoarsening, CanBeCloned)
{
using Mtx = typename TestFixture::Mtx;
using MgLevel = typename TestFixture::MgLevel;
using IndexType = typename TestFixture::index_type;
auto clone = this->mg_level->clone();
auto clone_mtx = clone->get_system_matrix();
auto clone_coarse_rows = clone->get_const_coarse_rows();
auto clone_coarse = clone->get_coarse_op();

this->assert_same_matrices(static_cast<const Mtx*>(clone_mtx.get()),
this->mtx.get());
this->assert_same_coarse_rows(clone_coarse_rows,
this->coarse_rows.get_data(),
this->coarse_rows.get_size());
this->assert_same_matrices(static_cast<const Mtx*>(clone_coarse.get()),
this->coarse.get());
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(clone_mtx.get()),
this->mtx.get(), 0.0);
GKO_ASSERT_ARRAY_EQ(
gko::array<IndexType>::const_view(
this->exec, this->coarse_rows.get_size(), clone_coarse_rows),
this->coarse_rows);
GKO_ASSERT_MTX_NEAR(static_cast<const Mtx*>(clone_coarse.get()),
this->coarse.get(), 0.0);
}


Expand Down Expand Up @@ -408,7 +388,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_coarse_skip(2u).on(this->exec);
auto mglevel_sort = MgLevel::build().with_coarse_skip(2).on(this->exec);
/* this unsorted matrix is stored as this->fine:
* 5 -3 -3 0 0
* -3 5 0 -2 -1
Expand Down
35 changes: 7 additions & 28 deletions test/multigrid/uniform_coarsening_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class UniformCoarsening : public CommonTestFixture {

UniformCoarsening() : rand_engine(30), m{597} {}

gko::array<index_type> gen_coarse_array(gko::size_type num,
gko::size_type coarse_skip)
gko::array<index_type> gen_coarse_array(gko::size_type num, int coarse_skip)
{
gko::array<index_type> coarse_array(ref, num);
coarse_array.fill(-1);
Expand All @@ -58,7 +57,7 @@ class UniformCoarsening : public CommonTestFixture {
std::normal_distribution<value_type>(-1.0, 1.0), rand_engine, ref);
}

void initialize_data(gko::size_type coarse_skip = 2)
void initialize_data(int coarse_skip = 2)
{
coarse_rows = gen_coarse_array(m, coarse_skip);
c_dim = (coarse_rows.get_size() + 1) / coarse_skip;
Expand Down Expand Up @@ -99,28 +98,8 @@ TEST_F(UniformCoarsening, FillIncrementalIndicesIsEquivalentToRef)
c_rows.fill(-gko::one<index_type>());
auto d_c_rows = gko::array<index_type>(exec, c_rows);

{
gko::size_type coarse_skip = 2;
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
ref, coarse_skip, &c_rows);
gko::kernels::GKO_DEVICE_NAMESPACE::uniform_coarsening::
fill_incremental_indices(exec, coarse_skip, &d_c_rows);
GKO_ASSERT_ARRAY_EQ(c_rows, d_c_rows);
}
{
gko::size_type coarse_skip = 3;
c_rows.fill(-gko::one<index_type>());
d_c_rows.fill(-gko::one<index_type>());
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
ref, coarse_skip, &c_rows);
gko::kernels::GKO_DEVICE_NAMESPACE::uniform_coarsening::
fill_incremental_indices(exec, coarse_skip, &d_c_rows);
GKO_ASSERT_ARRAY_EQ(c_rows, d_c_rows);
}
{
gko::size_type coarse_skip = 47;
c_rows.fill(-gko::one<index_type>());
d_c_rows.fill(-gko::one<index_type>());
for (int coarse_skip : {2, 3, 47}) {
SCOPED_TRACE("Using coarse skip:" + std::to_string(coarse_skip));
gko::kernels::reference::uniform_coarsening::fill_incremental_indices(
ref, coarse_skip, &c_rows);
gko::kernels::GKO_DEVICE_NAMESPACE::uniform_coarsening::
Expand Down Expand Up @@ -153,16 +132,16 @@ TEST_F(UniformCoarsening, FillRestrictOpIsEquivalentToRef)

TEST_F(UniformCoarsening, GenerateMgLevelIsEquivalentToRef)
{
gko::size_type coarse_skip = 2;
int coarse_skip = 2;
initialize_data(coarse_skip);
auto mg_level_factory =
gko::multigrid::UniformCoarsening<value_type, int>::build()
.with_coarse_skip(static_cast<unsigned>(coarse_skip))
.with_coarse_skip(coarse_skip)
.with_skip_sorting(true)
.on(ref);
auto d_mg_level_factory =
gko::multigrid::UniformCoarsening<value_type, int>::build()
.with_coarse_skip(static_cast<unsigned>(coarse_skip))
.with_coarse_skip(coarse_skip)
.with_skip_sorting(true)
.on(exec);

Expand Down

0 comments on commit 185f551

Please sign in to comment.