kokkos-utils
0.0.7
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_Macros.hpp"
4
#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
5
import
kokkos.core;
6
#else
7
#include "Kokkos_Core.hpp"
8
#endif
9
10
#include "
kokkos-utils/tests/scoped/ExecutionSpace.hpp
"
11
#include "
kokkos-utils/timer/Timer.hpp
"
12
23
24
using
execution_space
= Kokkos::DefaultExecutionSpace;
25
26
namespace
Kokkos::utils::tests::timer
27
{
28
29
using namespace
Kokkos::utils::timer;
30
31
template
<
typename
T>
32
struct
TimerTest
:
public
::testing::Test,
33
public
scoped::ExecutionSpace
<execution_space>
34
{};
35
36
using
TimerTypes
= ::testing::Types<
37
Timer<void>
,
38
Timer<execution_space>
39
>;
40
41
TYPED_TEST_SUITE
(
TimerTest
,
TimerTypes
);
42
47
TYPED_TEST
(
TimerTest
, invalid_if_not_started_and_not_stopped)
48
{
49
const
TypeParam
timer
;
50
ASSERT_FALSE(
timer
.is_valid());
51
}
52
57
TYPED_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
74
TYPED_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
91
TYPED_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
109
struct
TimerExecutionSpaceTest
:
public
TimerTest
<execution_space> {
110
using
timer_t
=
Timer<execution_space>
;
111
};
112
117
TEST_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
147
TYPED_TEST
(
TimerTest
, duration)
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
208
TEST_F
(
TimerExecutionSpaceTest
, reuse)
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
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:28
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:117
Kokkos::utils::tests::timer::TimerTypes
::testing::Types< Timer< void >, Timer< execution_space > > TimerTypes
Definition
test_Timer.cpp:36
Kokkos::utils::tests::timer::TYPED_TEST
TYPED_TEST(EventTest, impl_event_type)
Definition
test_Event.cpp:45
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:17
Kokkos::utils::tests::timer::TimerExecutionSpaceTest
Definition
test_Timer.cpp:109
Kokkos::utils::tests::timer::TimerExecutionSpaceTest::timer_t
Timer< execution_space > timer_t
Definition
test_Timer.cpp:110
Kokkos::utils::tests::timer::TimerTest
Definition
test_Timer.cpp:34
execution_space
Kokkos::DefaultExecutionSpace execution_space
Definition
test_InsertOp.cpp:13
tests
timer
test_Timer.cpp
Generated on
for kokkos-utils by
1.18.0