kokkos-utils 0.0.1
 
Loading...
Searching...
No Matches
test_Timer.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2
3#include "Kokkos_Core.hpp"
4
6
17
18using execution_space = Kokkos::DefaultExecutionSpace;
19
21{
22
23using namespace Kokkos::utils::timer;
24
26
27class TimerTest : public ::testing::Test
28{
29public:
30 void SetUp() override {
31 this->exec = Kokkos::Experimental::partition_space(execution_space{}, 1)[0];
32 }
33
34protected:
36};
37
42TEST_F(TimerTest, invalid_if_not_reset_and_not_stopped)
43{
44 const timer_t timer;
45 ASSERT_FALSE(timer.is_valid());
46}
47
52TEST_F(TimerTest, invalid_if_not_stopped)
53{
55 timer.reset(this->exec);
56 ASSERT_FALSE(timer.is_valid());
57}
58
63TEST_F(TimerTest, invalid_if_not_reset)
64{
66 timer.stop(this->exec);
67 ASSERT_FALSE(timer.is_valid());
68}
69
74TEST_F(TimerTest, valid_if_reset_and_stopped)
75{
77 timer.reset(this->exec);
78 timer.stop (this->exec);
79 ASSERT_TRUE(timer.is_valid());
80}
81
86TEST_F(TimerTest, start_stop_elapsed)
87{
88 using view_t = Kokkos::View<double*, execution_space>;
89
90 Kokkos::Timer timer_external;
92
93 timer_external.reset();
94 timer.reset(this->exec);
95
96 const view_t my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my view"), 10);
97 Kokkos::deep_copy(exec, my_view, 1.0);
98
99 timer.stop(this->exec);
100
101 const auto elapsed = timer.duration<seconds>().count();
102
108 const auto elapsed_external = timer_external.seconds();
109
110 ASSERT_LE(elapsed, elapsed_external);
111 ASSERT_GE(elapsed, 0.);
112 ASSERT_GE(elapsed_external, 0.);
113}
114
117{
119
120 timer.reset(this->exec);
121
122 std::this_thread::sleep_for(std::chrono::milliseconds(100));
123
124 timer.stop(this->exec);
125
126 const double elapsed = timer.duration<milliseconds>().count();
127
129 ASSERT_GE(elapsed, 100.);
130
132 const auto duration_ms = timer.duration<milliseconds>();
133 ASSERT_NEAR(duration_ms.count(), elapsed, /* abs_error */ 1e-3);
134
136 const auto duration_us = timer.duration<microseconds>();
137 ASSERT_NEAR(duration_us.count(), elapsed * 1e3, /* abs_error */ 1.);
138
140 const auto duration_se = timer.duration<seconds>();
141 ASSERT_NEAR(duration_se.count(), elapsed / 1e3, /* abs_error */ 1e-6);
142
144 ASSERT_LE(abs(duration_ms - duration_us), std::chrono::microseconds(1));
145 ASSERT_LE(abs(duration_ms - duration_se), std::chrono::microseconds(1));
146
148 ASSERT_EQ(duration_ms, (timer.duration<milliseconds>()));
149}
150
170{
171 constexpr size_t nreps = 10;
172
173 constexpr std::chrono::milliseconds wait(14);
174
176
177 for(size_t irep = 0; irep < nreps; ++irep)
178 {
179 const Kokkos::Timer timer_external;
180
181 timer.reset(this->exec);
182
183 this->exec.fence("ensure that the event 'started'");
184
185 std::this_thread::sleep_for(wait);
186
187 timer.stop(this->exec);
188
189 const auto elapsed = timer.duration<milliseconds>();
190
191 const seconds elapsed_external(timer_external.seconds());
192
196 ASSERT_GE(elapsed, wait);
197 ASSERT_LE(elapsed, elapsed_external);
198 }
199}
200
201} // namespace Kokkos::utils::tests::timer
Measure elapsed time between events.
Definition Timer.hpp:17
Timer< execution_space > timer_t
TEST_F(TimerTest, invalid_if_not_reset_and_not_stopped)
std::chrono::duration< double, std::ratio< 1, 1 > > seconds
Similar to std::chrono::seconds, but using double instead of an integer type to represent the tick co...
Definition Duration.hpp:18
std::chrono::duration< double, std::micro > microseconds
Definition Duration.hpp:12
std::chrono::duration< double, std::milli > milliseconds
Similar to std::chrono::milliseconds, but using double instead of an integer type to represent the ti...
Definition Duration.hpp:15
Kokkos::DefaultExecutionSpace execution_space