Skip to content

Commit

Permalink
Remove manual dynamic memory usage (#2254)
Browse files Browse the repository at this point in the history
Replaced new[] with vectors
Made RunTimeInfos static
  • Loading branch information
payetvin authored Jul 17, 2024
1 parent 89ac6c8 commit 6c3646b
Show file tree
Hide file tree
Showing 50 changed files with 209 additions and 310 deletions.
2 changes: 1 addition & 1 deletion src/api/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ SimulationResults APIInternal::execute() const
study_->parameters.resultFormat, study_->folderOutput, ioQueueService, durationCollector);
SimulationObserver simulationObserver;
// Run the simulation
switch (study_->runtime->mode)
switch (study_->runtime.mode)
{
case Data::SimulationMode::Economy:
case Data::SimulationMode::Expansion:
Expand Down
1 change: 0 additions & 1 deletion src/libs/antares/study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ set(SRC_STUDY
include/antares/study/load-options.h
load-options.cpp
include/antares/study/runtime/runtime.h
include/antares/study/runtime/runtime.hxx
runtime/runtime.cpp
include/antares/study/runtime.h
include/antares/study/study.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class Progression final

public:
//! The total number of ticks to achieve
int maxTickCount;
unsigned maxTickCount;
//! The current number of ticks
std::atomic<int> tickCount;
std::atomic<unsigned> tickCount;
//! The last number of ticks, to reduce the log verbosity
int lastTickCount;
unsigned lastTickCount;
// Caption to use when displaying logs
// Example: 'year: 10000, task: thermal'
Yuni::CString<40, false> caption;
Expand All @@ -104,7 +104,7 @@ class Progression final
return *this;
}

Task& operator+=(int value)
Task& operator+=(unsigned value)
{
pPart.tickCount += value;
return *this;
Expand Down Expand Up @@ -138,7 +138,7 @@ class Progression final
** \internal The number of ticks should remain an `int` because
** we can not use unsigned atomic integer
*/
void add(uint year, Section section, int nbTicks);
void add(uint year, Section section, unsigned nbTicks);

void add(Section section, int nbTicks);

Expand Down
12 changes: 9 additions & 3 deletions src/libs/antares/study/include/antares/study/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
#include <vector>

#include <antares/mersenne-twister/mersenne-twister.h>
#include "antares/study/study.h"
#include <antares/study/parameters.h>

namespace Antares::Data
{

class Study;

enum RangeLimitsIndex
{
rangeBegin = 0,
Expand Down Expand Up @@ -139,8 +141,12 @@ class StudyRuntimeInfos
void checkThermalTSGeneration(Study& study);
}; // struct StudyRuntimeInfos

} // namespace Antares::Data
#ifdef NDEBUG
inline void StudyRangeLimits::checkIntegrity() const
{
}
#endif

#include "runtime.hxx"
} // namespace Antares::Data

#endif // __ANTARES_LIBS_STUDY_RUNTIME_RUNTIME_INFOS_H__
37 changes: 0 additions & 37 deletions src/libs/antares/study/include/antares/study/runtime/runtime.hxx

This file was deleted.

4 changes: 2 additions & 2 deletions src/libs/antares/study/include/antares/study/study.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <antares/correlation/correlation.h>
#include <antares/date/date.h>
#include <antares/study/runtime/runtime.h>
#include <antares/writer/i_writer.h>
#include "antares/antares/antares.h"
#include "antares/study/binding_constraint/BindingConstraintGroupRepository.h"
Expand Down Expand Up @@ -596,7 +597,7 @@ class Study: public Yuni::NonCopyable<Study>, public LayerData
**
** These informations are only needed when a study is processed.
*/
StudyRuntimeInfos* runtime = nullptr;
StudyRuntimeInfos runtime;

// Antares::Solver::Variable::State* state;

Expand Down Expand Up @@ -690,7 +691,6 @@ YString StudyCreateOutputPath(SimulationMode mode,
int64_t startTime);
} // namespace Antares::Data

#include "runtime.h"
#include "study.hxx"

