Skip to content

Commit 240e13a

Browse files
committed
add highest precision test
1 parent cc7270b commit 240e13a

4 files changed

Lines changed: 164 additions & 0 deletions

File tree

core/test/base/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ginkgo_create_test(exception_helpers)
1616
ginkgo_create_test(extended_float)
1717
ginkgo_create_test(executor)
1818
ginkgo_create_test(half)
19+
ginkgo_create_test(highest_precision)
1920
ginkgo_create_test(index_range)
2021
ginkgo_create_test(iterator_factory)
2122
ginkgo_create_test(lin_op)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-FileCopyrightText: 2025 The Ginkgo authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
#include <complex>
6+
7+
#include <gtest/gtest.h>
8+
9+
#include <ginkgo/core/base/math.hpp>
10+
11+
#include "core/test/utils.hpp"
12+
13+
14+
TEST(HighestPrecision, SamePrecisionsGivesSame)
15+
{
16+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, gko::half>,
17+
gko::half>();
18+
::testing::StaticAssertTypeEq<
19+
gko::highest_precision<gko::bfloat16, gko::bfloat16>, gko::bfloat16>();
20+
::testing::StaticAssertTypeEq<gko::highest_precision<float, float>,
21+
float>();
22+
::testing::StaticAssertTypeEq<gko::highest_precision<double, double>,
23+
double>();
24+
}
25+
26+
TEST(HighestPrecision, Different16bitGivesFloat)
27+
{
28+
::testing::StaticAssertTypeEq<
29+
gko::highest_precision<gko::half, gko::bfloat16>, float>();
30+
::testing::StaticAssertTypeEq<
31+
gko::highest_precision<gko::bfloat16, gko::half>, float>();
32+
}
33+
34+
TEST(HighestPrecision, HalfCombinations)
35+
{
36+
// two same precisions give the same precision
37+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, gko::half>,
38+
gko::half>();
39+
// different 16 bit precisions give float
40+
::testing::StaticAssertTypeEq<
41+
gko::highest_precision<gko::half, gko::bfloat16>, float>();
42+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, float>,
43+
float>();
44+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, double>,
45+
double>();
46+
}
47+
48+
49+
TEST(HighestPrecision, Bfloat16Combinations)
50+
{
51+
// different 16 bit precisions give float
52+
::testing::StaticAssertTypeEq<
53+
gko::highest_precision<gko::bfloat16, gko::half>, float>();
54+
// two same precisions give the same precision
55+
::testing::StaticAssertTypeEq<
56+
gko::highest_precision<gko::bfloat16, gko::bfloat16>, gko::bfloat16>();
57+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::bfloat16, float>,
58+
float>();
59+
::testing::StaticAssertTypeEq<gko::highest_precision<gko::bfloat16, double>,
60+
double>();
61+
}
62+
63+
64+
TEST(HighestPrecision, FloatCombinations)
65+
{
66+
::testing::StaticAssertTypeEq<gko::highest_precision<float, gko::half>,
67+
float>();
68+
::testing::StaticAssertTypeEq<gko::highest_precision<float, gko::bfloat16>,
69+
float>();
70+
// two same precisions give the same precision
71+
::testing::StaticAssertTypeEq<gko::highest_precision<float, float>,
72+
float>();
73+
::testing::StaticAssertTypeEq<gko::highest_precision<float, double>,
74+
double>();
75+
}
76+
77+
78+
TEST(HighestPrecision, DoubleCombinations)
79+
{
80+
::testing::StaticAssertTypeEq<gko::highest_precision<double, gko::half>,
81+
double>();
82+
::testing::StaticAssertTypeEq<gko::highest_precision<double, gko::bfloat16>,
83+
double>();
84+
::testing::StaticAssertTypeEq<gko::highest_precision<double, float>,
85+
double>();
86+
// two same precisions give the same precision
87+
::testing::StaticAssertTypeEq<gko::highest_precision<double, double>,
88+
double>();
89+
}
90+
91+
92+
template <typename TwoRealType>
93+
class ComplexHigestPrecision : public ::testing::Test {
94+
public:
95+
using first_type =
96+
typename std::tuple_element<0, decltype(TwoRealType())>::type;
97+
using second_type =
98+
typename std::tuple_element<0, decltype(TwoRealType())>::type;
99+
};
100+
101+
using TwoRealType =
102+
gko::test::cartesian_type_product_t<gko::test::RealValueTypes,
103+
gko::test::RealValueTypes>;
104+
105+
TYPED_TEST_SUITE(ComplexHigestPrecision, TwoRealType,
106+
PairTypenameNameGenerator);
107+
108+
109+
TYPED_TEST(ComplexHigestPrecision, ComplexBasedOnReal)
110+
{
111+
using first_type = typename TestFixture::first_type;
112+
using second_type = typename TestFixture::second_type;
113+
::testing::StaticAssertTypeEq<
114+
gko::highest_precision<std::complex<first_type>,
115+
std::complex<second_type>>,
116+
std::complex<gko::highest_precision<first_type, second_type>>>();
117+
}

test/base/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ ginkgo_create_common_device_test(intrinsics)
55
ginkgo_create_common_device_test(iterator_factory)
66
ginkgo_create_common_device_test(kernel_launch_generic)
77
ginkgo_create_common_and_reference_test(executor)
8+
ginkgo_create_common_device_test(highest_precision)
89
ginkgo_create_common_device_test(segmented_range)
910
ginkgo_create_common_and_reference_test(timer)

test/base/highest_precision.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: 2025 The Ginkgo authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
#include <complex>
6+
7+
#include <gtest/gtest.h>
8+
9+
#include <ginkgo/core/base/math.hpp>
10+
11+
#include "common/unified/base/kernel_launch.hpp"
12+
#include "core/test/utils.hpp"
13+
14+
15+
template <typename TwoTypes>
16+
class HigestPrecision : public ::testing::Test {
17+
public:
18+
using first_type =
19+
typename std::tuple_element<0, decltype(TwoTypes())>::type;
20+
using second_type =
21+
typename std::tuple_element<0, decltype(TwoTypes())>::type;
22+
};
23+
24+
using TwoValueTypes = gko::test::merge_type_list_t<
25+
gko::test::cartesian_type_product_t<gko::test::RealValueTypes,
26+
gko::test::RealValueTypes>,
27+
gko::test::cartesian_type_product_t<gko::test::ComplexValueTypes,
28+
gko::test::ComplexValueTypes>>;
29+
30+
TYPED_TEST_SUITE(HigestPrecision, TwoValueTypes, PairTypenameNameGenerator);
31+
32+
33+
template <typename T>
34+
using device_type = gko::kernels::GKO_DEVICE_NAMESPACE::device_type<T>;
35+
36+
TYPED_TEST(HigestPrecision, DeviceShouldBeSameAsHost)
37+
{
38+
using first_type = typename TestFixture::first_type;
39+
using second_type = typename TestFixture::second_type;
40+
41+
::testing::StaticAssertTypeEq<
42+
gko::highest_precision<device_type<first_type>,
43+
device_type<second_type>>,
44+
device_type<gko::highest_precision<first_type, second_type>>>();
45+
}

0 commit comments

Comments
 (0)