File: TestViewMapping_b.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 (225 lines) | stat: -rw-r--r-- 6,781 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
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright Contributors to the Kokkos project

#include <gtest/gtest.h>

#include <sstream>
#include <iostream>

#include <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.core;
import kokkos.core_impl;
#else
#include <Kokkos_Core.hpp>
#endif

namespace Test {

/*--------------------------------------------------------------------------*/

template <class Space>
struct TestViewMappingAtomic {
  using ExecSpace = typename Space::execution_space;
  using MemSpace  = typename Space::memory_space;

  using mem_trait = Kokkos::MemoryTraits<Kokkos::Atomic>;

  using T      = Kokkos::View<int *, ExecSpace>;
  using T_atom = Kokkos::View<int *, ExecSpace, mem_trait>;

  T x;
  T_atom x_atom;

  enum { N = 100000 };

  struct TagInit {};
  struct TagUpdate {};
  struct TagVerify {};

  KOKKOS_INLINE_FUNCTION
  void operator()(const TagInit &, const int i) const { x(i) = i; }

  KOKKOS_INLINE_FUNCTION
  void operator()(const TagUpdate &, const int i) const { x_atom(i % 2) += 1; }

  KOKKOS_INLINE_FUNCTION
  void operator()(const TagVerify &, const int i, long &error_count) const {
    if (i < 2) {
      if (x(i) != int(i + N / 2)) ++error_count;
    } else {
      if (x(i) != int(i)) ++error_count;
    }
  }

  TestViewMappingAtomic() : x("x", N), x_atom(x) {}

  void run() {
    ASSERT_TRUE(T::reference_type_is_lvalue_reference);
    ASSERT_FALSE(T_atom::reference_type_is_lvalue_reference);

    Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace, TagInit>(0, N), *this);
    Kokkos::parallel_for(Kokkos::RangePolicy<ExecSpace, TagUpdate>(0, N),
                         *this);

    long error_count = -1;

    Kokkos::parallel_reduce(Kokkos::RangePolicy<ExecSpace, TagVerify>(0, N),
                            *this, error_count);

    ASSERT_EQ(0, error_count);

    typename T_atom::host_mirror_type x_host = Kokkos::create_mirror_view(x);
    Kokkos::deep_copy(x_host, x);

    error_count = -1;

    Kokkos::parallel_reduce(
        Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace, TagVerify>(0, N),
        [=](const TagVerify &, const int i, long &tmp_error_count) {
          if (i < 2) {
            if (x_host(i) != int(i + N / 2)) ++tmp_error_count;
          } else {
            if (x_host(i) != int(i)) ++tmp_error_count;
          }
        },
        error_count);

    ASSERT_EQ(0, error_count);
    Kokkos::deep_copy(x, x_host);
  }
};

TEST(TEST_CATEGORY, view_mapping_atomic) {
  TestViewMappingAtomic<TEST_EXECSPACE> f;
  f.run();
}

}  // namespace Test

/*--------------------------------------------------------------------------*/

namespace Test {

struct MappingClassValueType {
  KOKKOS_INLINE_FUNCTION
  MappingClassValueType() {
#if 0
    KOKKOS_IF_ON_DEVICE(
        (printf("TestViewMappingClassValue construct on Device\n");))
    KOKKOS_IF_ON_HOST((printf("TestViewMappingClassValue construct on Host\n");))
#endif
  }
  KOKKOS_INLINE_FUNCTION
  ~MappingClassValueType() {
#if 0
    KOKKOS_IF_ON_DEVICE(
        (printf("TestViewMappingClassValue destruct on Device\n");))
    KOKKOS_IF_ON_HOST((printf("TestViewMappingClassValue destruct on Host\n");))
#endif
  }
};

template <class Space>
void test_view_mapping_class_value() {
  using ExecSpace = typename Space::execution_space;

  ExecSpace().fence();
  {
    Kokkos::View<MappingClassValueType, ExecSpace> a("a");
    ExecSpace().fence();
  }
  ExecSpace().fence();
}

TEST(TEST_CATEGORY, view_mapping_class_value) {
  test_view_mapping_class_value<TEST_EXECSPACE>();
}

}  // namespace Test

/*--------------------------------------------------------------------------*/

namespace Test {

TEST(TEST_CATEGORY, view_mapping_assignable) {
  using exec_space = TEST_EXECSPACE;

  {  // Assignment of rank-0 Left = Right
    using dst_traits = Kokkos::ViewTraits<int, Kokkos::LayoutLeft, exec_space>;
    using src_traits = Kokkos::ViewTraits<int, Kokkos::LayoutRight, exec_space>;
    using mapping    = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(mapping::is_assignable);

    Kokkos::View<int, Kokkos::LayoutRight, exec_space> src;
    Kokkos::View<int, Kokkos::LayoutLeft, exec_space> dst(src);
    dst = src;
  }

  {  // Assignment of rank-0 Right = Left
    using dst_traits = Kokkos::ViewTraits<int, Kokkos::LayoutRight, exec_space>;
    using src_traits = Kokkos::ViewTraits<int, Kokkos::LayoutLeft, exec_space>;
    using mapping    = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(mapping::is_assignable);

    Kokkos::View<int, Kokkos::LayoutLeft, exec_space> src;
    Kokkos::View<int, Kokkos::LayoutRight, exec_space> dst(src);
    dst = src;
  }

  {  // Assignment of rank-1 Left = Right
    using dst_traits =
        Kokkos::ViewTraits<int *, Kokkos::LayoutLeft, exec_space>;
    using src_traits =
        Kokkos::ViewTraits<int *, Kokkos::LayoutRight, exec_space>;
    using mapping = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(mapping::is_assignable);

    Kokkos::View<int *, Kokkos::LayoutRight, exec_space> src;
    Kokkos::View<int *, Kokkos::LayoutLeft, exec_space> dst(src);
    dst = src;
  }

  {  // Assignment of rank-1 Right = Left
    using dst_traits =
        Kokkos::ViewTraits<int *, Kokkos::LayoutRight, exec_space>;
    using src_traits =
        Kokkos::ViewTraits<int *, Kokkos::LayoutLeft, exec_space>;
    using mapping = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(mapping::is_assignable);

    Kokkos::View<int *, Kokkos::LayoutLeft, exec_space> src;
    Kokkos::View<int *, Kokkos::LayoutRight, exec_space> dst(src);
    dst = src;
  }

  {  // Assignment of rank-2 Left = Right
    using dst_traits =
        Kokkos::ViewTraits<int **, Kokkos::LayoutLeft, exec_space>;
    using src_traits =
        Kokkos::ViewTraits<int **, Kokkos::LayoutRight, exec_space>;
    using mapping = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(!mapping::is_assignable);
  }

  {  // Assignment of rank-2 Right = Left
    using dst_traits =
        Kokkos::ViewTraits<int **, Kokkos::LayoutRight, exec_space>;
    using src_traits =
        Kokkos::ViewTraits<int **, Kokkos::LayoutLeft, exec_space>;
    using mapping = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;
    static_assert(!mapping::is_assignable);
  }
}

TEST(TEST_CATEGORY, view_mapping_trivially_copyable) {
  using exec_space = TEST_EXECSPACE;

  using dst_traits = Kokkos::ViewTraits<int *, exec_space>;
  using src_traits = dst_traits;
  using mapping    = Kokkos::Impl::ViewMapping<dst_traits, src_traits, void>;

  static_assert(std::is_trivially_copyable<mapping>{});
}

}  // namespace Test