kokkos-utils 0.0.7
Loading...
Searching...
No Matches
test_TimerListener.cpp
Go to the documentation of this file.
1#include "gmock/gmock.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
20
22
32
33using execution_space = Kokkos::DefaultExecutionSpace;
34
36{
37
38using namespace Kokkos::utils::callbacks;
39
42
43class TimerListenerTest : public ::testing::Test,
45 public scoped::ExecutionSpace<execution_space>
46{};
47
50{
53 static_assert(std::same_as<
55 Kokkos::Impl::type_list<BeginParallelForEvent, EndParallelForEvent>
56 >);
57
58 static_assert(Listener<parallel_for_timer_t>);
59}
60
65TEST_F(TimerListenerTest, begin_not_matched_and_end_not_matched)
66{
67 const parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
68 ASSERT_FALSE(par_for_timer.connected());
69 ASSERT_FALSE(par_for_timer.closed());
70}
71
76TEST_F(TimerListenerTest, begin_matched_but_end_not_matched)
77{
78 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
79 par_for_timer(BeginParallelForEvent{
80 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 1
81 });
82 ASSERT_TRUE(par_for_timer.connected());
83 ASSERT_FALSE(par_for_timer.closed());
84}
85
91 TEST_F(TimerListenerTest, begin_and_end_matched)
92 {
93 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
94 par_for_timer(BeginParallelForEvent{
95 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 1
96 });
97 ASSERT_TRUE(par_for_timer.connected());
98 par_for_timer(EndParallelForEvent{.event_id = 1});
99 ASSERT_FALSE(par_for_timer.connected());
100 ASSERT_TRUE(par_for_timer.closed());
101
102 par_for_timer.reset();
103 ASSERT_FALSE(par_for_timer.connected());
104 ASSERT_FALSE(par_for_timer.closed());
105
106 par_for_timer(BeginParallelForEvent{
107 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 2
108 });
109 ASSERT_TRUE(par_for_timer.connected());
110 ASSERT_FALSE(par_for_timer.closed());
111}
112
117TEST_F(TimerListenerTest, begin_not_matched_if_device_id_does_not_match)
118{
119 const auto other_exec = Kokkos::Experimental::partition_space(execution_space{}, 1)[0];
120 const auto other_exec_dev_id = Kokkos::Tools::Experimental::device_id(other_exec);
121 const auto exec_dev_id = Kokkos::Tools::Experimental::device_id( exec);
122
123 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
124 par_for_timer(BeginParallelForEvent{
125 .name = "my-name", .dev_id = other_exec_dev_id, .event_id = 1
126 });
127 ASSERT_EQ(par_for_timer.connected(), other_exec_dev_id == exec_dev_id) << "The event was enqueued on " << other_exec_dev_id << " and the timer should look at events on " << exec_dev_id << '.';
128}
129
132{
133 const auto par_for_timer_matching = std::make_shared<parallel_for_timer_t>(
134 std::regex("computation - level 0 - pfor"), exec
135 );
136 const auto par_for_timer_no_match = std::make_shared<parallel_for_timer_t>(
137 std::regex("this-should-not-match"), exec
138 );
139
142
144
145 ASSERT_GT(par_for_timer_matching->timer.duration(), Kokkos::utils::timer::seconds(0));
146
147 EXPECT_FALSE(par_for_timer_no_match->connected() || par_for_timer_no_match->closed())
148 << "The listener should not be connected or closed because the events did not match.";
149
150 Kokkos::utils::callbacks::Manager::unregister_listener(par_for_timer_matching.get());
151 Kokkos::utils::callbacks::Manager::unregister_listener(par_for_timer_no_match.get());
152}
153
156{
157 using parallel_for_with_launch_timer_t = ParallelForWithLaunchTimerListener<EventNameMatcher, execution_space>;
158
160
162
163 const auto par_for_timer = std::make_shared<parallel_for_with_launch_timer_t>(
164 "computation - level 0 - pfor", exec
165 );
166
168
169 timer_external.start();
170
172
173 timer_external.stop();
174
175 ASSERT_GT(par_for_timer->timer.duration(), unit_t{0.});
176 ASSERT_GT(par_for_timer->timer.launch(), unit_t{0.});
177 ASSERT_LE(par_for_timer->timer.launch(), timer_external.duration());
178
180}
181
184
187{
190 static_assert(std::same_as<
192 Kokkos::Impl::type_list<PushRegionEvent, PopRegionEvent>
193 >);
194
195 static_assert(Listener<region_timer_t>);
196}
197
210{
211 const auto par_for_timer = std::make_shared<parallel_for_timer_t>(
212 std::regex("computation - level 0 - pfor"), exec
213 );
214
215 const auto region_timer = std::make_shared<region_timer_t>(
216 std::regex("computation - level 0")
217 );
218
221
223
224 ASSERT_GT(par_for_timer->timer.duration(), Kokkos::utils::timer::milliseconds{0.});
225 ASSERT_LT(par_for_timer->timer.duration(), region_timer->timer.duration());
226
229}
230
232TEST(RegionTimerListener, traits_when_used_with_enqueued_event_timer)
233{
234 using region_timer_with_enqueued_event_timer_t = RegionTimerListener<
237 >;
238
239 static_assert(std::constructible_from<
240 region_timer_with_enqueued_event_timer_t,
241 std::regex, execution_space
242 >);
243}
244
245} // namespace Kokkos::utils::tests::callbacks
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:197
static listener_list_const_iter_t register_listener(std::shared_ptr< Callable > callable)
Register a callable object, passed as a shared pointer, as a listener.
Definition Manager.hpp:174
Measure elapsed time between events.
Definition Timer.hpp:18
void stop()
Stop the timer.
Definition Timer.hpp:55
void start()
Start the timer.
Definition Timer.hpp:38
BeginEndTimerListener< MatcherType, BeginParallelForEvent, Exec, EnqueuedEventWithLaunchTimer< Exec > > ParallelForWithLaunchTimerListener
typename impl::ListenerEventTypeList< Callable >::type listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
Definition Listener.hpp:72
BeginEndTimerListener< MatcherType, BeginParallelForEvent, Exec > ParallelForTimerListener
RegionTimerListener< EventRegexMatcher > region_timer_t
Listener to time regions whose name matches a regex.
TEST_F(EnqueuedEventTimerTest, duration)
ParallelForTimerListener< EventRegexMatcher, execution_space > parallel_for_timer_t
Listener to time parallel for regions whose name matches a regex.
TEST(EventTest, BeginEvent)
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::micro > microseconds
Similar to std::chrono::microseconds, but using double instead of an integer type to represent the ti...
Definition Duration.hpp:15
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
End-parallel-for event associated with Kokkos::Tools::Experimental::EventSet::end_parallel_for.
Definition Events.hpp:55
Timer for events that are enqueued on exec.
Matcher to select events whose name matches a regular expression.
Timer listener for a profiling region that matches MatcherType.
void reset()
Reset the listener so that it can be reused to match a begin event again.
bool closed() const
When the listener has matched both the begin and end events, it is said to be "closed".
bool connected() const
When the listener has matched the begin, but not yet the end event, it is said to be "connected".
Create a new execution space instance with RAII semantics.
Initializing and finalizing Kokkos::utils::callbacks::Manager in a RAII manner.
Definition Manager.hpp:10
Kokkos::DefaultExecutionSpace execution_space