Skip to content

Commit 2629fad

Browse files
committed
Refactor energy profiler timing functions
1 parent 5af4bf7 commit 2629fad

File tree

4 files changed

+31
-44
lines changed

4 files changed

+31
-44
lines changed

profiling/energy-profiler/kp_energy_profiler.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <cstdint>
2121
#include <cstdio>
2222
#include <cstdlib>
23-
#include <cstring>
2423
#include <iostream>
2524
#include <mutex>
2625
#include <string>
@@ -93,7 +92,8 @@ void end_region_by_type(RegionType type_to_end) {
9392
// End region by id
9493
void end_region_with_id(uint64_t expected_id) {
9594
try {
96-
auto& state = EnergyProfilerState::get_instance();
95+
auto end_time = std::chrono::high_resolution_clock::now();
96+
auto& state = EnergyProfilerState::get_instance();
9797
std::lock_guard<std::mutex> lock(state.get_mutex());
9898
auto& active_regions = state.get_active_regions();
9999
if (active_regions.empty()) {
@@ -106,9 +106,9 @@ void end_region_with_id(uint64_t expected_id) {
106106
return region.id == expected_id;
107107
});
108108
if (it != active_regions.end()) {
109-
auto region = *it;
109+
auto region = *it;
110+
region.end_time = end_time;
110111
active_regions.erase(it);
111-
region.end_time = std::chrono::high_resolution_clock::now();
112112
state.get_completed_timings().push_back(region);
113113
} else {
114114
std::cerr << "Warning: No active region found with ID " << expected_id
@@ -154,7 +154,9 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
154154
Kokkos_Profiling_KokkosPDeviceInfo* deviceInfo) {
155155
(void)devInfoCount;
156156
(void)deviceInfo;
157-
if (std::getenv("KOKKOS_TOOLS_ENERGY_VERBOSE")) {
157+
const char* verbose_env = std::getenv("KOKKOS_TOOLS_ENERGY_VERBOSE");
158+
if (verbose_env &&
159+
(std::string(verbose_env) == "1" || std::string(verbose_env) == "ON")) {
158160
KokkosTools::EnergyProfiler::EnergyProfilerState::get_instance()
159161
.set_verbose_enabled(true);
160162
}
@@ -170,7 +172,8 @@ void kokkosp_finalize_library() {
170172
printf("Kokkos Energy Profiler: Finalizing library\n");
171173
std::string prefix = KokkosTools::EnergyProfiler::generate_prefix();
172174
auto all_timings = KokkosTools::EnergyProfiler::get_all_timings();
173-
KokkosTools::EnergyProfiler::print_all_timings_summary(all_timings);
175+
KokkosTools::EnergyProfiler::print_all_timings_summary(
176+
std::cout, all_timings.begin(), all_timings.end());
174177
KokkosTools::EnergyProfiler::export_all_timings_csv(
175178
all_timings, prefix + "_timing_data.csv");
176179
printf("Kokkos Energy Profiler: Library finalized\n");

profiling/energy-profiler/timing_export.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,29 @@ std::string get_category_from_type(RegionType type) {
5959
}
6060
}
6161

62-
void print_all_timings_summary(const std::vector<TimingInfo>& all_timings) {
63-
std::cout << "\n==== TIMING SUMMARY ====\n";
64-
std::cout << "| Category | Name | Type "
65-
" | Start (ms) | End (ms) | Duration (ms) |\n";
66-
std::cout << "|------------|----------------------------------|--------------"
67-
"--|-------------------|-------------------|---------------|\n";
68-
for (const auto& timing_info : all_timings) {
69-
auto start_ms = get_epoch_ms(timing_info.start_time);
70-
auto end_ms = get_epoch_ms(timing_info.end_time);
62+
void print_all_timings_summary(std::ostream& os,
63+
std::vector<TimingInfo>::const_iterator begin,
64+
std::vector<TimingInfo>::const_iterator end) {
65+
os << "\n==== TIMING SUMMARY ====\n";
66+
os << "| Category | Name | Type "
67+
" | Start (ms) | End (ms) | Duration (ms) |\n";
68+
os << "|------------|----------------------------------|--------------"
69+
"--|-------------------|-------------------|---------------|\n";
70+
for (auto it = begin; it != end; ++it) {
71+
const auto& timing_info = *it;
72+
auto start_ms = get_epoch_ms(timing_info.start_time);
73+
auto end_ms = get_epoch_ms(timing_info.end_time);
7174
auto duration_ms =
7275
get_duration_ms(timing_info.start_time, timing_info.end_time);
7376
std::string type_str = region_type_to_string(timing_info.type);
7477
std::string category = get_category_from_type(timing_info.type);
75-
std::cout << "| " << std::setw(COLUMN_WIDTH_CATEGORY) << std::left
76-
<< category << " | " << std::setw(COLUMN_WIDTH_NAME) << std::left
77-
<< timing_info.name << " | " << std::setw(COLUMN_WIDTH_TYPE)
78-
<< std::left << type_str << " | " << std::setw(COLUMN_WIDTH_TIME)
79-
<< std::right << start_ms << " | " << std::setw(COLUMN_WIDTH_TIME)
80-
<< std::right << end_ms << " | "
81-
<< std::setw(COLUMN_WIDTH_DURATION) << std::right << duration_ms
82-
<< " |\n";
78+
os << "| " << std::setw(COLUMN_WIDTH_CATEGORY) << std::left << category
79+
<< " | " << std::setw(COLUMN_WIDTH_NAME) << std::left << timing_info.name
80+
<< " | " << std::setw(COLUMN_WIDTH_TYPE) << std::left << type_str
81+
<< " | " << std::setw(COLUMN_WIDTH_TIME) << std::right << start_ms
82+
<< " | " << std::setw(COLUMN_WIDTH_TIME) << std::right << end_ms << " | "
83+
<< std::setw(COLUMN_WIDTH_DURATION) << std::right << duration_ms
84+
<< " |\n";
8385
}
8486
}
8587

profiling/energy-profiler/timing_export.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ namespace EnergyProfiler {
2525

2626
void export_all_timings_csv(const std::vector<TimingInfo>& all_timings,
2727
const std::string& filename);
28-
void print_all_timings_summary(const std::vector<TimingInfo>& all_timings);
28+
void print_all_timings_summary(std::ostream& os,
29+
std::vector<TimingInfo>::const_iterator begin,
30+
std::vector<TimingInfo>::const_iterator end);
2931

3032
} // namespace EnergyProfiler
3133
} // namespace KokkosTools

profiling/energy-profiler/timing_utils.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,6 @@ void set_verbose_enabled(bool enabled);
9696
void log_verbose(const char* format, ...);
9797
std::vector<TimingInfo> get_all_timings();
9898

99-
/// @brief RAII class for automatic timing region management
100-
class ScopedTimingRegion {
101-
public:
102-
ScopedTimingRegion(const std::string& name, RegionType type)
103-
: region_id_(generate_new_region_id()) {
104-
start_region(name, type, region_id_);
105-
}
106-
107-
~ScopedTimingRegion() { end_region_with_id(region_id_); }
108-
109-
// Delete copy and move operations
110-
ScopedTimingRegion(const ScopedTimingRegion&) = delete;
111-
ScopedTimingRegion& operator=(const ScopedTimingRegion&) = delete;
112-
ScopedTimingRegion(ScopedTimingRegion&&) = delete;
113-
ScopedTimingRegion& operator=(ScopedTimingRegion&&) = delete;
114-
115-
private:
116-
uint64_t region_id_;
117-
};
118-
11999
// Filename prefix generation
120100
/// @brief Generate a prefix for output files based on hostname and PID
121101
std::string generate_prefix();

0 commit comments

Comments
 (0)