From 0839d94687ee54ad41515bb25c0e33a236777be7 Mon Sep 17 00:00:00 2001 From: "Ryan M. Richard" Date: Fri, 23 May 2025 17:14:07 +0000 Subject: [PATCH 1/2] Adds energy monitoring --- CMakeLists.txt | 17 +++++++++ src/parallelzone/hardware/cpu/cpu.cpp | 5 ++- .../hardware/cpu/energy_monitor.hpp | 38 +++++++++++++++++++ .../parallelzone/hardware/cpu/cpu.cpp | 2 + 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/parallelzone/hardware/cpu/energy_monitor.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b2f4656..5f0bdd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ nwx_cxx_api_docs("README.md" "${project_inc_dir}" "${project_src_dir}") cmaize_option_list( BUILD_TESTING OFF "Should we build the tests?" BUILD_PYBIND11_PYBINDINGS ON "Should we build pybind11 python bindings?" + BUILD_CPP_JOULES OFF "Enable energy usage tracking with CPP Joules library?" BUILD_CUDA_BINDINGS OFF "Enable CUDA Bindings" BUILD_HIP_BINDINGS OFF "Enable HIP Bindings" BUILD_SYCL_BINDINGS OFF "Enable SYCL Bindings" @@ -67,6 +68,18 @@ if("${BUILD_PAPI_BINDINGS}") include(build_papi) endif () +if("${BUILD_CPP_JOULES}") + cmaize_find_or_build_dependency( + cpp_joules + BUILD_CPP_JOULES + URL github.com/rishalab/CPPJoules + VERSION main + BUILD_TARGET CPP_Joules + FIND_TARGET CPP_Joules::CPP_Joules + ) + list(APPEND project_depends cpp_joules) +endif() + cmaize_find_or_build_dependency( cereal URL github.com/USCiLab/cereal @@ -84,6 +97,10 @@ cmaize_add_library( DEPENDS "${project_depends}" ) +if("${BUILD_CPP_JOULES}") + target_compile_definitions("${PROJECT_NAME}" PRIVATE BUILD_CPP_JOULES) +endif() + # N.B. this is a no-op if BUILD_PYBIND11_PYBINDINGS is not turned on include(nwx_pybind11) nwx_add_pybind11_module( diff --git a/src/parallelzone/hardware/cpu/cpu.cpp b/src/parallelzone/hardware/cpu/cpu.cpp index 7adb8c2..f24c9ed 100644 --- a/src/parallelzone/hardware/cpu/cpu.cpp +++ b/src/parallelzone/hardware/cpu/cpu.cpp @@ -15,14 +15,17 @@ */ #include - +#include "energy_monitor.hpp" namespace parallelzone::hardware { typename CPU::profile_return_type CPU::profile_it_(task_type&& task) const { profile_information i; + EnergyMonitor monitor; + monitor.start(); const auto t1 = std::chrono::high_resolution_clock::now(); auto result = task(); const auto t2 = std::chrono::high_resolution_clock::now(); + monitor.stop(); i.wall_time = (t2 - t1); return std::make_pair(std::move(result), std::move(i)); } diff --git a/src/parallelzone/hardware/cpu/energy_monitor.hpp b/src/parallelzone/hardware/cpu/energy_monitor.hpp new file mode 100644 index 0000000..33f3356 --- /dev/null +++ b/src/parallelzone/hardware/cpu/energy_monitor.hpp @@ -0,0 +1,38 @@ +#pragma once +#ifdef BUILD_CPP_JOULES +#include + +namespace parallelzone::hardware { + +class EnergyMonitor{ +public: + using energy_type = long long; + bool is_active() { return true; } + void start() { m_tracker_.start(); } + + void stop() { + m_tracker_.stop(); + m_tracker_.calculate_energy(); + m_tracker_.print_energy(); + } + +private: + EnergyTracker m_tracker_; + +}; + +} + +#else +namespace parallelzone::hardware { + +class EnergyMonitor { +public: + bool is_active() { return false; } + void start() {} + void stop() {} +}; + +} + +#endif \ No newline at end of file diff --git a/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp b/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp index c5a86e2..769730d 100644 --- a/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp +++ b/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp @@ -16,6 +16,8 @@ #include "../../catch.hpp" #include +#include +#include using namespace parallelzone; From 462d93cb0934c226a5592a4690e8c61d0f237607 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 23 May 2025 17:17:44 +0000 Subject: [PATCH 2/2] Committing clang-format changes --- src/parallelzone/hardware/cpu/cpu.cpp | 4 +-- .../hardware/cpu/energy_monitor.hpp | 25 +++++++++++++++---- .../parallelzone/hardware/cpu/cpu.cpp | 4 +-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/parallelzone/hardware/cpu/cpu.cpp b/src/parallelzone/hardware/cpu/cpu.cpp index f24c9ed..e39b5e7 100644 --- a/src/parallelzone/hardware/cpu/cpu.cpp +++ b/src/parallelzone/hardware/cpu/cpu.cpp @@ -14,8 +14,8 @@ * limitations under the License. */ -#include #include "energy_monitor.hpp" +#include namespace parallelzone::hardware { typename CPU::profile_return_type CPU::profile_it_(task_type&& task) const { @@ -26,7 +26,7 @@ typename CPU::profile_return_type CPU::profile_it_(task_type&& task) const { auto result = task(); const auto t2 = std::chrono::high_resolution_clock::now(); monitor.stop(); - i.wall_time = (t2 - t1); + i.wall_time = (t2 - t1); return std::make_pair(std::move(result), std::move(i)); } diff --git a/src/parallelzone/hardware/cpu/energy_monitor.hpp b/src/parallelzone/hardware/cpu/energy_monitor.hpp index 33f3356..3053bca 100644 --- a/src/parallelzone/hardware/cpu/energy_monitor.hpp +++ b/src/parallelzone/hardware/cpu/energy_monitor.hpp @@ -1,10 +1,26 @@ +/* + * Copyright 2025 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once #ifdef BUILD_CPP_JOULES #include namespace parallelzone::hardware { -class EnergyMonitor{ +class EnergyMonitor { public: using energy_type = long long; bool is_active() { return true; } @@ -15,13 +31,12 @@ class EnergyMonitor{ m_tracker_.calculate_energy(); m_tracker_.print_energy(); } - + private: EnergyTracker m_tracker_; - }; -} +} // namespace parallelzone::hardware #else namespace parallelzone::hardware { @@ -33,6 +48,6 @@ class EnergyMonitor { void stop() {} }; -} +} // namespace parallelzone::hardware #endif \ No newline at end of file diff --git a/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp b/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp index 769730d..d402234 100644 --- a/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp +++ b/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp @@ -15,9 +15,9 @@ */ #include "../../catch.hpp" -#include -#include #include +#include +#include using namespace parallelzone;