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
7
18
19using execution_space = Kokkos::DefaultExecutionSpace;
20
22{
23
24using namespace Kokkos::utils::timer;
25
26template <typename T>
27struct TimerTest : public ::testing::Test,
28 public scoped::ExecutionSpace<execution_space>
29{};
30
31using TimerTypes = ::testing::Types<
34>;
35
37
42TYPED_TEST(TimerTest, invalid_if_not_started_and_not_stopped)
43{
44 const TypeParam timer;
45 ASSERT_FALSE(timer.is_valid());
46}
47
52TYPED_TEST(TimerTest, invalid_if_not_stopped)
53{
54 TypeParam timer;
55
56 if constexpr (std::same_as<TypeParam, void>) {
57 timer.start();
58 } else {
59 timer.start(this->exec);
60 }
61
62 ASSERT_FALSE(timer.is_valid());
63}
64
69TYPED_TEST(TimerTest, invalid_if_not_started)
70{
71 TypeParam timer;
72
73 if constexpr (std::same_as<TypeParam, void>) {
74 timer.stop();
75 } else {
76 timer.stop(this->exec);
77 }
78
79 ASSERT_FALSE(timer.is_valid());
80}
81
86TYPED_TEST(TimerTest, valid_if_started_and_stopped)
87{
88 TypeParam timer;
89
90 if constexpr (std::same_as<TypeParam, void>)
91 {
92 timer.start();
93 timer.stop();
94 }
95 else
96 {
97 timer.start(this->exec);
98 timer.stop(this->exec);
99 }
100
101 ASSERT_TRUE(timer.is_valid());
102}
103
104struct TimerExecutionSpaceTest : public TimerTest<execution_space> {
106};
107
112TEST_F(TimerExecutionSpaceTest, start_stop_elapsed)
113{
114 using view_t = Kokkos::View<double*, execution_space>;
115
116 Kokkos::Timer timer_external;
117 timer_t timer;
118
119 timer_external.reset();
120 timer.start(this->exec);
121
122 const view_t my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my view"), 10);
123 Kokkos::deep_copy(exec, my_view, 1.0);
124
125 timer.stop(this->exec);
126
127 const auto elapsed = timer.duration<seconds>().count();
128
134 const auto elapsed_external = timer_external.seconds();
135
136 ASSERT_LE(elapsed, elapsed_external);
137 ASSERT_GE(elapsed, 0.);
138 ASSERT_GE(elapsed_external, 0.);
139}
140
143{
144 TypeParam timer;
145
146 if constexpr (std::same_as<TypeParam, void>) {
147 timer.start();
148 } else {
149 timer.start(this->exec);
150 }
151
152 std::this_thread::sleep_for(std::chrono::milliseconds(100));
153
154 if constexpr (std::same_as<TypeParam, void>) {
155 timer.stop();
156 } else {
157 timer.stop(this->exec);
158 }
159
160 const double elapsed = timer.template duration<milliseconds>().count();
161
163 ASSERT_GE(elapsed, 100.);
164
166 const auto duration_ms = timer.template duration<milliseconds>();
167 ASSERT_NEAR(duration_ms.count(), elapsed, /* abs_error */ 1e-3);
168
170 const auto duration_us = timer.template duration<microseconds>();
171 ASSERT_NEAR(duration_us.count(), elapsed * 1e3, /* abs_error */ 1.);
172
174 const auto duration_se = timer.template duration<seconds>();
175 ASSERT_NEAR(duration_se.count(), elapsed / 1e3, /* abs_error */ 1e-6);
176
178 ASSERT_LE(abs(duration_ms - duration_us), std::chrono::microseconds(1));
179 ASSERT_LE(abs(duration_ms - duration_se), std::chrono::microseconds(1));
180
182 ASSERT_EQ(duration_ms, (timer.template duration<milliseconds>()));
183}
184
204{
205 constexpr size_t nreps = 10;
206
207 constexpr std::chrono::milliseconds wait(14);
208
209 timer_t timer;
210
211 for(size_t irep = 0; irep < nreps; ++irep)
212 {
213 const Kokkos::Timer timer_external;
214
215 timer.start(this->exec);
216
217 this->exec.fence("ensure that the event 'started'");
218
219 std::this_thread::sleep_for(wait);
220
221 timer.stop(this->exec);
222
223 const auto elapsed = timer.duration<milliseconds>();
224
225 const seconds elapsed_external(timer_external.seconds());
226
230 ASSERT_GE(elapsed, wait);
231 ASSERT_LE(elapsed, elapsed_external);
232 }
233}
234
235} // namespace Kokkos::utils::tests::timer
Measure elapsed time between events.
Definition Timer.hpp:18
TYPED_TEST_SUITE(EventTest, EventTypes)
::testing::Types< Timer< void >, Timer< execution_space > > TimerTypes
TEST_F(TimerExecutionSpaceTest, start_stop_elapsed)
TYPED_TEST(EventTest, impl_event_type)
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:21
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:18
Create a new execution space instance with RAII semantics.
Kokkos::DefaultExecutionSpace execution_space