Debian Codegraph @

packages/kokkos-kernels/batched/dense/impl/KokkosBatched_Vector_SIMD_Misc.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 #ifndef KOKKOSBATCHED_VECTOR_SIMD_MISC_HPP
17 #define KOKKOSBATCHED_VECTOR_SIMD_MISC_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "Kokkos_Complex.hpp"
22
23 namespace KokkosBatched {
24
25 #define KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l) Vector<SIMD<T>, l>
26 #define KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) void
27 // typename std::enable_if<std::is_convertible< T1 , T0 >::value &&
28 // std::is_convertible< T2 , T0 >::value, void >::type
29
30 // scalar, scalar
31
32 template <typename T>
33 KOKKOS_INLINE_FUNCTION static T conditional_assign(const bool cond, const T &if_true_val, const T &if_false_val) {
34 return cond ? if_true_val : if_false_val;
35 }
36
37 template <typename T0, typename T1, typename T2>
38 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l)
39 conditional_assign(/* */ T0 &r_val, const bool cond, const T1 &if_true_val, const T2 &if_false_val) {
40 r_val = cond ? if_true_val : if_false_val;
41 }
42
43 // vector, scalar
44
45 template <typename T, int l>
46 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l)
47 conditional_assign(const Vector<SIMD<bool>, l> &cond, const Vector<SIMD<T>, l> &if_true_val,
48 const T &if_false_val) {
49 Vector<SIMD<T>, l> r_val;
50 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val;
51 return r_val;
52 }
53
54 template <typename T0, typename T1, typename T2, int l>
55 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l)
56 conditional_assign(/* */ Vector<SIMD<T0>, l> &r_val, const Vector<SIMD<bool>, l> &cond,
57 const Vector<SIMD<T1>, l> &if_true_val, const T2 &if_false_val) {
58 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val;
59 }
60
61 // scalar, vector
62
63 template <typename T, int l>
64 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l)
65 conditional_assign(const Vector<SIMD<bool>, l> &cond, const T &if_true_val,
66 const Vector<SIMD<T>, l> &if_false_val) {
67 Vector<SIMD<T>, l> r_val;
68 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val : if_false_val[i];
69 return r_val;
70 }
71
72 template <typename T0, typename T1, typename T2, int l>
73 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l)
74 conditional_assign(/* */ Vector<SIMD<T0>, l> &r_val, const Vector<SIMD<bool>, l> &cond, const T1 &if_true_val,
75 const Vector<SIMD<T2>, l> &if_false_val) {
76 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val : if_false_val[i];
77 }
78
79 // vector, vector
80
81 template <typename T, int l>
82 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l)
83 conditional_assign(const Vector<SIMD<bool>, l> &cond, const Vector<SIMD<T>, l> &if_true_val,
84 const Vector<SIMD<T>, l> &if_false_val) {
85 Vector<SIMD<T>, l> r_val;
86 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val[i];
87 return r_val;
88 }
89
90 template <typename T0, typename T1, typename T2, int l>
91 KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l)
92 conditional_assign(/* */ Vector<SIMD<T0>, l> &r_val, const Vector<SIMD<bool>, l> &cond,
93 const Vector<SIMD<T1>, l> &if_true_val, const Vector<SIMD<T2>, l> &if_false_val) {
94 for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val[i];
95 }
96
97 template <typename T, int l, typename BinaryOp>
98 KOKKOS_INLINE_FUNCTION static T reduce(const Vector<SIMD<T>, l> &val, const BinaryOp &func) {
99 T r_val = val[0];
100 for (int i = 1; i < l; ++i) r_val = func(r_val, val[i]);
101 return r_val;
102 }
103
104 template <typename T, int l, typename BinaryOp>
105 KOKKOS_INLINE_FUNCTION static T reduce(const Vector<SIMD<T>, l> &val, const BinaryOp &func, const T init) {
106 T r_val = init;
107 for (int i = 0; i < l; ++i) r_val = func(r_val, val[i]);
108 return r_val;
109 }
110
111 template <int l>
112 KOKKOS_INLINE_FUNCTION static bool is_all_true(const Vector<SIMD<bool>, l> &cond) {
113 return reduce(cond, [](const bool left, const bool right) -> bool { return (left && right); });
114 }
115
116 template <int l>
117 KOKKOS_INLINE_FUNCTION static bool is_any_true(const Vector<SIMD<bool>, l> &cond) {
118 return reduce(cond, [](const bool left, const bool right) -> bool { return left || right; });
119 }
120
121 template <typename T, int l>
122 KOKKOS_INLINE_FUNCTION static T min(const Vector<SIMD<T>, l> &val) {
123 return reduce(val, [](const T left, const T right) -> T {
124 const auto tmp = left < right;
125 return tmp * left + !tmp * right;
126 });
127 }
128
129 template <typename T, int l>
130 KOKKOS_INLINE_FUNCTION static T max(const Vector<SIMD<T>, l> &val) {
131 return reduce(val, [](const T left, const T right) -> T {
132 const auto tmp = left > right;
133 return tmp * left + !tmp * right;
134 });
135 }
136
137 template <typename T, int l>
138 KOKKOS_INLINE_FUNCTION static T sum(const Vector<SIMD<T>, l> &val) {
139 return reduce(
140 val, [](const T left, const T right) -> T { return left + right; }, T(0));
141 }
142
143 template <typename T, int l>
144 KOKKOS_INLINE_FUNCTION static T prod(const Vector<SIMD<T>, l> &val) {
145 return reduce(
146 val, [](const T left, const T right) -> T { return left * right; }, T(1));
147 }
148
149 #undef KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE
150 #undef KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE
151
152 } // namespace KokkosBatched
153
154 #endif