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..e39b5e7 100644 --- a/src/parallelzone/hardware/cpu/cpu.cpp +++ b/src/parallelzone/hardware/cpu/cpu.cpp @@ -14,16 +14,19 @@ * limitations under the License. */ +#include "energy_monitor.hpp" #include - 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(); - i.wall_time = (t2 - t1); + 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..3053bca --- /dev/null +++ b/src/parallelzone/hardware/cpu/energy_monitor.hpp @@ -0,0 +1,53 @@ +/* + * 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 { +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_; +}; + +} // namespace parallelzone::hardware + +#else +namespace parallelzone::hardware { + +class EnergyMonitor { +public: + bool is_active() { return false; } + void start() {} + 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 c5a86e2..d402234 100644 --- a/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp +++ b/tests/cxx/unit_tests/parallelzone/hardware/cpu/cpu.cpp @@ -15,6 +15,8 @@ */ #include "../../catch.hpp" +#include +#include #include using namespace parallelzone;