IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
dtype.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <complex>
18#include <cstddef>
19#include <string_view>
20#include <type_traits>
21
22#include <iganet/core/core.hpp>
23
24namespace iganet {
25
27struct half {};
28
31template <typename T> using normalized_type_t = std::remove_cv_t<T>;
32
36template <typename T> struct dtype_traits; // Unsupported by default.
37
41#define DEFINE_DTYPE(type, torch_dtype) \
42 \
43 template <> struct dtype_traits<type> { \
44 \
45 static constexpr torch::Dtype value = torch_dtype; \
46 }
47
48DEFINE_DTYPE(bool, torch::kBool);
49DEFINE_DTYPE(char, torch::kChar);
50DEFINE_DTYPE(short, torch::kShort);
51DEFINE_DTYPE(int, torch::kInt);
52DEFINE_DTYPE(long, torch::kLong);
53DEFINE_DTYPE(long long, torch::kLong);
54DEFINE_DTYPE(half, torch::kHalf);
55DEFINE_DTYPE(float, torch::kFloat);
56DEFINE_DTYPE(double, torch::kDouble);
57DEFINE_DTYPE(std::complex<half>, at::kComplexHalf);
58DEFINE_DTYPE(std::complex<float>, at::kComplexFloat);
59DEFINE_DTYPE(std::complex<double>, at::kComplexDouble);
60
61#undef DEFINE_DTYPE
62
65template <typename T>
66concept DType = requires { dtype_traits<normalized_type_t<T>>::value; };
67
74template <DType T>
75inline constexpr torch::Dtype dtype_v =
78
82template <typename T>
83struct type_name; // Intentionally undefined for unsupported types.
84
87#define DEFINE_TYPE_NAME(type) \
88 \
89 template <> \
90 struct type_name<type> { \
91 \
92 static constexpr std::string_view value = #type; \
93 }
94
97
99DEFINE_TYPE_NAME(signed char);
100DEFINE_TYPE_NAME(unsigned char);
105
107DEFINE_TYPE_NAME(unsigned short);
109DEFINE_TYPE_NAME(unsigned int);
111DEFINE_TYPE_NAME(unsigned long);
113DEFINE_TYPE_NAME(unsigned long long);
114
118DEFINE_TYPE_NAME(long double);
119
120#undef DEFINE_TYPE_NAME
121
123template <>
124struct type_name<std::complex<half>> {
126 static constexpr std::string_view value =
127 "std::complex<half>";
128};
129
131template <>
132struct type_name<std::complex<float>> {
134 static constexpr std::string_view value =
135 "std::complex<float>";
136};
137
139template <>
140struct type_name<std::complex<double>> {
142 static constexpr std::string_view value =
143 "std::complex<double>";
144};
145
147template <>
148struct type_name<std::complex<long double>> {
150 static constexpr std::string_view value =
151 "std::complex<long double>";
152};
153
155template <>
156struct type_name<std::nullptr_t> {
158 static constexpr std::string_view value = "std::nullptr_t";
159};
160
165template <typename T>
166inline constexpr std::string_view type_name_v =
169
170} // namespace iganet
Concept to identify template parameters that are acceptable as DTypes.
Definition dtype.hpp:66
Core components.
#define DEFINE_TYPE_NAME(type)
Defines a type_name specialization.
Definition dtype.hpp:87
#define DEFINE_DTYPE(type, torch_dtype)
Defines a dtype_traits specialization.
Definition dtype.hpp:41
Definition core.hpp:73
constexpr std::string_view type_name_v
Human-readable name associated with a supported C++ type.
Definition dtype.hpp:166
std::remove_cv_t< T > normalized_type_t
Removes top-level const and volatile qualifiers from a type.
Definition dtype.hpp:31
constexpr torch::Dtype dtype_v
Determines the LibTorch dtype from a template parameter.
Definition dtype.hpp:75
Type trait that maps C++ types to LibTorch dtypes.
Definition dtype.hpp:36
Tag type representing IEEE 754 half-precision floating-point data.
Definition dtype.hpp:27
Type trait to obtain the name of a fundamental type as std::string_view.
Definition dtype.hpp:83
STL namespace.