File: TestIrregularLayout.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 (236 lines) | stat: -rw-r--r-- 6,177 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
//@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

#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
#include <sstream>
#include <iostream>
#define OFFSET_LIST_MAX_SIZE 100

namespace Kokkos {

struct LayoutSelective {
  //! Tag this class as a kokkos array layout
  using array_layout = LayoutSelective;

  size_t offset_list[OFFSET_LIST_MAX_SIZE];
  size_t list_size;

  enum : bool { is_extent_constructible = false };

  KOKKOS_INLINE_FUNCTION
  LayoutSelective() {
    for (int i = 0; i < OFFSET_LIST_MAX_SIZE; i++) {
      offset_list[i] = i;
    }
  }

  KOKKOS_INLINE_FUNCTION
  void assign(const size_t ol_[], const size_t size_) {
    list_size = size_;
    for (int i = 0; i < (int)list_size; i++) {
      offset_list[i] = ol_[i];
    }
  }

  KOKKOS_INLINE_FUNCTION
  LayoutSelective(LayoutSelective const& rhs) {
    assign(rhs.offset_list, rhs.list_size);
  }

  KOKKOS_INLINE_FUNCTION
  LayoutSelective(LayoutSelective&& rhs) {
    assign(rhs.offset_list, rhs.list_size);
  }
  KOKKOS_INLINE_FUNCTION
  LayoutSelective& operator=(LayoutSelective const& rhs) {
    if (&rhs == this) return *this;
    assign(rhs.offset_list, rhs.list_size);
    return *this;
  }
  KOKKOS_INLINE_FUNCTION
  LayoutSelective& operator=(LayoutSelective&& rhs) {
    assign(rhs.offset_list, rhs.list_size);
    return *this;
  }

  KOKKOS_INLINE_FUNCTION
  explicit LayoutSelective(const size_t ol_[], const size_t size_) {
    assign(ol_, size_);
  }

  KOKKOS_INLINE_FUNCTION
  size_t offset(size_t ndx) const {
    KOKKOS_ASSERT(ndx < list_size);
    return offset_list[ndx];
  }
};

namespace Impl {
template <class Dimension>
struct ViewOffset<Dimension, Kokkos::LayoutSelective, void> {
 public:
  using is_mapping_plugin = std::true_type;
  using is_regular        = std::false_type;

  using size_type      = size_t;
  using dimension_type = Dimension;
  using array_layout   = Kokkos::LayoutSelective;

  //----------------------------------------
  dimension_type m_dim;
  array_layout m_selective;

  // rank 1
  template <typename I0>
  KOKKOS_INLINE_FUNCTION size_type operator()(I0 const& i0) const {
    return m_selective.offset(i0);
  }

  // This ViewOffset and the underlying layout only supports rank 1 Views

  //----------------------------------------

  KOKKOS_INLINE_FUNCTION
  array_layout layout() const { return array_layout(); }

  KOKKOS_INLINE_FUNCTION constexpr size_type dimension_0() const {
    return m_dim.N0;
  }

  /* Cardinality of the domain index space */
  KOKKOS_INLINE_FUNCTION
  constexpr size_type size() const { return m_dim.N0; }

 public:
  /* Span of the range space, largest stride * dimension */
  KOKKOS_INLINE_FUNCTION
  constexpr size_type span() const { return m_dim.N0; }

  KOKKOS_INLINE_FUNCTION constexpr bool span_is_contiguous() const {
    return false;
  }

  /* Strides of dimensions */
  KOKKOS_INLINE_FUNCTION constexpr size_type stride_0() const { return 1; }

  // Stride with [ rank ] value is the total length
  template <typename iType>
  KOKKOS_INLINE_FUNCTION void stride(iType* const s) const {
    if (0 < dimension_type::rank) {
      s[0] = 1;
    }
    for (int i = 1; i < 8; i++) s[i] = 0;
    s[dimension_type::rank] = span();
  }

  //----------------------------------------
  ViewOffset()                             = default;
  ViewOffset(const ViewOffset&)            = default;
  ViewOffset& operator=(const ViewOffset&) = default;

  KOKKOS_INLINE_FUNCTION
  ViewOffset(std::integral_constant<unsigned, 0> const&,
             Kokkos::LayoutSelective const& rhs)
      : m_dim(rhs.list_size, 0, 0, 0, 0, 0, 0, 0), m_selective(rhs) {}
};

}  // namespace Impl
}  // namespace Kokkos

namespace Test {

class InnerClass {
 public:
  long data[100];

  KOKKOS_INLINE_FUNCTION
  InnerClass() {
    for (int i = 0; i < 100; i++) {
      data[i] = (long)i;
    }
  }

  KOKKOS_INLINE_FUNCTION
  void update(long d) {
    for (int i = 0; i < 100; i++) {
      data[i] += d;
    }
  }

  KOKKOS_INLINE_FUNCTION
  void set(long d) {
    for (int i = 0; i < 100; i++) {
      data[i] = d;
    }
  }
};

template <class ExecutionSpace>
struct TestLayout {
  const int N       = 100;
  size_t offsets[2] = {20, 40};
  using Layout      = Kokkos::LayoutRight;
  using SubLayout   = Kokkos::LayoutSelective;

  // Allocate y, x vectors and Matrix A on device.
  using ViewVectorType =
      Kokkos::View<InnerClass*, Layout, typename ExecutionSpace::memory_space>;
  using SubViewVectorType = Kokkos::View<InnerClass*, SubLayout,
                                         typename ExecutionSpace::memory_space,
                                         Kokkos::MemoryUnmanaged>;
  struct InitTag {};
  struct UpdateTag {};

  ViewVectorType a;
  SubLayout sl;
  SubViewVectorType b;
  TestLayout() : a("a", N), sl(offsets, 2), b(a.data(), sl) {}

  void run_test() {
    Kokkos::parallel_for(Kokkos::RangePolicy<ExecutionSpace, InitTag>(0, N),
                         *this);

    Kokkos::parallel_for(Kokkos::RangePolicy<ExecutionSpace, UpdateTag>(0, 2),
                         *this);

    validate_results();
  }

  // set all values
  KOKKOS_INLINE_FUNCTION
  void operator()(const InitTag&, const int i) const { a(i).update(i); }

  // update selective values
  KOKKOS_INLINE_FUNCTION
  void operator()(const UpdateTag&, const int i) const {
    b(i).set(200 * (i + 1));
  }

  void validate_results() {
    auto a_h = Kokkos::create_mirror_view(a);
    Kokkos::deep_copy(a_h, a);
    ASSERT_EQ(a_h(20).data[0], 200);
    ASSERT_EQ(a_h(40).data[0], 400);
  }
};

TEST(TEST_CATEGORY, view_irregular_layout) {
  TestLayout<TEST_EXECSPACE> tl;
  tl.run_test();
}

}  // namespace Test