Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ if(KOKKOSTOOLS_HAS_VARIORUM)
add_subdirectory(profiling/variorum-connector)
endif()

add_subdirectory(profiling/energy-profiler)

# GPU profilers
if(Kokkos_ENABLE_CUDA)
add_subdirectory(profiling/nvtx-connector)
Expand Down
2 changes: 2 additions & 0 deletions profiling/energy-profiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory(tests)
add_subdirectory(kokkos)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not the best name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be better? I would have "unit-tests" and "kokkos-tools" but it might also be something else.

43 changes: 43 additions & 0 deletions profiling/energy-profiler/common/daemon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "daemon.hpp"
#include <stdexcept>
#include <thread>

void Daemon::start() {
if (!running_) {
running_ = true;
thread_ = std::thread(&Daemon::tick, this);
} else {
throw std::runtime_error("Daemon already started");
}
}

void Daemon::tick() {
while (running_) {
std::chrono::high_resolution_clock::time_point start_time =
std::chrono::high_resolution_clock::now();

// Execute the function
func_();

std::chrono::high_resolution_clock::time_point end_time =
std::chrono::high_resolution_clock::now();
std::chrono::milliseconds execution_duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end_time -
start_time);

// Calculate how long to sleep to maintain the interval
if (execution_duration < interval_) {
std::chrono::milliseconds sleep_duration = interval_ - execution_duration;
std::this_thread::sleep_for(sleep_duration);
}
}
}

void Daemon::stop() {
if (running_) {
running_ = false;
thread_.join();
} else {
throw std::runtime_error("Daemon not started");
}
}
23 changes: 23 additions & 0 deletions profiling/energy-profiler/common/daemon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <functional>
#include <thread>
#include <chrono>

class Daemon {
public:
Daemon(std::function<void()> func, int interval_ms)
: interval_(interval_ms), func_(func) {};

void start();
void tick();
void stop();
bool is_running() const { return running_; }
std::thread& get_thread() { return thread_; }

private:
std::chrono::milliseconds interval_;
bool running_{false};
std::function<void()> func_;
std::thread thread_;
};
8 changes: 8 additions & 0 deletions profiling/energy-profiler/common/filename_prefix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "filename_prefix.hpp"

std::string generate_prefix() {
char hostname[256];
gethostname(hostname, 256);
int pid = (int)getpid();
return std::string(hostname) + "-" + std::to_string(pid);
}
6 changes: 6 additions & 0 deletions profiling/energy-profiler/common/filename_prefix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <string>
#include <unistd.h>

std::string generate_prefix();
244 changes: 244 additions & 0 deletions profiling/energy-profiler/common/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER

#include "timer.hpp"
#include <iostream>
#include <deque>
#include <cstdio>
Comment on lines +18 to +20
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these could be reduced to the ones that are not in timer.hpp


// EnergyTiming implementations
EnergyTiming::EnergyTiming()
: timing_id_(0), name_(""), region_type_(RegionType::Unknown) {
start_time_ = std::chrono::high_resolution_clock::now();
}

EnergyTiming::EnergyTiming(uint64_t timing_id, RegionType type,
std::string name)
: timing_id_(timing_id), name_(name), region_type_(type) {
start_time_ = std::chrono::high_resolution_clock::now();
}

void EnergyTiming::end() {
end_time_ = std::chrono::high_resolution_clock::now();
}

bool EnergyTiming::is_ended() const {
return end_time_ !=
std::chrono::time_point<std::chrono::high_resolution_clock>{};
}

uint64_t EnergyTiming::get_duration_ms() const {
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end_time_ - start_time_);
return static_cast<uint64_t>(duration.count());
}

// EnergyTimer implementations
void EnergyTimer::start_timing(uint64_t timing_id, RegionType type,
std::string name) {
timings_.emplace(timing_id, EnergyTiming(timing_id, type, name));
}

void EnergyTimer::end_timing(uint64_t timing_id) {
auto it = timings_.find(timing_id);
if (it != timings_.end()) {
it->second.end();
}
}

std::unordered_map<uint64_t, EnergyTiming>& EnergyTimer::get_timings() {
return timings_;
}

