11 #include "TpetraCore_config.h"
12 #include "Kokkos_Core.hpp"
13 #include "Teuchos_TestForException.hpp"
21 namespace DeepCopyCounterDetails {
23 bool is_initialized =
true;
24 size_t count_same = 0;
25 size_t count_different = 0;
26 bool count_active =
false;
28 void kokkosp_begin_deep_copy(Kokkos::Tools::SpaceHandle dst_handle,
const char *dst_name,
const void *dst_ptr,
29 Kokkos::Tools::SpaceHandle src_handle,
const char *src_name,
const void *src_ptr,
32 if (strcmp(dst_handle.name, src_handle.name))
42 DeepCopyCounterDetails::count_active =
true;
43 Kokkos::Tools::Experimental::set_begin_deep_copy_callback(DeepCopyCounterDetails::kokkosp_begin_deep_copy);
47 DeepCopyCounterDetails::count_same = 0;
48 DeepCopyCounterDetails::count_different = 0;
52 DeepCopyCounterDetails::count_active =
false;
56 return DeepCopyCounterDetails::count_same;
60 return DeepCopyCounterDetails::count_different;
65 namespace FenceCounterDetails {
68 bool is_initialized =
false;
69 bool count_active =
false;
70 std::vector<size_t> count_instance;
71 std::vector<size_t> count_global;
74 void kokkosp_begin_fence(
const char *name,
const uint32_t deviceId,
77 using namespace Kokkos::Tools::Experimental;
78 ExecutionSpaceIdentifier eid = identifier_from_devid(deviceId);
81 int idx = (int)eid.type;
82 if (eid.instance_id == int_for_synchronization_reason(SpecialSynchronizationCases::GlobalDeviceSynchronization))
85 count_instance[idx]++;
89 std::string get_label(
int i) {
90 using namespace Kokkos::Tools::Experimental;
91 DeviceType i_type = devicetype_from_uint32t(i);
92 std::string device_label;
93 if (i_type == DeviceType::Serial)
94 device_label =
"Serial";
95 else if (i_type == DeviceType::OpenMP)
96 device_label =
"OpenMP";
97 else if (i_type == DeviceType::Cuda)
98 device_label =
"Cuda";
99 else if (i_type == DeviceType::HIP)
100 device_label =
"HIP";
101 else if (i_type == DeviceType::OpenMPTarget)
102 device_label =
"OpenMPTarget";
103 else if (i_type == DeviceType::HPX)
104 device_label =
"HPX";
105 else if (i_type == DeviceType::Threads)
106 device_label =
"Threats";
107 else if (i_type == DeviceType::SYCL)
108 device_label =
"SYCL";
109 else if (i_type == DeviceType::OpenACC)
110 device_label =
"OpenACC";
111 else if (i_type == DeviceType::Unknown)
112 device_label =
"Unknown";
118 using namespace Kokkos::Tools::Experimental;
119 num_devices = (int)DeviceType::Unknown;
120 count_instance.resize(num_devices);
121 count_instance.assign(num_devices, 0);
122 count_global.resize(num_devices);
123 count_global.assign(num_devices, 0);
124 is_initialized =
true;
130 if (!FenceCounterDetails::is_initialized)
131 FenceCounterDetails::initialize();
132 FenceCounterDetails::count_active =
true;
133 Kokkos::Tools::Experimental::set_begin_fence_callback(FenceCounterDetails::kokkosp_begin_fence);
137 FenceCounterDetails::count_instance.assign(FenceCounterDetails::num_devices, 0);
138 FenceCounterDetails::count_global.assign(FenceCounterDetails::num_devices, 0);
142 FenceCounterDetails::count_active =
false;
146 using namespace Kokkos::Tools::Experimental;
147 for (
int i = 0; i < FenceCounterDetails::num_devices; i++) {
148 std::string device_label = FenceCounterDetails::get_label(i);
150 if (device == device_label)
151 return FenceCounterDetails::count_global[i];
155 TEUCHOS_TEST_FOR_EXCEPTION(1, std::runtime_error, std::string(
"Error: ") + device + std::string(
" is not a device known to Tpetra"));
159 using namespace Kokkos::Tools::Experimental;
160 for (
int i = 0; i < FenceCounterDetails::num_devices; i++) {
161 std::string device_label = FenceCounterDetails::get_label(i);
163 if (device == device_label)
164 return FenceCounterDetails::count_instance[i];
168 TEUCHOS_TEST_FOR_EXCEPTION(1, std::runtime_error, std::string(
"Error: ") + device + std::string(
" is not a device known to Tpetra"));
171 namespace KokkosRegionCounterDetails {
172 std::vector<std::string> regions;
174 void push_region_callback(
const char *label) { regions.push_back(label); }
175 static_assert(std::is_same_v<decltype(&push_region_callback),
176 Kokkos_Profiling_pushFunction>,
177 "Unexpected Kokkos profiling interface API. This is an internal "
178 "Tpetra developer error, please report this.");
183 Kokkos::Tools::Experimental::set_push_region_callback(
184 KokkosRegionCounterDetails::push_region_callback);
188 KokkosRegionCounterDetails::regions.clear();
192 Kokkos::Tools::Experimental::set_push_region_callback(
nullptr);
198 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
199 count += (region.find(needle) != std::string::npos);
205 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
206 os << region <<
"\n";
211 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
212 os << region <<
"\n";
void initialize(int *argc, char ***argv)
Initialize Tpetra.
void reset()
Reset the deep_copy counter.
Declaration of various tools for counting Kokkos calls of various types using the Kokkos Profiling Li...
void dump_regions(std::ostream &os)
Print all observed region labels, separated by newline.
size_t get_count_same_space()
Query the deep_copy counter for copies in the same space.
void stop()
Stop the counter.
size_t get_count_global(const std::string &device)
Query the fence counter for given device, for an Kokkos::fence()
size_t get_count_region_contains(const std::string &substr)
How many regions containing substr have been seen.
void start()
Start the fence counter.
size_t get_count_different_space()
Query the deep_copy counter for copies between different spaces.
void reset()
Reset the fence counter.
void start()
Start the deep_copy counter.
void stop()
Stop the fence counter.
size_t get_count_instance(const std::string &device)
Query the fence counter for given device, for an exec_space_instance.fence()
void stop()
Stop the deep_copy counter.
void reset()
Reset the counter.
void start()
Start the counter.