Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class JobAdapter<MainModel, ComponentList<ComponentType...>>
}

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

void thread_safe_add_calculation_info_impl(CalculationInfo const& info) {
std::lock_guard const lock{calculation_info_mutex_};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class JobDispatch {
static BatchParameter batch_calculation(Adapter& adapter, ResultDataset const& result_data,
UpdateDataset const& update_data, Idx threading = sequential) {
if (update_data.empty()) {
adapter.reset_calculation_info();
adapter.calculate(result_data);
return BatchParameter{};
}
Expand All @@ -38,6 +39,7 @@ class JobDispatch {
}

// calculate once to cache, ignore results
adapter.reset_calculation_info();
adapter.cache_calculate();

// error messages
Expand Down Expand Up @@ -86,6 +88,7 @@ class JobDispatch {
};

auto run = [&adapter, &result_data, &thread_info](Idx scenario_idx) {
adapter.reset_calculation_info();
adapter.calculate(result_data, scenario_idx);
main_core::merge_into(thread_info, adapter.get_calculation_info());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ template <typename Adapter> class JobInterface {
{
return static_cast<const Adapter*>(this)->get_calculation_info_impl();
}
void reset_calculation_info()
requires requires(Adapter& adapter) { // NOSONAR
{ adapter.reset_calculation_info_impl() } -> std::same_as<void>;
}
{
static_cast<Adapter*>(this)->reset_calculation_info_impl();
}

void thread_safe_add_calculation_info(CalculationInfo const& info)
requires requires(Adapter& adapter) { // NOSONAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
using sym = typename SolverOutputType::sym;

assert(construction_complete_);
calculation_info_ = CalculationInfo{};
// prepare
auto const& input = [this, prepare_input_ = std::forward<PrepareInputFn>(prepare_input)] {
Timer const timer{calculation_info_, LogEvent::prepare};
Expand Down Expand Up @@ -558,6 +557,8 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
assert(construction_complete_);
main_core::merge_into(calculation_info_, info);
}
void reset_calculation_info() { calculation_info_ = CalculationInfo{}; }

auto const& state() const {
assert(construction_complete_);
return state_;
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark_cpp/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::string make_key(LogEvent code) {
}
key += "\t";
}
key += common::logging::to_string(code);
key += to_string(code);
return key;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/cpp_unit_tests/test_job_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ struct CallCounter {
std::atomic<Idx> setup_calls{};
std::atomic<Idx> winddown_calls{};
std::atomic<Idx> thread_safe_add_calculation_info_calls{};
std::atomic<Idx> reset_calculation_info_calls{};

void reset_counters() {
calculate_calls = 0;
cache_calculate_calls = 0;
setup_calls = 0;
winddown_calls = 0;
thread_safe_add_calculation_info_calls = 0;
reset_calculation_info_calls = 0;
}
};

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

private:
friend class JobInterface<JobAdapterMock>;
Expand All @@ -77,6 +80,7 @@ class JobAdapterMock : public JobInterface<JobAdapterMock> {
void thread_safe_add_calculation_info_impl(CalculationInfo const& /*info*/) const {
++(counter_->thread_safe_add_calculation_info_calls);
}
void reset_calculation_info_impl() const { ++(counter_->reset_calculation_info_calls); }
};

class SomeTestException : public std::runtime_error {
Expand All @@ -102,6 +106,8 @@ TEST_CASE("Test job dispatch logic") {
CHECK(expected_result == actual_result);
CHECK(adapter.get_calculate_counter() == 1);
CHECK(adapter.get_cache_calculate_counter() == 0); // no cache calculation in this case
CHECK(adapter.get_reset_calculation_info_counter() ==
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
}
SUBCASE("No scenarios") {
bool const has_data = true;
Expand All @@ -113,6 +119,8 @@ TEST_CASE("Test job dispatch logic") {
// no calculations should be done
CHECK(adapter.get_calculate_counter() == 0);
CHECK(adapter.get_cache_calculate_counter() == 0);
CHECK(adapter.get_reset_calculation_info_counter() ==
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
}
SUBCASE("With scenarios and update data") {
bool const has_data = true;
Expand All @@ -125,6 +133,8 @@ TEST_CASE("Test job dispatch logic") {
// n_scenarios calculations should be done as we run sequentially
CHECK(adapter.get_calculate_counter() == n_scenarios);
CHECK(adapter.get_cache_calculate_counter() == 1); // cache calculation is done
CHECK(adapter.get_reset_calculation_info_counter() ==
adapter.get_calculate_counter() + adapter.get_cache_calculate_counter());
}
}
SUBCASE("Test single_thread_job") {
Expand All @@ -151,6 +161,8 @@ TEST_CASE("Test job dispatch logic") {
CHECK(adapter_.get_winddown_counter() == expected_calls);
CHECK(adapter_.get_calculate_counter() == expected_calls);
CHECK(adapter_.get_thread_safe_add_calculation_info_counter() == 1); // always called once
CHECK(adapter_.get_reset_calculation_info_counter() ==
adapter_.get_calculate_counter() + adapter_.get_cache_calculate_counter());
};

adapter.prepare_job_dispatch(update_data); // replicate preparation step from batch_calculation
Expand Down
Loading