Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
types.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_TYPES_HPP_
34#define GKO_PUBLIC_CORE_BASE_TYPES_HPP_
35
36
37#include <array>
38#include <cassert>
39#include <climits>
40#include <complex>
41#include <cstddef>
42#include <cstdint>
43#include <limits>
44#include <stdexcept>
45#include <string>
46#include <type_traits>
47
48
49#ifdef __HIPCC__
50#include <hip/hip_runtime.h>
51#endif // __HIPCC__
52
53
54// Macros for handling different compilers / architectures uniformly
55#if defined(__CUDACC__) || defined(__HIPCC__)
56#define GKO_ATTRIBUTES __host__ __device__
57#define GKO_INLINE __forceinline__
58#define GKO_RESTRICT __restrict__
59#else
60#define GKO_ATTRIBUTES
61#define GKO_INLINE inline
62#define GKO_RESTRICT
63#endif // defined(__CUDACC__) || defined(__HIPCC__)
64
65
66#if (defined(__CUDA_ARCH__) && defined(__APPLE__)) || \
67 defined(__HIP_DEVICE_COMPILE__)
68
69#ifdef NDEBUG
70#define GKO_ASSERT(condition) ((void)0)
71#else // NDEBUG
72// Poor man's assertions on GPUs for MACs. They won't terminate the program
73// but will at least print something on the screen
74#define GKO_ASSERT(condition) \
75 ((condition) \
76 ? ((void)0) \
77 : ((void)printf("%s: %d: %s: Assertion `" #condition "' failed\n", \
78 __FILE__, __LINE__, __func__)))
79#endif // NDEBUG
80
81#else // (defined(__CUDA_ARCH__) && defined(__APPLE__)) ||
82 // defined(__HIP_DEVICE_COMPILE__)
83
84// Handle assertions normally on other systems
85#define GKO_ASSERT(condition) assert(condition)
86
87#endif // (defined(__CUDA_ARCH__) && defined(__APPLE__)) ||
88 // defined(__HIP_DEVICE_COMPILE__)
89
90
91// Handle deprecated notices correctly on different systems
92// clang-format off
93#define GKO_DEPRECATED(_msg) [[deprecated(_msg)]]
94#ifdef __NVCOMPILER
95#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_suppress 1445")
96#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("diag_warning 1445")
97#elif defined(__GNUC__) || defined(__clang__)
98#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
99 _Pragma("GCC diagnostic push") \
100 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
101#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic pop")
102#elif defined(_MSC_VER)
103#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS \
104 _Pragma("warning(push)") \
105 _Pragma("warning(disable : 5211 4973 4974 4996)")
106#define GKO_END_DISABLE_DEPRECATION_WARNINGS _Pragma("warning(pop)")
107#else
108#define GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
109#define GKO_END_DISABLE_DEPRECATION_WARNINGS
110#endif
111// clang-format on
112
113
114namespace gko {
115
116
120using size_type = std::size_t;
121
122
126using int8 = std::int8_t;
127
131using int16 = std::int16_t;
132
133
137using int32 = std::int32_t;
138
139
143using int64 = std::int64_t;
144
145
149using uint8 = std::uint8_t;
150
154using uint16 = std::uint16_t;
155
156
160using uint32 = std::uint32_t;
161
162
166using uint64 = std::uint64_t;
167
168
172using uintptr = std::uintptr_t;
173
174
175class half;
176
177
181using float16 = half;
182
183
188
189
194
195
200
201
206
207
212
213
222template <typename... Args>
223struct are_all_integral : public std::true_type {};
224
225template <typename First, typename... Args>
227 : public std::conditional<std::is_integral<std::decay_t<First>>::value,
228 are_all_integral<Args...>,
229 std::false_type>::type {};
230
231
273public:
278
279private:
280 static constexpr auto nonpreserving_bits = 4u;
281 static constexpr auto preserving_bits =
282 byte_size * sizeof(storage_type) - nonpreserving_bits;
283 static constexpr auto nonpreserving_mask =
284 storage_type{(0x1 << nonpreserving_bits) - 1};
285 static constexpr auto preserving_mask =
286 storage_type{(0x1 << preserving_bits) - 1} << nonpreserving_bits;
287
288public:
294 GKO_ATTRIBUTES constexpr precision_reduction() noexcept : data_{0x0} {}
295
303 GKO_ATTRIBUTES constexpr precision_reduction(
305 : data_((GKO_ASSERT(preserving < (0x1 << preserving_bits) - 1),
306 GKO_ASSERT(nonpreserving < (0x1 << nonpreserving_bits) - 1),
307 (preserving << nonpreserving_bits) | nonpreserving))
308 {}
309
315 GKO_ATTRIBUTES constexpr operator storage_type() const noexcept
316 {
317 return data_;
318 }
319
325 GKO_ATTRIBUTES constexpr storage_type get_preserving() const noexcept
326 {
327 return (data_ & preserving_mask) >> nonpreserving_bits;
328 }
329
335 GKO_ATTRIBUTES constexpr storage_type get_nonpreserving() const noexcept
336 {
337 return data_ & nonpreserving_mask;
338 }
339
347 GKO_ATTRIBUTES constexpr static precision_reduction autodetect() noexcept
348 {
349 return precision_reduction{preserving_mask | nonpreserving_mask};
350 }
351
363 GKO_ATTRIBUTES constexpr static precision_reduction common(
365 {
366 return precision_reduction(
367 min(x.data_ & preserving_mask, y.data_ & preserving_mask) |
368 min(x.data_ & nonpreserving_mask, y.data_ & nonpreserving_mask));
369 }
370
371private:
372 GKO_ATTRIBUTES constexpr precision_reduction(storage_type data)
373 : data_{data}
374 {}
375
376 GKO_ATTRIBUTES constexpr static storage_type min(storage_type x,
377 storage_type y) noexcept
378 {
379 return x < y ? x : y;
380 }
381
382 storage_type data_;
383};
384
385
394GKO_ATTRIBUTES constexpr bool operator==(precision_reduction x,
395 precision_reduction y) noexcept
396{
398 return static_cast<st>(x) == static_cast<st>(y);
399}
400
401
410GKO_ATTRIBUTES constexpr bool operator!=(precision_reduction x,
411 precision_reduction y) noexcept
412{
414 return static_cast<st>(x) != static_cast<st>(y);
415}
416
417
430#define GKO_ENABLE_FOR_ALL_EXECUTORS(_enable_macro) \
431 _enable_macro(OmpExecutor, omp); \
432 _enable_macro(HipExecutor, hip); \
433 _enable_macro(DpcppExecutor, dpcpp); \
434 _enable_macro(CudaExecutor, cuda)
435
436
445#if GINKGO_DPCPP_SINGLE_MODE
446#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
447 template _macro(float); \
448 template <> \
449 _macro(double) GKO_NOT_IMPLEMENTED
450#else
451#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro) \
452 template _macro(float); \
453 template _macro(double)
454#endif
455
456
465#if GINKGO_DPCPP_SINGLE_MODE
466#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
467 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro); \
468 template _macro(std::complex<float>); \
469 template <> \
470 _macro(std::complex<double>) GKO_NOT_IMPLEMENTED
471#else
472#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
473 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_TYPE(_macro); \
474 template _macro(std::complex<float>); \
475 template _macro(std::complex<double>)
476#endif
477
478
489#if GINKGO_DPCPP_SINGLE_MODE
490#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
491 template _macro(float, float); \
492 template <> \
493 _macro(double, double) GKO_NOT_IMPLEMENTED; \
494 template _macro(std::complex<float>, std::complex<float>); \
495 template <> \
496 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
497 template _macro(std::complex<float>, float); \
498 template <> \
499 _macro(std::complex<double>, double) GKO_NOT_IMPLEMENTED;
500#else
501#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_SCALAR_TYPE(_macro) \
502 template _macro(float, float); \
503 template _macro(double, double); \
504 template _macro(std::complex<float>, std::complex<float>); \
505 template _macro(std::complex<double>, std::complex<double>); \
506 template _macro(std::complex<float>, float); \
507 template _macro(std::complex<double>, double)
508#endif
509
510
519#define GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro) \
520 template _macro(int32); \
521 template _macro(int64)
522
523
533#if GINKGO_DPCPP_SINGLE_MODE
534#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
535 template _macro(float, int32); \
536 template <> \
537 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
538 template _macro(float, int64); \
539 template <> \
540 _macro(double, int64) GKO_NOT_IMPLEMENTED
541#else
542#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro) \
543 template _macro(float, int32); \
544 template _macro(double, int32); \
545 template _macro(float, int64); \
546 template _macro(double, int64)
547#endif
548
549#if GINKGO_DPCPP_SINGLE_MODE
550#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
551 template _macro(float, int32); \
552 template <> \
553 _macro(double, int32) GKO_NOT_IMPLEMENTED; \
554 template _macro(std::complex<float>, int32); \
555 template <> \
556 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED
557#else
558#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INT32_TYPE(_macro) \
559 template _macro(float, int32); \
560 template _macro(double, int32); \
561 template _macro(std::complex<float>, int32); \
562 template _macro(std::complex<double>, int32)
563#endif
564
565
574#if GINKGO_DPCPP_SINGLE_MODE
575#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
576 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
577 template _macro(std::complex<float>, int32); \
578 template <> \
579 _macro(std::complex<double>, int32) GKO_NOT_IMPLEMENTED; \
580 template _macro(std::complex<float>, int64); \
581 template <> \
582 _macro(std::complex<double>, int64) GKO_NOT_IMPLEMENTED
583#else
584#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
585 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(_macro); \
586 template _macro(std::complex<float>, int32); \
587 template _macro(std::complex<double>, int32); \
588 template _macro(std::complex<float>, int64); \
589 template _macro(std::complex<double>, int64)
590#endif
591
592
602#if GINKGO_DPCPP_SINGLE_MODE
603#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
604 _macro) \
605 template _macro(float, int32, int32); \
606 template _macro(float, int32, int64); \
607 template _macro(float, int64, int64); \
608 template <> \
609 _macro(double, int32, int32) GKO_NOT_IMPLEMENTED; \
610 template <> \
611 _macro(double, int32, int64) GKO_NOT_IMPLEMENTED; \
612 template <> \
613 _macro(double, int64, int64) GKO_NOT_IMPLEMENTED
614#else
615#define GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
616 _macro) \
617 template _macro(float, int32, int32); \
618 template _macro(float, int32, int64); \
619 template _macro(float, int64, int64); \
620 template _macro(double, int32, int32); \
621 template _macro(double, int32, int64); \
622 template _macro(double, int64, int64)
623#endif
624
625
634#if GINKGO_DPCPP_SINGLE_MODE
635#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
636 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
637 _macro); \
638 template _macro(std::complex<float>, int32, int32); \
639 template _macro(std::complex<float>, int32, int64); \
640 template _macro(std::complex<float>, int64, int64); \
641 template <> \
642 _macro(std::complex<double>, int32, int32) GKO_NOT_IMPLEMENTED; \
643 template <> \
644 _macro(std::complex<double>, int32, int64) GKO_NOT_IMPLEMENTED; \
645 template <> \
646 _macro(std::complex<double>, int64, int64) GKO_NOT_IMPLEMENTED
647#else
648#define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
649 GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_LOCAL_GLOBAL_INDEX_TYPE( \
650 _macro); \
651 template _macro(std::complex<float>, int32, int32); \
652 template _macro(std::complex<float>, int32, int64); \
653 template _macro(std::complex<float>, int64, int64); \
654 template _macro(std::complex<double>, int32, int32); \
655 template _macro(std::complex<double>, int32, int64); \
656 template _macro(std::complex<double>, int64, int64)
657#endif
658
659
660#if GINKGO_DPCPP_SINGLE_MODE
661#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
662 template <> \
663 _macro(float, double) GKO_NOT_IMPLEMENTED; \
664 template <> \
665 _macro(double, float) GKO_NOT_IMPLEMENTED; \
666 template <> \
667 _macro(std::complex<float>, std::complex<double>) GKO_NOT_IMPLEMENTED; \
668 template <> \
669 _macro(std::complex<double>, std::complex<float>) GKO_NOT_IMPLEMENTED
670
671
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 <> \
676 _macro(double, double) GKO_NOT_IMPLEMENTED; \
677 template _macro(std::complex<float>, std::complex<float>); \
678 template <> \
679 _macro(std::complex<double>, std::complex<double>) GKO_NOT_IMPLEMENTED
680#else
690#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro) \
691 template _macro(float, double); \
692 template _macro(double, float); \
693 template _macro(std::complex<float>, std::complex<double>); \
694 template _macro(std::complex<double>, std::complex<float>)
695
696
706#define GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION_OR_COPY(_macro) \
707 GKO_INSTANTIATE_FOR_EACH_VALUE_CONVERSION(_macro); \
708 template _macro(float, float); \
709 template _macro(double, double); \
710 template _macro(std::complex<float>, std::complex<float>); \
711 template _macro(std::complex<double>, std::complex<double>)
712#endif
713
714
723#define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_PAIR(_macro) \
724 template _macro(float, float); \
725 template _macro(double, double); \
726 template _macro(std::complex<float>, float); \
727 template _macro(std::complex<double>, double); \
728 template _macro(std::complex<float>, std::complex<float>); \
729 template _macro(std::complex<double>, std::complex<double>)
730
731
741#define GKO_INSTANTIATE_FOR_EACH_COMBINED_VALUE_AND_INDEX_TYPE(_macro) \
742 template _macro(char, char); \
743 template _macro(int32, int32); \
744 template _macro(int64, int64); \
745 template _macro(unsigned int, unsigned int); \
746 template _macro(unsigned long, unsigned long); \
747 template _macro(float, float); \
748 template _macro(double, double); \
749 template _macro(long double, long double); \
750 template _macro(std::complex<float>, std::complex<float>); \
751 template _macro(std::complex<double>, std::complex<double>)
752
761#define GKO_INSTANTIATE_FOR_EACH_POD_TYPE(_macro) \
762 template _macro(float); \
763 template _macro(double); \
764 template _macro(std::complex<float>); \
765 template _macro(std::complex<double>); \
766 template _macro(size_type); \
767 template _macro(bool); \
768 template _macro(int32); \
769 template _macro(int64)
770
771
780#define GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(_macro) \
781 GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro); \
782 GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro); \
783 template _macro(gko::size_type)
784
785
789template <typename IndexType>
790inline constexpr GKO_ATTRIBUTES IndexType invalid_index()
791{
792 static_assert(std::is_signed<IndexType>::value,
793 "IndexType needs to be signed");
794 return static_cast<IndexType>(-1);
795}
796
797
798namespace experimental {
799namespace distributed {
800
801
808
809
819#define GKO_INSTANTIATE_FOR_EACH_LOCAL_GLOBAL_INDEX_TYPE(_macro) \
820 template _macro(int32, int32); \
821 template _macro(int32, int64); \
822 template _macro(int64, int64)
823
824
825} // namespace distributed
826} // namespace experimental
827} // namespace gko
828
829
830#endif // GKO_PUBLIC_CORE_BASE_TYPES_HPP_
This class is used to encode storage precisions of low precision algorithms.
Definition types.hpp:272
uint8 storage_type
The underlying datatype used to store the encoding.
Definition types.hpp:277
constexpr precision_reduction() noexcept
Creates a default precision_reduction encoding.
Definition types.hpp:294
constexpr storage_type get_nonpreserving() const noexcept
Returns the number of non-preserving conversions in the encoding.
Definition types.hpp:335
static constexpr precision_reduction common(precision_reduction x, precision_reduction y) noexcept
Returns the common encoding of input encodings.
Definition types.hpp:363
static constexpr precision_reduction autodetect() noexcept
Returns a special encoding which instructs the algorithm to automatically detect the best precision.
Definition types.hpp:347
constexpr storage_type get_preserving() const noexcept
Returns the number of preserving conversions in the encoding.
Definition types.hpp:325
constexpr precision_reduction(storage_type preserving, storage_type nonpreserving) noexcept
Creates a precision_reduction encoding with the specified number of conversions.
Definition types.hpp:303
int comm_index_type
Index type for enumerating processes in a distributed application.
Definition types.hpp:807
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:149
std::uint64_t uint64
64-bit unsigned integral type.
Definition types.hpp:166
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:137
double full_precision
The most precise floating-point type.
Definition types.hpp:199
std::int16_t int16
16-bit signed integral type.
Definition types.hpp:131
std::uintptr_t uintptr
Unsigned integer type capable of holding a pointer to void.
Definition types.hpp:172
constexpr bool operator!=(const dim< Dimensionality, DimensionType > &x, const dim< Dimensionality, DimensionType > &y)
Checks if two dim objects are different.
Definition dim.hpp:258
std::uint32_t uint32
32-bit unsigned integral type.
Definition types.hpp:160
std::int8_t int8
8-bit signed integral type.
Definition types.hpp:126
double float64
Double precision floating point type.
Definition types.hpp:193
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:205
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:143
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
constexpr size_type byte_size
Number of bits in a byte.
Definition types.hpp:211
std::uint16_t uint16
16-bit unsigned integral type.
Definition types.hpp:154
float float32
Single precision floating point type.
Definition types.hpp:187
half float16
Half precision floating point type.
Definition types.hpp:181
constexpr IndexType invalid_index()
Value for an invalid signed index type.
Definition types.hpp:790
Evaluates if all template arguments Args fulfill std::is_integral.
Definition types.hpp:223