Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
types.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_BASE_TYPES_HPP_
6#define GKO_PUBLIC_CORE_BASE_TYPES_HPP_
7
8
9#include <array>
10#include <cassert>
11#include <climits>
12#include <complex>
13#include <cstddef>
14#include <cstdint>
15#include <limits>
16#include <stdexcept>
17#include <string>
18#include <type_traits>
19
20
21#ifdef __HIPCC__
22#include <hip/hip_runtime.h>
23#endif // __HIPCC__
24
25
26// Macros for handling different compilers / architectures uniformly
27#if defined(__CUDACC__) || defined(__HIPCC__)
28#define GKO_ATTRIBUTES __host__ __device__
29#define GKO_INLINE __forceinline__
30#define GKO_RESTRICT __restrict__
31#else
32#define GKO_ATTRIBUTES
33#define GKO_INLINE inline
34#define GKO_RESTRICT
35#endif // defined(__CUDACC__) || defined(__HIPCC__)
36
37
38// Macros for handling different device error return types uniformly
39#if defined(__CUDACC__)
40#define GKO_DEVICE_ERROR_TYPE cudaError_t
41#define GKO_DEVICE_ERROR_INVALID cudaErrorInvalidValue
42#define GKO_DEVICE_NO_ERROR cudaSuccess
43#elif defined(__HIPCC__)
44#define GKO_DEVICE_ERROR_TYPE hipError_t
45#define GKO_DEVICE_ERROR_INVALID hipErrorInvalidValue
46#define GKO_DEVICE_NO_ERROR hipSuccess
47#else
48#define GKO_DEVICE_ERROR_TYPE int
49#define GKO_DEVICE_ERROR_INVALID 1
50#define GKO_DEVICE_NO_ERROR 0
51#endif
52
53
54#define GKO_ASSERT(condition) assert(condition)
55
56
57// Handle deprecated notices correctly on different systems
58// clang-format off
59#define GKO_DEPRECATED(_msg) [[deprecated(_msg)]]
60#ifdef __NVCOMPILER
61#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_suppress 1445")
62#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_warning 1445")
63#elif defined(__GNUC__) || defined(__clang__)
64#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
65 _Pragma("GCC diagnostic push") \
66 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
67#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic pop")
68#elif defined(_MSC_VER)
69#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
70 _Pragma("warning(push)") \
71 _Pragma("warning(disable : 5211 4973 4974 4996)")
72#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("warning(pop)")
73#else
74#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
75#define GKO_END_DISABLE_DEPRECATION_WARNINGS
76#endif
77// clang-format on
78
79
80namespace gko {
81
82
86using size_type = std::size_t;
87
88
92using int8 = std::int8_t;
93
97using int16 = std::int16_t;
98
99
103using int32 = std::int32_t;
104
105
109using int64 = std::int64_t;
110
111
115using uint8 = std::uint8_t;
116
120using uint16 = std::uint16_t;
121
122
126using uint32 = std::uint32_t;
127
128
132using uint64 = std::uint64_t;
133
134
138using uintptr = std::uintptr_t;
139
140
141class half;
142
143
147using float16 = half;
148
149
153using float32 = float;
154
155
159using float64 = double;
160
161
165using full_precision = double;
166
167
171using default_precision = double;
172
173
177constexpr size_type byte_size = CHAR_BIT;
178
179
188template <typename... Args>
189struct are_all_integral : public std::true_type {};
190
191template <typename First, typename... Args>
192struct are_all_integral<First, Args...>
193 : public std::conditional<std::is_integral<std::decay_t<First>>::value,
194 are_all_integral<Args...>,
195 std::false_type>::type {};
196
197
239public:
244
245private:
246 static constexpr auto nonpreserving_bits = 4u;
247 static constexpr auto preserving_bits =
248 byte_size * sizeof(storage_type) - nonpreserving_bits;
249 static constexpr auto nonpreserving_mask =
250 storage_type{(0x1 << nonpreserving_bits) - 1};
251 static constexpr auto preserving_mask =
252 storage_type{(0x1 << preserving_bits) - 1} << nonpreserving_bits;
253
254public:
260 GKO_ATTRIBUTES constexpr precision_reduction() noexcept : data_{0x0} {}
261
269 GKO_ATTRIBUTES constexpr precision_reduction(
270 storage_type preserving, storage_type nonpreserving) noexcept
271 : data_((GKO_ASSERT(preserving < (0x1 << preserving_bits) - 1),
272 GKO_ASSERT(nonpreserving < (0x1 << nonpreserving_bits) - 1),
273 (preserving << nonpreserving_bits) | nonpreserving))
274 {}
275
281 GKO_ATTRIBUTES constexpr operator storage_type() const noexcept
282 {
283 return data_;
284 }
285
291 GKO_ATTRIBUTES constexpr storage_type get_preserving() const noexcept
292 {
293 return (data_ & preserving_mask) >> nonpreserving_bits;
294 }
295
301 GKO_ATTRIBUTES constexpr storage_type get_nonpreserving() const noexcept
302 {
303 return data_ & nonpreserving_mask;
304 }
305
313 GKO_ATTRIBUTES constexpr static precision_reduction autodetect() noexcept
314 {
315 return precision_reduction{preserving_mask | nonpreserving_mask};
316 }
317
329 GKO_ATTRIBUTES constexpr static precision_reduction common(
331 {
332 return precision_reduction(
333 min(x.data_ & preserving_mask, y.data_ & preserving_mask) |
334 min(x.data_ & nonpreserving_mask, y.data_ & nonpreserving_mask));
335 }
336
337private:
338 GKO_ATTRIBUTES constexpr precision_reduction(storage_type data)
339 : data_{data}
340 {}
341
342 GKO_ATTRIBUTES constexpr static storage_type min(storage_type x,
343 storage_type y) noexcept
344 {
345 return x < y ? x : y;
346 }
347
348 storage_type data_;
349};
350
351
360GKO_ATTRIBUTES constexpr bool operator==(precision_reduction x,
361 precision_reduction y) noexcept
362{
364 return static_cast<st>(x) == static_cast<st>(y);
365}
366
367
376GKO_ATTRIBUTES constexpr bool operator!=(precision_reduction x,
377 precision_reduction y) noexcept
378{
380 return static_cast<st>(x) != static_cast<st>(y);
381}
382
383
396#define GKO_ENABLE_FOR_ALL_EXECUTORS(_enable_macro) \
397 _enable_macro(OmpExecutor, omp); \
398 _enable_macro(HipExecutor, hip); \
399 _enable_macro(DpcppExecutor, dpcpp); \
400 _enable_macro(CudaExecutor, cuda)
401
402
411#if GINKGO_DPCPP_SINGLE_MODE
412#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
413 template _macro(float); \
414 template <> \
415 _macro(double) GKO_NOT_IMPLEMENTED
416#else
417#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
418 template _macro(float); \
419 template _macro(double)
420#endif
421
422
431#if GINKGO_DPCPP_SINGLE_MODE
432#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
433 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro); \
434 template _macro(std::complex<float>); \
435 template <> \
436 _macro(std::complex<double>) GKO_NOT_IMPLEMENTED
437#else
438#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
439 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro); \
440 template _macro(std::complex<float>); \
441 template _macro(std::complex<double>)
442#endif
443
444
455#if GINKGO_DPCPP_SINGLE_MODE
456#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
457 template _macro(float, float); \
458 template <> \
459 _macro(double, double) GKO_NOT_IMPLEMENTED; \
460 template _macro(std::complex<float>, std::complex<float>); \
461 template <> \
462 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
463 template _macro(std::complex<float>, float); \
464 template <> \
465 _macro(std::complex<double>, double) GKO_NOT_IMPLEMENTED;
466#else
467#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
468 template _macro(float, float); \
469 template _macro(double, double); \
470 template _macro(std::complex<float>, std::complex<float>); \
471 template _macro(std::complex<double>, std::complex<double>); \
472 template _macro(std::complex<float>, float); \
473 template _macro(std::complex<double>, double)
474#endif
475
476
485#define GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro) \
486 template _macro(int32); \
487 template _macro(int64)
488
489
499#if GINKGO_DPCPP_SINGLE_MODE
500#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
501 template _macro(float, int32); \
502 template <> \
503 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
504 template _macro(float, int64); \
505 template <> \
506 _macro(double, int64) GKO_NOT_IMPLEMENTED
507#else
508#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
509 template _macro(float, int32); \
510 template _macro(double, int32); \
511 template _macro(float, int64); \
512 template _macro(double, int64)
513#endif
514
515#if GINKGO_DPCPP_SINGLE_MODE
516#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
517 template _macro(float, int32); \
518 template <> \
519 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
520 template _macro(std::complex<float>, int32); \
521 template <> \
522 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED
523#else
524#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
525 template _macro(float, int32); \
526 template _macro(double, int32); \
527 template _macro(std::complex<float>, int32); \
528 template _macro(std::complex<double>, int32)
529#endif
530
531
540#if GINKGO_DPCPP_SINGLE_MODE
541#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
542 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
543 template _macro(std::complex<float>, int32); \
544 template <> \
545 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED; \
546 template _macro(std::complex<float>, int64); \
547 template <> \
548 _macro(std::complex<double>, int64) GKO_NOT_IMPLEMENTED
549#else
550#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
551 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
552 template _macro(std::complex<float>, int32); \
553 template _macro(std::complex<double>, int32); \
554 template _macro(std::complex<float>, int64); \
555 template _macro(std::complex<double>, int64)
556#endif
557
558
568#if GINKGO_DPCPP_SINGLE_MODE
569#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
570 _macro) \
571 template _macro(float, int32, int32); \
572 template _macro(float, int32, int64); \
573 template _macro(float, int64, int64); \
574 template <> \
575 _macro(double, int32, int32) GKO_NOT_IMPLEMENTED; \
576 template <> \
577 _macro(double, int32, int64) GKO_NOT_IMPLEMENTED; \
578 template <> \
579 _macro(double, int64, int64) GKO_NOT_IMPLEMENTED
580#else
581#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
582 _macro) \
583 template _macro(float, int32, int32); \
584 template _macro(float, int32, int64); \
585 template _macro(float, int64, int64); \
586 template _macro(double, int32, int32); \
587 template _macro(double, int32, int64); \
588 template _macro(double, int64, int64)
589#endif
590
591
600#if GINKGO_DPCPP_SINGLE_MODE
601#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
602 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
603 _macro); \
604 template _macro(std::complex<float>, int32, int32); \
605 template _macro(std::complex<float>, int32, int64); \
606 template _macro(std::complex<float>, int64, int64); \
607 template <> \
608 _macro(std::complex<double>, int32, int32) GKO_NOT_IMPLEMENTED; \
609 template <> \
610 _macro(std::complex<double>, int32, int64) GKO_NOT_IMPLEMENTED; \
611 template <> \
612 _macro(std::complex<double>, int64, int64) GKO_NOT_IMPLEMENTED
613#else
614#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
615 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
616 _macro); \
617 template _macro(std::complex<float>, int32, int32); \
618 template _macro(std::complex<float>, int32, int64); \
619 template _macro(std::complex<float>, int64, int64); \
620 template _macro(std::complex<double>, int32, int32); \
621 template _macro(std::complex<double>, int32, int64); \
622 template _macro(std::complex<double>, int64, int64)
623#endif
624
625
626#if GINKGO_DPCPP_SINGLE_MODE
627#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
628 template <> \
629 _macro(float, double) GKO_NOT_IMPLEMENTED; \
630 template <> \
631 _macro(double, float) GKO_NOT_IMPLEMENTED; \
632 template <> \
633 _macro(std::complex<float>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
634 template <> \
635 _macro(std::complex<double>, std::complex<float>) GKO_NOT_IMPLEMENTED
636
637
638#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION_OR_COPY(_macro) \
639 GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro); \
640 template _macro(float, float); \
641 template <> \
642 _macro(double, double) GKO_NOT_IMPLEMENTED; \
643 template _macro(std::complex<float>, std::complex<float>); \
644 template <> \
645 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED
646#else
656#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
657 template _macro(float, double); \
658 template _macro(double, float); \
659 template _macro(std::complex<float>, std::complex<double>); \
660 template _macro(std::complex<double>, std::complex<float>)
661
662
672#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION_OR_COPY(_macro) \
673 GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro); \
674 template _macro(float, float); \
675 template _macro(double, double); \
676 template _macro(std::complex<float>, std::complex<float>); \
677 template _macro(std::complex<double>, std::complex<double>)
678#endif
679
680
689#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_PAIR(_macro) \
690 template _macro(float, float); \
691 template _macro(double, double); \
692 template _macro(std::complex<float>, float); \
693 template _macro(std::complex<double>, double); \
694 template _macro(std::complex<float>, std::complex<float>); \
695 template _macro(std::complex<double>, std::complex<double>)
696
697
707#define GKO_INSTANTIATE_FOR_EACH_COMBINED_VALUE_AND_INDEX_TYPE(_macro) \
708 template _macro(char, char); \
709 template _macro(int32, int32); \
710 template _macro(int64, int64); \
711 template _macro(unsigned int, unsigned int); \
712 template _macro(unsigned long, unsigned long); \
713 template _macro(float, float); \
714 template _macro(double, double); \
715 template _macro(long double, long double); \
716 template _macro(std::complex<float>, std::complex<float>); \
717 template _macro(std::complex<double>, std::complex<double>)
718
727#define GKO_INSTANTIATE_FOR_EACH_POD_TYPE(_macro) \
728 template _macro(float); \
729 template _macro(double); \
730 template _macro(std::complex<float>); \
731 template _macro(std::complex<double>); \
732 template _macro(size_type); \
733 template _macro(bool); \
734 template _macro(int32); \
735 template _macro(int64)
736
737
746#define GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(_macro) \
747 GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro); \
748 GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro); \
749 template _macro(gko::size_type)
750
751
760#define GKO_INSTANTIATE_FOR_INT32_TYPE(_macro) template _macro(int32)
761
762
766template <typename IndexType>
767inline constexpr GKO_ATTRIBUTES IndexType invalid_index()
768{
769 static_assert(std::is_signed<IndexType>::value,
770 "IndexType needs to be signed");
771 return static_cast<IndexType>(-1);
772}
773
774
775namespace experimental {
776namespace distributed {
777
778
784using comm_index_type = int;
785
786
796#define GKO_INSTANTIATE_FOR_EACH_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
797 template _macro(int32, int32); \
798 template _macro(int32, int64); \
799 template _macro(int64, int64)
800
801
802} // namespace distributed
803} // namespace experimental
804} // namespace gko
805
806
807#endif // GKO_PUBLIC_CORE_BASE_TYPES_HPP_
This class is used to encode storage precisions of low precision algorithms.
Definition types.hpp:238
uint8 storage_type
The underlying datatype used to store the encoding.
Definition types.hpp:243
constexpr precision_reduction() noexcept
Creates a default precision_reduction encoding.
Definition types.hpp:260
constexpr storage_type get_nonpreserving() const noexcept
Returns the number of non-preserving conversions in the encoding.
Definition types.hpp:301
static constexpr precision_reduction common(precision_reduction x, precision_reduction y) noexcept
Returns the common encoding of input encodings.
Definition types.hpp:329
static constexpr precision_reduction autodetect() noexcept
Returns a special encoding which instructs the algorithm to automatically detect the best precision.
Definition types.hpp:313
constexpr storage_type get_preserving() const noexcept
Returns the number of preserving conversions in the encoding.
Definition types.hpp:291
constexpr precision_reduction(storage_type preserving, storage_type nonpreserving) noexcept
Creates a precision_reduction encoding with the specified number of conversions.
Definition types.hpp:269
int comm_index_type
Index type for enumerating processes in a distributed application.
Definition types.hpp:784
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:115
std::uint64_t uint64
64-bit unsigned integral type.
Definition types.hpp:132
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:103
double full_precision
The most precise floating-point type.
Definition types.hpp:165
std::int16_t int16
16-bit signed integral type.
Definition types.hpp:97
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:138
std::uint32_t uint32
32-bit unsigned integral type.
Definition types.hpp:126
std::int8_t int8
8-bit signed integral type.
Definition types.hpp:92
double float64
Double precision floating point type.
Definition types.hpp:159
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:171
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:109
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
constexpr size_type byte_size
Number of bits in a byte.
Definition types.hpp:177
std::uint16_t uint16
16-bit unsigned integral type.
Definition types.hpp:120
float float32
Single precision floating point type.
Definition types.hpp:153
half float16
Half precision floating point type.
Definition types.hpp:147
constexpr IndexType invalid_index()
Value for an invalid signed index type.
Definition types.hpp:767
Evaluates if all template arguments Args fulfill std::is_integral.
Definition types.hpp:189