| 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_VIEW_HPP |
| 17 |
#define KOKKOSBATCHED_VECTOR_SIMD_VIEW_HPP |
| 18 |
|
| 19 |
#include <KokkosKernels_helpers.hpp> |
| 20 |
|
| 21 |
/// \author Kyungjoo Kim ([email protected]) |
| 22 |
#pragma GCC diagnostic push |
| 23 |
#pragma GCC diagnostic ignored "-Wswitch" |
| 24 |
|
| 25 |
namespace KokkosBatched { |
| 26 |
|
| 27 |
template <int dim> |
| 28 |
struct PackDim { |
| 29 |
enum : int { value = dim }; |
| 30 |
}; |
| 31 |
|
| 32 |
// temporary solution until kokkos support SIMD layout or I do support it |
| 33 |
template <typename ViewType, typename PackDim> |
| 34 |
struct SimdViewAccess { |
| 35 |
private: |
| 36 |
ViewType _a; |
| 37 |
|
| 38 |
public: |
| 39 |
typedef typename ViewType::reference_type reference_simd_type; |
| 40 |
typedef typename ViewType::pointer_type pointer_simd_type; |
| 41 |
typedef typename ViewType::value_type value_simd_type; |
| 42 |
|
| 43 |
typedef typename value_simd_type::value_type value_type; |
| 44 |
typedef value_type &reference_type; |
| 45 |
typedef value_type *pointer_type; |
| 46 |
|
| 47 |
enum : int { rank = ViewType::rank }; |
| 48 |
enum : int { pack_dim = PackDim::value }; |
| 49 |
enum : int { vector_length = value_simd_type::vector_length }; |
| 50 |
|
| 51 |
SimdViewAccess() : _a() {} |
| 52 |
SimdViewAccess(const ViewType &a) : _a(a) {} |
| 53 |
|
| 54 |
SimdViewAccess &operator=(const ViewType &b) { |
| 55 |
_a = b; |
| 56 |
return *this; |
| 57 |
} |
| 58 |
SimdViewAccess &operator=(const SimdViewAccess &b) { |
| 59 |
if (this != &b) { |
| 60 |
_a = b._a; |
| 61 |
} |
| 62 |
return *this; |
| 63 |
} |
| 64 |
|
| 65 |
template <typename iType> |
| 66 |
KOKKOS_INLINE_FUNCTION constexpr typename std::enable_if<std::is_integral<iType>::value, size_t>::type extent( |
| 67 |
const iType &r) const { |
| 68 |
return _a.extent(r) * (r == PackDim::value ? vector_length : 1); |
| 69 |
} |
| 70 |
|
| 71 |
template <typename iType> |
| 72 |
KOKKOS_INLINE_FUNCTION constexpr typename std::enable_if<std::is_integral<iType>::value, int>::type extent_int( |
| 73 |
const iType &r) const { |
| 74 |
return static_cast<int>(_a.extent(r) * (r == PackDim::value ? vector_length : 1)); |
| 75 |
} |
| 76 |
|
| 77 |
KOKKOS_INLINE_FUNCTION constexpr size_t size() const { return (_a.size() * vector_length); } |
| 78 |
|
| 79 |
KOKKOS_INLINE_FUNCTION constexpr size_t span() const { return _a.span() * vector_length; } |
| 80 |
KOKKOS_INLINE_FUNCTION constexpr bool span_span_is_contiguous() const { return _a.span_span_is_contiguous(); } |
| 81 |
KOKKOS_INLINE_FUNCTION constexpr pointer_type data() const { return _a.data(); } |
| 82 |
|
| 83 |
/// rank 0 |
| 84 |
/// this does not make sense as this is flat view to simd view |
| 85 |
|
| 86 |
/// rank 1 |
| 87 |
template <typename I0, class... Args> |
| 88 |
KOKKOS_FORCEINLINE_FUNCTION |
| 89 |
std::enable_if_t<KokkosKernels::Impl::are_integral_v<I0, Args...> && 1 == ViewType::rank, reference_type> |
| 90 |
operator()(const I0 &i0, Args... /*args*/) const { |
| 91 |
return _a(i0 / vector_length)[i0 % vector_length]; |
| 92 |
} |
| 93 |
|
| 94 |
/// rank 2 |
| 95 |
template <typename I0, typename I1, class... Args> |
| 96 |
KOKKOS_FORCEINLINE_FUNCTION |
| 97 |
std::enable_if_t<KokkosKernels::Impl::are_integral_v<I0, I1, Args...> && 2 == ViewType::rank, reference_type> |
| 98 |
operator()(const I0 &i0, const I1 &i1, Args... /*args*/) const { |
| 99 |
switch (PackDim::value) { |
| 100 |
case 0: return _a(i0 / vector_length, i1)[i0 % vector_length]; |
| 101 |
case 1: break; |
| 102 |
default: break; |
| 103 |
} |
| 104 |
return _a(i0, i1 / vector_length)[i1 % vector_length]; |
| 105 |
} |
| 106 |
|
| 107 |
/// rank 3 |
| 108 |
template <typename I0, typename I1, typename I2, class... Args> |
| 109 |
KOKKOS_FORCEINLINE_FUNCTION |
| 110 |
std::enable_if_t<KokkosKernels::Impl::are_integral_v<I0, I1, I2, Args...> && 3 == ViewType::rank, reference_type> |
| 111 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, Args... /*args*/) const { |
| 112 |
switch (PackDim::value) { |
| 113 |
case 0: return _a(i0 / vector_length, i1, i2)[i0 % vector_length]; |
| 114 |
case 1: return _a(i0, i1 / vector_length, i2)[i1 % vector_length]; |
| 115 |
case 2: break; |
| 116 |
default: break; |
| 117 |
} |
| 118 |
return _a(i0, i1, i2 / vector_length)[i2 % vector_length]; |
| 119 |
} |
| 120 |
|
| 121 |
/// rank 4 |
| 122 |
template <typename I0, typename I1, typename I2, typename I3, class... Args> |
| 123 |
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t< |
| 124 |
KokkosKernels::Impl::are_integral_v<I0, I1, I2, I3, Args...> && 4 == ViewType::rank, reference_type> |
| 125 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, const I3 &i3, Args... /*args*/) const { |
| 126 |
switch (PackDim::value) { |
| 127 |
case 0: return _a(i0 / vector_length, i1, i2, i3)[i0 % vector_length]; |
| 128 |
case 1: return _a(i0, i1 / vector_length, i2, i3)[i1 % vector_length]; |
| 129 |
case 2: return _a(i0, i1, i2 / vector_length, i3)[i2 % vector_length]; |
| 130 |
case 3: break; |
| 131 |
default: break; |
| 132 |
} |
| 133 |
return _a(i0, i1, i2, i3 / vector_length)[i3 % vector_length]; |
| 134 |
} |
| 135 |
|
| 136 |
/// rank 5 |
| 137 |
template <typename I0, typename I1, typename I2, typename I3, typename I4, class... Args> |
| 138 |
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t< |
| 139 |
KokkosKernels::Impl::are_integral_v<I0, I1, I2, I3, I4, Args...> && 5 == ViewType::rank, reference_type> |
| 140 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, const I3 &i3, const I4 &i4, Args... /*args*/) const { |
| 141 |
switch (PackDim::value) { |
| 142 |
case 0: return _a(i0 / vector_length, i1, i2, i3, i4)[i0 % vector_length]; |
| 143 |
case 1: return _a(i0, i1 / vector_length, i2, i3, i4)[i1 % vector_length]; |
| 144 |
case 2: return _a(i0, i1, i2 / vector_length, i3, i4)[i2 % vector_length]; |
| 145 |
case 3: return _a(i0, i1, i2, i3 / vector_length, i4)[i3 % vector_length]; |
| 146 |
case 4: break; |
| 147 |
default: break; |
| 148 |
} |
| 149 |
return _a(i0, i1, i2, i3, i4 / vector_length)[i4 % vector_length]; |
| 150 |
} |
| 151 |
|
| 152 |
/// rank 6 |
| 153 |
template <typename I0, typename I1, typename I2, typename I3, typename I4, typename I5, class... Args> |
| 154 |
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t< |
| 155 |
KokkosKernels::Impl::are_integral_v<I0, I1, I2, I3, I4, I5, Args...> && 6 == ViewType::rank, reference_type> |
| 156 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, const I3 &i3, const I4 &i4, const I5 &i5, |
| 157 |
Args... /*args*/) const { |
| 158 |
switch (PackDim::value) { |
| 159 |
case 0: return _a(i0 / vector_length, i1, i2, i3, i4, i5)[i0 % vector_length]; |
| 160 |
case 1: return _a(i0, i1 / vector_length, i2, i3, i4, i5)[i1 % vector_length]; |
| 161 |
case 2: return _a(i0, i1, i2 / vector_length, i3, i4, i5)[i2 % vector_length]; |
| 162 |
case 3: return _a(i0, i1, i2, i3 / vector_length, i4, i5)[i3 % vector_length]; |
| 163 |
case 4: return _a(i0, i1, i2, i3, i4 / vector_length, i5)[i4 % vector_length]; |
| 164 |
case 5: break; |
| 165 |
default: break; |
| 166 |
} |
| 167 |
return _a(i0, i1, i2, i3, i4, i5 / vector_length)[i5 % vector_length]; |
| 168 |
} |
| 169 |
|
| 170 |
/// rank 7 |
| 171 |
template <typename I0, typename I1, typename I2, typename I3, typename I4, typename I5, typename I6, class... Args> |
| 172 |
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t< |
| 173 |
KokkosKernels::Impl::are_integral_v<I0, I1, I2, I3, I4, I5, I6, Args...> && 7 == ViewType::rank, reference_type> |
| 174 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, const I3 &i3, const I4 &i4, const I5 &i5, const I6 &i6, |
| 175 |
Args... /*args*/) const { |
| 176 |
switch (PackDim::value) { |
| 177 |
case 0: return _a(i0 / vector_length, i1, i2, i3, i4, i5, i6)[i0 % vector_length]; |
| 178 |
case 1: return _a(i0, i1 / vector_length, i2, i3, i4, i5, i6)[i1 % vector_length]; |
| 179 |
case 2: return _a(i0, i1, i2 / vector_length, i3, i4, i5, i6)[i2 % vector_length]; |
| 180 |
case 3: return _a(i0, i1, i2, i3 / vector_length, i4, i5, i6)[i3 % vector_length]; |
| 181 |
case 4: return _a(i0, i1, i2, i3, i4 / vector_length, i5, i6)[i4 % vector_length]; |
| 182 |
case 5: return _a(i0, i1, i2, i3, i4, i5 / vector_length, i6)[i5 % vector_length]; |
| 183 |
case 6: break; |
| 184 |
default: break; |
| 185 |
} |
| 186 |
return _a(i0, i1, i2, i3, i4, i5, i6 / vector_length)[i6 % vector_length]; |
| 187 |
} |
| 188 |
|
| 189 |
/// rank 8 |
| 190 |
template <typename I0, typename I1, typename I2, typename I3, typename I4, typename I5, typename I6, typename I7, |
| 191 |
class... Args> |
| 192 |
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t< |
| 193 |
KokkosKernels::Impl::are_integral_v<I0, I1, I2, I3, I4, I5, I6, I7, Args...> && 8 == ViewType::rank, |
| 194 |
reference_type> |
| 195 |
operator()(const I0 &i0, const I1 &i1, const I2 &i2, const I3 &i3, const I4 &i4, const I5 &i5, const I6 &i6, |
| 196 |
const I7 &i7, Args... /*args*/) const { |
| 197 |
switch (PackDim::value) { |
| 198 |
case 0: return _a(i0 / vector_length, i1, i2, i3, i4, i5, i6, i7)[i0 % vector_length]; |
| 199 |
case 1: return _a(i0, i1 / vector_length, i2, i3, i4, i5, i6, i7)[i1 % vector_length]; |
| 200 |
case 2: return _a(i0, i1, i2 / vector_length, i3, i4, i5, i6, i7)[i2 % vector_length]; |
| 201 |
case 3: return _a(i0, i1, i2, i3 / vector_length, i4, i5, i6, i7)[i3 % vector_length]; |
| 202 |
case 4: return _a(i0, i1, i2, i3, i4 / vector_length, i5, i6, i7)[i4 % vector_length]; |
| 203 |
case 5: return _a(i0, i1, i2, i3, i4, i5 / vector_length, i6, i7)[i5 % vector_length]; |
| 204 |
case 6: return _a(i0, i1, i2, i3, i4, i5, i6 / vector_length, i7)[i6 % vector_length]; |
| 205 |
case 7: break; |
| 206 |
default: break; |
| 207 |
} |
| 208 |
return _a(i0, i1, i2, i3, i4, i5, i6, i7 / vector_length)[i7 % vector_length]; |
| 209 |
} |
| 210 |
}; |
| 211 |
} // namespace KokkosBatched |
| 212 |
|
| 213 |
#pragma GCC diagnostic pop |
| 214 |
|
| 215 |
#endif |