Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
types.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_TYPES_HPP_
34 #define GKO_CORE_TYPES_HPP_
35 
36 
37 #include <cassert>
38 #include <climits>
39 #include <cstddef>
40 #include <cstdint>
41 
42 
43 #include <complex>
44 #include <type_traits>
45 
46 
47 // Macros for handling different compilers / architectures uniformly
48 
49 #ifdef __CUDACC__
50 #define GKO_ATTRIBUTES __host__ __device__
51 #define GKO_INLINE __forceinline__
52 #else
53 #define GKO_ATTRIBUTES
54 #define GKO_INLINE inline
55 #endif // __CUDACC__
56 
57 
58 #if defined(__CUDA_ARCH__) && defined(__APPLE__)
59 
60 #ifdef NDEBUG
61 #define GKO_ASSERT(condition) ((void)0)
62 #else // NDEBUG
63 // Poor man's assertions on GPUs for MACs. They won't terminate the program
64 // but will at least print something on the screen
65 #define GKO_ASSERT(condition) \
66  ((condition) \
67  ? ((void)0) \
68  : ((void)printf("%s: %d: %s: Assertion `" #condition "' failed\n", \
69  __FILE__, __LINE__, __func__)))
70 #endif // NDEBUG
71 
72 #else // defined(__CUDA_ARCH__) && defined(__APPLE__)
73 
74 // Handle assertions normally on other systems
75 #define GKO_ASSERT(condition) assert(condition)
76 
77 #endif // defined(__CUDA_ARCH__) && defined(__APPLE__)
78 
79 
80 // Handle deprecated notices correctly on different systems
81 #if defined(_WIN32)
82 #define GKO_DEPRECATED(msg) __declspec(deprecated(msg))
83 #else
84 #define GKO_DEPRECATED(msg) __attribute__((deprecated(msg)))
85 #endif // defined(_WIN32)
86 
87 
88 namespace gko {
89 
90 
94 using size_type = std::size_t;
95 
96 
100 using int8 = std::int8_t;
101 
105 using int16 = std::int16_t;
106 
107 
111 using int32 = std::int32_t;
112 
113 
117 using int64 = std::int64_t;
118 
119 
123 using uint8 = std::uint8_t;
124 
128 using uint16 = std::uint16_t;
129 
130 
134 using uint32 = std::uint32_t;
135 
136 
140 using uint64 = std::uint64_t;
141 
142 
146 using uintptr = std::uintptr_t;
147 
148 
149 class half;
150 
151 
155 using float16 = half;
156 
157 
161 using float32 = float;
162 
163 
167 using float64 = double;
168 
169 
173 using full_precision = double;
174 
175 
179 using default_precision = double;
180 
181 
185 constexpr size_type byte_size = CHAR_BIT;
186 
187 
229 public:
234 
235 private:
236  static constexpr auto nonpreserving_bits = 4u;
237  static constexpr auto preserving_bits =
238  byte_size * sizeof(storage_type) - nonpreserving_bits;
239  static constexpr auto nonpreserving_mask =
240  storage_type{(0x1 << nonpreserving_bits) - 1};
241  static constexpr auto preserving_mask =
242  storage_type{(0x1 << preserving_bits) - 1} << nonpreserving_bits;
243 
244 public:
250  GKO_ATTRIBUTES constexpr precision_reduction() noexcept : data_{0x0} {}
251 
259  GKO_ATTRIBUTES constexpr precision_reduction(
260  storage_type preserving, storage_type nonpreserving) noexcept
261  : data_((GKO_ASSERT(preserving < (0x1 << preserving_bits) - 1),
262  GKO_ASSERT(nonpreserving < (0x1 << nonpreserving_bits) - 1),
263  (preserving << nonpreserving_bits) | nonpreserving))
264  {}
265 
271  GKO_ATTRIBUTES constexpr operator storage_type() const noexcept
272  {
273  return data_;
274  }
275 
281  GKO_ATTRIBUTES constexpr storage_type get_preserving() const noexcept
282  {
283  return (data_ & preserving_mask) >> nonpreserving_bits;
284  }
285 
291  GKO_ATTRIBUTES constexpr storage_type get_nonpreserving() const noexcept
292  {
293  return data_ & nonpreserving_mask;
294  }
295 
303  GKO_ATTRIBUTES constexpr static precision_reduction autodetect() noexcept
304  {
305  return precision_reduction{preserving_mask | nonpreserving_mask};
306  }
307 
319  GKO_ATTRIBUTES constexpr static precision_reduction common(
321  {
322  return precision_reduction(
323  min(x.data_ & preserving_mask, y.data_ & preserving_mask) |
324  min(x.data_ & nonpreserving_mask, y.data_ & nonpreserving_mask));
325  }
326 
327 private:
328  GKO_ATTRIBUTES constexpr precision_reduction(storage_type data)
329  : data_{data}
330  {}
331 
332  GKO_ATTRIBUTES constexpr static storage_type min(storage_type x,
333  storage_type y) noexcept
334  {
335  return x < y ? x : y;
336  }
337 
338  storage_type data_;
339 };
340 
341 
350 GKO_ATTRIBUTES constexpr bool operator==(precision_reduction x,
351  precision_reduction y) noexcept
352 {
354  return static_cast<st>(x) == static_cast<st>(y);
355 }
356 
357 
366 GKO_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 
386 #define GKO_ENABLE_FOR_ALL_EXECUTORS(_enable_macro) \
387  _enable_macro(OmpExecutor, omp); \
388  _enable_macro(CudaExecutor, cuda)
389 
390 
399 #define GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(_macro) \
400  template _macro(float); \
401  template _macro(double); \
402  template _macro(std::complex<float>); \
403  template _macro(std::complex<double>)
404 
405 
414 #define GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(_macro) \
415  template _macro(int32); \
416  template _macro(int64)
417 
418 
427 #define GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
428  template _macro(float, int32); \
429  template _macro(double, int32); \
430  template _macro(std::complex<float>, int32); \
431  template _macro(std::complex<double>, int32); \
432  template _macro(float, int64); \
433  template _macro(double, int64); \
434  template _macro(std::complex<float>, int64); \
435  template _macro(std::complex<double>, int64)
436 
437 
438 } // namespace gko
439 
440 
441 #endif // GKO_CORE_TYPES_HPP_
constexpr precision_reduction(storage_type preserving, storage_type nonpreserving) noexcept
Creates a precision_reduction encoding with the specified number of conversions.
Definition: types.hpp:259
std::uint16_t uint16
16-bit unsigned integral type.
Definition: types.hpp:128
This class is used to encode storage precisions of low precision algorithms.
Definition: types.hpp:228
constexpr size_type byte_size
Number of bits in a byte.
Definition: types.hpp:185
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:179
double float64
Double precision floating point type.
Definition: types.hpp:167
constexpr bool operator!=(const dim< Dimensionality, DimensionType > &x, const dim< Dimensionality, DimensionType > &y)
Checks if two dim objects are different.
Definition: dim.hpp:216
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:111
std::uint32_t uint32
32-bit unsigned integral type.
Definition: types.hpp:134
float float32
Single precision floating point type.
Definition: types.hpp:161
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
static constexpr precision_reduction autodetect() noexcept
Returns a special encoding which instructs the algorithm to automatically detect the best precision...
Definition: types.hpp:303
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
std::uint64_t uint64
64-bit unsigned integral type.
Definition: types.hpp:140
constexpr storage_type get_nonpreserving() const noexcept
Returns the number of non-preserving conversions in the encoding.
Definition: types.hpp:291
constexpr precision_reduction() noexcept
Creates a default precision_reduction encoding.
Definition: types.hpp:250
std::int8_t int8
8-bit signed integral type.
Definition: types.hpp:100
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:123
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:117
double full_precision
The most precise floating-point type.
Definition: types.hpp:173
static constexpr precision_reduction common(precision_reduction x, precision_reduction y) noexcept
Returns the common encoding of input encodings.
Definition: types.hpp:319
uint8 storage_type
The underlying datatype used to store the encoding.
Definition: types.hpp:233
std::int16_t int16
16-bit signed integral type.
Definition: types.hpp:105
half float16
Half precision floating point type.
Definition: types.hpp:155
constexpr storage_type get_preserving() const noexcept
Returns the number of preserving conversions in the encoding.
Definition: types.hpp:281