Debian Codegraph @

packages/kokkos-kernels/batched/dense/src/KokkosBatched_Vector.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_HPP
17 #define KOKKOSBATCHED_VECTOR_HPP
18
19 /// \author Kyungjoo Kim ([email protected])
20
21 #include "KokkosBatched_Util.hpp"
22
23 // forward declaration
24 namespace KokkosBatched {
25
26 template <typename T, int l>
27 class Vector;
28
29 template <typename T, int l>
30 struct is_vector<Vector<SIMD<T>, l>> : public std::true_type {};
31
32 template <typename ValueType, typename MemorySpace>
33 struct DefaultVectorLength {
34 enum : int { value = 1 };
35 };
36
37 template <>
38 struct DefaultVectorLength<float, Kokkos::HostSpace> {
39 #if defined(__AVX512F__)
40 enum : int{value = 16};
41 #elif defined(__AVX__) || defined(__AVX2__)
42 enum : int{value = 8};
43 #elif defined(__ARM_ARCH)
44 enum : int{value = 8};
45 #else
46 enum : int { value = 8 };
47 #endif
48 };
49 template <>
50 struct DefaultVectorLength<double, Kokkos::HostSpace> {
51 #if defined(__AVX512F__)
52 enum : int{value = 8};
53 #elif defined(__AVX__) || defined(__AVX2__)
54 enum : int{value = 4};
55 #elif defined(__ARM_ARCH)
56 enum : int{value = 4};
57 #else
58 enum : int { value = 4 };
59 #endif
60 };
61 template <>
62 struct DefaultVectorLength<Kokkos::complex<float>, Kokkos::HostSpace> {
63 #if defined(__AVX512F__)
64 enum : int{value = 8};
65 #elif defined(__AVX__) || defined(__AVX2__)
66 enum : int{value = 4};
67 #elif defined(__ARM_ARCH)
68 enum : int{value = 4};
69 #else
70 enum : int { value = 4 };
71 #endif
72 };
73 template <>
74 struct DefaultVectorLength<Kokkos::complex<double>, Kokkos::HostSpace> {
75 #if defined(__AVX512F__)
76 enum : int{value = 4};
77 #elif defined(__AVX__) || defined(__AVX2__)
78 enum : int{value = 2};
79 #elif defined(__ARM_ARCH)
80 enum : int{value = 2};
81 #else
82 enum : int { value = 2 };
83 #endif
84 };
85
86 #if defined(KOKKOS_ENABLE_CUDA)
87 template <>
88 struct DefaultVectorLength<float, Kokkos::CudaSpace> {
89 enum : int { value = 8 };
90 };
91 template <>
92 struct DefaultVectorLength<double, Kokkos::CudaSpace> {
93 enum : int { value = 8 };
94 };
95 template <>
96 struct DefaultVectorLength<Kokkos::complex<float>, Kokkos::CudaSpace> {
97 enum : int { value = 8 };
98 };
99 template <>
100 struct DefaultVectorLength<Kokkos::complex<double>, Kokkos::CudaSpace> {
101 enum : int { value = 8 };
102 };
103 template <>
104 struct DefaultVectorLength<float, Kokkos::CudaUVMSpace> {
105 enum : int { value = 8 };
106 };
107 template <>
108 struct DefaultVectorLength<double, Kokkos::CudaUVMSpace> {
109 enum : int { value = 8 };
110 };
111 template <>
112 struct DefaultVectorLength<Kokkos::complex<float>, Kokkos::CudaUVMSpace> {
113 enum : int { value = 8 };
114 };
115 template <>
116 struct DefaultVectorLength<Kokkos::complex<double>, Kokkos::CudaUVMSpace> {
117 enum : int { value = 8 };
118 };
119 #endif
120
121 #if defined(KOKKOS_ENABLE_HIP)
122 template <>
123 struct DefaultVectorLength<float, Kokkos::HIPSpace> {
124 enum : int { value = 16 };
125 };
126 template <>
127 struct DefaultVectorLength<double, Kokkos::HIPSpace> {
128 enum : int { value = 16 };
129 };
130 template <>
131 struct DefaultVectorLength<Kokkos::complex<float>, Kokkos::HIPSpace> {
132 enum : int { value = 16 };
133 };
134 template <>
135 struct DefaultVectorLength<Kokkos::complex<double>, Kokkos::HIPSpace> {
136 enum : int { value = 16 };
137 };
138 #endif
139
140 template <typename ValueType, typename MemorySpace>
141 struct DefaultInternalVectorLength {
142 enum : int { value = 1 };
143 };
144 template <typename ValueType>
145 struct DefaultInternalVectorLength<ValueType, Kokkos::HostSpace> {
146 enum : int { value = DefaultVectorLength<ValueType, Kokkos::HostSpace>::value };
147 };
148
149 #if defined(KOKKOS_ENABLE_CUDA)
150 template <>
151 struct DefaultInternalVectorLength<float, Kokkos::CudaSpace> {
152 enum : int { value = 4 };
153 };
154 template <>
155 struct DefaultInternalVectorLength<double, Kokkos::CudaSpace> {
156 enum : int { value = 2 };
157 };
158 template <>
159 struct DefaultInternalVectorLength<Kokkos::complex<float>, Kokkos::CudaSpace> {
160 enum : int { value = 2 };
161 };
162 template <>
163 struct DefaultInternalVectorLength<Kokkos::complex<double>, Kokkos::CudaSpace> {
164 enum : int { value = 1 };
165 };
166 template <>
167 struct DefaultInternalVectorLength<float, Kokkos::CudaUVMSpace> {
168 enum : int { value = 4 };
169 };
170 template <>
171 struct DefaultInternalVectorLength<double, Kokkos::CudaUVMSpace> {
172 enum : int { value = 2 };
173 };
174 template <>
175 struct DefaultInternalVectorLength<Kokkos::complex<float>, Kokkos::CudaUVMSpace> {
176 enum : int { value = 2 };
177 };
178 template <>
179 struct DefaultInternalVectorLength<Kokkos::complex<double>, Kokkos::CudaUVMSpace> {
180 enum : int { value = 1 };
181 };
182 #endif
183
184 #if defined(KOKKOS_ENABLE_HIP)
185 template <>
186 struct DefaultInternalVectorLength<float, Kokkos::HIPSpace> {
187 enum : int { value = 8 };
188 };
189 template <>
190 struct DefaultInternalVectorLength<double, Kokkos::HIPSpace> {
191 enum : int { value = 4 };
192 };
193 template <>
194 struct DefaultInternalVectorLength<Kokkos::complex<float>, Kokkos::HIPSpace> {
195 enum : int { value = 4 };
196 };
197 template <>
198 struct DefaultInternalVectorLength<Kokkos::complex<double>, Kokkos::HIPSpace> {
199 enum : int { value = 2 };
200 };
201 #endif
202
203 template <typename T>
204 struct MagnitudeScalarType;
205
206 template <>
207 struct MagnitudeScalarType<float> {
208 typedef float type;
209 };
210 template <>
211 struct MagnitudeScalarType<double> {
212 typedef double type;
213 };
214 template <>
215 struct MagnitudeScalarType<Kokkos::complex<float>> {
216 typedef float type;
217 };
218 template <>
219 struct MagnitudeScalarType<Kokkos::complex<double>> {
220 typedef double type;
221 };
222
223 template <int l>
224 struct MagnitudeScalarType<Vector<SIMD<float>, l>> {
225 typedef float type;
226 };
227 template <int l>
228 struct MagnitudeScalarType<Vector<SIMD<double>, l>> {
229 typedef double type;
230 };
231 template <int l>
232 struct MagnitudeScalarType<Vector<SIMD<Kokkos::complex<float>>, l>> {
233 typedef float type;
234 };
235 template <int l>
236 struct MagnitudeScalarType<Vector<SIMD<Kokkos::complex<double>>, l>> {
237 typedef double type;
238 };
239
240 } // namespace KokkosBatched
241
242 #include "KokkosBatched_Vector_SIMD.hpp"
243
244 // arith traits overload for vector types
245 namespace Kokkos {
246
247 // do not use Vector alone as other can use the name.
248
249 template <typename T, int l>
250 class ArithTraits<KokkosBatched::Vector<KokkosBatched::SIMD<T>, l>> {
251 public:
252 typedef typename ArithTraits<T>::val_type val_scalar_type;
253 typedef typename ArithTraits<T>::mag_type mag_scalar_type;
254
255 typedef KokkosBatched::Vector<KokkosBatched::SIMD<val_scalar_type>, l> val_type;
256 typedef KokkosBatched::Vector<KokkosBatched::SIMD<mag_scalar_type>, l> mag_type;
257
258 static KOKKOS_FORCEINLINE_FUNCTION mag_type real(const val_type &val) { return val; }
259
260 static KOKKOS_FORCEINLINE_FUNCTION val_type conj(const val_type &val) { return val; }
261
262 static KOKKOS_FORCEINLINE_FUNCTION val_type abs(const val_type &val) {
263 using KAT = ArithTraits<typename val_type::value_type>;
264 val_type v{};
265 for (int i = 0; i < l; ++i) {
266 v[i] = KAT::abs(val[i]);
267 }
268 return v;
269 }
270
271 static const bool is_specialized = ArithTraits<T>::is_specialized;
272 static const bool is_signed = ArithTraits<T>::is_signed;
273 static const bool is_integer = ArithTraits<T>::is_integer;
274 static const bool is_exact = ArithTraits<T>::is_exact;
275 static const bool is_complex = ArithTraits<T>::is_complex;
276 };
277
278 template <typename T, int l>
279 class ArithTraits<KokkosBatched::Vector<KokkosBatched::SIMD<Kokkos::complex<T>>, l>> {
280 public:
281 typedef typename ArithTraits<T>::val_type val_scalar_type;
282 typedef typename ArithTraits<T>::mag_type mag_scalar_type;
283
284 typedef KokkosBatched::Vector<KokkosBatched::SIMD<Kokkos::complex<val_scalar_type>>, l> val_type;
285 typedef KokkosBatched::Vector<KokkosBatched::SIMD<mag_scalar_type>, l> mag_type;
286
287 static KOKKOS_FORCEINLINE_FUNCTION mag_type real(const val_type &val) {
288 mag_type r_val;
289 for (int i = 0; i < l; ++i) {
290 r_val[i] = val[i].real();
291 }
292 return r_val;
293 }
294 static KOKKOS_FORCEINLINE_FUNCTION mag_type imag(const val_type &val) {
295 mag_type r_val;
296 for (int i = 0; i < l; ++i) {
297 r_val[i] = val[i].imag();
298 }
299 return r_val;
300 }
301
302 static KOKKOS_FORCEINLINE_FUNCTION val_type conj(const val_type &val) {
303 using KAT = ArithTraits<typename val_type::value_type>;
304 val_type v{};
305 for (int i = 0; i < l; ++i) {
306 v[i] = KAT::conj(val[i]);
307 }
308 return v;
309 }
310
311 static KOKKOS_FORCEINLINE_FUNCTION val_type abs(const val_type &val) {
312 using KAT = ArithTraits<typename val_type::value_type>;
313 val_type v{};
314 for (int i = 0; i < l; ++i) {
315 v[i] = KAT::abs(val[i]);
316 }
317 return v;
318 }
319 };
320
321 } // namespace Kokkos
322
323 #endif