#endif /* __ANTARES_LIBS_STUDY_STUDY_H__ */
2 changes: 1 addition & 1 deletion src/libs/antares/study/progression/progression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Progression::Task::Task(const Antares::Data::Study& study, uint year, Section se
assert(&pProgression);
}

void Progression::add(uint year, Section section, int nbTicks)
void Progression::add(uint year, Section section, unsigned nbTicks)
{
// This section is not thread-safe because always called before really launching
// the simulation
Expand Down
1 change: 1 addition & 0 deletions src/libs/antares/study/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "antares/study/runtime/runtime.h"

#include <antares/study/study.h>
#include <antares/utils/utils.h>
#include "antares/antares/fatal-error.h"
#include "antares/study/area/scratchpad.h"
Expand Down
43 changes: 19 additions & 24 deletions src/libs/antares/study/study.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ Study::~Study()

void Study::clear()
{
// Releasing runtime infos
FreeAndNil(runtime);
FreeAndNil(scenarioRules);
FreeAndNil(uiinfo);

Expand Down Expand Up @@ -504,9 +502,7 @@ void Study::getNumberOfCores(const bool forceParallel, const uint nbYearsParalle

bool Study::initializeRuntimeInfos()
{
delete runtime;
runtime = new StudyRuntimeInfos();
return runtime->loadFromStudy(*this);
return runtime.loadFromStudy(*this);
}

void Study::performTransformationsBeforeLaunchingSimulation()
Expand Down Expand Up @@ -1164,25 +1160,24 @@ struct TS final

void Study::initializeProgressMeter(bool tsGeneratorOnly)
{
uint years = tsGeneratorOnly ? 1 : (runtime->rangeLimits.year[rangeEnd] + 1);
assert(runtime);
uint years = tsGeneratorOnly ? 1 : (runtime.rangeLimits.year[rangeEnd] + 1);

int ticksPerYear = 0;
int ticksPerOutput = 0;
unsigned ticksPerYear = 0;
unsigned ticksPerOutput = 0;

if (not tsGeneratorOnly)
{
// One tick at the begining and 2 at the end of the year
// Output - Areas
ticksPerOutput += (int)areas.size();
ticksPerOutput += areas.size();
// Output - Links
ticksPerOutput += (int)runtime->interconnectionsCount();
ticksPerOutput += runtime.interconnectionsCount();
// Output - digest
ticksPerOutput += 1;
ticksPerYear = 1;
}

int n;
unsigned n;

for (uint y = 0; y != years; ++y)
{
Expand All @@ -1191,7 +1186,7 @@ void Study::initializeProgressMeter(bool tsGeneratorOnly)
n = parameters.nbTimeSeriesLoad * areas.size() * 365;
if (0 != (timeSeriesLoad & parameters.timeSeriesToArchive))
{
n += (int)areas.size();
n += areas.size();
}
progression.add(y, Solver::Progression::sectTSGLoad, n);
}
Expand All @@ -1200,7 +1195,7 @@ void Study::initializeProgressMeter(bool tsGeneratorOnly)
n = parameters.nbTimeSeriesSolar * areas.size() * 365;
if (0 != (timeSeriesSolar & parameters.timeSeriesToArchive))
{
n += (int)areas.size();
n += areas.size();
}
progression.add(y, Solver::Progression::sectTSGSolar, n);
}
Expand All @@ -1209,7 +1204,7 @@ void Study::initializeProgressMeter(bool tsGeneratorOnly)
n = parameters.nbTimeSeriesWind * areas.size() * 365;
if (0 != (timeSeriesWind & parameters.timeSeriesToArchive))
{
n += (int)areas.size();
n += areas.size();
}
progression.add(y, Solver::Progression::sectTSGWind, n);
}
Expand All @@ -1219,17 +1214,17 @@ void Study::initializeProgressMeter(bool tsGeneratorOnly)
n = parameters.nbTimeSeriesHydro;
if (0 != (timeSeriesHydro & parameters.timeSeriesToArchive))
{
n += (int)areas.size();
n += areas.size();
}
progression.add(y, Solver::Progression::sectTSGHydro, n);
}
if (TS<timeSeriesThermal>::IsNeeded(*this, y))
{
n = runtime->thermalPlantTotalCount;
n = runtime.thermalPlantTotalCount;
if (0 != (timeSeriesThermal & parameters.timeSeriesToArchive))
{
n += (int)runtime->thermalPlantTotalCount;
n += (int)runtime->thermalPlantTotalCountMustRun;
n += runtime.thermalPlantTotalCount;
n += runtime.thermalPlantTotalCountMustRun;
}
progression.add(y, Solver::Progression::sectTSGThermal, n);
}
Expand All @@ -1249,23 +1244,23 @@ void Study::initializeProgressMeter(bool tsGeneratorOnly)
n = 0;
if (0 != (timeSeriesLoad & parameters.exportTimeSeriesInInput))
{
n += (int)areas.size();
n += areas.size();
}
if (0 != (timeSeriesSolar & parameters.exportTimeSeriesInInput))
{
n += (int)areas.size();
n += areas.size();
}
if (0 != (timeSeriesWind & parameters.exportTimeSeriesInInput))
{
n += (int)areas.size();
n += areas.size();
}
if (0 != (timeSeriesHydro & parameters.exportTimeSeriesInInput))
{
n += (int)areas.size();
n += areas.size();
}
if (0 != (timeSeriesThermal & parameters.exportTimeSeriesInInput))
{
n += (int)areas.size();
n += areas.size();
}
if (n)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/study/study.importprepro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool Study::importTimeseriesIntoInput()
{
// Special case: some thermal clusters may force TS generation
const bool importThermal = parameters.haveToImport(timeSeriesThermal)
&& runtime->thermalTSRefresh;
&& runtime.thermalTSRefresh;
// Something to import ?
if ((parameters.exportTimeSeriesInInput && parameters.timeSeriesToGenerate) || importThermal)
{
Expand Down
2 changes: 1 addition & 1 deletion src/solver/application/ScenarioBuilderOwner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void Antares::Solver::ScenarioBuilderOwner::callScenarioBuilder()
// We will resize all matrix related to the time-series numbers
// This operation can be done once since the number of years is constant
// for a single simulation
study_.resizeAllTimeseriesNumbers(1 + study_.runtime->rangeLimits.year[Data::rangeEnd]);
study_.resizeAllTimeseriesNumbers(1 + study_.runtime.rangeLimits.year[Data::rangeEnd]);
if (not TimeSeriesNumbers::CheckNumberOfColumns(study_.areas))
{
throw FatalError(
Expand Down
3 changes: 1 addition & 2 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ void Application::readDataForTheStudy(Data::StudyLoadOptions& options)
writeComment(study);
}

// Runtime data dedicated for the solver
if (!study.initializeRuntimeInfos())
{
throw Error::RuntimeInfoInitialization();
Expand Down Expand Up @@ -391,7 +390,7 @@ void Application::execute()
pStudy->computePThetaInfForThermalClusters();

// Run the simulation
switch (pStudy->runtime->mode)
switch (pStudy->runtime.mode)
{
case Data::SimulationMode::Economy:
case Data::SimulationMode::Expansion:
Expand Down
4 changes: 2 additions & 2 deletions src/solver/simulation/adequacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ bool Adequacy::year(Progression::Task& progression,
state.resSpilled.zero();

auto nbAreas = study.areas.size();
auto& runtime = *(study.runtime);
auto& runtime = study.runtime;

for (uint i = 0; i != nbHoursInAWeek; ++i)
{
Expand Down Expand Up @@ -399,7 +399,7 @@ static std::vector<AvgExchangeResults*> retrieveBalance(

void Adequacy::simulationEnd()
{
if (!preproOnly && study.runtime->interconnectionsCount() > 0)
if (!preproOnly && study.runtime.interconnectionsCount() > 0)
{
auto balance = retrieveBalance(study, variables);
ComputeFlowQuad(study, pProblemesHebdo[0], balance, pNbWeeks);
Expand Down
Loading

0 comments on commit 6c3646b

Please sign in to comment.