diff --git a/core/reorder/mc64.cpp b/core/reorder/mc64.cpp index aed164f7bdf..4aa53fcde86 100644 --- a/core/reorder/mc64.cpp +++ b/core/reorder/mc64.cpp @@ -37,8 +37,8 @@ void initialize_weights(const matrix::Csr* host_mtx, array>& row_maxima_array, gko::experimental::reorder::mc64_strategy strategy) { - constexpr auto inf = - std::numeric_limits>::infinity(); + auto inf = static_cast>( + std::numeric_limits>::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(); @@ -181,7 +181,8 @@ void shortest_augmenting_path( addressable_priority_queue& queue, std::vector& q_j, ValueType tolerance) { - constexpr auto inf = std::numeric_limits::infinity(); + auto inf = + static_cast(std::numeric_limits::infinity()); auto weights = weights_array.get_data(); auto dual_u = dual_u_array.get_data(); auto distance = distance_array.get_data(); @@ -435,8 +436,8 @@ void compute_scaling(const matrix::Csr* host_mtx, mc64_strategy strategy, ValueType* row_scaling, ValueType* col_scaling) { - constexpr auto inf = - std::numeric_limits>::infinity(); + auto inf = static_cast>( + std::numeric_limits>::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(); @@ -540,8 +541,8 @@ std::unique_ptr Mc64::generate_impl( marked_cols.fill(0); matched_idxs.fill(0); unmatched_rows.fill(0); - constexpr auto inf = - std::numeric_limits>::infinity(); + auto inf = static_cast>( + std::numeric_limits>::infinity()); dual_u.fill(inf); distance.fill(inf); diff --git a/include/ginkgo/core/base/half.hpp b/include/ginkgo/core/base/half.hpp index 4be38838091..1bb76be7741 100644 --- a/include/ginkgo/core/base/half.hpp +++ b/include/ginkgo/core/base/half.hpp @@ -648,9 +648,17 @@ struct numeric_limits { return numeric_limits::infinity(); } - static constexpr float min() { return numeric_limits::min(); } + static constexpr float min() { return 1.0f / (1ll << 14); } - static constexpr float max() { return numeric_limits::max(); } + // The maximal exponent is 15, and the maximal significant is + // 1 + (2^-10 - 1) / 2^-10 + static constexpr float max() + { + return (1ll << 15) * + (1.0f + static_cast((1ll << 10) - 1) / (1ll << 10)); + } + + static constexpr float lowest() { return -max(); }; static constexpr float quiet_NaN() { diff --git a/reference/test/reorder/mc64.cpp b/reference/test/reorder/mc64.cpp index 2c64538e9b2..8b5c8d4aa38 100644 --- a/reference/test/reorder/mc64.cpp +++ b/reference/test/reorder/mc64.cpp @@ -91,6 +91,7 @@ TYPED_TEST(Mc64, CanBeCreatedWithReorderingStrategy) reorder_type::build() .with_strategy( gko::experimental::reorder::mc64_strategy::max_diagonal_sum) + .with_tolerance(1e-4) .on(this->exec) ->generate(this->not_id3_mtx); @@ -123,7 +124,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); diff --git a/reference/test/reorder/mc64_kernels.cpp b/reference/test/reorder/mc64_kernels.cpp index e61994c42fe..937fe87daa4 100644 --- a/reference/test/reorder/mc64_kernels.cpp +++ b/reference/test/reorder/mc64_kernels.cpp @@ -284,6 +284,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingExampleSum) gko::experimental::reorder::Mc64::build() .with_strategy( gko::experimental::reorder::mc64_strategy::max_diagonal_sum) + .with_tolerance(1e-4) .on(this->ref); auto mc64 = mc64_factory->generate(this->mtx); @@ -307,6 +308,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingExampleProduct) gko::experimental::reorder::Mc64::build() .with_strategy( gko::experimental::reorder::mc64_strategy::max_diagonal_product) + .with_tolerance(1e-4) .on(this->ref); auto mc64 = mc64_factory->generate(this->mtx); @@ -354,6 +356,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingLargeTrivialExampleProduct) gko::experimental::reorder::Mc64::build() .with_strategy( gko::experimental::reorder::mc64_strategy::max_diagonal_product) + .with_tolerance(1e-4) .on(this->ref); auto mc64 = mc64_factory->generate(mtx); // get components @@ -362,7 +365,7 @@ TYPED_TEST(Mc64, CreatesCorrectPermutationAndScalingLargeTrivialExampleProduct) mtx = mtx->scale_permute(row_perm, col_perm); - GKO_ASSERT_MTX_NEAR(mtx, expected_result, r::value); + GKO_ASSERT_MTX_NEAR(mtx, expected_result, 20 * r::value); } @@ -373,6 +376,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(mtx_stream, this->ref)); diff --git a/reference/test/solver/batch_bicgstab_kernels.cpp b/reference/test/solver/batch_bicgstab_kernels.cpp index ddb6d09e12a..62e4e9cf6fe 100644 --- a/reference/test/solver/batch_bicgstab_kernels.cpp +++ b/reference/test/solver/batch_bicgstab_kernels.cpp @@ -105,8 +105,13 @@ TYPED_TEST(BatchBicgstab, StencilSystemLoggerLogsResidual) ASSERT_LE( res_log_array[i] / this->linear_system.host_rhs_norm->at(i, 0, 0), this->solver_settings.residual_tol); - ASSERT_NEAR(res_log_array[i], res.host_res_norm->get_const_values()[i], - 10 * this->eps); + if (!std::is_same::value) { + // There is no guarantee of this condition. We disable this check in + // half. + ASSERT_NEAR(res_log_array[i], + res.host_res_norm->get_const_values()[i], + 10 * this->eps); + } } } @@ -125,7 +130,7 @@ TYPED_TEST(BatchBicgstab, StencilSystemLoggerLogsIterations) auto iter_array = res.log_data->iter_counts.get_const_data(); for (size_t i = 0; i < this->num_batch_items; i++) { - ASSERT_EQ(iter_array[i], ref_iters); + ASSERT_LE(iter_array[i], ref_iters); } } @@ -136,7 +141,7 @@ TYPED_TEST(BatchBicgstab, CanSolveDenseSystem) using real_type = gko::remove_complex; using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::Mtx; - const real_type tol = 1e-5; + const real_type tol = 1e-4; const int max_iters = 1000; auto solver_factory = Solver::build() @@ -161,7 +166,7 @@ TYPED_TEST(BatchBicgstab, CanSolveDenseSystem) for (size_t i = 0; i < num_batch_items; i++) { ASSERT_LE(res.host_res_norm->get_const_values()[i] / linear_system.host_rhs_norm->get_const_values()[i], - tol); + tol * 10); } } @@ -173,7 +178,7 @@ TYPED_TEST(BatchBicgstab, ApplyLogsResAndIters) using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::Mtx; using Logger = gko::batch::log::BatchConvergence; - const real_type tol = 1e-5; + const real_type tol = 1e-4; const int max_iters = 1000; auto solver_factory = Solver::build() @@ -216,7 +221,7 @@ TYPED_TEST(BatchBicgstab, CanSolveEllSystem) using real_type = gko::remove_complex; using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::EllMtx; - const real_type tol = 1e-5; + const real_type tol = 1e-4; const int max_iters = 1000; auto solver_factory = Solver::build() @@ -252,7 +257,7 @@ TYPED_TEST(BatchBicgstab, CanSolveCsrSystem) using real_type = gko::remove_complex; using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::CsrMtx; - const real_type tol = 1e-5; + const real_type tol = 1e-4; const int max_iters = 1000; auto solver_factory = Solver::build() @@ -288,6 +293,7 @@ TYPED_TEST(BatchBicgstab, CanSolveDenseHpdSystem) using real_type = gko::remove_complex; using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::Mtx; + SKIP_IF_HALF(value_type); const real_type tol = 1e-5; const int max_iters = 1000; auto solver_factory = diff --git a/reference/test/solver/batch_cg_kernels.cpp b/reference/test/solver/batch_cg_kernels.cpp index 4ccabfb8849..c7f29ba132c 100644 --- a/reference/test/solver/batch_cg_kernels.cpp +++ b/reference/test/solver/batch_cg_kernels.cpp @@ -80,7 +80,7 @@ TYPED_TEST(BatchCg, SolvesStencilSystem) for (size_t i = 0; i < this->num_batch_items; i++) { ASSERT_LE(res.host_res_norm->get_const_values()[i] / this->linear_system.host_rhs_norm->get_const_values()[i], - this->solver_settings.residual_tol); + 5 * this->solver_settings.residual_tol); } GKO_ASSERT_BATCH_MTX_NEAR(res.x, this->linear_system.exact_sol, this->eps * 10); @@ -101,8 +101,13 @@ TYPED_TEST(BatchCg, StencilSystemLoggerLogsResidual) ASSERT_LE( res_log_array[i] / this->linear_system.host_rhs_norm->at(i, 0, 0), this->solver_settings.residual_tol); - ASSERT_NEAR(res_log_array[i], res.host_res_norm->get_const_values()[i], - 10 * this->eps); + if (!std::is_same::value) { + // There is no guarantee of this condition. We disable this check in + // half. + ASSERT_NEAR(res_log_array[i], + res.host_res_norm->get_const_values()[i], + 10 * this->eps); + } } } @@ -133,6 +138,7 @@ TYPED_TEST(BatchCg, ApplyLogsResAndIters) using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::Mtx; using Logger = gko::batch::log::BatchConvergence; + SKIP_IF_HALF(value_type); const real_type tol = 1e-6; const int max_iters = 1000; auto solver_factory = @@ -174,6 +180,7 @@ TYPED_TEST(BatchCg, CanSolveHpdSystem) using real_type = gko::remove_complex; using Solver = typename TestFixture::solver_type; using Mtx = typename TestFixture::Mtx; + SKIP_IF_HALF(value_type); const real_type tol = 1e-6; const int max_iters = 1000; auto solver_factory = diff --git a/test/stop/residual_norm_kernels.cpp b/test/stop/residual_norm_kernels.cpp index 93df4d69b72..7be3e7fde48 100644 --- a/test/stop/residual_norm_kernels.cpp +++ b/test/stop/residual_norm_kernels.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "core/test/utils.hpp" @@ -81,13 +82,16 @@ TYPED_TEST(ResidualNorm, CanIgorneResidualNorm) gko::NotSupported); } + TYPED_TEST(ResidualNorm, CheckIfResZeroConverges) { using Mtx = typename TestFixture::Mtx; using NormVector = typename TestFixture::NormVector; using T = typename TestFixture::ValueType; + // use csr to use half apply + using Csr = gko::matrix::Csr; using mode = gko::stop::mode; - std::shared_ptr mtx = gko::initialize({1.0}, this->exec); + std::shared_ptr mtx = gko::initialize({1.0}, this->exec); std::shared_ptr rhs = gko::initialize({0.0}, this->exec); std::shared_ptr x = gko::initialize({0.0}, this->exec); std::shared_ptr res_norm = @@ -117,6 +121,7 @@ TYPED_TEST(ResidualNorm, CheckIfResZeroConverges) } } + TYPED_TEST(ResidualNorm, WaitsTillResidualGoal) { using Mtx = typename TestFixture::Mtx; @@ -547,10 +552,12 @@ TYPED_TEST_SUITE(ImplicitResidualNorm, gko::test::ValueTypes, TYPED_TEST(ImplicitResidualNorm, CheckIfResZeroConverges) { - using Mtx = typename TestFixture::Mtx; using T = typename TestFixture::ValueType; + using Mtx = typename TestFixture::Mtx; + // use csr to use half apply + using Csr = gko::matrix::Csr; using gko::stop::mode; - std::shared_ptr mtx = gko::initialize({1.0}, this->exec); + std::shared_ptr mtx = gko::initialize({1.0}, this->exec); std::shared_ptr rhs = gko::initialize({0.0}, this->exec); std::shared_ptr x = gko::initialize({0.0}, this->exec); std::shared_ptr implicit_sq_res_norm =