Skip to content

Commit

Permalink
WIP fix half of failed test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Apr 4, 2023
1 parent cab1f1e commit 79eebb0
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 60 deletions.
4 changes: 4 additions & 0 deletions core/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,12 +909,16 @@ matrix_data<ValueType, IndexType> read_binary_raw(std::istream& is)
}
DECLARE_OVERLOAD(double, int32)
DECLARE_OVERLOAD(float, int32)
DECLARE_OVERLOAD(half, int32)
DECLARE_OVERLOAD(std::complex<double>, int32)
DECLARE_OVERLOAD(std::complex<float>, int32)
DECLARE_OVERLOAD(std::complex<half>, int32)
DECLARE_OVERLOAD(double, int64)
DECLARE_OVERLOAD(float, int64)
DECLARE_OVERLOAD(half, int64)
DECLARE_OVERLOAD(std::complex<double>, int64)
DECLARE_OVERLOAD(std::complex<float>, int64)
DECLARE_OVERLOAD(std::complex<half>, int64)
#undef DECLARE_OVERLOAD
else
{
Expand Down
18 changes: 17 additions & 1 deletion core/test/base/extended_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <gtest/gtest.h>

#include <complex>
#include "ginkgo/core/base/math.hpp"

namespace {

Expand Down Expand Up @@ -222,6 +223,21 @@ TEST_F(FloatToHalf, TruncatesLargeNumberRoundToEven)
}


TEST_F(FloatToHalf, Convert)
{
float rho = 86.25;
float beta = 1110;
auto float_res = rho/beta;
gko::half rho_h = rho;
gko::half beta_h = beta;
auto half_res = rho_h/beta_h;
std::cout << float_res << std::endl;
std::cout << float(half_res) << std::endl;

std::complex<gko::half> cpx{100.0, 0.0};
std::cout << float(gko::squared_norm(cpx)) << std::endl;
}

// clang-format on


Expand Down
70 changes: 35 additions & 35 deletions core/test/log/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,17 @@ TYPED_TEST(Stream, CatchesLinOpApplyStartedWithVerbose)
std::stringstream out;
auto logger = gko::log::Stream<TypeParam>::create(
gko::log::Logger::linop_apply_started_mask, out, true);
auto A = gko::initialize<Dense>({1.1}, exec);
auto b = gko::initialize<Dense>({-2.2}, exec);
auto x = gko::initialize<Dense>({3.3}, exec);
auto A = gko::initialize<Dense>({1.5}, exec);
auto b = gko::initialize<Dense>({-2.25}, exec);
auto x = gko::initialize<Dense>({3.125}, exec);

logger->template on<gko::log::Logger::linop_apply_started>(A.get(), b.get(),
x.get());

auto os = out.str();
GKO_ASSERT_STR_CONTAINS(os, "1.1");
GKO_ASSERT_STR_CONTAINS(os, "-2.2");
GKO_ASSERT_STR_CONTAINS(os, "3.3");
GKO_ASSERT_STR_CONTAINS(os, "1.5");
GKO_ASSERT_STR_CONTAINS(os, "-2.25");
GKO_ASSERT_STR_CONTAINS(os, "3.125");
}


Expand Down Expand Up @@ -462,17 +462,17 @@ TYPED_TEST(Stream, CatchesLinOpApplyCompletedWithVerbose)
std::stringstream out;
auto logger = gko::log::Stream<TypeParam>::create(
gko::log::Logger::linop_apply_completed_mask, out, true);
auto A = gko::initialize<Dense>({1.1}, exec);
auto b = gko::initialize<Dense>({-2.2}, exec);
auto x = gko::initialize<Dense>({3.3}, exec);
auto A = gko::initialize<Dense>({1.5}, exec);
auto b = gko::initialize<Dense>({-2.25}, exec);
auto x = gko::initialize<Dense>({3.125}, exec);

logger->template on<gko::log::Logger::linop_apply_completed>(
A.get(), b.get(), x.get());

auto os = out.str();
GKO_ASSERT_STR_CONTAINS(os, "1.1");
GKO_ASSERT_STR_CONTAINS(os, "-2.2");
GKO_ASSERT_STR_CONTAINS(os, "3.3");
GKO_ASSERT_STR_CONTAINS(os, "1.5");
GKO_ASSERT_STR_CONTAINS(os, "-2.25");
GKO_ASSERT_STR_CONTAINS(os, "3.125");
}


