Skip to content

Commit

Permalink
reorder with half
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Oct 28, 2024
1 parent af35c1f commit e3bccda
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 29 deletions.
32 changes: 16 additions & 16 deletions core/reorder/mc64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ void initialize_weights(const matrix::Csr<ValueType, IndexType>* host_mtx,
array<remove_complex<ValueType>>& row_maxima_array,
gko::experimental::reorder::mc64_strategy strategy)
{
constexpr auto inf =
std::numeric_limits<remove_complex<ValueType>>::infinity();
const auto inf = std::numeric_limits<remove_complex<ValueType>>::infinity();
const auto num_rows = host_mtx->get_size()[0];
const auto row_ptrs = host_mtx->get_const_row_ptrs();
const auto col_idxs = host_mtx->get_const_col_idxs();
Expand Down Expand Up @@ -67,11 +66,13 @@ void initialize_weights(const matrix::Csr<ValueType, IndexType>* host_mtx,
}
}
};
if (strategy ==
gko::experimental::reorder::mc64_strategy::max_diagonal_sum) {
run_computation([](ValueType a) { return abs(a); });
if (strategy == mc64_strategy::max_diagonal_sum) {
run_computation(
[](ValueType a) -> remove_complex<ValueType> { return abs(a); });
} else {
run_computation([](ValueType a) { return std::log2(abs(a)); });
run_computation([](ValueType a) -> remove_complex<ValueType> {
return std::log2(abs(a));
});
}
}

Expand Down Expand Up @@ -179,7 +180,7 @@ void shortest_augmenting_path(
addressable_priority_queue<ValueType, IndexType>& queue,
std::vector<IndexType>& q_j, ValueType tolerance)
{
constexpr auto inf = std::numeric_limits<ValueType>::infinity();
const auto inf = std::numeric_limits<ValueType>::infinity();
auto weights = weights_array.get_data();
auto dual_u = dual_u_array.get_data();
auto distance = distance_array.get_data();
Expand Down Expand Up @@ -433,8 +434,7 @@ void compute_scaling(const matrix::Csr<ValueType, IndexType>* host_mtx,
mc64_strategy strategy, ValueType* row_scaling,
ValueType* col_scaling)
{
constexpr auto inf =
std::numeric_limits<remove_complex<ValueType>>::infinity();
const auto inf = std::numeric_limits<remove_complex<ValueType>>::infinity();
const auto num_rows = host_mtx->get_size()[0];
const auto weights = weights_array.get_const_data();
const auto dual_u = dual_u_array.get_const_data();
Expand All @@ -459,13 +459,14 @@ void compute_scaling(const matrix::Csr<ValueType, IndexType>* host_mtx,
}


GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE_WITH_HALF(
GKO_DECLARE_MC64_INITIALIZE_WEIGHTS);
GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(
GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE_WITH_HALF(
GKO_DECLARE_MC64_INITIAL_MATCHING);
GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(
GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE_WITH_HALF(
GKO_DECLARE_MC64_SHORTEST_AUGMENTING_PATH);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_MC64_COMPUTE_SCALING);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE_WITH_HALF(
GKO_DECLARE_MC64_COMPUTE_SCALING);


} // namespace mc64
Expand Down Expand Up @@ -538,8 +539,7 @@ std::unique_ptr<LinOp> Mc64<ValueType, IndexType>::generate_impl(
marked_cols.fill(0);
matched_idxs.fill(0);
unmatched_rows.fill(0);
constexpr auto inf =
std::numeric_limits<remove_complex<ValueType>>::infinity();
const auto inf = std::numeric_limits<remove_complex<ValueType>>::infinity();
dual_u.fill(inf);
distance.fill(inf);

Expand Down Expand Up @@ -588,7 +588,7 @@ std::unique_ptr<LinOp> Mc64<ValueType, IndexType>::generate_impl(


#define GKO_DECLARE_MC64(ValueType, IndexType) class Mc64<ValueType, IndexType>
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_MC64);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE_WITH_HALF(GKO_DECLARE_MC64);


} // namespace reorder
Expand Down
2 changes: 1 addition & 1 deletion core/reorder/rcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Rcm<ValueType, IndexType>::Rcm(const Factory* factory,


#define GKO_DECLARE_RCM(ValueType, IndexType) class Rcm<ValueType, IndexType>
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_RCM);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE_WITH_HALF(GKO_DECLARE_RCM);


} // namespace reorder
Expand Down
3 changes: 2 additions & 1 deletion core/reorder/scaled_reordered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void ScaledReordered<ValueType, IndexType>::apply_impl(const LinOp* alpha,

#define GKO_DECLARE_SCALED_REORDERED(ValueType, IndexType) \
class ScaledReordered<ValueType, IndexType>
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SCALED_REORDERED);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE_WITH_HALF(
GKO_DECLARE_SCALED_REORDERED);


} // namespace reorder
Expand Down
7 changes: 5 additions & 2 deletions reference/test/reorder/mc64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class Mc64 : public ::testing::Test {
std::unique_ptr<reorder_type> mc64_factory;
};

