kokkos-utils 0.0.1
 
Loading...
Searching...
No Matches
test_TimerListener.cpp
Go to the documentation of this file.
1#include "gmock/gmock.h"
2
3#include "Kokkos_Core.hpp"
4
15
17
27
28using execution_space = Kokkos::DefaultExecutionSpace;
29
31{
32
33using namespace Kokkos::utils::callbacks;
34
37
38class TimerListenerTest : public ::testing::Test,
40 public scoped::ExecutionSpace<execution_space>
41{};
42
45{
48 static_assert(std::same_as<
50 Kokkos::Impl::type_list<BeginParallelForEvent, EndParallelForEvent>
51 >);
52
53 static_assert(Listener<parallel_for_timer_t>);
54}
55
60TEST_F(TimerListenerTest, begin_not_matched_and_end_not_matched)
61{
62 const parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
63 ASSERT_FALSE(par_for_timer.connected());
64 ASSERT_FALSE(par_for_timer.closed());
65}
66
71TEST_F(TimerListenerTest, begin_matched_but_end_not_matched)
72{
73 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
74 par_for_timer(BeginParallelForEvent{
75 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 1
76 });
77 ASSERT_TRUE(par_for_timer.connected());
78 ASSERT_FALSE(par_for_timer.closed());
79}
80
86 TEST_F(TimerListenerTest, begin_and_end_matched)
87 {
88 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
89 par_for_timer(BeginParallelForEvent{
90 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 1
91 });
92 ASSERT_TRUE(par_for_timer.connected());
93 par_for_timer(EndParallelForEvent{.event_id = 1});
94 ASSERT_FALSE(par_for_timer.connected());
95 ASSERT_TRUE(par_for_timer.closed());
96
97 par_for_timer.reset();
98 ASSERT_FALSE(par_for_timer.connected());
99 ASSERT_FALSE(par_for_timer.closed());
100
101 par_for_timer(BeginParallelForEvent{
102 .name = "my-name", .dev_id = Kokkos::Tools::Experimental::device_id(exec), .event_id = 2
103 });
104 ASSERT_TRUE(par_for_timer.connected());
105 ASSERT_FALSE(par_for_timer.closed());
106}
107
112TEST_F(TimerListenerTest, begin_not_matched_if_device_id_does_not_match)
113{
114 const auto other_exec = Kokkos::Experimental::partition_space(execution_space{}, 1)[0];
115 const auto other_exec_dev_id = Kokkos::Tools::Experimental::device_id(other_exec);
116 const auto exec_dev_id = Kokkos::Tools::Experimental::device_id( exec);
117
118 parallel_for_timer_t par_for_timer(std::regex("my-name"), exec);
119 par_for_timer(BeginParallelForEvent{
120 .name = "my-name", .dev_id = other_exec_dev_id, .event_id = 1
121 });
122 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 << '.';
123}
124
127{
128 const auto par_for_timer_matching = std::make_shared<parallel_for_timer_t>(
129 std::regex("computation - level 0 - pfor"), exec
130 );
131 const auto par_for_timer_no_match = std::make_shared<parallel_for_timer_t>(
132 std::regex("this-should-not-match"), exec
133 );
134
137
139
140 ASSERT_GT(par_for_timer_matching->timer.duration(), Kokkos::utils::timer::seconds(0));
141
142 EXPECT_FALSE(par_for_timer_no_match->connected() || par_for_timer_no_match->closed())
143 << "The listener should not be connected or closed because the events did not match.";
144
145 Kokkos::utils::callbacks::Manager::unregister_listener(par_for_timer_matching.get());
146 Kokkos::utils::callbacks::Manager::unregister_listener(par_for_timer_no_match.get());
147}
148
151{
152 using parallel_for_with_launch_timer_t = ParallelForWithLaunchTimerListener<EventNameMatcher, execution_space>;
153
155
157
158 const auto par_for_timer = std::make_shared<parallel_for_with_launch_timer_t>(
159 "computation - level 0 - pfor", exec
160 );
161
163
164 timer_external.start();
165
167
168 timer_external.stop();
169
170 ASSERT_GT(par_for_timer->timer.duration(), unit_t{0.});
171 ASSERT_GT(par_for_timer->timer.launch(), unit_t{0.});
172 ASSERT_LE(par_for_timer->timer.launch(), timer_external.duration());
173
175}
176
179
182{
185 static_assert(std::same_as<
187 Kokkos::Impl::type_list<PushRegionEvent, PopRegionEvent>
188 >);
189
190 static_assert(Listener<region_timer_t>);
191}
192
205{
206 const auto par_for_timer = std::make_shared<parallel_for_timer_t>(
207 std::regex("computation - level 0 - pfor"), exec
208 );
209
210 const auto region_timer = std::make_shared<region_timer_t>(
211 std::regex("computation - level 0")
212 );
213
216
218
219 ASSERT_GT(par_for_timer->timer.duration(), Kokkos::utils::timer::milliseconds{0.});
220 ASSERT_LT(par_for_timer->timer.duration(), region_timer->timer.duration());
221
224}
225
227TEST(RegionTimerListener, traits_when_used_with_enqueued_event_timer)
228{
229 using region_timer_with_enqueued_event_timer_t = RegionTimerListener<
232 >;
233
234 static_assert(std::constructible_from<
235 region_timer_with_enqueued_event_timer_t,
236 std::regex, execution_space
237 >);
238}
239
240} // namespace Kokkos::utils::tests::callbacks
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:194
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:171
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
Kokkos::Impl::filter_type_list_t< impl::IsListenerFor< Callable >::template type, EventTypeList > listener_event_type_list_t
Type list holding the event types that Callable can be a listener for.
Definition Listener.hpp:39
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:40
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