Expand Down Expand Up @@ -519,21 +519,21 @@ TYPED_TEST(Stream, CatchesLinOpAdvancedApplyStartedWithVerbose)
std::stringstream out;
auto logger = gko::log::Stream<TypeParam>::create(
gko::log::Logger::linop_advanced_apply_started_mask, out, true);
auto A = gko::initialize<Dense>({1.1}, exec);
auto alpha = gko::initialize<Dense>({-4.4}, exec);
auto b = gko::initialize<Dense>({-2.2}, exec);
auto A = gko::initialize<Dense>({1.5}, exec);
auto alpha = gko::initialize<Dense>({-4.75}, exec);
auto b = gko::initialize<Dense>({-2.25}, exec);
auto beta = gko::initialize<Dense>({-5.5}, exec);
auto x = gko::initialize<Dense>({3.3}, exec);
auto x = gko::initialize<Dense>({3.125}, exec);

logger->template on<gko::log::Logger::linop_advanced_apply_started>(
A.get(), alpha.get(), b.get(), beta.get(), x.get());

auto os = out.str();
GKO_ASSERT_STR_CONTAINS(os, "1.1");
GKO_ASSERT_STR_CONTAINS(os, "-4.4");
GKO_ASSERT_STR_CONTAINS(os, "-2.2");
GKO_ASSERT_STR_CONTAINS(os, "1.5");
GKO_ASSERT_STR_CONTAINS(os, "-4.75");
GKO_ASSERT_STR_CONTAINS(os, "-2.25");
GKO_ASSERT_STR_CONTAINS(os, "-5.5");
GKO_ASSERT_STR_CONTAINS(os, "3.3");
GKO_ASSERT_STR_CONTAINS(os, "3.125");
}


Expand Down Expand Up @@ -580,21 +580,21 @@ TYPED_TEST(Stream, CatchesLinOpAdvancedApplyCompletedWithVerbose)
std::stringstream out;
auto logger = gko::log::Stream<TypeParam>::create(
gko::log::Logger::linop_advanced_apply_completed_mask, out, true);
auto A = gko::initialize<Dense>({1.1}, exec);
auto alpha = gko::initialize<Dense>({-4.4}, exec);
auto b = gko::initialize<Dense>({-2.2}, exec);
auto A = gko::initialize<Dense>({1.5}, exec);
auto alpha = gko::initialize<Dense>({-4.75}, exec);
auto b = gko::initialize<Dense>({-2.25}, exec);
auto beta = gko::initialize<Dense>({-5.5}, exec);
auto x = gko::initialize<Dense>({3.3}, exec);
auto x = gko::initialize<Dense>({3.125}, exec);

logger->template on<gko::log::Logger::linop_advanced_apply_completed>(
A.get(), alpha.get(), b.get(), beta.get(), x.get());

auto os = out.str();
GKO_ASSERT_STR_CONTAINS(os, "1.1");
GKO_ASSERT_STR_CONTAINS(os, "-4.4");
GKO_ASSERT_STR_CONTAINS(os, "-2.2");
GKO_ASSERT_STR_CONTAINS(os, "1.5");
GKO_ASSERT_STR_CONTAINS(os, "-4.75");
GKO_ASSERT_STR_CONTAINS(os, "-2.25");
GKO_ASSERT_STR_CONTAINS(os, "-5.5");
GKO_ASSERT_STR_CONTAINS(os, "3.3");
GKO_ASSERT_STR_CONTAINS(os, "3.125");
}


Expand Down Expand Up @@ -784,19 +784,19 @@ TYPED_TEST(Stream, CatchesIterationsWithVerbose)
.with_criteria(
gko::stop::Iteration::build().with_max_iters(3u).on(exec))
.on(exec);
auto solver = factory->generate(gko::initialize<Dense>({1.1}, exec));
auto residual = gko::initialize<Dense>({-4.4}, exec);
auto solution = gko::initialize<Dense>({-2.2}, exec);
auto residual_norm = gko::initialize<Dense>({-3.3}, exec);
auto solver = factory->generate(gko::initialize<Dense>({1.25}, exec));
auto residual = gko::initialize<Dense>({-4.5}, exec);
auto solution = gko::initialize<Dense>({-2.25}, exec);
auto residual_norm = gko::initialize<Dense>({-3.125}, exec);

logger->template on<gko::log::Logger::iteration_complete>(
solver.get(), num_iters, residual.get(), solution.get(),
residual_norm.get());