TYPED_TEST_SUITE(Mc64, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Mc64, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Mc64, HasSensibleDefaults)
Expand All @@ -86,11 +87,13 @@ TYPED_TEST(Mc64, HasSensibleDefaults)
TYPED_TEST(Mc64, CanBeCreatedWithReorderingStrategy)
{
using reorder_type = typename TestFixture::reorder_type;
using real_type = typename TestFixture::real_type;

auto mc64 =
reorder_type::build()
.with_strategy(
gko::experimental::reorder::mc64_strategy::max_diagonal_sum)
.with_tolerance(real_type{1e-4})
.on(this->exec)
->generate(this->not_id3_mtx);

Expand Down Expand Up @@ -123,7 +126,7 @@ TYPED_TEST(Mc64, CanBeCreatedWithTolerance)
using real_type = typename TestFixture::real_type;

auto mc64 = reorder_type::build()
.with_tolerance(real_type{1e-10})
.with_tolerance(real_type{1e-4})
.on(this->exec)
->generate(this->id3_mtx);

Expand Down
18 changes: 13 additions & 5 deletions reference/test/reorder/mc64_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <gtest/gtest.h>

#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/permutation.hpp>
Expand Down Expand Up @@ -118,7 +119,7 @@ class Mc64 : public ::testing::Test {
0., 2., 3., 0.}},
final_dual_u{ref, I<real_type>{0., 1., -1., -2., 0., 0.}},
final_distance{ref, I<real_type>{inf(), inf(), 1., 0., inf(), 1.}},
zero_tol{1e-14}
zero_tol{1e-4}
{}

std::pair<std::shared_ptr<const perm_type>,
Expand All @@ -134,8 +135,8 @@ class Mc64 : public ::testing::Test {
{
ASSERT_EQ(a.get_size(), b.get_size());
for (gko::size_type i = 0; i < a.get_size(); i++) {
if (std::isfinite(a.get_const_data()[i]) ||
std::isfinite(b.get_const_data()[i])) {
if (gko::is_finite(a.get_const_data()[i]) ||
gko::is_finite(b.get_const_data()[i])) {
ASSERT_NEAR(a.get_const_data()[i], b.get_const_data()[i],
r<value_type>::value)
<< name << '[' << i << ']';
Expand Down Expand Up @@ -180,7 +181,8 @@ class Mc64 : public ::testing::Test {
const real_type zero_tol;
};

TYPED_TEST_SUITE(Mc64, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Mc64, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Mc64, InitializeWeightsSum)
Expand Down Expand Up @@ -284,6 +286,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingExampleSum)
gko::experimental::reorder::Mc64<value_type, index_type>::build()
.with_strategy(
gko::experimental::reorder::mc64_strategy::max_diagonal_sum)
.with_tolerance(real_type{1e-4})
.on(this->ref);

auto mc64 = mc64_factory->generate(this->mtx);
Expand All @@ -303,10 +306,12 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingExampleProduct)
{
using index_type = typename TestFixture::index_type;
using value_type = typename TestFixture::value_type;
using real_type = typename TestFixture::real_type;
auto mc64_factory =
gko::experimental::reorder::Mc64<value_type, index_type>::build()
.with_strategy(
gko::experimental::reorder::mc64_strategy::max_diagonal_product)
.with_tolerance(real_type{1e-4})
.on(this->ref);
auto mc64 = mc64_factory->generate(this->mtx);

Expand Down Expand Up @@ -354,6 +359,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingLargeTrivialExampleProduct)
gko::experimental::reorder::Mc64<value_type, index_type>::build()
.with_strategy(
gko::experimental::reorder::mc64_strategy::max_diagonal_product)
.with_tolerance(real_type{1e-4})
.on(this->ref);
auto mc64 = mc64_factory->generate(mtx);
// get components
Expand All @@ -362,7 +368,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingLargeTrivialExampleProduct)

mtx = mtx->scale_permute(row_perm, col_perm);

GKO_ASSERT_MTX_NEAR(mtx, expected_result, r<value_type>::value);
GKO_ASSERT_MTX_NEAR(mtx, expected_result, 20 * r<value_type>::value);
}


Expand All @@ -373,6 +379,8 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingLargeExampleProduct)
using value_type = typename TestFixture::value_type;
using matrix_type = typename TestFixture::matrix_type;
using perm_type = typename TestFixture::perm_type;
// this example can not be finished in half precision
SKIP_IF_HALF(value_type);
// read input data
std::ifstream mtx_stream{gko::matrices::location_nontrivial_mc64_example};
auto mtx = gko::share(gko::read<matrix_type>(mtx_stream, this->ref));
Expand Down
3 changes: 2 additions & 1 deletion reference/test/reorder/rcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Rcm : public ::testing::Test {
std::unique_ptr<reorder_type> reorder_op;
};

