Ginkgo Generated from develop branch based on develop. 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#if (defined(__CUDA_ARCH__) && defined(__APPLE__)) || \
39 defined(__HIP_DEVICE_COMPILE__)
40
41#ifdef NDEBUG
42#define GKO_ASSERT(condition) ((void)0)
43#else // NDEBUG
44// Poor man's assertions on GPUs for MACs. They won't terminate the program
45// but will at least print something on the screen
46#define GKO_ASSERT(condition) \
47 ((condition) \
48 ? ((void)0) \
49 : ((void)printf("%s: %d: %s: Assertion `" #condition "' failed\n", \
50 __FILE__, __LINE__, __func__)))
51#endif // NDEBUG
52
53#else // (defined(__CUDA_ARCH__) && defined(__APPLE__)) ||
54 // defined(__HIP_DEVICE_COMPILE__)
55
56// Handle assertions normally on other systems
57#define GKO_ASSERT(condition) assert(condition)
58
59#endif // (defined(__CUDA_ARCH__) && defined(__APPLE__)) ||
60 // defined(__HIP_DEVICE_COMPILE__)
61
62
63// Handle deprecated notices correctly on different systems
64// clang-format off
65#define GKO_DEPRECATED(_msg) [[deprecated(_msg)]]
66#ifdef __NVCOMPILER
67#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_suppress 1445")
68#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_warning 1445")
69#elif defined(__GNUC__) || defined(__clang__)
70#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
71 _Pragma("GCC diagnostic push") \
72 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
73#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic pop")
74#elif defined(_MSC_VER)
75#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
76 _Pragma("warning(push)") \
77 _Pragma("warning(disable : 5211 4973 4974 4996)")
78#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("warning(pop)")
79#else
80#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
81#define GKO_END_DISABLE_DEPRECATION_WARNINGS
82#endif
83// clang-format on
84
85
86namespace gko {
87
88
92using size_type = std::size_t;
93
94
98using int8 = std::int8_t;
99
103using int16 = std::int16_t;
104
105
109using int32 = std::int32_t;
110
111
115using int64 = std::int64_t;
116
117
121using uint8 = std::uint8_t;
122
126using uint16 = std::uint16_t;
127
128
132using uint32 = std::uint32_t;
133
134
138using uint64 = std::uint64_t;
139
140
144using uintptr = std::uintptr_t;
145
146
147class half;
148
149
153using float16 = half;
154
155
160
161
166
167
172
173
178
179
184
185
194template <typename... Args>
195struct are_all_integral : public std::true_type {};
196
197template <typename First, typename... Args>
199 : public std::conditional<std::is_integral<std::decay_t<First>>::value,
200 are_all_integral<Args...>,
201 std::false_type>::type {};
202
203
245public:
250
251private:
252 static constexpr auto nonpreserving_bits = 4u;
253 static constexpr auto preserving_bits =
254 byte_size * sizeof(storage_type) - nonpreserving_bits;
255 static constexpr auto nonpreserving_mask =
256 storage_type{(0x1 << nonpreserving_bits) - 1};
257 static constexpr auto preserving_mask =
258 storage_type{(0x1 << preserving_bits) - 1} << nonpreserving_bits;
259
260public:
266 GKO_ATTRIBUTES constexpr precision_reduction() noexcept : data_{0x0} {}
267
275 GKO_ATTRIBUTES constexpr precision_reduction(
277 : data_((GKO_ASSERT(preserving < (0x1 << preserving_bits) - 1),
278 GKO_ASSERT(nonpreserving < (0x1 << nonpreserving_bits) - 1),
279 (preserving << nonpreserving_bits) | nonpreserving))
280 {}
281
287 GKO_ATTRIBUTES constexpr operator storage_type() const noexcept
288 {
289 return data_;
290 }
291
297 GKO_ATTRIBUTES constexpr storage_type get_preserving() const noexcept
298 {
299 return (data_ & preserving_mask) >> nonpreserving_bits;
300 }
301
307 GKO_ATTRIBUTES constexpr storage_type get_nonpreserving() const noexcept
308 {
309 return data_ & nonpreserving_mask;
310 }
311
319 GKO_ATTRIBUTES constexpr static precision_reduction autodetect() noexcept
320 {
321 return precision_reduction{preserving_mask | nonpreserving_mask};
322 }
323
335 GKO_ATTRIBUTES constexpr static precision_reduction common(
337 {
338 return precision_reduction(
339 min(x.data_ & preserving_mask, y.data_ & preserving_mask) |
340 min(x.data_ & nonpreserving_mask, y.data_ & nonpreserving_mask));
341 }
342
343private:
344 GKO_ATTRIBUTES constexpr precision_reduction(storage_type data)
345 : data_{data}
346 {}
347
348 GKO_ATTRIBUTES constexpr static storage_type min(storage_type x,
349 storage_type y) noexcept
350 {
351 return x < y ? x : y;
352 }
353
354 storage_type data_;
355};
356
357
366GKO_ATTRIBUTES constexpr bool operator==(precision_reduction x,
367 precision_reduction y) noexcept
368{
370 return static_cast<st>(x) == static_cast<st>(y);
371}
372
373
382GKO_ATTRIBUTES constexpr bool operator!=(precision_reduction x,
383 precision_reduction y) noexcept
384{
386 return static_cast<st>(x) != static_cast<st>(y);
387}
388
389
402#define GKO_ENABLE_FOR_ALL_EXECUTORS(_enable_macro) \
403 _enable_macro(OmpExecutor, omp); \
404 _enable_macro(HipExecutor, hip); \
405 _enable_macro(DpcppExecutor, dpcpp); \
406 _enable_macro(CudaExecutor, cuda)
407
408
417#if GINKGO_DPCPP_SINGLE_MODE
418#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
419 template _macro(float); \
420 template <> \
421 _macro(double) GKO_NOT_IMPLEMENTED
422#else
423#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
424 template _macro(float); \
425 template _macro(double)
426#endif
427
428
437#if GINKGO_DPCPP_SINGLE_MODE
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 <> \
442 _macro(std::complex<double>) GKO_NOT_IMPLEMENTED
443#else
444#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
445 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro); \
446 template _macro(std::complex<float>); \
447 template _macro(std::complex<double>)
448#endif
449
450
461#if GINKGO_DPCPP_SINGLE_MODE
462#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
463 template _macro(float, float); \
464 template <> \
465 _macro(double, double) GKO_NOT_IMPLEMENTED; \
466 template _macro(std::complex<float>, std::complex<float>); \
467 template <> \
468 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
469 template _macro(std::complex<float>, float); \
470 template <> \
471 _macro(std::complex<double>, double) GKO_NOT_IMPLEMENTED;
472#else
473#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
474 template _macro(float, float); \
475 template _macro(double, double); \
476 template _macro(std::complex<float>, std::complex<float>); \
477 template _macro(std::complex<double>, std::complex<double>); \
478 template _macro(std::complex<float>, float); \
479 template _macro(std::complex<double>, double)
480#endif
481
482
491#define GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro) \
492 template _macro(int32); \
493 template _macro(int64)
494
495
505#if GINKGO_DPCPP_SINGLE_MODE
506#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
507 template _macro(float, int32); \
508 template <> \
509 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
510 template _macro(float, int64); \
511 template <> \
512 _macro(double, int64) GKO_NOT_IMPLEMENTED
513#else
514#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
515 template _macro(float, int32); \
516 template _macro(double, int32); \
517 template _macro(float, int64); \
518 template _macro(double, int64)
519#endif
520
521#if GINKGO_DPCPP_SINGLE_MODE
522#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
523 template _macro(float, int32); \
524 template <> \
525 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
526 template _macro(std::complex<float>, int32); \
527 template <> \
528 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED
529#else
530#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
531 template _macro(float, int32); \
532 template _macro(double, int32); \
533 template _macro(std::complex<float>, int32); \
534 template _macro(std::complex<double>, int32)
535#endif
536
537
546#if GINKGO_DPCPP_SINGLE_MODE
547#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
548 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
549 template _macro(std::complex<float>, int32); \
550 template <> \
551 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED; \
552 template _macro(std::complex<float>, int64); \
553 template <> \
554 _macro(std::complex<double>, int64) GKO_NOT_IMPLEMENTED
555#else
556#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
557 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
558 template _macro(std::complex<float>, int32); \
559 template _macro(std::complex<double>, int32); \
560 template _macro(std::complex<float>, int64); \
561 template _macro(std::complex<double>, int64)
562#endif
563
564
574#if GINKGO_DPCPP_SINGLE_MODE
575#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
576 _macro) \
577 template _macro(float, int32, int32); \
578 template _macro(float, int32, int64); \
579 template _macro(float, int64, int64); \
580 template <> \
581 _macro(double, int32, int32) GKO_NOT_IMPLEMENTED; \
582 template <> \
583 _macro(double, int32, int64) GKO_NOT_IMPLEMENTED; \
584 template <> \
585 _macro(double, int64, int64) GKO_NOT_IMPLEMENTED
586#else
587#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
588 _macro) \
589 template _macro(float, int32, int32); \
590 template _macro(float, int32, int64); \
591 template _macro(float, int64, int64); \
592 template _macro(double, int32, int32); \
593 template _macro(double, int32, int64); \
594 template _macro(double, int64, int64)
595#endif
596
597
606#if GINKGO_DPCPP_SINGLE_MODE
607#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
608 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
609 _macro); \
610 template _macro(std::complex<float>, int32, int32); \
611 template _macro(std::complex<float>, int32, int64); \
612 template _macro(std::complex<float>, int64, int64); \
613 template <> \
614 _macro(std::complex<double>, int32, int32) GKO_NOT_IMPLEMENTED; \
615 template <> \
616 _macro(std::complex<double>, int32, int64) GKO_NOT_IMPLEMENTED; \
617 template <> \
618 _macro(std::complex<double>, int64, int64) GKO_NOT_IMPLEMENTED
619#else
620#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
621 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
622 _macro); \
623 template _macro(std::complex<float>, int32, int32); \
624 template _macro(std::complex<float>, int32, int64); \
625 template _macro(std::complex<float>, int64, int64); \
626 template _macro(std::complex<double>, int32, int32); \
627 template _macro(std::complex<double>, int32, int64); \
628 template _macro(std::complex<double>, int64, int64)
629#endif
630
631
632#if GINKGO_DPCPP_SINGLE_MODE
633#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
634 template <> \
635 _macro(float, double) GKO_NOT_IMPLEMENTED; \
636 template <> \
637 _macro(double, float) GKO_NOT_IMPLEMENTED; \
638 template <> \
639 _macro(std::complex<float>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
640 template <> \
641 _macro(std::complex<double>, std::complex<float>) GKO_NOT_IMPLEMENTED
642
643
644#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION_OR_COPY(_macro) \
645 GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro); \
646 template _macro(float, float); \
647 template <> \
648 _macro(double, double) GKO_NOT_IMPLEMENTED; \
649 template _macro(std::complex<float>, std::complex<float>); \
650 template <> \
651 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED
652#else
662#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
663 template _macro(float, double); \
664 template _macro(double, float); \
665 template _macro(std::complex<float>, std::complex<double>); \
666 template _macro(std::complex<double>, std::complex<float>)
667
668
678#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION_OR_COPY(_macro) \
679 GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro); \
680 template _macro(float, float); \
681 template _macro(double, double); \
682 template _macro(std::complex<float>, std::complex<float>); \
683 template _macro(std::complex<double>, std::complex<double>)
684#endif
685
686
695#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_PAIR(_macro) \
696 template _macro(float, float); \
697 template _macro(double, double); \
698 template _macro(std::complex<float>, float); \
699 template _macro(std::complex<double>, double); \
700 template _macro(std::complex<float>, std::complex<float>); \
701 template _macro(std::complex<double>, std::complex<double>)
702
703
713#define GKO_INSTANTIATE_FOR_EACH_COMBINED_VALUE_AND_INDEX_TYPE(_macro) \
714 template _macro(char, char); \
715 template _macro(int32, int32); \
716 template _macro(int64, int64); \
717 template _macro(unsigned int, unsigned int); \
718 template _macro(unsigned long, unsigned long); \
719 template _macro(float, float); \
720 template _macro(double, double); \
721 template _macro(long double, long double); \
722 template _macro(std::complex<float>, std::complex<float>); \
723 template _macro(std::complex<double>, std::complex<double>)
724
733#define GKO_INSTANTIATE_FOR_EACH_POD_TYPE(_macro) \
734 template _macro(float); \
735 template _macro(double); \
736 template _macro(std::complex<float>); \
737 template _macro(std::complex<double>); \
738 template _macro(size_type); \
739 template _macro(bool); \
740 template _macro(int32); \
741 template _macro(int64)
742
743
752#define GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(_macro) \
753 GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro); \
754 GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro); \
755 template _macro(gko::size_type)
756
757
761template <typename IndexType>
762inline constexpr GKO_ATTRIBUTES IndexType invalid_index()
763{
764 static_assert(std::is_signed<IndexType>::value,
765 "IndexType needs to be signed");
766 return static_cast<IndexType>(-1);
767}
768
769
770namespace experimental {
771namespace distributed {
772
773
780
781
791#define GKO_INSTANTIATE_FOR_EACH_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
792 template _macro(int32, int32); \
793 template _macro(int32, int64); \
794 template _macro(int64, int64)
795
796
797} // namespace distributed
798} // namespace experimental
799} // namespace gko
800
801
802#endif // GKO_PUBLIC_CORE_BASE_TYPES_HPP_
This class is used to encode storage precisions of low precision algorithms.
Definition types.hpp:244
uint8 storage_type
The underlying datatype used to store the encoding.
Definition types.hpp:249
constexpr precision_reduction() noexcept
Creates a default precision_reduction encoding.
Definition types.hpp:266
constexpr storage_type get_nonpreserving() const noexcept
Returns the number of non-preserving conversions in the encoding.
Definition types.hpp:307
static constexpr precision_reduction common(precision_reduction x, precision_reduction y) noexcept
Returns the common encoding of input encodings.
Definition types.hpp:335
static constexpr precision_reduction autodetect() noexcept
Returns a special encoding which instructs the algorithm to automatically detect the best precision.
Definition types.hpp:319
constexpr storage_type get_preserving() const noexcept
Returns the number of preserving conversions in the encoding.
Definition types.hpp:297
constexpr precision_reduction(storage_type preserving, storage_type nonpreserving) noexcept
Creates a precision_reduction encoding with the specified number of conversions.
Definition types.hpp:275
int comm_index_type
Index type for enumerating processes in a distributed application.
Definition types.hpp:779
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:121
std::uint64_t uint64
64-bit unsigned integral type.
Definition types.hpp:138
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:109
double full_precision
The most precise floating-point type.
Definition types.hpp:171
std::int16_t int16
16-bit signed integral type.
Definition types.hpp:103
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:144
std::uint32_t uint32
32-bit unsigned integral type.
Definition types.hpp:132
std::int8_t int8
8-bit signed integral type.
Definition types.hpp:98
double float64
Double precision floating point type.
Definition types.hpp:165
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:177
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:115
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92
constexpr size_type byte_size
Number of bits in a byte.
Definition types.hpp:183
std::uint16_t uint16
16-bit unsigned integral type.
Definition types.hpp:126
float float32
Single precision floating point type.
Definition types.hpp:159
half float16
Half precision floating point type.
Definition types.hpp:153
constexpr IndexType invalid_index()
Value for an invalid signed index type.
Definition types.hpp:762
Evaluates if all template arguments Args fulfill std::is_integral.
Definition types.hpp:195