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