Skip to content

Commit 636a40b

Browse files
authored
Merge pull request #1087 from PowerGridModel/feature/timer-using-codes
Refactor calculation-info: timer using codes
2 parents 01e2589 + 6c39ebe commit 636a40b

File tree

13 files changed

+203
-81
lines changed

13 files changed

+203
-81
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/common/calculation_info.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
#pragma once
66

7+
#include "logging.hpp"
8+
79
#include <cstddef>
810
#include <functional>
911
#include <map>
1012
#include <string>
1113

1214
namespace power_grid_model {
1315

14-
using CalculationInfo = std::map<std::string, double, std::less<>>;
16+
using CalculationInfo = std::map<LogEvent, double>;
1517

1618
} // namespace power_grid_model
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#pragma once
6+
7+
#include "common.hpp"
8+
9+
#include <string_view>
10+
11+
namespace power_grid_model {
12+
namespace common::logging {
13+
14+
enum class LogEvent : int16_t {
15+
unknown = -1,
16+
total = 0000, // TODO(mgovers): find other error code?
17+
build_model = 1000, // TODO(mgovers): find other error code?
18+
total_single_calculation_in_thread = 0100,
19+
total_batch_calculation_in_thread = 0200,
20+
copy_model = 1100,
21+
update_model = 1200,
22+
restore_model = 1201,
23+
scenario_exception = 1300,
24+
recover_from_bad = 1400,
25+
prepare = 2100,
26+
create_math_solver = 2210,
27+
math_calculation = 2200,
28+
math_solver = 2220,
29+
initialize_calculation = 2221,
30+
preprocess_measured_value = 2231, // TODO(mgovers): find other error code + make plural?
31+
prepare_matrix = 2222,
32+
prepare_matrix_including_prefactorization = 2232, // TODO(mgovers): find other error code
33+
prepare_matrices = 2242, // TODO(mgovers): find other error code
34+
initialize_voltages = 2223,
35+
calculate_rhs = 2224,
36+
prepare_lhs_rhs = 2244, // TODO(mgovers): find other error code
37+
solve_sparse_linear_equation = 2225,
38+
solve_sparse_linear_equation_prefactorized = 2235, // TODO(mgovers): find other error code
39+
iterate_unknown = 2226,
40+
calculate_math_result = 2227,
41+
produce_output = 3000,
42+
iterative_pf_solver_max_num_iter = 2246, // TODO(mgovers): find other error code
43+
max_num_iter = 2248, // TODO(mgovers): find other error code
44+
};
45+
46+
} // namespace common::logging
47+
48+
using common::logging::LogEvent;
49+
50+
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/common/timer.hpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "calculation_info.hpp"
88
#include "common.hpp"
9+
#include "logging.hpp"
910

1011
#include <chrono>
1112
#include <iomanip>
@@ -19,15 +20,12 @@ using Duration = std::chrono::duration<double>;
1920
class Timer {
2021
private:
2122
CalculationInfo* info_;
22-
int code_;
23-
std::string name_;
23+
LogEvent code_;
2424
Clock::time_point start_;
2525

2626
public:
27-
Timer() : info_(nullptr), code_{-1} {};
28-
29-
Timer(CalculationInfo& info, int code, std::string name)
30-
: info_{&info}, code_{code}, name_{std::move(name)}, start_{Clock::now()} {}
27+
Timer() : info_(nullptr), code_{LogEvent::unknown} {};
28+
Timer(CalculationInfo& info, LogEvent code) : info_{&info}, code_{code}, start_{Clock::now()} {}
3129

3230
Timer(Timer const&) = delete;
3331
Timer(Timer&&) = default;
@@ -40,7 +38,6 @@ class Timer {
4038
// Copy/move members
4139
info_ = timer.info_;
4240
code_ = timer.code_;
43-
name_ = std::move(timer.name_);
4441
start_ = timer.start_;
4542

4643
// Disable original timer
@@ -60,24 +57,10 @@ class Timer {
6057
if (info_ != nullptr) {
6158
auto const now = Clock::now();
6259
auto const duration = Duration(now - start_);
63-
info_->operator[](Timer::make_key(code_, name_)) += (double)duration.count();
60+
info_->operator[](code_) += static_cast<double>(duration.count());
6461
info_ = nullptr;
6562
}
6663
}
67-
68-
static std::string make_key(int code, std::string_view name) {
69-
std::stringstream ss;
70-
ss << std::setw(4) << std::setfill('0') << code << ".";
71-
auto key = ss.str();
72-
for (size_t i = 0, n = key.length() - 1; i < n; ++i) {
73-
if (key[i] == '0') {
74-
break;
75-
}
76-
key += "\t";
77-
}
78-
key += name;
79-
return key;
80-
}
8164
};
8265

8366
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/job_dispatch.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,22 @@ class JobDispatch {
6060
assert(n_scenarios <= narrow_cast<Idx>(exceptions.size()));
6161
CalculationInfo thread_info;
6262

63-
Timer t_total(thread_info, 0200, "Total batch calculation in thread");
63+
Timer t_total{thread_info, LogEvent::total_batch_calculation_in_thread};
6464

6565
auto const copy_adapter_functor = [&base_adapter, &thread_info]() {
66-
Timer const t_copy_adapter_functor(thread_info, 1100, "Copy model");
66+
Timer const t_copy_adapter_functor{thread_info, LogEvent::copy_model};
6767
return Adapter{base_adapter};
6868
};
6969

7070
auto adapter = copy_adapter_functor();
7171

7272
auto setup = [&adapter, &update_data, &thread_info](Idx scenario_idx) {
73-
Timer const t_update_model(thread_info, 1200, "Update model");
73+
Timer const t_update_model{thread_info, LogEvent::update_model};
7474
adapter.setup(update_data, scenario_idx);
7575
};
7676

7777
auto winddown = [&adapter, &thread_info]() {
78-
Timer const t_restore_model(thread_info, 1201, "Restore model");
78+
Timer const t_restore_model{thread_info, LogEvent::restore_model};
7979
adapter.winddown();
8080
};
8181

@@ -95,7 +95,7 @@ class JobDispatch {
9595
scenario_exception_handler(adapter, exceptions, thread_info), std::move(recover_from_bad));
9696

9797
for (Idx scenario_idx = start; scenario_idx < n_scenarios; scenario_idx += stride) {
98-
Timer const t_total_single(thread_info, 0100, "Total single calculation in thread");
98+
Timer const t_total_single{thread_info, LogEvent::total_single_calculation_in_thread};
9999
calculate_scenario(scenario_idx);
100100
}
101101

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/calculation_info.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
namespace power_grid_model::main_core {
1212

1313
inline CalculationInfo& merge_into(CalculationInfo& destination, CalculationInfo const& source) {
14-
static auto const key = Timer::make_key(2226, "Max number of iterations");
14+
static constexpr auto key =
15+
LogEvent::iterative_pf_solver_max_num_iter; // TODO(mgovers) also add LogEvent::max_num_iter; this is a bug
16+
// in main
1517
for (auto const& [k, v] : source) {
1618
if (k == key) {
1719
destination[k] = std::max(destination[k], v);

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,14 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
423423
calculation_info_ = CalculationInfo{};
424424
// prepare
425425
auto const& input = [this, prepare_input_ = std::forward<PrepareInputFn>(prepare_input)] {
426-
Timer const timer(calculation_info_, 2100, "Prepare");
426+
Timer const timer{calculation_info_, LogEvent::prepare};
427427
prepare_solvers<sym>();
428428
assert(is_topology_up_to_date_ && is_parameter_up_to_date<sym>());
429429
return prepare_input_(n_math_solvers_);
430430
}();
431431
// calculate
432432
return [this, &input, solve_ = std::forward<SolveFn>(solve)] {
433-
Timer const timer(calculation_info_, 2200, "Math Calculation");
433+
Timer const timer{calculation_info_, LogEvent::math_calculation};
434434
auto& solvers = get_solvers<sym>();
435435
auto& y_bus_vec = get_y_bus<sym>();
436436
std::vector<SolverOutputType> solver_output;
@@ -595,7 +595,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
595595
}
596596
};
597597

598-
Timer const t_output(calculation_info_, 3000, "Produce output");
598+
Timer const t_output{calculation_info_, LogEvent::produce_output};
599599
main_core::utils::run_functor_with_all_types_return_void<ComponentType...>(output_func);
600600
}
601601

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/iterative_linear_se_solver.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,22 @@ template <symmetry_tag sym_type> class IterativeLinearSESolver {
9393
output.bus_injection.resize(n_bus_);
9494
double max_dev = std::numeric_limits<double>::max();
9595

96-
main_timer = Timer(calculation_info, 2220, "Math solver");
96+
main_timer = Timer{calculation_info, LogEvent::math_solver};
9797

9898
// preprocess measured value
99-
sub_timer = Timer(calculation_info, 2221, "Pre-process measured value");
99+
sub_timer = Timer{calculation_info, LogEvent::preprocess_measured_value};
100100
MeasuredValues<sym> const measured_values{y_bus.shared_topology(), input};
101101
auto const observability_result =
102102
observability_check(measured_values, y_bus.math_topology(), y_bus.y_bus_structure());
103103

104104
// prepare matrix
105-
sub_timer = Timer(calculation_info, 2222, "Prepare matrix, including pre-factorization");
105+
sub_timer = Timer{calculation_info, LogEvent::prepare_matrix_including_prefactorization};
106106
prepare_matrix(y_bus, measured_values);
107107
// prefactorize
108108
sparse_solver_.prefactorize(data_gain_, perm_, observability_result.use_perturbation());
109109

110110
// initialize voltage with initial angle
111-
sub_timer = Timer(calculation_info, 2223, "Initialize voltages");
111+
sub_timer = Timer{calculation_info, LogEvent::initialize_voltages}; // TODO(mgovers): make scoped subtimers
112112
RealValue<sym> const mean_angle_shift = measured_values.mean_angle_shift();
113113
for (Idx bus = 0; bus != n_bus_; ++bus) {
114114
output.u[bus] = exp(1.0i * (mean_angle_shift + math_topo_->phase_shift[bus]));
@@ -120,25 +120,25 @@ template <symmetry_tag sym_type> class IterativeLinearSESolver {
120120
if (num_iter++ == max_iter) {
121121
throw IterationDiverge{max_iter, max_dev, err_tol};
122122
}
123-
sub_timer = Timer(calculation_info, 2224, "Calculate rhs");
123+
sub_timer = Timer{calculation_info, LogEvent::calculate_rhs};
124124
prepare_rhs(y_bus, measured_values, output.u);
125125
// solve with prefactorization
126-
sub_timer = Timer(calculation_info, 2225, "Solve sparse linear equation (pre-factorized)");
126+
sub_timer = Timer{calculation_info, LogEvent::solve_sparse_linear_equation_prefactorized};
127127
sparse_solver_.solve_with_prefactorized_matrix(data_gain_, perm_, x_rhs_, x_rhs_);
128-
sub_timer = Timer(calculation_info, 2226, "Iterate unknown");
128+
sub_timer = Timer{calculation_info, LogEvent::iterate_unknown};
129129
max_dev = iterate_unknown(output.u, measured_values.has_angle());
130130
};
131131

132132
// calculate math result
133-
sub_timer = Timer(calculation_info, 2227, "Calculate math result");
133+
sub_timer = Timer{calculation_info, LogEvent::calculate_math_result};
134134
detail::calculate_se_result<sym>(y_bus, measured_values, output);
135135

136136
// Manually stop timers to avoid "Max number of iterations" to be included in the timing.
137137
sub_timer.stop();
138138
main_timer.stop();
139139

140-
auto const key = Timer::make_key(2228, "Max number of iterations");
141-
calculation_info[key] = std::max(calculation_info[key], static_cast<double>(num_iter));
140+
calculation_info[LogEvent::max_num_iter] =
141+
std::max(calculation_info[LogEvent::max_num_iter], static_cast<double>(num_iter));
142142

143143
return output;
144144
}

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/iterative_pf_solver.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ template <symmetry_tag sym, typename DerivedSolver> class IterativePFSolver {
3434
output.u.resize(n_bus_);
3535
double max_dev = std::numeric_limits<double>::infinity();
3636

37-
Timer main_timer{calculation_info, 2220, "Math solver"};
37+
Timer main_timer{calculation_info, LogEvent::math_solver};
3838

3939
// initialize
4040
{
41-
Timer const sub_timer{calculation_info, 2221, "Initialize calculation"};
41+
Timer const sub_timer{calculation_info, LogEvent::initialize_calculation};
4242
// Further initialization specific to the derived solver
4343
derived_solver.initialize_derived_solver(y_bus, input, output);
4444
}
@@ -52,31 +52,31 @@ template <symmetry_tag sym, typename DerivedSolver> class IterativePFSolver {
5252
}
5353
{
5454
// Prepare the matrices of linear equations to be solved
55-
Timer const sub_timer{calculation_info, 2222, "Prepare the matrices"};
55+
Timer const sub_timer{calculation_info, LogEvent::prepare_matrices};
5656
derived_solver.prepare_matrix_and_rhs(y_bus, input, output.u);
5757
}
5858
{
5959
// Solve the linear equations
60-
Timer const sub_timer{calculation_info, 2223, "Solve sparse linear equation"};
60+
Timer const sub_timer{calculation_info, LogEvent::solve_sparse_linear_equation};
6161
derived_solver.solve_matrix();
6262
}
6363
{
6464
// Calculate maximum deviation of voltage at any bus
65-
Timer const sub_timer{calculation_info, 2224, "Iterate unknown"};
65+
Timer const sub_timer{calculation_info, LogEvent::iterate_unknown};
6666
max_dev = derived_solver.iterate_unknown(output.u);
6767
}
6868
}
6969

7070
// calculate math result
7171
{
72-
Timer const sub_timer{calculation_info, 2225, "Calculate math result"};
72+
Timer const sub_timer{calculation_info, LogEvent::calculate_math_result};
7373
calculate_result(y_bus, input, output);
7474
}
7575
// Manually stop timers to avoid "Max number of iterations" to be included in the timing.
7676
main_timer.stop();
7777

78-
auto const key = Timer::make_key(2226, "Max number of iterations");
79-
calculation_info[key] = std::max(calculation_info[key], static_cast<double>(num_iter));
78+
calculation_info[LogEvent::iterative_pf_solver_max_num_iter] =
79+
std::max(calculation_info[LogEvent::iterative_pf_solver_max_num_iter], static_cast<double>(num_iter));
8080

8181
return output;
8282
}

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/linear_pf_solver.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,26 @@ template <symmetry_tag sym_type> class LinearPFSolver {
6464

6565
SolverOutput<sym> run_power_flow(YBus<sym> const& y_bus, PowerFlowInput<sym> const& input,
6666
CalculationInfo& calculation_info) {
67+
using enum LogEvent;
68+
6769
// output
6870
SolverOutput<sym> output;
6971
output.u.resize(n_bus_);
7072

71-
Timer const main_timer(calculation_info, 2220, "Math solver");
73+
Timer const main_timer{calculation_info, math_solver};
7274

7375
// prepare matrix
74-
Timer sub_timer(calculation_info, 2221, "Prepare matrix");
76+
Timer sub_timer{calculation_info, prepare_matrix};
7577
detail::copy_y_bus<sym>(y_bus, mat_data_);
7678
prepare_matrix_and_rhs(y_bus, input, output);
7779

7880
// solve
7981
// u vector will have I_injection for slack bus for now
80-
sub_timer = Timer(calculation_info, 2222, "Solve sparse linear equation");
82+
sub_timer = Timer{calculation_info, solve_sparse_linear_equation};
8183
sparse_solver_.prefactorize_and_solve(mat_data_, perm_, output.u, output.u);
8284

8385
// calculate math result
84-
sub_timer = Timer(calculation_info, 2223, "Calculate math result");
86+
sub_timer = Timer{calculation_info, calculate_math_result};
8587
calculate_result(y_bus, input, output);
8688

8789
// output

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/math_solver.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
8585

8686
// construct model if needed
8787
if (!iec60909_sc_solver_.has_value()) {
88-
Timer const timer(calculation_info, 2210, "Create math solver");
88+
Timer const timer{calculation_info, LogEvent::create_math_solver};
8989
iec60909_sc_solver_.emplace(y_bus, topo_ptr_);
9090
}
9191

@@ -119,7 +119,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
119119
SolverOutput<sym> run_power_flow_newton_raphson(PowerFlowInput<sym> const& input, double err_tol, Idx max_iter,
120120
CalculationInfo& calculation_info, YBus<sym> const& y_bus) {
121121
if (!newton_raphson_pf_solver_.has_value()) {
122-
Timer const timer(calculation_info, 2210, "Create math solver");
122+
Timer const timer{calculation_info, LogEvent::create_math_solver};
123123
newton_raphson_pf_solver_.emplace(y_bus, topo_ptr_);
124124
}
125125
return newton_raphson_pf_solver_.value().run_power_flow(y_bus, input, err_tol, max_iter, calculation_info);
@@ -128,7 +128,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
128128
SolverOutput<sym> run_power_flow_linear(PowerFlowInput<sym> const& input, double /* err_tol */, Idx /* max_iter */,
129129
CalculationInfo& calculation_info, YBus<sym> const& y_bus) {
130130
if (!linear_pf_solver_.has_value()) {
131-
Timer const timer(calculation_info, 2210, "Create math solver");
131+
Timer const timer{calculation_info, LogEvent::create_math_solver};
132132
linear_pf_solver_.emplace(y_bus, topo_ptr_);
133133
}
134134
return linear_pf_solver_.value().run_power_flow(y_bus, input, calculation_info);
@@ -137,7 +137,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
137137
SolverOutput<sym> run_power_flow_iterative_current(PowerFlowInput<sym> const& input, double err_tol, Idx max_iter,
138138
CalculationInfo& calculation_info, YBus<sym> const& y_bus) {
139139
if (!iterative_current_pf_solver_.has_value()) {
140-
Timer const timer(calculation_info, 2210, "Create math solver");
140+
Timer const timer{calculation_info, LogEvent::create_math_solver};
141141
iterative_current_pf_solver_.emplace(y_bus, topo_ptr_);
142142
}
143143
return iterative_current_pf_solver_.value().run_power_flow(y_bus, input, err_tol, max_iter, calculation_info);
@@ -155,7 +155,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
155155
YBus<sym> const& y_bus) {
156156
// construct model if needed
157157
if (!iterative_linear_se_solver_.has_value()) {
158-
Timer const timer(calculation_info, 2210, "Create math solver");
158+
Timer const timer{calculation_info, LogEvent::create_math_solver};
159159
iterative_linear_se_solver_.emplace(y_bus, topo_ptr_);
160160
}
161161

@@ -169,7 +169,7 @@ template <symmetry_tag sym> class MathSolver : public MathSolverBase<sym> {
169169
YBus<sym> const& y_bus) {
170170
// construct model if needed
171171
if (!newton_raphson_se_solver_.has_value()) {
172-
Timer const timer(calculation_info, 2210, "Create math solver");
172+
Timer const timer{calculation_info, LogEvent::create_math_solver};
173173
newton_raphson_se_solver_.emplace(y_bus, topo_ptr_);
174174
}
175175

0 commit comments

Comments
 (0)