43 #include "TpetraCore_config.h"
44 #include "Kokkos_Core.hpp"
45 #include "Teuchos_TestForException.hpp"
54 namespace DeepCopyCounterDetails {
56 bool is_initialized=
true;
58 size_t count_different=0;
59 bool count_active=
false;
61 void kokkosp_begin_deep_copy(Kokkos::Tools::SpaceHandle dst_handle,
const char* dst_name,
const void* dst_ptr,
62 Kokkos::Tools::SpaceHandle src_handle,
const char* src_name,
const void* src_ptr,
66 if(strcmp(dst_handle.name,src_handle.name))
77 DeepCopyCounterDetails::count_active=
true;
78 Kokkos::Tools::Experimental::set_begin_deep_copy_callback(DeepCopyCounterDetails::kokkosp_begin_deep_copy);
82 DeepCopyCounterDetails::count_same=0;
83 DeepCopyCounterDetails::count_different=0;
87 DeepCopyCounterDetails::count_active=
false;
91 return DeepCopyCounterDetails::count_same;
95 return DeepCopyCounterDetails::count_different;
103 namespace FenceCounterDetails {
106 bool is_initialized=
false;
107 bool count_active=
false;
108 std::vector<size_t> count_instance;
109 std::vector<size_t> count_global;
113 void kokkosp_begin_fence(
const char* name,
const uint32_t deviceId,
117 using namespace Kokkos::Tools::Experimental;
118 ExecutionSpaceIdentifier eid = identifier_from_devid(deviceId);
121 int idx = (int) eid.type;
122 if(eid.instance_id == Impl::int_for_synchronization_reason(SpecialSynchronizationCases::GlobalDeviceSynchronization))
125 count_instance[idx]++;
130 std::string get_label(
int i) {
131 using namespace Kokkos::Tools::Experimental;
132 DeviceType i_type = devicetype_from_uint32t(i);
133 std::string device_label;
134 if (i_type == DeviceType::Serial) device_label=
"Serial";
135 else if (i_type == DeviceType::OpenMP) device_label=
"OpenMP";
136 else if (i_type == DeviceType::Cuda) device_label=
"Cuda";
137 else if (i_type == DeviceType::HIP) device_label=
"HIP";
138 else if (i_type == DeviceType::OpenMPTarget) device_label=
"OpenMPTarget";
139 else if (i_type == DeviceType::HPX) device_label=
"HPX";
140 else if (i_type == DeviceType::Threads) device_label=
"Threats";
141 else if (i_type == DeviceType::SYCL) device_label=
"SYCL";
142 else if (i_type == DeviceType::OpenACC) device_label=
"OpenACC";
143 else if (i_type == DeviceType::Unknown) device_label=
"Unknown";
149 using namespace Kokkos::Tools::Experimental;
150 num_devices = (int) DeviceType::Unknown;
151 count_instance.resize(num_devices);
152 count_instance.assign(num_devices,0);
153 count_global.resize(num_devices);
154 count_global.assign(num_devices,0);
164 if(!FenceCounterDetails::is_initialized)
165 FenceCounterDetails::initialize();
166 FenceCounterDetails::count_active=
true;
167 Kokkos::Tools::Experimental::set_begin_fence_callback(FenceCounterDetails::kokkosp_begin_fence);
171 FenceCounterDetails::count_instance.assign(FenceCounterDetails::num_devices,0);
172 FenceCounterDetails::count_global.assign(FenceCounterDetails::num_devices,0);
176 FenceCounterDetails::count_active=
false;
180 using namespace Kokkos::Tools::Experimental;
181 for(
int i=0;i<FenceCounterDetails::num_devices; i++) {
182 std::string device_label = FenceCounterDetails::get_label(i);
184 if(device == device_label)
185 return FenceCounterDetails::count_global[i];
189 TEUCHOS_TEST_FOR_EXCEPTION(1,std::runtime_error,std::string(
"Error: ") + device + std::string(
" is not a device known to Tpetra"));
194 using namespace Kokkos::Tools::Experimental;
195 for(
int i=0;i<FenceCounterDetails::num_devices; i++) {
196 std::string device_label = FenceCounterDetails::get_label(i);
198 if(device == device_label)
199 return FenceCounterDetails::count_instance[i];
203 TEUCHOS_TEST_FOR_EXCEPTION(1,std::runtime_error,std::string(
"Error: ") + device + std::string(
" is not a device known to Tpetra"));
207 namespace KokkosRegionCounterDetails {
208 std::vector<std::string> regions;
210 void push_region_callback(
const char *label) { regions.push_back(label); }
211 static_assert(std::is_same_v<decltype(&push_region_callback),
212 Kokkos_Profiling_pushFunction>,
213 "Unexpected Kokkos profiling interface API. This is an internal "
214 "Tpetra developer error, please report this.");
219 Kokkos::Tools::Experimental::set_push_region_callback(
220 KokkosRegionCounterDetails::push_region_callback);
224 KokkosRegionCounterDetails::regions.clear();
228 Kokkos::Tools::Experimental::set_push_region_callback(
nullptr);
234 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
235 count += (region.find(needle) != std::string::npos);
241 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
242 os << region <<
"\n";
247 for (
const auto ®ion : KokkosRegionCounterDetails::regions) {
248 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.