kokkos-utils 0.0.7
Loading...
Searching...
No Matches
test_Manager.cpp
Go to the documentation of this file.
1#include "gmock/gmock.h"
2#include "gtest/gtest.h"
3
4#include "Kokkos_Macros.hpp"
5#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6import kokkos.core;
7#else
8#include "Kokkos_Core.hpp"
9#endif
10
15
17
26
27using execution_space = Kokkos::DefaultExecutionSpace;
28
30{
31
32using namespace Kokkos::utils::callbacks;
33
35constexpr bool test_singleton_traits()
36{
37 static_assert( ! std::is_copy_constructible_v<Manager>);
38 static_assert( ! std::is_copy_assignable_v <Manager>);
39
40 static_assert( ! std::is_move_constructible_v<Manager>);
41 static_assert( ! std::is_move_assignable_v <Manager>);
42
43 return true;
44}
45static_assert(test_singleton_traits());
46
51TEST(Manager, initialize_finalize)
52{
53 ASSERT_FALSE(Manager::is_initialized());
54
56 ASSERT_TRUE(Manager::is_initialized());
57
58 const auto* ptr = std::addressof(Manager::get_instance());
59
61 ASSERT_TRUE(Manager::is_initialized());
62
63 ASSERT_EQ(ptr, std::addressof(Manager::get_instance()));
64
66 ASSERT_TRUE(Manager::is_initialized());
67
68 ASSERT_EQ(ptr, std::addressof(Manager::get_instance()));
69
71 ASSERT_FALSE(Manager::is_initialized());
72
74 ASSERT_FALSE(Manager::is_initialized());
75
77 ASSERT_FALSE(Manager::is_initialized());
78}
79
85 ASSERT_FALSE(Manager::is_initialized());
86
88}
89
107
108class ManagerTest : public ::testing::Test,
110 public scoped::ExecutionSpace<execution_space>
111{};
112
120template <Event... EventTypes>
122{
123 template <Kokkos::utils::impl::IsTypeOneOf<EventTypes...> EventType>
124 void operator()(const EventType& event) {
125 std::get<std::vector<EventType>>(events).push_back(event);
126 }
127
128 std::tuple<std::vector<EventTypes>...> events {};
129};
130
132TEST_F(ManagerTest, dispatch_to_call_operators_of_single_listener)
133{
134 const auto tester_listener = std::make_shared<TesterListener<BeginParallelForEvent, EndParallelForEvent, BeginFenceEvent>>();
135
137
139
141
142 const auto& [begin_parallel_for_events, end_parallel_for_events, begin_fence_events] = tester_listener->events;
143
145 ASSERT_EQ(begin_parallel_for_events.size(), 1);
146 ASSERT_EQ(begin_parallel_for_events[0].name, "computation - level 0 - pfor");
147
149 ASSERT_EQ(end_parallel_for_events.size(), 1);
150 ASSERT_EQ(begin_parallel_for_events[0].event_id, end_parallel_for_events[0].event_id);
151
153 ASSERT_GT(begin_fence_events.size(), 2);
154 ASSERT_THAT(
155 begin_fence_events,
156 ::testing::Contains(::testing::Field(&BeginFenceEvent::name, ::testing::StrEq("computation - level 0 - fence after pfor and single")))
157 );
158 ASSERT_THAT(
159 begin_fence_events,
160 ::testing::Contains(::testing::Field(&BeginFenceEvent::name, ::testing::StrEq("other fence after stopping the profile section")))
161 );
162}
163
165TEST_F(ManagerTest, dispatch_to_call_operators_of_multiple_listeners)
166{
167 const auto tester_listener_a = std::make_shared<TesterListener<BeginParallelForEvent>>();
168 const auto tester_listener_b = std::make_shared<TesterListener<BeginParallelForEvent, EndParallelForEvent>>();
169
172
174
177
178 const auto& [begin_parallel_for_events_listener_a] = tester_listener_a->events;
179 const auto& [begin_parallel_for_events_listener_b, end_parallel_for_events_listener_b] = tester_listener_b->events;
180
182 ASSERT_EQ(begin_parallel_for_events_listener_a.size(), 1);
183 ASSERT_EQ(begin_parallel_for_events_listener_a[0].name, "computation - level 0 - pfor");
184 ASSERT_EQ(begin_parallel_for_events_listener_b.size(), 1);
185 ASSERT_EQ(begin_parallel_for_events_listener_b[0].name, "computation - level 0 - pfor");
186
188 ASSERT_EQ(end_parallel_for_events_listener_b.size(), 1);
189 ASSERT_EQ(begin_parallel_for_events_listener_b[0].event_id, end_parallel_for_events_listener_b[0].event_id);
190}
191
193TEST_F(ManagerTest, listener_from_lambda)
194{
195 bool matched = false;
196
197 using view_t = Kokkos::View<int, execution_space>;
198
199 const view_t my_src_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, exec, "my rank-0 src view"));
200 const view_t my_dst_view(Kokkos::view_alloc(Kokkos::WithoutInitializing, exec, "my rank-0 dst view"));
201
202 const auto listener_handle = Kokkos::utils::callbacks::Manager::register_listener(
203 [&](const BeginDeepCopyEvent& event) {
204 if (event.dst.size == sizeof(int) && event.src.ptr == my_src_view.data()
205 && event.dst.ptr == my_dst_view.data()) matched = true;
206 }
207 );
208
209 Kokkos::deep_copy(exec, my_dst_view, my_src_view);
210
211 ASSERT_TRUE(matched);
212
214}
215
216} // namespace Kokkos::utils::tests::callbacks
Class to manage Kokkos profiling callback calls.
Definition Manager.hpp:141
static void unregister_listener(const Callable *const callable)
Unregister a callable object as a listener.
Definition Manager.hpp:197
static Manager & get_instance()
Definition Manager.hpp:166
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
Check if T is the same as one of the types Ts.
auto get_next_event_id() noexcept
If the Manager is initialized, return Manager::get_next_event_id. Otherwise, return EventTraits::inva...
Definition Manager.hpp:492
void dispatch(const EventType &event)
If the Manager is initialized, call Manager::dispatch with event.
Definition Manager.hpp:486
TEST_F(EnqueuedEventTimerTest, duration)
constexpr bool test_singleton_traits()
TEST(EventTest, BeginEvent)
Begin-deep-copy event associated with Kokkos::Tools::Experimental::EventSet::begin_deep_copy.
Definition Events.hpp:162
static constexpr event_id_t invalid_event_id
Definition Events.hpp:25
Profile event associated with Kokkos::Tools::Experimental::EventSet::profile_event.
Definition Events.hpp:224
Listener that stores the events it receives.
std::tuple< std::vector< EventTypes >... > events
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