File: TestSIMD_LoadStore.hpp

package info (click to toggle)
kokkos 5.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,140 kB
  • sloc: cpp: 225,293; sh: 1,250; python: 78; makefile: 16; fortran: 4; ansic: 2
file content (236 lines) | stat: -rw-r--r-- 8,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project

#ifndef KOKKOS_TEST_SIMD_LOADSTORE_HPP
#define KOKKOS_TEST_SIMD_LOADSTORE_HPP

#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.simd;
import kokkos.simd_impl;
#else
#include <Kokkos_SIMD.hpp>
#endif
#include <SIMDTesting_Utilities.hpp>

template <typename SimdType, typename... Args>
inline void host_test_simd_load(SimdType const& init, SimdType const& expected,
                                Args... args) {
  using data_type = typename SimdType::value_type;
  using abi_type  = typename SimdType::abi_type;

  constexpr size_t alignment =
      SimdType::size() * sizeof(typename SimdType::value_type);

  alignas(alignment) typename SimdType::value_type arr[SimdType::size()] = {0};
  simd_unchecked_store(init, arr, Kokkos::Experimental::simd_flag_default);

  SimdType result;

  if constexpr (sizeof...(args) > 1) {
    result = simd_partial_load(arr, args...);
  } else {
    if constexpr (std::is_same_v<Kokkos::Experimental::simd_abi::Impl::
                                     host_fixed_native<data_type>,
                                 abi_type>) {
      result = Kokkos::Experimental::simd_unchecked_load(arr, args...);
    } else {
      result =
          Kokkos::Experimental::simd_unchecked_load<SimdType>(arr, args...);
    }
  }

  auto mask = (result == expected);
  for (size_t i = 0; i < SimdType::size(); ++i) {
    EXPECT_TRUE(mask[i]);
  }
}

template <typename SimdType, typename... Args>
inline void host_test_simd_store(SimdType const& init, SimdType const& expected,
                                 Args... args) {
  constexpr size_t alignment =
      SimdType::size() * sizeof(typename SimdType::value_type);

  alignas(alignment) typename SimdType::value_type arr[SimdType::size()] = {0};

  if constexpr (sizeof...(args) > 1) {
    simd_partial_store(init, arr, args...);
  } else {
    simd_unchecked_store(init, arr, args...);
  }

  for (size_t i = 0; i < SimdType::size(); ++i) {
    EXPECT_EQ(arr[i], expected[i]);
  }
}

template <typename Abi, typename DataType>
inline void host_test_simd_loadstore() {
  using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
  using mask_type = typename simd_type::mask_type;

  mask_type mask(KOKKOS_LAMBDA(std::size_t i) { return i % 2 == 0; });
  simd_type expected(KOKKOS_LAMBDA(std::size_t i) { return (i + 1) * 12; });
  simd_type expected_masked(KOKKOS_LAMBDA(std::size_t i) {
    return (i % 2 == 0) ? (i + 1) * 12 : DataType();
  });

  host_test_simd_store(expected, expected,
                       Kokkos::Experimental::simd_flag_default);
  host_test_simd_store(expected, expected,
                       Kokkos::Experimental::simd_flag_aligned);
  host_test_simd_store(expected, expected_masked, mask,
                       Kokkos::Experimental::simd_flag_default);
  host_test_simd_store(expected, expected_masked, mask,
                       Kokkos::Experimental::simd_flag_aligned);

  host_test_simd_load(expected, expected,
                      Kokkos::Experimental::simd_flag_default);
  host_test_simd_load(expected, expected,
                      Kokkos::Experimental::simd_flag_aligned);
  host_test_simd_load(expected, expected_masked, mask,
                      Kokkos::Experimental::simd_flag_default);
  host_test_simd_load(expected, expected_masked, mask,
                      Kokkos::Experimental::simd_flag_aligned);
}

template <typename Abi, typename DataType>
inline void host_check_loadstore() {
  if constexpr (is_simd_avail_v<DataType, Abi>) {
    host_test_simd_loadstore<Abi, DataType>();
  }
}

template <typename Abi, typename... DataTypes>
inline void host_check_loadstore_all_types(
    Kokkos::Experimental::Impl::data_types<DataTypes...>) {
  (host_check_loadstore<Abi, DataTypes>(), ...);
}

template <typename... Abis>
inline void host_check_loadstore_all_abis(
    Kokkos::Experimental::Impl::abi_set<Abis...>) {
  using DataTypes = Kokkos::Experimental::Impl::data_type_set;
  (host_check_loadstore_all_types<Abis>(DataTypes()), ...);
}

