17 #include <cuda/std/complex>
20#define MKL_Complex8 std::complex<float>
21#define MKL_Complex16 std::complex<double>
40 using vec3d = std::vector<std::vector<std::vector<T>>>;
43 using vec2d = std::vector<std::vector<T>>;
45 typedef double cytnx_double;
46 typedef float cytnx_float;
47 typedef uint64_t cytnx_uint64;
48 typedef uint32_t cytnx_uint32;
49 typedef uint16_t cytnx_uint16;
50 typedef int64_t cytnx_int64;
51 typedef int32_t cytnx_int32;
52 typedef int16_t cytnx_int16;
53 typedef std::size_t cytnx_size_t;
54 typedef std::complex<float> cytnx_complex64;
55 typedef std::complex<double> cytnx_complex128;
56 typedef bool cytnx_bool;
59 using cytnx_cuda_complex64 = cuda::std::complex<float>;
60 using cytnx_cuda_complex128 = cuda::std::complex<double>;
65 struct is_complex_impl : std::false_type {};
68 struct is_complex_impl<std::complex<T>> : std::true_type {};
72 struct is_complex_impl<cuda::std::complex<T>> : std::true_type {};
76 struct is_complex_floating_point_impl : std::false_type {};
79 struct is_complex_floating_point_impl<std::complex<T>> : std::is_floating_point<T> {};
83 struct is_complex_floating_point_impl<cuda::std::complex<T>> : std::is_floating_point<T> {};
86 template <std::
size_t Idx,
typename T,
typename Tuple>
87 constexpr std::size_t index_in_tuple_helper() {
88 static_assert(Idx < std::tuple_size_v<Tuple>,
"Type not found!");
89 if constexpr (std::is_same_v<T, std::tuple_element_t<Idx, Tuple>>) {
92 return index_in_tuple_helper<Idx + 1, T, Tuple>();
100 template <
typename V,
template <
typename>
class Transform>
101 struct make_variant_from_transform;
103 template <
template <
typename>
class Transform,
typename... Args>
104 struct make_variant_from_transform<std::variant<Args...>, Transform> {
105 using type = std::variant<typename Transform<Args>::type...>;
109 template <
typename V,
template <
typename>
class Transform>
110 using make_variant_from_transform_t =
typename make_variant_from_transform<V, Transform>::type;
112 template <
typename T>
113 using is_complex = internal::is_complex_impl<std::remove_cv_t<T>>;
115 template <
typename T>
116 using is_complex_floating_point = internal::is_complex_floating_point_impl<std::remove_cv_t<T>>;
120 template <
typename T>
121 constexpr bool is_complex_v = is_complex<T>::value;
125 template <
typename T>
126 constexpr bool is_complex_floating_point_v = is_complex_floating_point<T>::value;
129 inline constexpr bool always_false_v =
false;
131 template <
typename T,
typename Variant>
132 inline constexpr bool variant_contains_v =
false;
134 template <
typename T,
typename... Types>
135 inline constexpr bool variant_contains_v<T, std::variant<Types...>> = (std::is_same_v<T, Types> ||
139 template <
typename T,
typename Variant>
140 struct variant_index;
142 template <
typename T>
143 struct variant_index<T, std::variant<>> {
144 static_assert(always_false_v<T>,
"variant_index<T, Variant>: T is not in Variant");
145 static constexpr std::size_t value = 0;
148 template <
typename T,
typename... Types>
149 struct variant_index<T, std::variant<T, Types...>> {
150 static constexpr std::size_t value = 0;
153 template <
typename T,
typename U,
typename... Types>
154 struct variant_index<T, std::variant<U, Types...>> {
155 static constexpr std::size_t value = 1 + variant_index<T, std::variant<Types...>>::value;
159 template <
typename T,
typename Variant>
160 static constexpr std::size_t variant_index_v = variant_index<T, Variant>::value;
165 template <
typename T>
166 inline constexpr int type_size =
sizeof(T);
168 inline constexpr int type_size<void> = 0;
176 std::variant<void, cytnx_complex128, cytnx_complex64, cytnx_double, cytnx_float, cytnx_int64,
177 cytnx_uint64, cytnx_int32, cytnx_uint32, cytnx_int16, cytnx_uint16, cytnx_bool>;
182 using Type_list_gpu =
183 std::variant<void, cytnx_cuda_complex128, cytnx_cuda_complex64, cytnx_double, cytnx_float,
184 cytnx_int64, cytnx_uint64, cytnx_int32, cytnx_uint32, cytnx_int16, cytnx_uint16,
193 template <
typename T>
194 concept CytnxType = variant_contains_v<T, Type_list> && !std::is_void_v<T>;
200 template <
typename T>
201 concept GpuComplexView =
202 std::is_same_v<T, cuDoubleComplex> || std::is_same_v<T, cuFloatComplex> ||
203 std::is_same_v<T, cytnx_cuda_complex128> || std::is_same_v<T, cytnx_cuda_complex64>;
206 template <
typename T>
207 concept StorageDataType = CytnxType<T> || GpuComplexView<T>;
209 template <
typename T>
210 concept StorageDataType = CytnxType<T>;
214 constexpr int N_Type = std::variant_size_v<Type_list>;
215 constexpr int N_fType = 5;
218 template <
typename T>
219 inline constexpr char* Type_names =
nullptr;
221 inline constexpr const char* Type_names<void> =
"Void";
223 inline constexpr const char* Type_names<cytnx_complex128> =
"Complex Double (Complex Float64)";
225 inline constexpr const char* Type_names<cytnx_complex64> =
"Complex Float (Complex Float32)";
227 inline constexpr const char* Type_names<cytnx_double> =
"Double (Float64)";
229 inline constexpr const char* Type_names<cytnx_float> =
"Float (Float32)";
231 inline constexpr const char* Type_names<cytnx_int64> =
"Int64";
233 inline constexpr const char* Type_names<cytnx_uint64> =
"Uint64";
235 inline constexpr const char* Type_names<cytnx_int32> =
"Int32";
237 inline constexpr const char* Type_names<cytnx_uint32> =
"Uint32";
239 inline constexpr const char* Type_names<cytnx_int16> =
"Int16";
241 inline constexpr const char* Type_names<cytnx_uint16> =
"Uint16";
243 inline constexpr const char* Type_names<cytnx_bool> =
"Bool";
246 template <
typename T>
247 inline constexpr char* Type_enum_name =
nullptr;
249 inline constexpr const char* Type_enum_name<void> =
"Void";
254 inline constexpr const char* Type_enum_name<std::monostate> =
"Void";
256 inline constexpr const char* Type_enum_name<cytnx_complex128> =
"ComplexDouble";
258 inline constexpr const char* Type_enum_name<cytnx_complex64> =
"ComplexFloat";
260 inline constexpr const char* Type_enum_name<cytnx_double> =
"Double";
262 inline constexpr const char* Type_enum_name<cytnx_float> =
"Float";
264 inline constexpr const char* Type_enum_name<cytnx_int64> =
"Int64";
266 inline constexpr const char* Type_enum_name<cytnx_uint64> =
"Uint64";
268 inline constexpr const char* Type_enum_name<cytnx_int32> =
"Int32";
270 inline constexpr const char* Type_enum_name<cytnx_uint32> =
"Uint32";
272 inline constexpr const char* Type_enum_name<cytnx_int16> =
"Int16";
274 inline constexpr const char* Type_enum_name<cytnx_uint16> =
"Uint16";
276 inline constexpr const char* Type_enum_name<cytnx_bool> =
"Bool";
280 const char* enum_name;
285 unsigned int typeSize;
288 template <
typename T>
289 struct Type_struct_t {
290 static constexpr unsigned int cy_typeid = variant_index_v<T, Type_list>;
292 static constexpr unsigned int cy_typeid_gpu = variant_index_v<T, Type_list_gpu>;
294 static constexpr const char* name = Type_names<T>;
295 static constexpr const char* enum_name = Type_enum_name<T>;
296 static constexpr bool is_complex = is_complex_v<T>;
297 static constexpr bool is_unsigned = std::is_unsigned_v<T>;
298 static constexpr bool is_float = std::is_floating_point_v<T> || is_complex_floating_point_v<T>;
299 static constexpr bool is_int = std::is_integral_v<T> && !std::is_same_v<T, bool>;
300 static constexpr std::size_t typeSize = internal::type_size<T>;
302 static constexpr Type_struct construct() {
303 return {name, enum_name, is_unsigned, is_complex, is_float, is_int, typeSize};
308 template <
typename Variant, std::size_t... Indices>
309 constexpr auto make_type_array_helper(std::index_sequence<Indices...>) {
310 return std::array<Type_struct,
sizeof...(Indices)>{
311 Type_struct_t<std::variant_alternative_t<Indices, Variant>>::construct()...};
313 template <
typename Variant>
314 constexpr auto make_type_array() {
315 return make_type_array_helper<Variant>(
316 std::make_index_sequence<std::variant_size_v<Variant>>());
324 static constexpr auto Typeinfos = internal::make_type_array<Type_list>();
326 template <
typename T>
327 static constexpr unsigned int cy_typeid_v = variant_index_v<T, Type_list>;
330 template <
typename T>
331 static constexpr unsigned int cy_typeid_gpu_v = variant_index_v<T, Type_list_gpu>;
334 enum Type :
unsigned int {
335 Void = cy_typeid_v<void>,
336 ComplexDouble = cy_typeid_v<cytnx_complex128>,
337 ComplexFloat = cy_typeid_v<cytnx_complex64>,
338 Double = cy_typeid_v<cytnx_double>,
339 Float = cy_typeid_v<cytnx_float>,
340 Int64 = cy_typeid_v<cytnx_int64>,
341 Uint64 = cy_typeid_v<cytnx_uint64>,
342 Int32 = cy_typeid_v<cytnx_int32>,
343 Uint32 = cy_typeid_v<cytnx_uint32>,
344 Int16 = cy_typeid_v<cytnx_int16>,
345 Uint16 = cy_typeid_v<cytnx_uint16>,
346 Bool = cy_typeid_v<cytnx_bool>
349 static constexpr void check_type(
unsigned int type_id) {
350 cytnx_error_msg(type_id >= N_Type,
"[ERROR] invalid type_id: %s", type_id);
355 static std::string getname(
unsigned int type_id) {
357 return Typeinfos[type_id].name;
361 static unsigned int c_typename_to_id(
const std::string& c_name);
362 static char const* enum_name(
unsigned int type_id) {
364 return Typeinfos[type_id].enum_name;
366 static constexpr unsigned int typeSize(
unsigned int type_id) {
368 return Typeinfos[type_id].typeSize;
370 static constexpr bool is_unsigned(
unsigned int type_id) {
372 return Typeinfos[type_id].is_unsigned;
374 static constexpr bool is_complex(
unsigned int type_id) {
376 return Typeinfos[type_id].is_complex;
378 static constexpr bool is_float(
unsigned int type_id) {
380 return Typeinfos[type_id].is_float;
382 static constexpr bool is_int(
unsigned int type_id) {
384 return Typeinfos[type_id].is_int;
388 static constexpr unsigned int cy_typeid(
const T& rc) {
389 return cy_typeid_v<T>;
395 static constexpr unsigned int to_real(
unsigned int type_id) {
397 if (type_id == ComplexDouble)
return Double;
398 if (type_id == ComplexFloat)
return Float;
404 static constexpr unsigned int to_complex(
unsigned int type_id) {
406 if (is_complex(type_id))
return type_id;
407 if (type_id == Double)
return ComplexDouble;
408 if (type_id == Float)
return ComplexFloat;
409 if (type_id == Void)
return Void;
410 return ComplexDouble;
419 static constexpr unsigned int norm_result_dtype(
unsigned int type_id) {
424 "[ERROR] norm_result_dtype: Void has no norm result dtype.%s",
"\n");
425 if (is_float(type_id))
return to_real(type_id);
430 static constexpr unsigned int type_promote(
unsigned int typeL,
unsigned int typeR) {
431 if (typeL == Void || typeR == Void)
return Void;
436 if (is_complex(typeL) != is_complex(typeR)) {
437 return to_complex(type_promote(to_real(typeL), to_real(typeR)));
440 if (!is_unsigned(typeR) && is_unsigned(typeL)) {
446 if (!is_unsigned(typeL) && is_unsigned(typeR)) {
455 template <
typename TL,
typename TR>
456 using type_promote_t =
457 std::variant_alternative_t<Type_class::type_promote(variant_index_v<TL, Type_list>,
458 variant_index_v<TR, Type_list>),
467 static constexpr unsigned int make_floating_point_dtype(
unsigned int type_id) {
469 if (is_float(type_id))
return type_id;
478 template <
typename T>
479 struct make_floating_point {
481 std::conditional_t<std::is_floating_point_v<T> || is_complex_v<T>, T, cytnx_double>;
484 template <
typename T>
485 using make_floating_point_t =
typename make_floating_point<T>::type;
488 template <
typename TL,
typename TR>
489 struct type_promote_from_pointer {
493 template <
typename TL,
typename TR>
494 struct type_promote_from_pointer<TL*, TR*> {
495 using type = type_promote_t<std::decay_t<TL>, std::decay_t<TR>>;
499 template <
typename TL,
typename TR>
500 using type_promote_from_pointer_t =
typename type_promote_from_pointer<TL, TR>::type;
504 template <
typename TL,
typename TR>
505 using type_promote_gpu_t =
506 std::variant_alternative_t<Type_class::type_promote(variant_index_v<TL, Type_list_gpu>,
507 variant_index_v<TR, Type_list_gpu>),
510 template <
typename TL,
typename TR>
511 struct type_promote_from_gpu_pointer {
515 template <
typename TL,
typename TR>
516 struct type_promote_from_gpu_pointer<TL*, TR*> {
517 using type = type_promote_gpu_t<std::decay_t<TL>, std::decay_t<TR>>;
521 template <
typename TL,
typename TR>
522 using type_promote_from_gpu_pointer_t =
typename type_promote_from_gpu_pointer<TL, TR>::type;
int32_t blas_int
Definition Type.hpp:31
constexpr Type_class Type
data type
Definition Type.hpp:555
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:115
Definition Accessor.hpp:12
@ U
Definition Symmetry.hpp:30