TYPED_TEST_SUITE(Rcm, gko::test::ValueIndexTypes, PairTypenameNameGenerator);
TYPED_TEST_SUITE(Rcm, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


TYPED_TEST(Rcm, CanBeCleared)
Expand Down
14 changes: 11 additions & 3 deletions reference/test/reorder/scaled_reordered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ScaledReordered : public ::testing::Test {
gko::remove_complex<value_type> tol;
};

TYPED_TEST_SUITE(ScaledReordered, gko::test::ValueIndexTypes,
TYPED_TEST_SUITE(ScaledReordered, gko::test::ValueIndexTypesWithHalf,
PairTypenameNameGenerator);


Expand Down Expand Up @@ -364,6 +364,8 @@ TYPED_TEST(ScaledReordered, AppliesWithRcmReordering)
TYPED_TEST(ScaledReordered, SolvesSingleRhsWithOnlyInnerOperator)
{
using SR = typename TestFixture::SR;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto scaled_reordered_fact =
SR::build().with_inner_operator(this->solver_factory).on(this->exec);
auto scaled_reordered = scaled_reordered_fact->generate(this->rcm_mtx);
Expand Down Expand Up @@ -410,6 +412,8 @@ TYPED_TEST(ScaledReordered, SolvesSingleRhsWithColScaling)
TYPED_TEST(ScaledReordered, SolvesSingleRhsWithRcmReordering)
{
using SR = typename TestFixture::SR;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto scaled_reordered_fact = SR::build()
.with_reordering(this->rcm_factory)
.with_inner_operator(this->solver_factory)
Expand Down Expand Up @@ -445,7 +449,8 @@ TYPED_TEST(ScaledReordered, SolvesSingleRhsWithScalingAndRcmReorderingMixed)
{
using SR = typename TestFixture::SR;
using T = typename TestFixture::value_type;
using Vec = gko::matrix::Dense<gko::next_precision<T>>;
using Vec = gko::matrix::Dense<gko::next_precision_with_half<T>>;
SKIP_IF_HALF(T);
auto scaled_reordered_fact = SR::build()
.with_row_scaling(this->diag2)
.with_col_scaling(this->diag3)
Expand All @@ -467,6 +472,8 @@ TYPED_TEST(ScaledReordered, AdvancedSolvesSingleRhsWithScalingAndRcmReordering)
{
using SR = typename TestFixture::SR;
using Vec = typename TestFixture::Vec;
using T = typename TestFixture::value_type;
SKIP_IF_HALF(T);
const auto alpha = gko::initialize<Vec>({2.0}, this->exec);
const auto beta = gko::initialize<Vec>({-1.0}, this->exec);
auto scaled_reordered_fact = SR::build()
Expand All @@ -489,8 +496,9 @@ TYPED_TEST(ScaledReordered,
{
using SR = typename TestFixture::SR;
using T = typename TestFixture::value_type;
using value_type = gko::next_precision<T>;
using value_type = gko::next_precision_with_half<T>;
using Vec = gko::matrix::Dense<value_type>;
SKIP_IF_HALF(T);
auto scaled_reordered_fact = SR::build()
.with_row_scaling(this->diag2)
.with_col_scaling(this->diag3)
Expand Down

0 comments on commit e3bccda

Please sign in to comment.