Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
system_metrics.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19#include "shambase/memory.hpp"
20#include "shamcmdopt/env.hpp"
21#include <memory>
22#include <optional>
23
24inline std::optional<std::string> SHAM_SYSTEM_METRICS_REPORTER = shamcmdopt::getenv_str_register(
25 "SHAM_SYSTEM_METRICS_REPORTER", "The name of the system metrics reporter to use");
26
27namespace shamsys {
28
30 public:
31 virtual ~ISystemMetricReporter() = default;
32
33 virtual std::optional<f64> get_rank_energy_consummed() = 0;
34 virtual std::optional<f64> get_gpu_energy_consummed() = 0;
35 virtual std::optional<f64> get_cpu_energy_consummed() = 0;
36 virtual std::optional<f64> get_dram_energy_consummed() = 0;
37
38 virtual bool support_rank_energy_consummed() = 0;
39 virtual bool support_gpu_energy_consummed() = 0;
40 virtual bool support_cpu_energy_consummed() = 0;
41 virtual bool support_dram_energy_consummed() = 0;
42 };
43
44 std::unique_ptr<ISystemMetricReporter> &current_reporter();
45
46 inline std::optional<f64> get_rank_energy_consummed() {
47 return shambase::get_check_ref(current_reporter()).get_rank_energy_consummed();
48 }
49
50 inline bool support_rank_energy_consummed() {
51 return shambase::get_check_ref(current_reporter()).support_rank_energy_consummed();
52 }
53
54 inline std::optional<f64> get_gpu_energy_consummed() {
55 return shambase::get_check_ref(current_reporter()).get_gpu_energy_consummed();
56 }
57
58 inline bool support_gpu_energy_consummed() {
59 return shambase::get_check_ref(current_reporter()).support_gpu_energy_consummed();
60 }
61
62 inline std::optional<f64> get_cpu_energy_consummed() {
63 return shambase::get_check_ref(current_reporter()).get_cpu_energy_consummed();
64 }
65
66 inline bool support_cpu_energy_consummed() {
67 return shambase::get_check_ref(current_reporter()).support_cpu_energy_consummed();
68 }
69
70 inline std::optional<f64> get_dram_energy_consummed() {
71 return shambase::get_check_ref(current_reporter()).get_dram_energy_consummed();
72 }
73
74 inline bool support_dram_energy_consummed() {
75 return shambase::get_check_ref(current_reporter()).support_dram_energy_consummed();
76 }
77
79 f64 wall_time;
80 std::optional<f64> rank_energy_consummed;
81 std::optional<f64> gpu_energy_consummed;
82 std::optional<f64> cpu_energy_consummed;
83 std::optional<f64> dram_energy_consummed;
84 };
85
86 SystemMetrics get_system_metrics(bool barrier = true);
87
88 std::vector<SystemMetrics> gather_rank_metrics(const SystemMetrics &input);
89
90 SystemMetrics aggregate_rank_metrics(const std::vector<SystemMetrics> &input);
91
93 std::string wall_time;
94 std::optional<std::string> rank_energy_consummed;
95 std::optional<std::string> gpu_energy_consummed;
96 std::optional<std::string> cpu_energy_consummed;
97 std::optional<std::string> dram_energy_consummed;
98 std::optional<std::string> rank_power;
99 std::optional<std::string> gpu_power;
100 std::optional<std::string> cpu_power;
101 std::optional<std::string> dram_power;
102 };
103
106
107 inline SystemMetrics operator-(const SystemMetrics &lhs, const SystemMetrics &rhs) {
108 auto optional_sub = [](const std::optional<f64> &lhs,
109 const std::optional<f64> &rhs) -> std::optional<f64> {
110 return (lhs.has_value() && rhs.has_value())
111 ? std::optional<f64>(lhs.value() - rhs.value())
112 : std::nullopt;
113 };
114 return SystemMetrics{
115 lhs.wall_time - rhs.wall_time,
116 optional_sub(lhs.rank_energy_consummed, rhs.rank_energy_consummed),
117 optional_sub(lhs.gpu_energy_consummed, rhs.gpu_energy_consummed),
118 optional_sub(lhs.cpu_energy_consummed, rhs.cpu_energy_consummed),
119 optional_sub(lhs.dram_energy_consummed, rhs.dram_energy_consummed)};
120 }
121
122 // Returns true if the current reporter is not a NoopSystemMetricReporter (defined below).
123 bool has_reporter();
124
125} // namespace shamsys
double f64
Alias for double.
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:110
std::optional< std::string > getenv_str_register(const char *env_var, std::string desc)
Get the content of the environment variable if it exist and register it documentation.
Definition env.hpp:70
namespace for the system handling
FormattedSystemMetrics format_system_metrics(const SystemMetrics &input)
Only to be used on deltas, not the raw one.
STL namespace.