File: TestViewOfViews.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 (129 lines) | stat: -rw-r--r-- 3,790 bytes parent folder | download | duplicates (2)
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
//@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>

namespace {

// User-defined types with a View data member
template <class V>
class S {
  V v_;

 public:
  template <class... Extents>
  S(std::string label, Extents... extents) : v_(std::move(label), extents...) {}
  KOKKOS_DEFAULTED_FUNCTION S() = default;
};

template <class V>
class N {  // not default constructible
  V v_;

 public:
  template <class... Extents>
  N(std::string label, Extents... extents) : v_(std::move(label), extents...) {}
};

template <class V>
class H {  // constructible and destructible only from on the host side
  V v_;

 public:
  template <class... Extents>
  H(std::string label, Extents... extents) : v_(std::move(label), extents...) {}
  H() {}
  ~H() {}
};

template <class V>
void test_view_of_views_default() {
  // assigning a default-constructed view to destruct the inner objects
  using VoV = Kokkos::View<V**, Kokkos::HostSpace>;
  VoV vov("vov", 2, 3);
  V a("a");
  V b("b");
  vov(0, 0) = a;
  vov(1, 0) = a;
  vov(0, 1) = b;
#ifndef KOKKOS_ENABLE_IMPL_VIEW_OF_VIEWS_DESTRUCTOR_PRECONDITION_VIOLATION_WORKAROUND
  vov(0, 0) = V();
  vov(1, 0) = V();
  vov(0, 1) = V();
#endif
}

template <class V>
void test_view_of_views_without_initializing() {
  // using placement new to construct the inner objects and explicitly
  // calling the destructor
  using VoV = Kokkos::View<V**, Kokkos::HostSpace>;
  VoV vov(Kokkos::view_alloc("vov", Kokkos::WithoutInitializing), 2, 3);
  V a("a");
  V b("b");
  new (&vov(0, 0)) V(a);
  new (&vov(1, 0)) V(a);
  new (&vov(0, 1)) V(b);
#ifndef KOKKOS_ENABLE_IMPL_VIEW_OF_VIEWS_DESTRUCTOR_PRECONDITION_VIOLATION_WORKAROUND
  vov(0, 0).~V();
  vov(1, 0).~V();
  vov(0, 1).~V();
#else
  // leaks memory
#endif
}

template <class V>
void test_view_of_views_sequential_host_init() {
  // inner views value-initialized sequentially on the host, and also
  // sequentially destructed on the host, without the need to cleanup
  using VoV = Kokkos::View<V**, Kokkos::HostSpace>;
  VoV vov(Kokkos::view_alloc("vov", Kokkos::SequentialHostInit), 2, 3);
  V a("a");
  V b("b");
  vov(0, 0) = a;
  vov(1, 0) = a;
  vov(0, 1) = b;
}

TEST(TEST_CATEGORY, view_of_views_default) {
  test_view_of_views_default<Kokkos::View<int, TEST_EXECSPACE>>();
  test_view_of_views_default<Kokkos::View<int[4], TEST_EXECSPACE>>();
  // User-defined type with View data member
  test_view_of_views_default<S<Kokkos::View<float, TEST_EXECSPACE>>>();
}

TEST(TEST_CATEGORY, view_of_views_without_initializing) {
  test_view_of_views_without_initializing<Kokkos::View<int, TEST_EXECSPACE>>();
  test_view_of_views_without_initializing<
      S<Kokkos::View<float, TEST_EXECSPACE>>>();
  test_view_of_views_without_initializing<
      N<Kokkos::View<double, TEST_EXECSPACE>>>();
  test_view_of_views_without_initializing<
      H<Kokkos::View<int, TEST_EXECSPACE>>>();
}

TEST(TEST_CATEGORY, test_view_of_views_sequential_host_init) {
  test_view_of_views_sequential_host_init<Kokkos::View<int, TEST_EXECSPACE>>();
  test_view_of_views_sequential_host_init<
      S<Kokkos::View<float, TEST_EXECSPACE>>>();
  test_view_of_views_sequential_host_init<
      H<Kokkos::View<int, TEST_EXECSPACE>>>();
}

}  // namespace