Skip to content

Commit

Permalink
Properly set common data in API output when using playlist (#2364)
Browse files Browse the repository at this point in the history
Common data are only computed once.
In the current implementation this computation is done only for the week
1 of the year 1. However when using playlist the year 1 may not exists

This PR fix this issue by computing common data is no data exist yet.

---------

Co-authored-by: Florian Omnès <[email protected]>
  • Loading branch information
JasonMarechal25 and flomnes authored Sep 4, 2024
1 parent 9accb36 commit 69a8b12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/api/SimulationObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ namespace
auto translate(const PROBLEME_HEBDO& problemeHebdo,
std::string_view name,
const Solver::HebdoProblemToLpsTranslator& translator,
const unsigned int year,
const unsigned int week)
std::once_flag& flag)
{
auto weekly_data = translator.translate(problemeHebdo.ProblemeAResoudre.get(), name);
Solver::ConstantDataFromAntares common_data;
if (year == 1 && week == 1)
std::optional<Solver::ConstantDataFromAntares> common_data;
bool translateCommonData = false;
std::call_once(flag, [&translateCommonData]() { translateCommonData = true; });
if (translateCommonData)
{
common_data = translator.commonProblemData(problemeHebdo.ProblemeAResoudre.get());
}
Expand All @@ -56,18 +57,18 @@ void SimulationObserver::notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo,
const unsigned int year = problemeHebdo.year + 1;
const unsigned int week = problemeHebdo.weekInTheYear + 1;
// common_data and weekly_data computed before the mutex lock to prevent blocking the thread
auto [common_data, weekly_data] = translate(problemeHebdo, name, translator, year, week);
std::lock_guard lock(mutex_);
if (year == 1 && week == 1)
auto [common_data, weekly_data] = translate(problemeHebdo, name, translator, flag_);
std::lock_guard lock(lps_mutex_);
if (common_data)
{
lps_.setConstantData(common_data);
lps_.setConstantData(common_data.value());
}
lps_.addWeeklyData({year, week}, weekly_data);
}

Solver::LpsFromAntares&& SimulationObserver::acquireLps() noexcept
{
std::lock_guard lock(mutex_);
std::lock_guard lock(lps_mutex_);
return std::move(lps_);
}
} // namespace Antares::API
3 changes: 2 additions & 1 deletion src/api/private/SimulationObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class SimulationObserver: public Solver::Simulation::ISimulationObserver

private:
Solver::LpsFromAntares lps_;
std::mutex mutex_;
mutable std::mutex lps_mutex_;
mutable std::once_flag flag_;
};

} // namespace Antares::API

0 comments on commit 69a8b12

Please sign in to comment.