Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
BendersMPI.h
1#pragma once
2
3#include "antares-xpansion/benders/benders_core/BendersBase.h"
4#include "antares-xpansion/benders/benders_core/BendersStructsDatas.h"
5#include "antares-xpansion/benders/benders_core/SubproblemCut.h"
6#include "antares-xpansion/benders/benders_core/SubproblemWorker.h"
7#include "antares-xpansion/benders/benders_core/Worker.h"
8#include "antares-xpansion/benders/benders_core/WorkerMaster.h"
9#include "antares-xpansion/helpers/ArchiveReader.h"
10#include "antares-xpansion/helpers/Timer.h"
11#include "antares-xpansion/xpansion_interfaces/ILogger.h"
12#include "antares-xpansion/xpansion_interfaces/LoggerUtils.h"
13#include "common_mpi.h"
14
19class BendersMpi : public BendersBase {
20 public:
21 ~BendersMpi() override = default;
22 BendersMpi(BendersBaseOptions const &options, Logger logger, Writer writer,
23 mpi::environment &env, mpi::communicator &world,
24 std::shared_ptr<MathLoggerDriver> mathLoggerDriver);
25
26 void launch() override;
27 std::string BendersName() const override { return "Benders mpi"; }
28 const unsigned int rank_0 = 0;
29
30 protected:
31 void free() override;
32 void Run() override;
33 void InitializeProblems() override;
34 void BroadcastXCut();
35
36 mpi::communicator &_world;
37
38 private:
39 void step_1_solve_master();
40 void step_2_solve_subproblems_and_build_cuts();
41 void step_4_update_best_solution(int rank);
42
43 void master_build_cuts(
44 std::vector<SubProblemDataMap> gathered_subproblem_map);
45 SubProblemDataMap get_subproblem_cut_package();
46
47 void solve_master_and_create_trace();
48
49
50 void do_solve_master_create_trace_and_update_cuts();
51
52 virtual void gather_subproblems_cut_package_and_build_cuts(
53 const SubProblemDataMap &subproblem_data_map, const Timer &process_timer);
54
55 void write_exception_message(const std::exception &ex) const;
56
57 void check_if_some_proc_had_a_failure(int success);
58
59 mpi::environment &_env;
60
61 protected:
62 [[nodiscard]] bool shouldParallelize() const final { return false; }
63 void PreRunInitialization();
64 int Rank() const { return _world.rank(); }
65 template <typename T>
66 void BroadCast(T &value, int root) const {
67 mpi::broadcast(_world, value, root);
68 }
69 template <typename T>
70 void BroadCast(T *values, int n, int root) const {
71 mpi::broadcast(_world, values, n, root);
72 }
73 template <typename T>
74 void Gather(const T &value, std::vector<T> &vector_of_values,
75 int root) const {
76 mpi::gather(_world, value, vector_of_values, root);
77 }
78 void BuildMasterProblem();
79 int WorldSize() const { return _world.size(); }
80 void Barrier() const { _world.barrier(); }
81
82 template <typename T, typename Op>
83 void Reduce(const T &in_value, T &out_value, Op op, int root) const {
84 mpi::reduce(_world, in_value, out_value, op, root);
85 }
86 template <typename T, typename Op>
87 void AllReduce(const T &in_value, T &out_value, Op op) const {
88 mpi::all_reduce(_world, in_value, out_value, op);
89 }
90 virtual void GatherCuts(const SubProblemDataMap &subproblem_data_map,
91 const Timer &walltime);
92 void BroadCastVariablesIndices();
93 virtual void ComputeSubproblemsContributionToCriteria(
94 const SubProblemDataMap &subproblem_data_map);
95 void SolveSubproblem(
96 SubProblemDataMap &subproblem_data_map,
97 PlainData::SubProblemData &subproblem_data, const std::string &name,
98 const std::shared_ptr<SubproblemWorker> &worker) override;
99 void UpdateMaxCriterionArea();
100};
Definition BendersBase.h:32
Class use run the benders algorithm in parallel.
Definition BendersMPI.h:19
void InitializeProblems() override
Method to load each problem in a thread.
Definition BendersMPI.cpp:27
void Run() override
Run Benders algorithm in parallel.
Definition BendersMPI.cpp:316
void free() override
Method to free the memory used by each problem.
Definition BendersMPI.cpp:301
Definition Timer.h:10
Definition launch.py:1
Definition common.h:171
Definition SubproblemCut.h:9