18 #include <cuda/std/complex>
25using ssize_t = std::make_signed_t<std::size_t>;
28#define MKL_Complex8 std::complex<float>
29#define MKL_Complex16 std::complex<double>
42 using vec3d = std::vector<std::vector<std::vector<T>>>;
45 using vec2d = std::vector<std::vector<T>>;
47 typedef double cytnx_double;
48 typedef float cytnx_float;
49 typedef uint64_t cytnx_uint64;
50 typedef uint32_t cytnx_uint32;
51 typedef uint16_t cytnx_uint16;
52 typedef int64_t cytnx_int64;
53 typedef int32_t cytnx_int32;
54 typedef int16_t cytnx_int16;
55 typedef std::size_t cytnx_size_t;
56 typedef std::complex<float> cytnx_complex64;
57 typedef std::complex<double> cytnx_complex128;
58 typedef bool cytnx_bool;
61 using cytnx_cuda_complex64 = cuda::std::complex<float>;
62 using cytnx_cuda_complex128 = cuda::std::complex<double>;
67 struct is_complex_impl : std::false_type {};
70 struct is_complex_impl<std::complex<T>> : std::true_type {};
74 struct is_complex_impl<cuda::std::complex<T>> : std::true_type {};
78 struct is_complex_floating_point_impl : std::false_type {};
81 struct is_complex_floating_point_impl<std::complex<T>> : std::is_floating_point<T> {};
85 struct is_complex_floating_point_impl<cuda::std::complex<T>> : std::is_floating_point<T> {};
88 template <std::
size_t Idx,
typename T,
typename Tuple>
89 constexpr std::size_t index_in_tuple_helper() {
90 static_assert(Idx < std::tuple_size_v<Tuple>,
"Type not found!");
91 if constexpr (std::is_same_v<T, std::tuple_element_t<Idx, Tuple>>) {
94 return index_in_tuple_helper<Idx + 1, T, Tuple>();
102 template <
typename V,
template <
typename>
class Transform>
103 struct make_variant_from_transform;
105 template <
template <
typename>
class Transform,
typename... Args>
106 struct make_variant_from_transform<std::variant<Args...>, Transform> {
107 using type = std::variant<typename Transform<Args>::type...>;
111 template <
typename V,
template <
typename>
class Transform>
112 using make_variant_from_transform_t =
typename make_variant_from_transform<V, Transform>::type;
114 template <
typename T>
115 using is_complex = internal::is_complex_impl<std::remove_cv_t<T>>;
117 template <
typename T>
118 using is_complex_floating_point = internal::is_complex_floating_point_impl<std::remove_cv_t<T>>;
122 template <
typename T>
123 constexpr bool is_complex_v = is_complex<T>::value;
127 template <
typename T>
128 constexpr bool is_complex_floating_point_v = is_complex_floating_point<T>::value;
131 inline constexpr bool always_false_v =
false;
133 template <
typename T,
typename Variant>
134 inline constexpr bool variant_contains_v =
false;
136 template <
typename T,
typename... Types>
137 inline constexpr bool variant_contains_v<T, std::variant<Types...>> = (std::is_same_v<T, Types> ||
141 template <
typename T,
typename Variant>
142 struct variant_index;
144 template <
typename T>
145 struct variant_index<T, std::variant<>> {
146 static_assert(always_false_v<T>,
"variant_index<T, Variant>: T is not in Variant");
147 static constexpr std::size_t value = 0;
150 template <
typename T,
typename... Types>
151 struct variant_index<T, std::variant<T, Types...>> {
152 static constexpr std::size_t value = 0;
155 template <
typename T,
typename U,
typename... Types>
156 struct variant_index<T, std::variant<U, Types...>> {
157 static constexpr std::size_t value = 1 + variant_index<T, std::variant<Types...>>::value;
161 template <
typename T,
typename Variant>
162 static constexpr std::size_t variant_index_v = variant_index<T, Variant>::value;
167 template <
typename T>
168 inline constexpr int type_size =
sizeof(T);
170 inline constexpr int type_size<void> = 0;
178 std::variant<void, cytnx_complex128, cytnx_complex64, cytnx_double, cytnx_float, cytnx_int64,
179 cytnx_uint64, cytnx_int32, cytnx_uint32, cytnx_int16, cytnx_uint16, cytnx_bool>;
184 using Type_list_gpu =
185 std::variant<void, cytnx_cuda_complex128, cytnx_cuda_complex64, cytnx_double, cytnx_float,
186 cytnx_int64, cytnx_uint64, cytnx_int32, cytnx_uint32, cytnx_int16, cytnx_uint16,
190 template <
typename T>
191 struct gpu_element_type {
195 struct gpu_element_type<cytnx_complex128> {
196 using type = cytnx_cuda_complex128;
199 struct gpu_element_type<cytnx_complex64> {
200 using type = cytnx_cuda_complex64;
202 template <
typename T>
203 using gpu_element_type_t =
typename gpu_element_type<T>::type;
212 template <
typename T>
213 concept CytnxType = variant_contains_v<T, Type_list> && !std::is_void_v<T>;
219 template <
typename T>
220 concept GpuComplexView =
221 std::is_same_v<T, cuDoubleComplex> || std::is_same_v<T, cuFloatComplex> ||
222 std::is_same_v<T, cytnx_cuda_complex128> || std::is_same_v<T, cytnx_cuda_complex64>;
225 template <
typename T>
226 concept StorageDataType = CytnxType<T> || GpuComplexView<T>;
228 template <
typename T>
229 concept StorageDataType = CytnxType<T>;
233 constexpr int N_Type = std::variant_size_v<Type_list>;
234 constexpr int N_fType = 5;
237 template <
typename T>
238 inline constexpr char* Type_names =
nullptr;
240 inline constexpr const char* Type_names<void> =
"Void";
242 inline constexpr const char* Type_names<cytnx_complex128> =
"Complex Double (Complex Float64)";
244 inline constexpr const char* Type_names<cytnx_complex64> =
"Complex Float (Complex Float32)";
246 inline constexpr const char* Type_names<cytnx_double> =
"Double (Float64)";
248 inline constexpr const char* Type_names<cytnx_float> =
"Float (Float32)";
250 inline constexpr const char* Type_names<cytnx_int64> =
"Int64";
252 inline constexpr const char* Type_names<cytnx_uint64> =
"Uint64";
254 inline constexpr const char* Type_names<cytnx_int32> =
"Int32";
256 inline constexpr const char* Type_names<cytnx_uint32> =
"Uint32";
258 inline constexpr const char* Type_names<cytnx_int16> =
"Int16";
260 inline constexpr const char* Type_names<cytnx_uint16> =
"Uint16";
262 inline constexpr const char* Type_names<cytnx_bool> =
"Bool";
265 template <
typename T>
266 inline constexpr char* Type_enum_name =
nullptr;
268 inline constexpr const char* Type_enum_name<void> =
"Void";
273 inline constexpr const char* Type_enum_name<std::monostate> =
"Void";
275 inline constexpr const char* Type_enum_name<cytnx_complex128> =
"ComplexDouble";
277 inline constexpr const char* Type_enum_name<cytnx_complex64> =
"ComplexFloat";
279 inline constexpr const char* Type_enum_name<cytnx_double> =
"Double";
281 inline constexpr const char* Type_enum_name<cytnx_float> =
"Float";
283 inline constexpr const char* Type_enum_name<cytnx_int64> =
"Int64";
285 inline constexpr const char* Type_enum_name<cytnx_uint64> =
"Uint64";
287 inline constexpr const char* Type_enum_name<cytnx_int32> =
"Int32";
289 inline constexpr const char* Type_enum_name<cytnx_uint32> =
"Uint32";
291 inline constexpr const char* Type_enum_name<cytnx_int16> =
"Int16";
293 inline constexpr const char* Type_enum_name<cytnx_uint16> =
"Uint16";
295 inline constexpr const char* Type_enum_name<cytnx_bool> =
"Bool";
299 const char* enum_name;
304 unsigned int typeSize;
307 template <
typename T>
308 struct Type_struct_t {
309 static constexpr unsigned int cy_typeid = variant_index_v<T, Type_list>;
311 static constexpr unsigned int cy_typeid_gpu =
312 variant_index_v<internal::gpu_element_type_t<T>, Type_list_gpu>;
314 static constexpr const char* name = Type_names<T>;
315 static constexpr const char* enum_name = Type_enum_name<T>;
316 static constexpr bool is_complex = is_complex_v<T>;
317 static constexpr bool is_unsigned = std::is_unsigned_v<T>;
318 static constexpr bool is_float = std::is_floating_point_v<T> || is_complex_floating_point_v<T>;
319 static constexpr bool is_int = std::is_integral_v<T> && !std::is_same_v<T, bool>;
320 static constexpr std::size_t typeSize = internal::type_size<T>;
322 static constexpr Type_struct construct() {
323 return {name, enum_name, is_unsigned, is_complex, is_float, is_int, typeSize};
328 template <
typename Variant, std::size_t... Indices>
329 constexpr auto make_type_array_helper(std::index_sequence<Indices...>) {
330 return std::array<Type_struct,
sizeof...(Indices)>{
331 Type_struct_t<std::variant_alternative_t<Indices, Variant>>::construct()...};
333 template <
typename Variant>
334 constexpr auto make_type_array() {
335 return make_type_array_helper<Variant>(
336 std::make_index_sequence<std::variant_size_v<Variant>>());
344 static constexpr auto Typeinfos = internal::make_type_array<Type_list>();
346 template <
typename T>
347 static constexpr unsigned int cy_typeid_v = variant_index_v<T, Type_list>;
350 template <
typename T>
351 static constexpr unsigned int cy_typeid_gpu_v =
352 variant_index_v<internal::gpu_element_type_t<T>, Type_list_gpu>;
355 enum Type :
unsigned int {
356 Void = cy_typeid_v<void>,
357 ComplexDouble = cy_typeid_v<cytnx_complex128>,
358 ComplexFloat = cy_typeid_v<cytnx_complex64>,
359 Double = cy_typeid_v<cytnx_double>,
360 Float = cy_typeid_v<cytnx_float>,
361 Int64 = cy_typeid_v<cytnx_int64>,
362 Uint64 = cy_typeid_v<cytnx_uint64>,
363 Int32 = cy_typeid_v<cytnx_int32>,
364 Uint32 = cy_typeid_v<cytnx_uint32>,
365 Int16 = cy_typeid_v<cytnx_int16>,
366 Uint16 = cy_typeid_v<cytnx_uint16>,
367 Bool = cy_typeid_v<cytnx_bool>
370 static constexpr void check_type(
unsigned int type_id) {
371 cytnx_error_msg(type_id >= N_Type,
"[ERROR] invalid type_id: %s", type_id);
376 static std::string getname(
unsigned int type_id) {
378 return Typeinfos[type_id].name;
382 static unsigned int c_typename_to_id(
const std::string& c_name);
383 static char const* enum_name(
unsigned int type_id) {
385 return Typeinfos[type_id].enum_name;
387 static constexpr unsigned int typeSize(
unsigned int type_id) {
389 return Typeinfos[type_id].typeSize;
391 static constexpr bool is_unsigned(
unsigned int type_id) {
393 return Typeinfos[type_id].is_unsigned;
395 static constexpr bool is_complex(
unsigned int type_id) {
397 return Typeinfos[type_id].is_complex;
399 static constexpr bool is_float(
unsigned int type_id) {
401 return Typeinfos[type_id].is_float;
403 static constexpr bool is_int(
unsigned int type_id) {
405 return Typeinfos[type_id].is_int;
409 static constexpr unsigned int cy_typeid(
const T& rc) {
410 return cy_typeid_v<T>;
416 static constexpr unsigned int to_real(
unsigned int type_id) {
418 if (type_id == ComplexDouble)
return Double;
419 if (type_id == ComplexFloat)
return Float;
425 static constexpr unsigned int to_complex(
unsigned int type_id) {
427 if (is_complex(type_id))
return type_id;
428 if (type_id == Double)
return ComplexDouble;
429 if (type_id == Float)
return ComplexFloat;
430 if (type_id == Void)
return Void;
431 return ComplexDouble;
440 static constexpr unsigned int norm_result_dtype(
unsigned int type_id) {
445 "[ERROR] norm_result_dtype: Void has no norm result dtype.%s",
"\n");
446 if (is_float(type_id))
return to_real(type_id);
451 static constexpr unsigned int type_promote(
unsigned int typeL,
unsigned int typeR) {
452 if (typeL == Void || typeR == Void)
return Void;
457 if (is_complex(typeL) != is_complex(typeR)) {
458 return to_complex(type_promote(to_real(typeL), to_real(typeR)));
461 if (!is_unsigned(typeR) && is_unsigned(typeL)) {
467 if (!is_unsigned(typeL) && is_unsigned(typeR)) {
476 template <
typename TL,
typename TR>
477 using type_promote_t =
478 std::variant_alternative_t<Type_class::type_promote(variant_index_v<TL, Type_list>,
479 variant_index_v<TR, Type_list>),
488 static constexpr unsigned int make_floating_point_dtype(
unsigned int type_id) {
490 if (is_float(type_id))
return type_id;
499 template <
typename T>
500 struct make_floating_point {
502 std::conditional_t<std::is_floating_point_v<T> || is_complex_v<T>, T, cytnx_double>;
505 template <
typename T>
506 using make_floating_point_t =
typename make_floating_point<T>::type;
509 template <
typename TL,
typename TR>
510 struct type_promote_from_pointer {
514 template <
typename TL,
typename TR>
515 struct type_promote_from_pointer<TL*, TR*> {
516 using type = type_promote_t<std::decay_t<TL>, std::decay_t<TR>>;
520 template <
typename TL,
typename TR>
521 using type_promote_from_pointer_t =
typename type_promote_from_pointer<TL, TR>::type;
int32_t blas_int
Definition Type.hpp:35
constexpr Type_class Type
data type
Definition Type.hpp:553
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:118
Definition Accessor.hpp:12
@ U
Definition Symmetry.hpp:30