File: TestSIMD_WhereExpressions.hpp

package info (click to toggle)
kokkos 4.7.01-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,636 kB
  • sloc: cpp: 223,676; sh: 2,446; makefile: 2,437; python: 91; fortran: 4; ansic: 2
file content (227 lines) | stat: -rw-r--r-- 8,417 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
//@HEADER
// ************************************************************************
//
//                        Kokkos v. 4.0
//       Copyright (2022) National Technology & Engineering
//               Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#ifndef KOKKOS_TEST_SIMD_WHERE_EXPRESSIONS_HPP
#define KOKKOS_TEST_SIMD_WHERE_EXPRESSIONS_HPP

#include <Kokkos_SIMD.hpp>
#include <SIMDTesting_Utilities.hpp>

template <typename Abi, typename DataType>
inline void host_check_where_expr_scatter_to() {
  if constexpr (is_simd_avail_v<DataType, Abi>) {
    using simd_type  = Kokkos::Experimental::basic_simd<DataType, Abi>;
    using index_type = Kokkos::Experimental::basic_simd<std::int32_t, Abi>;
    using mask_type  = typename simd_type::mask_type;

    constexpr std::size_t nlanes = simd_type::size();
    DataType init[]              = {11, 13, 17, 19, 23, 29, 31, 37,
                                    53, 71, 79, 83, 89, 93, 97, 103};
    simd_type src;
    src.copy_from(init, Kokkos::Experimental::simd_flag_default);

    for (std::size_t idx = 0; idx < nlanes; ++idx) {
      if constexpr (std::is_same_v<Abi,
                                   Kokkos::Experimental::simd_abi::scalar>) {
        mask_type mask(KOKKOS_LAMBDA(std::size_t i) { return i != idx; });

        DataType dst[nlanes] = {0};
        for (std::size_t i = 0; i < nlanes; ++i) {
          dst[i] = (2 + (i * 2));
        }
        index_type index(KOKKOS_LAMBDA(std::size_t i) { return i; });
        simd_type expected_result(KOKKOS_LAMBDA(std::size_t i) {
          return (mask[i]) ? src[index[i]] : dst[i];
        });
        where(mask, src).scatter_to(dst, index);

        simd_type dst_simd;
        dst_simd.copy_from(dst, Kokkos::Experimental::simd_flag_default);

        host_check_equality(expected_result, dst_simd, nlanes);
      } else {
        mask_type mask([=](std::size_t i) { return i != idx; });

        DataType dst[nlanes] = {0};
        for (std::size_t i = 0; i < nlanes; ++i) {
          dst[i] = (2 + (i * 2));
        }
        index_type index([=](std::size_t i) { return i; });
        simd_type expected_result(
            [=](std::size_t i) { return (mask[i]) ? src[index[i]] : dst[i]; });
        where(mask, src).scatter_to(dst, index);

        simd_type dst_simd;
        dst_simd.copy_from(dst, Kokkos::Experimental::simd_flag_default);

        host_check_equality(expected_result, dst_simd, nlanes);
      }
    }
  }
}

