Skip to content

Commit e1bd651

Browse files
authored
Merge checks for ValueType and IndexType at template instantiation
This turns unsupported ValueType or IndexType from linker errors to compile-time errors with a more helpful error message. Related PR: #1945
2 parents 8d56676 + f517177 commit e1bd651

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+195
-49
lines changed

include/ginkgo/core/base/batch_multi_vector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class MultiVector
6262
friend class EnablePolymorphicObject<MultiVector>;
6363
friend class MultiVector<to_complex<ValueType>>;
6464
friend class MultiVector<previous_precision<ValueType>>;
65+
GKO_ASSERT_SUPPORTED_VALUE_TYPE;
6566

6667
public:
6768
using EnablePolymorphicAssignment<MultiVector>::convert_to;

include/ginkgo/core/base/combination.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -33,6 +33,7 @@ class Combination : public EnableLinOp<Combination<ValueType>>,
3333
public Transposable {
3434
friend class EnablePolymorphicObject<Combination, LinOp>;
3535
friend class EnableCreateMethod<Combination>;
36+
GKO_ASSERT_SUPPORTED_VALUE_TYPE;
3637

3738
public:
3839
using value_type = ValueType;

include/ginkgo/core/base/composition.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Composition : public EnableLinOp<Composition<ValueType>>,
4141
public Transposable {
4242
friend class EnablePolymorphicObject<Composition, LinOp>;
4343
friend class EnableCreateMethod<Composition>;
44+
GKO_ASSERT_SUPPORTED_VALUE_TYPE;
4445

4546
public:
4647
using value_type = ValueType;

include/ginkgo/core/base/device_matrix_data.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -34,6 +34,8 @@ namespace gko {
3434
*/
3535
template <typename ValueType, typename IndexType>
3636
class device_matrix_data {
37+
GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
38+
3739
public:
3840
using value_type = ValueType;
3941
using index_type = IndexType;

include/ginkgo/core/base/matrix_assembly_data.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -57,6 +57,8 @@ struct symbolic_nonzero_hash {
5757
*/
5858
template <typename ValueType = default_precision, typename IndexType = int32>
5959
class matrix_assembly_data {
60+
GKO_ASSERT_SUPPORTED_VALUE_TYPE;
61+
6062
public:
6163
using value_type = ValueType;
6264
using index_type = IndexType;

include/ginkgo/core/base/matrix_data.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -124,6 +124,7 @@ struct matrix_data_entry {
124124
*/
125125
template <typename ValueType = default_precision, typename IndexType = int32>
126126
struct matrix_data {
127+
GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
127128
using value_type = ValueType;
128129
using index_type = IndexType;
129130
using nonzero_type = matrix_data_entry<value_type, index_type>;

include/ginkgo/core/base/perturbation.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -39,6 +39,7 @@ class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
3939
public EnableCreateMethod<Perturbation<ValueType>> {
4040
friend class EnablePolymorphicObject<Perturbation, LinOp>;
4141
friend class EnableCreateMethod<Perturbation>;
42+
GKO_ASSERT_SUPPORTED_VALUE_TYPE;
4243

4344
public:
4445
using value_type = ValueType;

include/ginkgo/core/base/types.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,70 @@ GKO_ATTRIBUTES constexpr bool operator!=(precision_reduction x,
10361036
#define GKO_INSTANTIATE_FOR_INT32_TYPE(_macro) template _macro(int32)
10371037

10381038

1039+
namespace detail {
1040+
1041+
template <typename ValueType>
1042+
struct is_supported_value_type : std::false_type {};
1043+
1044+
template <typename ValueType>
1045+
struct is_supported_index_type : std::false_type {};
1046+
1047+
#if GINKGO_ENABLE_HALF
1048+
template <>
1049+
struct is_supported_value_type<float16> : std::true_type {};
1050+
template <>
1051+
struct is_supported_value_type<std::complex<float16>> : std::true_type {};
1052+
#endif
1053+
#if GINKGO_ENABLE_BFLOAT16
1054+
template <>
1055+
struct is_supported_value_type<bfloat16> : std::true_type {};
1056+
template <>
1057+
struct is_supported_value_type<std::complex<bfloat16>> : std::true_type {};
1058+
#endif
1059+
template <>
1060+
struct is_supported_value_type<float> : std::true_type {};
1061+
template <>
1062+
struct is_supported_value_type<double> : std::true_type {};
1063+
template <>
1064+
struct is_supported_value_type<std::complex<float>> : std::true_type {};
1065+
template <>
1066+
struct is_supported_value_type<std::complex<double>> : std::true_type {};
1067+
template <>
1068+
struct is_supported_index_type<int32> : std::true_type {};
1069+
template <>
1070+
struct is_supported_index_type<int64> : std::true_type {};
1071+
1072+
1073+
} // namespace detail
1074+
1075+
1076+
// TODO20: Replace this by concepts
1077+
#define GKO_ASSERT_SUPPORTED_VALUE_TYPE \
1078+
static_assert(::gko::detail::is_supported_value_type<ValueType>::value, \
1079+
"Unsupported value type")
1080+
1081+
// TODO20: Replace this by concepts
1082+
#define GKO_ASSERT_SUPPORTED_INDEX_TYPE \
1083+
static_assert(::gko::detail::is_supported_index_type<IndexType>::value, \
1084+
"Unsupported index type")
1085+
1086+
#define GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE \
1087+
GKO_ASSERT_SUPPORTED_VALUE_TYPE; \
1088+
GKO_ASSERT_SUPPORTED_INDEX_TYPE
1089+
1090+
#define GKO_ASSERT_SUPPORTED_VALUE_AND_DIST_INDEX_TYPE \
1091+
GKO_ASSERT_SUPPORTED_VALUE_TYPE; \
1092+
static_assert( \
1093+
::gko::detail::is_supported_index_type<GlobalIndexType>::value, \
1094+
"Unsupported global index type"); \
1095+
static_assert( \
1096+
::gko::detail::is_supported_index_type<LocalIndexType>::value, \
1097+
"Unsupported local index type"); \
1098+
static_assert( \
1099+
sizeof(GlobalIndexType) >= sizeof(LocalIndexType), \
1100+
"global index type must not be smaller than local index type")
1101+
1102+
10391103
/**
10401104
* Value for an invalid signed index type.
10411105
*/

include/ginkgo/core/distributed/matrix.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class Matrix
276276
friend class Matrix<previous_precision<ValueType>, LocalIndexType,
277277
GlobalIndexType>;
278278
friend class multigrid::Pgm<ValueType, LocalIndexType>;
279-
279+
GKO_ASSERT_SUPPORTED_VALUE_AND_DIST_INDEX_TYPE;
280280

281281
public:
282282
using value_type = ValueType;

include/ginkgo/core/distributed/preconditioner/schwarz.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Schwarz
7070
: public EnableLinOp<Schwarz<ValueType, LocalIndexType, GlobalIndexType>> {
7171
friend class EnableLinOp<Schwarz>;
7272
friend class EnablePolymorphicObject<Schwarz, LinOp>;
73+
GKO_ASSERT_SUPPORTED_VALUE_AND_DIST_INDEX_TYPE;
7374

7475
public:
7576
using EnableLinOp<Schwarz>::convert_to;

0 commit comments

Comments
 (0)