kokkos-utils 0.0.7
Loading...
Searching...
No Matches
test_View.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
3#include "Kokkos_Macros.hpp"
4#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
5import kokkos.core;
6#else
7#include "Kokkos_Core.hpp"
8#endif
9
12
13using execution_space = Kokkos::DefaultExecutionSpace;
14
26
28{
29
30using namespace Kokkos::utils::printers;
31
32struct PrintersTest : public ::testing::Test,
33 public scoped::ExecutionSpace<execution_space>
34{};
35
37TEST_F(PrintersTest, Kokkos_View_0D)
38{
39 const Kokkos::View<double, execution_space> my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my nice 0D view"));
40 Kokkos::deep_copy(this->exec, my_view, 5.52);
41 this->exec.fence();
42
43 std::ostringstream oss;
44 oss << my_view;
45 EXPECT_EQ(oss.str(), "[5.52]");
46}
47
48template <concepts::ViewOfRank<1> ViewType>
50{
51 ViewType view;
52
53 template <std::integral T>
54 KOKKOS_FUNCTION void operator()(const T) const
55 {
56 view(0) = 1.5;
57 view(1) = 96.;
58 view(2) = 8.6;
59 view(3) = 456.;
60 view(4) = 456.15;
61 }
62};
63
65TEST_F(PrintersTest, Kokkos_View_1D)
66{
67 const Kokkos::View<double*, execution_space> my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my nice 1D view"), 5);
68 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>(this->exec, 0, 1), DeepAssign1D{my_view});
69 this->exec.fence();
70
71 std::ostringstream oss;
72 oss << my_view;
73 EXPECT_EQ(oss.str(), "[1.5, 96, 8.6, 456, 456.15]");
74}
75
76template <concepts::ViewOfRank<1> ViewType>
78{
79 ViewType view;
80
81 template <std::integral T>
82 KOKKOS_FUNCTION void operator()(const T) const
83 {
84 view(0) = 1.5;
85 view(1) = 96.;
86 view(2) = 8.6;
87 view(3) = 456.;
88 view(4) = 456.15e15;
89 }
90};
91
93TEST_F(PrintersTest, Kokkos_View_1D_formatting)
94{
95 const Kokkos::View<double*, execution_space> my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my nice 1D view"), 5);
96 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>(this->exec, 0, 1), DeepAssign1DFormatting{my_view});
97 this->exec.fence();
98
99 std::ostringstream oss;
100 oss << std::scientific;
101 oss.precision(12);
102 oss << my_view;
103 EXPECT_EQ(oss.str(), "[1.500000000000e+00, 9.600000000000e+01, 8.600000000000e+00, 4.560000000000e+02, 4.561500000000e+17]");
104}
105
106template <concepts::ViewOfRank<2> ViewType>
108{
109 ViewType view;
110
111 template <std::integral T>
112 KOKKOS_FUNCTION void operator()(const T) const
113 {
114 view(0, 0) = 1.;
115 view(0, 1) = 2.;
116 view(1, 0) = 3.;
117 view(1, 1) = 4.;
118 }
119};
120
122TEST_F(PrintersTest, Kokkos_View_2D)
123{
124 const Kokkos::View<double**, execution_space> my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my nice 2D view"), 2, 2);
125 Kokkos::parallel_for(Kokkos::RangePolicy<execution_space>(this->exec, 0, 1), DeepAssign2D{my_view});
126 this->exec.fence();
127
128 std::ostringstream oss;
129 oss << my_view;
130 EXPECT_EQ(oss.str(),"[[1, 2], [3, 4]]");
131}
132
133} // namespace Kokkos::utils::tests::printers
TEST_F(PrintersTest, Kokkos_View_0D)
Definition test_View.cpp:37
KOKKOS_FUNCTION void operator()(const T) const
Definition test_View.cpp:82
KOKKOS_FUNCTION void operator()(const T) const
Definition test_View.cpp:54
KOKKOS_FUNCTION void operator()(const T) const
Create a new execution space instance with RAII semantics.
Kokkos::DefaultExecutionSpace execution_space