File: TestStdAlgorithmsConstraints.cpp

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 (196 lines) | stat: -rw-r--r-- 8,379 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
//@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 <Kokkos_Macros.hpp>
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
import kokkos.std_algorithms;
#include <std_algorithms/impl/Kokkos_Constraints.hpp>
#else
#include <Kokkos_StdAlgorithms.hpp>
#endif

namespace Test {
namespace stdalgos {

TEST(std_algorithms, is_admissible_to_std_algorithms) {
  namespace KE     = Kokkos::Experimental;
  using value_type = double;

  static constexpr size_t extent0 = 13;
  static constexpr size_t extent1 = 18;
  static constexpr size_t extent2 = 18;

  //-------------
  // 1d views
  //-------------
  using static_view_1d_t = Kokkos::View<value_type[extent0]>;
  static_view_1d_t static_view_1d{"std-algo-test-1d-contiguous-view-static"};

  using dyn_view_1d_t = Kokkos::View<value_type*>;
  dyn_view_1d_t dynamic_view_1d{"std-algo-test-1d-contiguous-view-dynamic",
                                extent0};

  using strided_view_1d_t = Kokkos::View<value_type*, Kokkos::LayoutStride>;
  Kokkos::LayoutStride layout1d{extent0, 2};
  strided_view_1d_t strided_view_1d{"std-algo-test-1d-strided-view", layout1d};
  ASSERT_EQ(layout1d.dimension[0], 13u);
  ASSERT_EQ(layout1d.stride[0], 2u);
  // they are admissible
  KE::Impl::static_assert_is_admissible_to_kokkos_std_algorithms(
      static_view_1d);
  KE::Impl::static_assert_is_admissible_to_kokkos_std_algorithms(
      dynamic_view_1d);
  KE::Impl::static_assert_is_admissible_to_kokkos_std_algorithms(
      strided_view_1d);

  //-------------
  // 2d views
  //-------------
  using static_view_2d_t  = Kokkos::View<value_type[extent0][extent1]>;
  using dyn_view_2d_t     = Kokkos::View<value_type**>;
  using strided_view_2d_t = Kokkos::View<value_type**, Kokkos::LayoutStride>;
  // non admissible
  EXPECT_FALSE(KE::Impl::is_admissible_to_kokkos_std_algorithms<
               static_view_2d_t>::value);
  EXPECT_FALSE(
      KE::Impl::is_admissible_to_kokkos_std_algorithms<dyn_view_2d_t>::value);
  EXPECT_FALSE(KE::Impl::is_admissible_to_kokkos_std_algorithms<
               strided_view_2d_t>::value);

  //-------------
  // 3d views
  //-------------
  using static_view_3d_t  = Kokkos::View<value_type[extent0][extent1][extent2]>;
  using dyn_view_3d_t     = Kokkos::View<value_type***>;
  using strided_view_3d_t = Kokkos::View<value_type***, Kokkos::LayoutStride>;
  // non admissible
  EXPECT_FALSE(KE::Impl::is_admissible_to_kokkos_std_algorithms<
               static_view_3d_t>::value);
  EXPECT_FALSE(
      KE::Impl::is_admissible_to_kokkos_std_algorithms<dyn_view_3d_t>::value);
  EXPECT_FALSE(KE::Impl::is_admissible_to_kokkos_std_algorithms<
               strided_view_3d_t>::value);
}

TEST(std_algorithms_DeathTest, expect_no_overlap) {
  namespace KE     = Kokkos::Experimental;
  using value_type = double;

  static constexpr size_t extent0 = 13;

  //-------------
  // 1d views
  //-------------
  using static_view_1d_t = Kokkos::View<value_type[extent0]>;
  [[maybe_unused]] static_view_1d_t static_view_1d{
      "std-algo-test-1d-contiguous-view-static"};

  using dyn_view_1d_t = Kokkos::View<value_type*>;
  [[maybe_unused]] dyn_view_1d_t dynamic_view_1d{
      "std-algo-test-1d-contiguous-view-dynamic", extent0};

  using strided_view_1d_t = Kokkos::View<value_type*, Kokkos::LayoutStride>;
  Kokkos::LayoutStride layout1d{extent0, 2};
  strided_view_1d_t strided_view_1d{"std-algo-test-1d-strided-view", layout1d};

// Overlapping because iterators are identical
#if defined(KOKKOS_ENABLE_DEBUG)
  ::testing::FLAGS_gtest_death_test_style = "threadsafe";

  auto first_s = KE::begin(static_view_1d);
  auto last_s  = first_s + extent0;
  EXPECT_DEATH({ KE::Impl::expect_no_overlap(first_s, last_s, first_s); },
               "Kokkos contract violation:.*");

  auto first_d = KE::begin(dynamic_view_1d);
  auto last_d  = first_d + extent0;
  EXPECT_DEATH({ KE::Impl::expect_no_overlap(first_d, last_d, first_d); },
               "Kokkos contract violation:.*");

  auto first_st = KE::begin(strided_view_1d);
  auto last_st  = first_st + extent0;
  EXPECT_DEATH({ KE::Impl::expect_no_overlap(first_st, last_st, first_st); },
               "Kokkos contract violation:.*");
#endif

  // Ranges are overlapped
  static constexpr size_t sub_extent0 = 6, offset0 = 3;
  std::pair<size_t, size_t> range0(0, sub_extent0),
      range1(offset0, offset0 + sub_extent0);
#if defined(KOKKOS_ENABLE_DEBUG)
  auto static_view_1d_0 = Kokkos::subview(static_view_1d, range0);
  auto static_view_1d_1 = Kokkos::subview(static_view_1d, range1);
  auto first_s0         = KE::begin(static_view_1d_0);  // [0, 6)
  auto last_s0          = first_s0 + static_view_1d_0.extent(0);
  auto first_s1         = KE::begin(static_view_1d_1);  // [3, 9)
  EXPECT_DEATH({ KE::Impl::expect_no_overlap(first_s0, last_s0, first_s1); },
               "Kokkos contract violation:.*");

  auto dynamic_view_1d_0 = Kokkos::subview(dynamic_view_1d, range0);
  auto dynamic_view_1d_1 = Kokkos::subview(dynamic_view_1d, range1);
  auto first_d0          = KE::begin(dynamic_view_1d_0);  // [0, 6)
  auto last_d0           = first_d0 + dynamic_view_1d_0.extent(0);
  auto first_d1          = KE::begin(dynamic_view_1d_1);  // [3, 9)
  EXPECT_DEATH({ KE::Impl::expect_no_overlap(first_d0, last_d0, first_d1); },
               "Kokkos contract violation:.*");
#endif

  auto strided_view_1d_0 = Kokkos::subview(strided_view_1d, range0);
  auto strided_view_1d_1 = Kokkos::subview(strided_view_1d, range1);
  auto first_st0         = KE::begin(strided_view_1d_0);  // [0, 12)
  auto last_st0          = first_st0 + strided_view_1d_0.extent(0);
  auto first_st1         = KE::begin(strided_view_1d_1);  // [3, 15)
  // Does not overlap since offset (=3) is not divisible by stride (=2)
  KE::Impl::expect_no_overlap(first_st0, last_st0, first_st1);

  // Iterating over the same range without overlapping
  Kokkos::View<value_type[2][extent0], Kokkos::LayoutLeft> static_view_2d{
      "std-algo-test-2d-contiguous-view-static"};
  auto sub_static_view_1d_0 = Kokkos::subview(static_view_2d, 0, Kokkos::ALL);
  auto sub_static_view_1d_1 = Kokkos::subview(static_view_2d, 1, Kokkos::ALL);
  auto sub_first_s0         = KE::begin(sub_static_view_1d_0);  // 0, 2, 4, ...
  auto sub_last_s0          = sub_first_s0 + sub_static_view_1d_0.extent(0);
  auto sub_first_s1         = KE::begin(sub_static_view_1d_1);  // 1, 3, 5, ...

  KE::Impl::expect_no_overlap(sub_first_s0, sub_last_s0, sub_first_s1);

  Kokkos::View<value_type**, Kokkos::LayoutLeft> dynamic_view_2d{
      "std-algo-test-2d-contiguous-view-dynamic", 2, extent0};
  auto sub_dynamic_view_1d_0 = Kokkos::subview(dynamic_view_2d, 0, Kokkos::ALL);
  auto sub_dynamic_view_1d_1 = Kokkos::subview(dynamic_view_2d, 1, Kokkos::ALL);
  auto sub_first_d0 = KE::begin(sub_dynamic_view_1d_0);  // 0, 2, 4, ...
  auto sub_last_d0  = sub_first_d0 + sub_dynamic_view_1d_0.extent(0);
  auto sub_first_d1 = KE::begin(sub_dynamic_view_1d_1);  // 1, 3, 5, ...

  KE::Impl::expect_no_overlap(sub_first_d0, sub_last_d0, sub_first_d1);

  // NOLINTNEXTLINE(bugprone-implicit-widening-of-multiplication-result)
  Kokkos::LayoutStride layout2d{2, 3, extent0, 2 * 3};
  Kokkos::View<value_type**, Kokkos::LayoutStride> strided_view_2d{
      "std-algo-test-2d-contiguous-view-strided", layout2d};
  auto sub_strided_view_1d_0 = Kokkos::subview(strided_view_2d, 0, Kokkos::ALL);
  auto sub_strided_view_1d_1 = Kokkos::subview(strided_view_2d, 1, Kokkos::ALL);
  auto sub_first_st0 = KE::begin(sub_strided_view_1d_0);  // 0, 6, 12, ...
  auto sub_last_st0  = sub_first_st0 + sub_strided_view_1d_0.extent(0);
  auto sub_first_st1 = KE::begin(sub_strided_view_1d_1);  // 1, 7, 13, ...

  KE::Impl::expect_no_overlap(sub_first_st0, sub_last_st0, sub_first_st1);
}

}  // namespace stdalgos
}  // namespace Test