Skip to content

Commit 2c9ebd6

Browse files
authored
Merge pull request #1094 from PowerGridModel/feature/migrate-reset-calculation-info
Clean-up main model: migrate reset calculation info to job dispatch
2 parents 636a40b + e0eaa37 commit 2c9ebd6

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/job_adapter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
136136
}
137137

138138
CalculationInfo get_calculation_info_impl() const { return model_reference_.get().calculation_info(); }
139+
void reset_calculation_info_impl() { model_reference_.get().reset_calculation_info(); }
139140

140141
void thread_safe_add_calculation_info_impl(CalculationInfo const& info) {
141142
std::lock_guard const lock{calculation_info_mutex_};

power_grid_model_c/power_grid_model/include/power_grid_model/job_dispatch.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class JobDispatch {
2424
static BatchParameter batch_calculation(Adapter& adapter, ResultDataset const& result_data,
2525
UpdateDataset const& update_data, Idx threading = sequential) {
2626
if (update_data.empty()) {
27+
adapter.reset_calculation_info();
2728
adapter.calculate(result_data);
2829
return BatchParameter{};
2930
}
@@ -38,6 +39,7 @@ class JobDispatch {
3839
}
3940

4041
// calculate once to cache, ignore results
42+
adapter.reset_calculation_info();
4143
adapter.cache_calculate();
4244

4345
// error messages
@@ -86,6 +88,7 @@ class JobDispatch {
8688
};
8789

8890
auto run = [&adapter, &result_data, &thread_info](Idx scenario_idx) {
91+
adapter.reset_calculation_info();
8992
adapter.calculate(result_data, scenario_idx);
9093
main_core::merge_into(thread_info, adapter.get_calculation_info());
9194
};

power_grid_model_c/power_grid_model/include/power_grid_model/job_interface.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ template <typename Adapter> class JobInterface {
6868
{
6969
return static_cast<const Adapter*>(this)->get_calculation_info_impl();
7070
}
71+
void reset_calculation_info()
72+
requires requires(Adapter& adapter) { // NOSONAR
73+
{ adapter.reset_calculation_info_impl() } -> std::same_as<void>;
74+
}
75+
{
76+
static_cast<Adapter*>(this)->reset_calculation_info_impl();
77+
}
7178

7279
void thread_safe_add_calculation_info(CalculationInfo const& info)
7380
requires requires(Adapter& adapter) { // NOSONAR

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
420420
using sym = typename SolverOutputType::sym;
421421

422422
assert(construction_complete_);
423-
calculation_info_ = CalculationInfo{};
424423
// prepare
425424
auto const& input = [this, prepare_input_ = std::forward<PrepareInputFn>(prepare_input)] {
426425
Timer const timer{calculation_info_, LogEvent::prepare};
@@ -558,6 +557,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
558557
assert(construction_complete_);
559558
main_core::merge_into(calculation_info_, info);
560559
}
560+
void reset_calculation_info() { calculation_info_ = CalculationInfo{}; }
561+
561562
auto const& state() const {
562563
assert(construction_complete_);
563564
return state_;

tests/benchmark_cpp/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ std::string make_key(LogEvent code) {
9999
}
100100
key += "\t";
101101
}
102-
key += common::logging::to_string(code);
102+
key += to_string(code);
103103
return key;
104104
}
105105

tests/cpp_unit_tests/test_job_dispatch.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ struct CallCounter {
3333
std::atomic<Idx> setup_calls{};
3434
std::atomic<Idx> winddown_calls{};
3535
std::atomic<Idx> thread_safe_add_calculation_info_calls{};
36+
std::atomic<Idx> reset_calculation_info_calls{};
3637

3738
void reset_counters() {
3839
calculate_calls = 0;
3940
cache_calculate_calls = 0;
4041
setup_calls = 0;
4142
winddown_calls = 0;
4243
thread_safe_add_calculation_info_calls = 0;
44+
reset_calculation_info_calls = 0;
4345
}
4446
};
4547

@@ -60,6 +62,7 @@ class JobAdapterMock : public JobInterface<JobAdapterMock> {
6062
Idx get_thread_safe_add_calculation_info_counter() const {
6163
return counter_->thread_safe_add_calculation_info_calls;
6264
}
65+
Idx get_reset_calculation_info_counter() const { return counter_->reset_calculation_info_calls; }
6366

6467
private:
6568
friend class JobInterface<JobAdapterMock>;
@@ -77,6 +80,7 @@ class JobAdapterMock : public JobInterface<JobAdapterMock> {
7780
void thread_safe_add_calculation_info_impl(CalculationInfo const& /*info*/) const {
7881
++(counter_->thread_safe_add_calculation_info_calls);
7982
}
83+
void reset_calculation_info_impl() const { ++(counter_->reset_calculation_info_calls); }
8084
};
8185

8286
class SomeTestException : public std::runtime_error {
@@ -102,6 +106,8 @@ TEST_CASE("Test job dispatch logic") {
102106
CHECK(expected_result == actual_result);
103107
CHECK(adapter.get_calculate_counter() == 1);
104108
CHECK(adapter.get_cache_calculate_counter() == 0); // no cache calculation in this case
109+
CHECK(adapter.get_reset_calculation_info_counter() ==
110+
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
105111
}
106112
SUBCASE("No scenarios") {
107113
bool const has_data = true;
@@ -113,6 +119,8 @@ TEST_CASE("Test job dispatch logic") {
113119
// no calculations should be done
114120
CHECK(adapter.get_calculate_counter() == 0);
115121
CHECK(adapter.get_cache_calculate_counter() == 0);
122+
CHECK(adapter.get_reset_calculation_info_counter() ==
123+
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
116124
}
117125
SUBCASE("With scenarios and update data") {
118126
bool const has_data = true;
@@ -125,6 +133,8 @@ TEST_CASE("Test job dispatch logic") {
125133
// n_scenarios calculations should be done as we run sequentially
126134
CHECK(adapter.get_calculate_counter() == n_scenarios);
127135
CHECK(adapter.get_cache_calculate_counter() == 1); // cache calculation is done
136+
CHECK(adapter.get_reset_calculation_info_counter() ==
137+
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
128138
}
129139
}
130140
SUBCASE("Test single_thread_job") {
@@ -151,6 +161,8 @@ TEST_CASE("Test job dispatch logic") {
151161
CHECK(adapter_.get_winddown_counter() == expected_calls);
152162
CHECK(adapter_.get_calculate_counter() == expected_calls);
153163
CHECK(adapter_.get_thread_safe_add_calculation_info_counter() == 1); // always called once
164+
CHECK(adapter_.get_reset_calculation_info_counter() ==
165+
adapter_.get_calculate_counter() + adapter_.get_cache_calculate_counter());
154166
};
155167

156168
adapter.prepare_job_dispatch(update_data); // replicate preparation step from batch_calculation

0 commit comments

Comments
 (0)