File: TestViewMapping_subview.hpp

package info (click to toggle)
kokkos 5.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,148 kB
  • sloc: cpp: 225,388; sh: 1,250; python: 78; makefile: 16; fortran: 4; ansic: 2
file content (178 lines) | stat: -rw-r--r-- 5,859 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
// 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;
#else
#include <Kokkos_Core.hpp>
#endif

namespace Test {

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

  using range = Kokkos::pair<int, int>;

  enum { AN = 10 };
  using AT  = Kokkos::View<int*, ExecSpace>;
  using ACT = Kokkos::View<const int*, ExecSpace>;
  using AS  = Kokkos::Subview<AT, range>;

  enum { BN0 = 10, BN1 = 11, BN2 = 12 };
  using BT = Kokkos::View<int***, ExecSpace>;
  using BS = Kokkos::Subview<BT, range, range, range>;

  enum { CN0 = 10, CN1 = 11, CN2 = 12 };
  using CT = Kokkos::View<int*** [13][14], ExecSpace>;
  // changing CS to CTS here because when compiling with nvshmem, there is a
  // define for CS that makes this fail...
  using CTS = Kokkos::Subview<CT, range, range, range, int, int>;

  enum { DN0 = 10, DN1 = 11, DN2 = 12, DN3 = 13, DN4 = 14 };
  using DT = Kokkos::View<int*** [DN3][DN4], ExecSpace>;
  using DS = Kokkos::Subview<DT, int, range, range, range, int>;

  using DLT  = Kokkos::View<int*** [13][14], Kokkos::LayoutLeft, ExecSpace>;
  using DLS1 = Kokkos::Subview<DLT, range, int, int, int, int>;

  static_assert(
      DLS1::rank == 1 &&
          std::is_same_v<typename DLS1::array_layout, Kokkos::LayoutLeft>,
      "Subview layout error for rank 1 subview of left-most range of "
      "LayoutLeft");

  using DRT  = Kokkos::View<int*** [13][14], Kokkos::LayoutRight, ExecSpace>;
  using DRS1 = Kokkos::Subview<DRT, int, int, int, int, range>;

  static_assert(
      DRS1::rank == 1 &&
          std::is_same_v<typename DRS1::array_layout, Kokkos::LayoutRight>,
      "Subview layout error for rank 1 subview of right-most range of "
      "LayoutRight");

  AT Aa;
  AS Ab;
  ACT Ac;
  BT Ba;
  BS Bb;
  CT Ca;
  CTS Cb;
  DT Da;
  DS Db;

  TestViewMappingSubview()
      : Aa("Aa", AN),
        Ab(Kokkos::subview(Aa, std::pair<int, int>(1, AN - 1))),
        Ac(Aa, std::pair<int, int>(1, AN - 1)),
        Ba("Ba", BN0, BN1, BN2),
        Bb(Kokkos::subview(Ba, std::pair<int, int>(1, BN0 - 1),
                           std::pair<int, int>(1, BN1 - 1),
                           std::pair<int, int>(1, BN2 - 1))),
        Ca("Ca", CN0, CN1, CN2),
        Cb(Kokkos::subview(Ca, std::pair<int, int>(1, CN0 - 1),
                           std::pair<int, int>(1, CN1 - 1),
                           std::pair<int, int>(1, CN2 - 1), 1, 2)),
        Da("Da", DN0, DN1, DN2),
        Db(Kokkos::subview(Da, 1, std::pair<int, int>(1, DN1 - 1),
                           std::pair<int, int>(1, DN2 - 1),
                           std::pair<int, int>(1, DN3 - 1), 2)) {}

