kokkos-utils 0.0.7
Loading...
Searching...
No Matches
test_Timer.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
23
24using execution_space = Kokkos::DefaultExecutionSpace;
25
27{
28
29using namespace Kokkos::utils::timer;
30
31template <typename T>
32struct TimerTest : public ::testing::Test,
33 public scoped::ExecutionSpace<execution_space>
34{};
35
36using TimerTypes = ::testing::Types<
39>;
40
42
47TYPED_TEST(TimerTest, invalid_if_not_started_and_not_stopped)
48{
49 const TypeParam timer;
50 ASSERT_FALSE(timer.is_valid());
51}
52
57TYPED_TEST(TimerTest, invalid_if_not_stopped)
58{
59 TypeParam timer;
60
61 if constexpr (std::same_as<TypeParam, void>) {
62 timer.start();
63 } else {
64 timer.start(this->exec);
65 }
66
67 ASSERT_FALSE(timer.is_valid());
68}
69
74TYPED_TEST(TimerTest, invalid_if_not_started)
75{
76 TypeParam timer;
77
78 if constexpr (std::same_as<TypeParam, void>) {
79 timer.stop();
80 } else {
81 timer.stop(this->exec);
82 }
83
84 ASSERT_FALSE(timer.is_valid());
85}
86
91TYPED_TEST(TimerTest, valid_if_started_and_stopped)
92{
93 TypeParam timer;
94
95 if constexpr (std::same_as<TypeParam, void>)
96 {
97 timer.start();
98 timer.stop();
99 }
100 else
101 {
102 timer.start(this->exec);
103 timer.stop(this->exec);
104 }
105
106 ASSERT_TRUE(timer.is_valid());
107}
108
109struct TimerExecutionSpaceTest : public TimerTest<execution_space> {
111};
112
117TEST_F(TimerExecutionSpaceTest, start_stop_elapsed)
118{
119 using view_t = Kokkos::View<double*, execution_space>;
120
121 Kokkos::Timer timer_external;
122 timer_t timer;
123
124 timer_external.reset();
125 timer.start(this->exec);
126
127 const view_t my_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, this->exec, "my view"), 10);
128 Kokkos::deep_copy(exec, my_view, 1.0);
129
130 timer.stop(this->exec);
131
132 const auto elapsed = timer.duration<seconds>().count();
133
139 const auto elapsed_external = timer_external.seconds();
140
141 ASSERT_LE(elapsed, elapsed_external);
142 ASSERT_GE(elapsed, 0.);
143 ASSERT_GE(elapsed_external, 0.);
144}
145
148{
149 TypeParam timer;
150
151 if constexpr (std::same_as<TypeParam, void>) {
152 timer.start();
153 } else {
154 timer.start(this->exec);
155 }
156
157 std::this_thread::sleep_for(std::chrono::milliseconds(100));
158
159 if constexpr (std::same_as<TypeParam, void>) {
160 timer.stop();
161 } else {
162 timer.stop(this->exec);
163 }
164
165 const double elapsed = timer.template duration<milliseconds>().count();
166
168 ASSERT_GE(elapsed, 100.);
169
171 const auto duration_ms = timer.template duration<milliseconds>();
172 ASSERT_NEAR(duration_ms.count(), elapsed, /* abs_error */ 1e-3);
173
175 const auto duration_us = timer.template duration<microseconds>();
176 ASSERT_NEAR(duration_us.count(), elapsed * 1e3, /* abs_error */ 1.);
177
179 const auto duration_se = timer.template duration<seconds>();
180 ASSERT_NEAR(duration_se.count(), elapsed / 1e3, /* abs_error */ 1e-6);
181
183 ASSERT_LE(abs(duration_ms - duration_us), std::chrono::microseconds(1));
184 ASSERT_LE(abs(duration_ms - duration_se), std::chrono::microseconds(1));
185
187 ASSERT_EQ(duration_ms, (timer.template duration<milliseconds>()));
188}
189
209{
210 constexpr size_t nreps = 10;
211
212 constexpr std::chrono::milliseconds wait(14);
213
214 timer_t timer;
215
216 for(size_t irep = 0; irep < nreps; ++irep)
217 {
218 const Kokkos::Timer timer_external;
219
220 timer.start(this->exec);
221
222 this->exec.fence("ensure that the event 'started'");
223
224 std::this_thread::sleep_for(wait);
225
226 timer.stop(this->exec);
227
228 const auto elapsed = timer.duration<milliseconds>();
229
230 const seconds elapsed_external(timer_external.seconds());
231
235 ASSERT_GE(elapsed, wait);
236 ASSERT_LE(elapsed, elapsed_external);
237 }
238}
239
240} // namespace Kokkos::utils::tests::timer
Measure elapsed time between events.
Definition Timer.hpp:18
TYPED_TEST_SUITE(EventTest, EventTypes)
TEST_F(TimerExecutionSpaceTest, start_stop_elapsed)
::testing::Types< Timer< void >, Timer< execution_space > > TimerTypes
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