namespace KokkosTools {
namespace Timer {

void export_kernels_csv(const std::deque<TimingInfo>& timings,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this is a lot of repetition ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently working on refactoring this because indeed there is a lot of unnecessary repetition.

const std::string& filename) {
if (timings.empty()) return;

FILE* file = fopen(filename.c_str(), "w");
if (file) {
fprintf(file,
"name,type,start_time_epoch_ms,end_time_epoch_ms,duration_ms\n");
for (const auto& timing : timings) {
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.end_time.time_since_epoch())
.count();
auto duration_ms = timing.duration.count() / 1000000;

std::string type;
switch (timing.type) {
case RegionType::ParallelFor: type = "parallel_for"; break;
case RegionType::ParallelScan: type = "parallel_scan"; break;
case RegionType::ParallelReduce: type = "parallel_reduce"; break;
default: type = "unknown";
}

fprintf(file, "%s,%s,%ld,%ld,%ld\n", timing.name.c_str(), type.c_str(),
start_ms, end_ms, duration_ms);
}
fclose(file);
std::cout << "Timing data exported to " << filename << std::endl;
} else {
std::cerr << "ERROR: Unable to open file " << filename << " for writing.\n";
}
}

void export_regions_csv(const std::deque<TimingInfo>& timings,
const std::string& filename) {
if (timings.empty()) return;

FILE* file = fopen(filename.c_str(), "w");
if (file) {
fprintf(file, "name,start_time_epoch_ms,end_time_epoch_ms,duration_ms\n");
for (const auto& timing : timings) {
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.end_time.time_since_epoch())
.count();
auto duration_ms = timing.duration.count() / 1000000;

fprintf(file, "%s,%ld,%ld,%ld\n", timing.name.c_str(), start_ms, end_ms,
duration_ms);
}
fclose(file);
std::cout << "Region data exported to " << filename << std::endl;
} else {
std::cerr << "ERROR: Unable to open file " << filename << " for writing.\n";
}
}

void export_deepcopies_csv(const std::deque<TimingInfo>& timings,
const std::string& filename) {
if (timings.empty()) return;

FILE* file = fopen(filename.c_str(), "w");
if (file) {
fprintf(file, "name,start_time_epoch_ms,end_time_epoch_ms,duration_ms\n");
for (const auto& timing : timings) {
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
timing.end_time.time_since_epoch())
.count();
auto duration_ms = timing.duration.count() / 1000000;

fprintf(file, "%s,%ld,%ld,%ld\n", timing.name.c_str(), start_ms, end_ms,
duration_ms);
}
fclose(file);
std::cout << "Deep copy data exported to " << filename << std::endl;
} else {
std::cerr << "ERROR: Unable to open file " << filename << " for writing.\n";
}
}

void print_kernels_summary(const std::deque<TimingInfo>& kernels) {
std::cout << "\n==== KERNELS ====\n";
std::cout << "| Name | Type | "
"Start(ms) | End(ms) | Duration (ms) |\n";
std::cout << "|--------------------------------------|----------------|------"
"-------------|-------------------|---------------|\n";
for (const auto& info : kernels) {
std::string type;
switch (info.type) {
case RegionType::ParallelFor: type = "parallel_for"; break;
case RegionType::ParallelScan: type = "parallel_scan"; break;
case RegionType::ParallelReduce: type = "parallel_reduce"; break;
default: type = "unknown";
}
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.end_time.time_since_epoch())
.count();
std::cout
<< "| " << info.name
<< std::string(38 - std::min<size_t>(info.name.size(), 38), ' ') << "| "
<< type << std::string(16 - type.size(), ' ') << "| " << start_ms
<< std::string(19 - std::to_string(start_ms).size(), ' ') << "| "
<< end_ms << std::string(19 - std::to_string(end_ms).size(), ' ')
<< "| " << (info.duration.count() / 1000000)
<< std::string(
13 - std::to_string(info.duration.count() / 1000000).size(), ' ')
<< "|\n";
}
}

void print_regions_summary(const std::deque<TimingInfo>& regions) {
std::cout << "\n==== REGIONS ====\n";
std::cout << "| Name | Start(ms) | "
"End(ms) | Duration (ms) |\n";
std::cout << "|--------------------------------------|-------------------|---"
"----------------|---------------|\n";
for (const auto& info : regions) {
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.end_time.time_since_epoch())
.count();
std::cout << "| " << info.name
<< std::string(38 - std::min<size_t>(info.name.size(), 38), ' ')
<< "| " << start_ms
<< std::string(19 - std::to_string(start_ms).size(), ' ') << "| "
<< end_ms << std::string(19 - std::to_string(end_ms).size(), ' ')
<< "| " << (info.duration.count() / 1000000)
<< std::string(
13 -
std::to_string(info.duration.count() / 1000000).size(),
' ')
<< "|\n";
}
}

void print_deepcopies_summary(const std::deque<TimingInfo>& deepcopies) {
std::cout << "\n==== DEEP COPIES ====\n";
std::cout << "| Name | Start(ms) | "
"End(ms) | Duration (ms) |\n";
std::cout << "|--------------------------------------|-------------------|---"
"----------------|---------------|\n";
for (const auto& info : deepcopies) {
auto start_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.start_time.time_since_epoch())
.count();
auto end_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
info.end_time.time_since_epoch())
.count();
std::cout << "| " << info.name
<< std::string(38 - std::min<size_t>(info.name.size(), 38), ' ')
<< "| " << start_ms
<< std::string(19 - std::to_string(start_ms).size(), ' ') << "| "
<< end_ms << std::string(19 - std::to_string(end_ms).size(), ' ')
<< "| " << (info.duration.count() / 1000000)
<< std::string(
13 -
std::to_string(info.duration.count() / 1000000).size(),
' ')
<< "|\n";
}
}

} // namespace Timer
} // namespace KokkosTools
Loading
Loading