  KOKKOS_INLINE_FUNCTION
  void operator()(const int, long& error_count) const {
    auto Ad = Kokkos::subview(Aa, Kokkos::pair<int, int>(1, AN - 1));

    for (int i = 1; i < AN - 1; ++i)
      if (&Aa[i] != &Ab[i - 1]) ++error_count;
    for (int i = 1; i < AN - 1; ++i)
      if (&Aa[i] != &Ac[i - 1]) ++error_count;
    for (int i = 1; i < AN - 1; ++i)
      if (&Aa[i] != &Ad[i - 1]) ++error_count;

    for (int i2 = 1; i2 < BN2 - 1; ++i2)
      for (int i1 = 1; i1 < BN1 - 1; ++i1)
        for (int i0 = 1; i0 < BN0 - 1; ++i0) {
          if (&Ba(i0, i1, i2) != &Bb(i0 - 1, i1 - 1, i2 - 1)) ++error_count;
        }

    for (int i2 = 1; i2 < CN2 - 1; ++i2)
      for (int i1 = 1; i1 < CN1 - 1; ++i1)
        for (int i0 = 1; i0 < CN0 - 1; ++i0) {
          if (&Ca(i0, i1, i2, 1, 2) != &Cb(i0 - 1, i1 - 1, i2 - 1))
            ++error_count;
        }

    for (int i2 = 1; i2 < DN3 - 1; ++i2)
      for (int i1 = 1; i1 < DN2 - 1; ++i1)
        for (int i0 = 1; i0 < DN1 - 1; ++i0) {
          if (&Da(1, i0, i1, i2, 2) != &Db(i0 - 1, i1 - 1, i2 - 1))
            ++error_count;
        }
  }

  void run() {
    TestViewMappingSubview<ExecSpace> self;

    ASSERT_EQ(Aa.extent(0), AN);
    ASSERT_EQ(Ab.extent(0), (size_t)AN - 2);
    ASSERT_EQ(Ac.extent(0), (size_t)AN - 2);
    ASSERT_EQ(Ba.extent(0), BN0);
    ASSERT_EQ(Ba.extent(1), BN1);
    ASSERT_EQ(Ba.extent(2), BN2);
    ASSERT_EQ(Bb.extent(0), (size_t)BN0 - 2);
    ASSERT_EQ(Bb.extent(1), (size_t)BN1 - 2);
    ASSERT_EQ(Bb.extent(2), (size_t)BN2 - 2);

    ASSERT_EQ(Ca.extent(0), CN0);
    ASSERT_EQ(Ca.extent(1), CN1);
    ASSERT_EQ(Ca.extent(2), CN2);
    ASSERT_EQ(Ca.extent(3), (size_t)13);
    ASSERT_EQ(Ca.extent(4), (size_t)14);
    ASSERT_EQ(Cb.extent(0), (size_t)CN0 - 2);
    ASSERT_EQ(Cb.extent(1), (size_t)CN1 - 2);
    ASSERT_EQ(Cb.extent(2), (size_t)CN2 - 2);

    ASSERT_EQ(Da.extent(0), DN0);
    ASSERT_EQ(Da.extent(1), DN1);
    ASSERT_EQ(Da.extent(2), DN2);
    ASSERT_EQ(Da.extent(3), DN3);
    ASSERT_EQ(Da.extent(4), DN4);

    ASSERT_EQ(Db.extent(0), (size_t)DN1 - 2);
    ASSERT_EQ(Db.extent(1), (size_t)DN2 - 2);
    ASSERT_EQ(Db.extent(2), (size_t)DN3 - 2);

    ASSERT_EQ(Da.stride(1), Db.stride(0));
    ASSERT_EQ(Da.stride(2), Db.stride(1));
    ASSERT_EQ(Da.stride(3), Db.stride(2));

#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
    KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH()
    ASSERT_EQ(Da.stride_1(), Db.stride_0());  // NOLINT
    ASSERT_EQ(Da.stride_2(), Db.stride_1());  // NOLINT
    ASSERT_EQ(Da.stride_3(), Db.stride_2());  // NOLINT
    KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_POP()
#endif

    long error_count = -1;
    Kokkos::parallel_reduce(Kokkos::RangePolicy<ExecSpace>(0, 1), *this,
                            error_count);

    ASSERT_EQ(error_count, 0);
  }
};

TEST(TEST_CATEGORY, view_mapping_subview) {
  TestViewMappingSubview<TEST_EXECSPACE> f;
  f.run();
}

}  // namespace Test