-
Notifications
You must be signed in to change notification settings - Fork 75
NVML and Variorum based energy measurement tool for Kokkos #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
97c884b
5931cab
7d9565a
48780b0
573ea3b
fbf4f79
51e900b
c55fb56
032538b
1c31665
eadef98
7d46730
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| add_subdirectory(tests) | ||
| add_subdirectory(kokkos) | ||
| 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"); | ||
| } | ||
| } |
| 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_; | ||
| }; |
| 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); | ||
| } |
| 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(); |
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm this is a lot of repetition ...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.