template <typename Abi, typename DataType>
inline void host_check_where_expr_gather_from() {
  if constexpr (is_simd_avail_v<DataType, Abi>) {
    using simd_type  = Kokkos::Experimental::basic_simd<DataType, Abi>;
    using index_type = Kokkos::Experimental::basic_simd<std::int32_t, Abi>;
    using mask_type  = typename simd_type::mask_type;

    std::size_t nlanes = simd_type::size();
    DataType src[]     = {11, 13, 17, 19, 23, 29, 31, 37,
                          53, 71, 79, 83, 89, 93, 97, 103};

    for (std::size_t idx = 0; idx < nlanes; ++idx) {
      if constexpr (std::is_same_v<Abi,
                                   Kokkos::Experimental::simd_abi::scalar>) {
        mask_type mask(KOKKOS_LAMBDA(std::size_t i) { return i != idx; });

        simd_type dst(KOKKOS_LAMBDA(std::size_t i) { return (2 + (i * 2)); });
        index_type index(KOKKOS_LAMBDA(std::size_t i) { return i; });
        simd_type expected_result(KOKKOS_LAMBDA(std::size_t i) {
          return (mask[i]) ? src[index[i]] : dst[i];
        });
        where(mask, dst).gather_from(src, index);

        host_check_equality(expected_result, dst, nlanes);
      } else {
        mask_type mask([=](std::size_t i) { return i != idx; });

        simd_type dst([=](std::size_t i) { return (2 + (i * 2)); });
        index_type index([=](std::size_t i) { return i; });
        simd_type expected_result(
            [=](std::size_t i) { return (mask[i]) ? src[index[i]] : dst[i]; });
        where(mask, dst).gather_from(src, index);

        host_check_equality(expected_result, dst, nlanes);
      }
    }
  }
}

template <class Abi, typename DataType>
inline void host_check_where_expr() {
  host_check_where_expr_scatter_to<Abi, DataType>();
  host_check_where_expr_gather_from<Abi, DataType>();
}

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

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

template <typename Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_where_expr_scatter_to() {
  if constexpr (is_type_v<Kokkos::Experimental::basic_simd<DataType, Abi>>) {
    using simd_type  = Kokkos::Experimental::basic_simd<DataType, Abi>;
    using index_type = Kokkos::Experimental::basic_simd<std::int32_t, Abi>;
    using mask_type  = typename simd_type::mask_type;

    constexpr std::size_t nlanes = simd_type::size();
    DataType init[]              = {11, 13, 17, 19, 23, 29, 31, 37,
                                    53, 71, 79, 83, 89, 93, 97, 103};
    simd_type src;
    src.copy_from(init, Kokkos::Experimental::simd_flag_default);

    for (std::size_t idx = 0; idx < nlanes; ++idx) {
      mask_type mask(KOKKOS_LAMBDA(std::size_t i) { return i != idx; });

      DataType dst[nlanes] = {0};
      for (std::size_t i = 0; i < nlanes; ++i) {
        dst[i] = (2 + (i * 2));
      }
      index_type index([=](std::size_t i) { return i; });
      simd_type expected_result(KOKKOS_LAMBDA(std::size_t i) {
        return (mask[i]) ? src[index[i]] : dst[i];
      });
      where(mask, src).scatter_to(dst, index);

      simd_type dst_simd;
      dst_simd.copy_from(dst, Kokkos::Experimental::simd_flag_default);

      device_check_equality(expected_result, dst_simd, nlanes);
    }
  }
}

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

  std::size_t nlanes = simd_type::size();
  DataType src[]     = {11, 13, 17, 19, 23, 29, 31, 37,
                        53, 71, 79, 83, 89, 93, 97, 103};

  for (std::size_t idx = 0; idx < nlanes; ++idx) {
    mask_type mask(KOKKOS_LAMBDA(std::size_t i) { return i != idx; });

    simd_type dst([=](std::size_t i) { return (2 + (i * 2)); });
    index_type index([=](std::size_t i) { return i; });
    simd_type expected_result(KOKKOS_LAMBDA(std::size_t i) {
      return (mask[i]) ? src[index[i]] : dst[i];
    });
    where(mask, dst).gather_from(src, index);

    device_check_equality(expected_result, dst, nlanes);
  }
}

template <class Abi, typename DataType>
KOKKOS_INLINE_FUNCTION void device_check_where_expr() {
  device_check_where_expr_scatter_to<Abi, DataType>();
  device_check_where_expr_gather_from<Abi, DataType>();
}

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

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

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

TEST(simd, host_where_expressions) {
  host_check_where_expr_all_abis(Kokkos::Experimental::Impl::host_abi_set());
}

TEST(simd, device_where_expressions) {
  Kokkos::parallel_for(1, simd_device_where_expr_functor());
}

#endif