kokkos-utils
0.0.5
Toggle main menu visibility
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
5
#include "
kokkos-utils/tests/scoped/ExecutionSpace.hpp
"
6
#include "
kokkos-utils/timer/Timer.hpp
"
7
18
19
using
execution_space
= Kokkos::DefaultExecutionSpace;
20
21
namespace
Kokkos::utils::tests::timer
22
{
23
24
using namespace
Kokkos::utils::timer;
25
26
template
<
typename
T>
27
struct
TimerTest
:
public
::testing::Test,
28
public
scoped::ExecutionSpace
<execution_space>
29
{};
30
31
using
TimerTypes
= ::testing::Types<
32
Timer<void>
,
33
Timer<execution_space>
34
>;
35
36
TYPED_TEST_SUITE
(
TimerTest
,
TimerTypes
);
37
42
TYPED_TEST
(
TimerTest
, invalid_if_not_started_and_not_stopped)
43
{
44
const
TypeParam
timer
;
45
ASSERT_FALSE(
timer
.is_valid());
46
}
47
52
TYPED_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
69
TYPED_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
86
TYPED_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
104
struct
TimerExecutionSpaceTest
:
public
TimerTest
<execution_space> {
105
using
timer_t
=
Timer<execution_space>
;
106
};
107
112
TEST_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
142
TYPED_TEST
(
TimerTest
, duration)
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
203
TEST_F
(
TimerExecutionSpaceTest
, reuse)
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
ExecutionSpace.hpp
Timer.hpp
Kokkos::utils::timer::Timer
Measure elapsed time between events.
Definition
Timer.hpp:18
Kokkos::utils::tests::timer
Definition
test_Event.cpp:23
Kokkos::utils::tests::timer::TYPED_TEST_SUITE
TYPED_TEST_SUITE(EventTest, EventTypes)
Kokkos::utils::tests::timer::TEST_F
TEST_F(TimerExecutionSpaceTest, start_stop_elapsed)
Definition
test_Timer.cpp:112
Kokkos::utils::tests::timer::TimerTypes
::testing::Types< Timer< void >, Timer< execution_space > > TimerTypes
Definition
test_Timer.cpp:31
Kokkos::utils::tests::timer::TYPED_TEST
TYPED_TEST(EventTest, impl_event_type)
Definition
test_Event.cpp:40
Kokkos::utils::timer::seconds
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
Kokkos::utils::timer::milliseconds
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
Kokkos::utils::tests::scoped::ExecutionSpace
Create a new execution space instance with RAII semantics.
Definition
ExecutionSpace.hpp:12
Kokkos::utils::tests::timer::TimerExecutionSpaceTest
Definition
test_Timer.cpp:104
Kokkos::utils::tests::timer::TimerExecutionSpaceTest::timer_t
Timer< execution_space > timer_t
Definition
test_Timer.cpp:105
Kokkos::utils::tests::timer::TimerTest
Definition
test_Timer.cpp:29
execution_space
Kokkos::DefaultExecutionSpace execution_space
Definition
test_InsertOp.cpp:8
tests
timer
test_Timer.cpp
Generated on
for kokkos-utils by
1.18.0