template <typename SimdType, typename... Args>
KOKKOS_INLINE_FUNCTION void device_test_simd_load(SimdType const& init,
                                                  SimdType const& expected,
                                                  Args... args) {
  using data_type = typename SimdType::value_type;
  using abi_type  = typename SimdType::abi_type;

  constexpr size_t alignment =
      SimdType::size() * sizeof(typename SimdType::value_type);

  alignas(alignment) typename SimdType::value_type arr[SimdType::size()] = {0};
  simd_unchecked_store(init, arr, Kokkos::Experimental::simd_flag_default);

  SimdType result;

  if constexpr (sizeof...(args) > 1) {
    result = simd_partial_load(arr, args...);
  } else {
    if constexpr (std::is_same_v<Kokkos::Experimental::simd_abi::Impl::
                                     host_fixed_native<data_type>,
                                 abi_type>) {
      result = Kokkos::Experimental::simd_unchecked_load(arr, args...);
    } else {
      result =
          Kokkos::Experimental::simd_unchecked_load<SimdType>(arr, args...);
    }
  }

  device_check_equality(result, expected, SimdType::size());
}

template <typename SimdType, typename... Args>
KOKKOS_INLINE_FUNCTION void device_test_simd_store(SimdType const& init,
                                                   SimdType const& expected,
                                                   Args... args) {
  constexpr size_t alignment =
      SimdType::size() * sizeof(typename SimdType::value_type);

  alignas(alignment) typename SimdType::value_type arr[SimdType::size()] = {0};

  if constexpr (sizeof...(args) > 1) {
    simd_partial_store(init, arr, args...);
  } else {
    simd_unchecked_store(init, arr, args...);
  }

  kokkos_checker checker;
  for (size_t i = 0; i < SimdType::size(); ++i) {
    checker.equality(arr[i], expected[i]);
  }
}

template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_test_simd_loadstore() {
  using simd_type = Kokkos::Experimental::basic_simd<DataType, Abi>;
  using mask_type = typename simd_type::mask_type;

  mask_type mask([=](std::size_t i) { return i % 2 == 0; });
  simd_type expected([=](std::size_t i) { return (i + 1) * 12; });
  simd_type expected_masked(
      [=](std::size_t i) { return (mask[i]) ? (i + 1) * 12 : DataType(); });

  device_test_simd_store(expected, expected,
                         Kokkos::Experimental::simd_flag_default);
  device_test_simd_store(expected, expected,
                         Kokkos::Experimental::simd_flag_aligned);
  device_test_simd_store(expected, expected_masked, mask,
                         Kokkos::Experimental::simd_flag_default);
  device_test_simd_store(expected, expected_masked, mask,
                         Kokkos::Experimental::simd_flag_aligned);

  device_test_simd_load(expected, expected,
                        Kokkos::Experimental::simd_flag_default);
  device_test_simd_load(expected, expected,
                        Kokkos::Experimental::simd_flag_aligned);
  device_test_simd_load(expected, expected_masked, mask,
                        Kokkos::Experimental::simd_flag_default);
  device_test_simd_load(expected, expected_masked, mask,
                        Kokkos::Experimental::simd_flag_aligned);
}

template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_loadstore() {
  if constexpr (is_simd_avail_v<DataType, Abi>) {
    device_test_simd_loadstore<Abi, DataType>();
  }
}

template <typename Abi, typename... DataTypes>
KOKKOS_INLINE_FUNCTION void device_check_loadstore_all_types(
    Kokkos::Experimental::Impl::data_types<DataTypes...>) {
  (device_check_loadstore<Abi, DataTypes>(), ...);
}

template <typename... Abis>
KOKKOS_INLINE_FUNCTION void device_check_loadstore_all_abis(
    Kokkos::Experimental::Impl::abi_set<Abis...>) {
  using DataTypes = Kokkos::Experimental::Impl::data_type_set;
  (device_check_loadstore_all_types<Abis>(DataTypes()), ...);
}

class simd_device_loadstore_functor {
 public:
  KOKKOS_INLINE_FUNCTION void operator()(int) const {
    device_check_loadstore_all_abis(
        Kokkos::Experimental::Impl::device_abi_set());
  }
};

TEST(simd, host_loadstore) {
  host_check_loadstore_all_abis(Kokkos::Experimental::Impl::host_abi_set());
}

TEST(simd, device_loadstore) {
  Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::IndexType<int>>(0, 1),
                       simd_device_loadstore_functor());
}

#endif