auto os = out.str();
GKO_ASSERT_STR_CONTAINS(os, "-4.4");
GKO_ASSERT_STR_CONTAINS(os, "-2.2");
GKO_ASSERT_STR_CONTAINS(os, "-3.3");
GKO_ASSERT_STR_CONTAINS(os, "-4.5");
GKO_ASSERT_STR_CONTAINS(os, "-2.25");
GKO_ASSERT_STR_CONTAINS(os, "-3.125");
}


Expand Down
6 changes: 3 additions & 3 deletions core/test/solver/gcr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class Gcr : public ::testing::Test {
using Solver = gko::solver::Gcr<value_type>;
using Big_solver = gko::solver::Gcr<double>;

static constexpr gko::remove_complex<T> reduction_factor =
gko::remove_complex<T>(1e-6);
static const gko::remove_complex<T> reduction_factor;

Gcr()
: exec(gko::ReferenceExecutor::create()),
Expand Down Expand Up @@ -108,7 +107,8 @@ class Gcr : public ::testing::Test {
};

template <typename T>
constexpr gko::remove_complex<T> Gcr<T>::reduction_factor;
const gko::remove_complex<T> Gcr<T>::reduction_factor =
gko::remove_complex<T>(1e-6);

TYPED_TEST_SUITE(Gcr, gko::test::ValueTypes, TypenameNameGenerator);

Expand Down
11 changes: 10 additions & 1 deletion core/test/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ struct next_precision_impl {};

template <>
struct next_precision_impl<gko::half> {
using type = gko::half;
using type = float;
};

template <>
Expand All @@ -365,4 +365,13 @@ template <typename T>
using next_precision = typename detail::next_precision_impl<T>::type;


#define SKIP_IF_HALF(type) \
if (std::is_same<gko::remove_complex<type>, gko::half>::value) { \
GTEST_SKIP() << "Skip due to single mode"; \
} \
static_assert(true, \
"This assert is used to counter the false positive extra " \
"semi-colon warnings")


#endif // GKO_CORE_TEST_UTILS_HPP_
12 changes: 7 additions & 5 deletions core/test/utils/matrix_generator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ template <typename T>
class MatrixGenerator : public ::testing::Test {
protected:
using value_type = T;
using check_type =
typename gko::detail::arth_type<gko::remove_complex<value_type>>::type;
using real_type = gko::remove_complex<T>;
using mtx_type = gko::matrix::Dense<T>;

Expand Down Expand Up @@ -126,15 +128,15 @@ class MatrixGenerator : public ::testing::Test {


template <typename InputIterator, typename ValueType, typename Closure>
ValueType get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
check_type get_nth_moment(int n, ValueType c, InputIterator sample_start,
InputIterator sample_end, Closure closure_op)
{
using std::pow;
ValueType res = 0;
ValueType num_elems = 0;
check_type res = 0;
check_type num_elems = 0;
while (sample_start != sample_end) {
auto tmp = *(sample_start++);
res += pow(closure_op(tmp) - c, n);
res += pow(check_type{closure_op(tmp)} - check_type{c}, n);
num_elems += 1;
}
return res / num_elems;
Expand Down
4 changes: 2 additions & 2 deletions core/test/utils/matrix_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ TYPED_TEST(MatrixUtils, MakeHpdMatrixCorrectly)
TYPED_TEST(MatrixUtils, MakeHpdMatrixWithRatioCorrectly)
{
using T = typename TestFixture::value_type;
gko::remove_complex<T> ratio = 1.00001;
gko::remove_complex<T> ratio = 1.002;
auto cpy_data = this->data;

gko::utils::make_hpd(this->data, ratio);
Expand Down Expand Up @@ -308,7 +308,7 @@ TYPED_TEST(MatrixUtils, MakeSpdMatrixCorrectly)
TYPED_TEST(MatrixUtils, MakeSpdMatrixWithRatioCorrectly)
{
using T = typename TestFixture::value_type;
gko::remove_complex<T> ratio = 1.00001;
gko::remove_complex<T> ratio = 1.002;
auto cpy_data = this->data;

gko::utils::make_spd(this->data, ratio);
Expand Down
4 changes: 2 additions & 2 deletions cuda/base/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace thrust {
template <>
GKO_ATTRIBUTES GKO_INLINE __half abs<__half>(const complex<__half>& z)
{
return hypot(z.real(), z.imag());
return abs(static_cast<complex<float>>(z));
}


Expand All @@ -83,7 +83,7 @@ GKO_ATTRIBUTES GKO_INLINE __half abs<__half>(const complex<__half>& z)
GKO_ATTRIBUTES GKO_INLINE thrust::complex<__half> operator _op( \
const thrust::complex<__half> lhs, const thrust::complex<__half> rhs) \
{ \
return thrust::complex<float>{lhs} + thrust::complex<float>(rhs); \
return thrust::complex<float>{lhs} _op thrust::complex<float>(rhs); \
}

THRUST_HALF_FRIEND_OPERATOR(+, +=)
Expand Down
24 changes: 17 additions & 7 deletions include/ginkgo/core/base/half.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,18 +610,28 @@ class complex<gko::half> {
template <typename T>
complex& operator*=(const complex<T>& val)
{
auto tmp = real_;
real_ = real_ * val.real() - imag_ * val.imag();
imag_ = tmp * val.imag() + imag_ * val.real();
auto val_f = static_cast<std::complex<float>>(val);
auto result_f = static_cast<std::complex<float>>(*this);
result_f *= val_f;
real_ = result_f.real();
imag_ = result_f.imag();
// auto tmp = real_;
// real_ = real_ * val.real() - imag_ * val.imag();
// imag_ = tmp * val.imag() + imag_ * val.real();
return *this;
}
template <typename T>
complex& operator/=(const complex<T>& val)
{
auto real = val.real();
auto imag = val.imag();
(*this) *= complex<T>{val.real(), -val.imag()};
(*this) /= (real * real + imag * imag);
// auto real = val.real();
// auto imag = val.imag();
// (*this) *= complex<T>{val.real(), -val.imag()};
// (*this) /= (real * real + imag * imag);
auto val_f = static_cast<std::complex<float>>(val);
auto result_f = static_cast<std::complex<float>>(*this);
result_f /= val_f;
real_ = result_f.real();
imag_ = result_f.imag();
return *this;
}

Expand Down
3 changes: 2 additions & 1 deletion include/ginkgo/core/base/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ inline gko::half abs(gko::half a) { return gko::half((a > 0) ? a : -a); }

inline gko::half abs(std::complex<gko::half> a)
{
return gko::half(sqrt(float(a.real() * a.real() + a.imag() * a.imag())));
// Using float abs not sqrt on norm to avoid overflow
return gko::half(abs(std::complex<float>(a)));
}


Expand Down
2 changes: 2 additions & 0 deletions reference/test/matrix/coo_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ TYPED_TEST(Coo, MovesToPrecision)
this->mtx->move_to(tmp);
tmp->move_to(res);

// TODO: When use move_to to the different precision, it will keep the
// original data
GKO_ASSERT_MTX_NEAR(this->mtx, res, residual);
}

Expand Down
4 changes: 4 additions & 0 deletions reference/test/solver/bicg_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ TYPED_TEST(Bicg, SolvesBigDenseSystem1)
{
using Mtx = typename TestFixture::Mtx;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto solver = this->bicg_factory_big->generate(this->mtx_big);
auto b = gko::initialize<Mtx>(
{1300083.0, 1018120.5, 906410.0, -42679.5, 846779.5, 1176858.5},
Expand All @@ -504,6 +505,7 @@ TYPED_TEST(Bicg, SolvesBigDenseSystem2)
{
using Mtx = typename TestFixture::Mtx;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto solver = this->bicg_factory_big->generate(this->mtx_big);
auto b = gko::initialize<Mtx>(
{886630.5, -172578.0, 684522.0, -65310.5, 455487.5, 607436.0},
Expand All @@ -521,6 +523,7 @@ TYPED_TEST(Bicg, SolvesBigDenseSystemImplicitResNormCrit)
{
using Mtx = typename TestFixture::Mtx;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto solver = this->bicg_factory_big2->generate(this->mtx_big);
auto b = gko::initialize<Mtx>(
{886630.5, -172578.0, 684522.0, -65310.5, 455487.5, 607436.0},
Expand Down Expand Up @@ -552,6 +555,7 @@ TYPED_TEST(Bicg, SolvesMultipleDenseSystemForDivergenceCheck)
{
using Mtx = typename TestFixture::Mtx;
using value_type = typename TestFixture::value_type;
SKIP_IF_HALF(value_type);
auto solver = this->bicg_factory_big->generate(this->mtx_big);
auto b1 = gko::initialize<Mtx>(
{1300083.0, 1018120.5, 906410.0, -42679.5, 846779.5, 1176858.5},
Expand Down
Loading

0 comments on commit 79eebb0

Please sign in to comment.