BioDynaMo  v1.05.120-25dc9790
scheduler.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_SCHEDULER_H_
16 #define CORE_SCHEDULER_H_
17 
18 #include <algorithm>
19 #include <chrono>
20 #include <functional>
21 #include <string>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #include "core/functor.h"
28 #include "core/param/param.h"
29 #include "core/util/progress_bar.h"
31 
32 namespace bdm {
33 
34 class SchedulerTest;
35 class Agent;
36 class SimulationBackup;
37 class VisualizationAdaptor;
38 class RootAdaptor;
39 struct BoundSpace;
40 class MechanicalForcesOp;
41 class DiffusionOp;
42 
44 
45 class Scheduler {
46  public:
47  using Clock = std::chrono::high_resolution_clock;
48 
49  Scheduler();
50 
51  virtual ~Scheduler();
52 
54  void Simulate(uint64_t steps);
55 
67  void SimulateUntil(const std::function<bool()>& exit_condition);
68 
73  void FinalizeInitialization() const;
74 
76  uint64_t GetSimulatedSteps() const;
77 
80  real_t GetSimulatedTime() const;
81 
86  void ScheduleOp(Operation* op, OpType op_type = OpType::kSchedule);
87 
88  void UnscheduleOp(Operation* op);
89 
93  std::vector<Operation*> GetOps(const std::string& name);
94 
98  void SetAgentFilters(const std::vector<Functor<bool, Agent*>*>& filters);
99 
100  const std::vector<Functor<bool, Agent*>*>& GetAgentFilters() const;
101 
103 
105 
112  void PrintInfo(std::ostream& out) const;
113 
114  protected:
115  uint64_t total_steps_ = 0;
117 
120  virtual void Execute();
121 
122  private:
123  friend void RunAgentsTest(Param::MappedDataArrayMode, uint64_t, bool, bool);
125 
127  uint64_t restore_point_;
128  std::chrono::time_point<Clock> last_backup_ = Clock::now();
131 
136  std::vector<Operation*> all_ops_;
137  std::vector<std::pair<OpType, Operation*>> schedule_ops_;
139  std::vector<Operation*> unschedule_ops_;
141  std::vector<Operation*> scheduled_standalone_ops_;
143  std::vector<Operation*> scheduled_agent_ops_;
145  std::vector<std::string> protected_op_names_;
147  // Operations that are run before setting up, running and tearing down
148  // scheduled operations
149  std::vector<Operation*> pre_scheduled_ops_;
150  // Operations that are run after setting up, running and tearing down
151  // scheduled operations
152  std::vector<Operation*> post_scheduled_ops_;
155 
159  std::vector<Functor<bool, Agent*>*> agent_filters_;
160 
162  void Backup();
163 
167  bool Restore(uint64_t* steps);
168 
169  void UpdateSimulatedTime();
170 
171  // TODO(lukas, ahmad) After https://trello.com/c/0D6sHCK4 has been resolved
172  // think about a better solution, because some operations are executed twice
173  // if Simulate is called with one timestep.
174  void Initialize(uint64_t steps = 0);
175 
177  template <typename Lambda>
178  void ForEachOperationInList(const std::vector<Operation*>& operations,
179  Lambda lambda) const {
180  for (auto* op : operations) {
181  lambda(op);
182  }
183  }
184 
186  template <typename Lambda>
187  void ForEachScheduledOperation(Lambda lambda) {
190  }
191 
193  template <typename Lambda>
194  void ForEachOperation(Lambda lambda) {
198  }
199 
201  std::vector<std::string> GetListOfScheduledAgentOps() const;
202 
204  std::vector<std::string> GetListOfScheduledStandaloneOps() const;
205 
206  // Run the Operation::SetUp() call for each scheduled operation
207  void SetUpOps();
208 
209  void TearDownOps();
210 
211  // Run the operations in scheduled_*_ops_
212  void RunScheduledOps();
213 
214  // Run the operations in pre_scheduled_ops_ (executed before RunScheduledOps)
215  void RunPreScheduledOps() const;
216 
217  void RunAgentOps(Functor<bool, Agent*>* filter) const;
218 
219  // Run the operations in post_scheduled_ops_ (executed after RunScheduledOps)
220  void RunPostScheduledOps() const;
221 
222  void ScheduleOps();
223 };
224 
225 } // namespace bdm
226 
227 #endif // CORE_SCHEDULER_H_
bdm::Scheduler::Backup
void Backup()
Backup the simulation. Backup interval based on Param::backup_interval
Definition: scheduler.cc:420
bdm::kSchedule
@ kSchedule
Definition: scheduler.h:43
timing_aggregator.h
bdm::Scheduler::Initialize
void Initialize(uint64_t steps=0)
Definition: scheduler.cc:458
bdm::Scheduler::SchedulerTest
friend SchedulerTest
Definition: scheduler.h:124
bdm::Scheduler::Execute
virtual void Execute()
Definition: scheduler.cc:354
bdm::Scheduler::progress_bar_
ProgressBar * progress_bar_
Definition: scheduler.h:130
bdm::Scheduler::FinalizeInitialization
void FinalizeInitialization() const
Definition: scheduler.cc:153
bdm::Scheduler::ForEachOperation
void ForEachOperation(Lambda lambda)
Runs a lambda for each operation that is executed in the Execute() call.
Definition: scheduler.h:194
bdm
Definition: agent.cc:39
bdm::Scheduler::Restore
bool Restore(uint64_t *steps)
Definition: scheduler.cc:435
bdm::Scheduler::post_scheduled_ops_
std::vector< Operation * > post_scheduled_ops_
Definition: scheduler.h:152
bdm::kPostSchedule
@ kPostSchedule
Definition: scheduler.h:43
operation.h
bdm::Scheduler::op_times_
TimingAggregator op_times_
Tracks operations' execution times.
Definition: scheduler.h:154
bdm::Scheduler::protected_op_names_
std::vector< std::string > protected_op_names_
List of operations that cannot be affected by the user.
Definition: scheduler.h:146
bdm::Scheduler::~Scheduler
virtual ~Scheduler()
Definition: scheduler.cc:121
bdm::Scheduler::GetOpTimes
TimingAggregator * GetOpTimes()
Definition: scheduler.cc:162
bdm::Scheduler::restore_point_
uint64_t restore_point_
Definition: scheduler.h:127
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::Scheduler::last_backup_
std::chrono::time_point< Clock > last_backup_
Definition: scheduler.h:128
bdm::Scheduler::scheduled_agent_ops_
std::vector< Operation * > scheduled_agent_ops_
List of operations will be executed on all agents.
Definition: scheduler.h:144
bdm::Scheduler::SimulateUntil
void SimulateUntil(const std::function< bool()> &exit_condition)
Definition: scheduler.cc:144
bdm::Scheduler::unschedule_ops_
std::vector< Operation * > unschedule_ops_
List of operations that are to be removed in the upcoming timestep.
Definition: scheduler.h:140
bdm::Scheduler::backup_
SimulationBackup * backup_
Definition: scheduler.h:126
bdm::Scheduler::TearDownOps
void TearDownOps()
Definition: scheduler.cc:271
bdm::Scheduler::RunPostScheduledOps
void RunPostScheduledOps() const
Definition: scheduler.cc:346
bdm::Scheduler::GetListOfScheduledStandaloneOps
std::vector< std::string > GetListOfScheduledStandaloneOps() const
Return a list of StandAloneOperations that are scheduled.
Definition: scheduler.cc:210
bdm::Scheduler::PrintInfo
void PrintInfo(std::ostream &out) const
Definition: scheduler.cc:371
bdm::Param::MappedDataArrayMode
MappedDataArrayMode
Definition: param.h:545
bdm::Functor
Definition: functor.h:24
bdm::Scheduler::RunPreScheduledOps
void RunPreScheduledOps() const
Definition: scheduler.cc:279
bdm::OpType
OpType
Definition: scheduler.h:43
bdm::RootAdaptor
The class that bridges the simulation code with ROOT Visualization.
Definition: adaptor.h:43
bdm::Scheduler::RunAgentOps
void RunAgentOps(Functor< bool, Agent * > *filter) const
Definition: scheduler.cc:288
bdm::Scheduler::scheduled_standalone_ops_
std::vector< Operation * > scheduled_standalone_ops_
List of operations will be executed as a stand-alone operation.
Definition: scheduler.h:142
bdm::Scheduler::ForEachScheduledOperation
void ForEachScheduledOperation(Lambda lambda)
Runs a lambda for each scheduled operation.
Definition: scheduler.h:187
bdm::Scheduler::RunAgentsTest
friend void RunAgentsTest(Param::MappedDataArrayMode, uint64_t, bool, bool)
bdm::Scheduler::RunScheduledOps
void RunScheduledOps()
Definition: scheduler.cc:324
bdm::Scheduler::Simulate
void Simulate(uint64_t steps)
Simulate steps number of iterations.
Definition: scheduler.cc:130
bdm::Scheduler::GetSimulatedTime
real_t GetSimulatedTime() const
Definition: scheduler.cc:160
bdm::Scheduler::ForEachOperationInList
void ForEachOperationInList(const std::vector< Operation * > &operations, Lambda lambda) const
Runs a lambda for each operation in the specified list of operations.
Definition: scheduler.h:178
bdm::Scheduler::GetAgentFilters
const std::vector< Functor< bool, Agent * > * > & GetAgentFilters() const
Definition: scheduler.cc:245
bdm::Scheduler::SetAgentFilters
void SetAgentFilters(const std::vector< Functor< bool, Agent * > * > &filters)
Definition: scheduler.cc:239
bdm::Scheduler::Scheduler
Scheduler()
Definition: scheduler.cc:36
bdm::Scheduler::all_ops_
std::vector< Operation * > all_ops_
Definition: scheduler.h:136
bdm::Scheduler::UpdateSimulatedTime
void UpdateSimulatedTime()
Definition: scheduler.cc:451
bdm::Scheduler::Clock
std::chrono::high_resolution_clock Clock
Definition: scheduler.h:47
bdm::Scheduler::GetListOfScheduledAgentOps
std::vector< std::string > GetListOfScheduledAgentOps() const
Return a list of AgentOperations that are scheduled.
Definition: scheduler.cc:202
bdm::Scheduler::GetRootVisualization
RootAdaptor * GetRootVisualization()
Definition: scheduler.h:102
bdm::kPreSchedule
@ kPreSchedule
Definition: scheduler.h:43
bdm::SimulationBackup
Definition: simulation_backup.h:33
bdm::Scheduler::agent_filters_
std::vector< Functor< bool, Agent * > * > agent_filters_
Definition: scheduler.h:159
bdm::Scheduler::pre_scheduled_ops_
std::vector< Operation * > pre_scheduled_ops_
Definition: scheduler.h:149
bdm::Scheduler::GetOps
std::vector< Operation * > GetOps(const std::string &name)
Definition: scheduler.cc:218
bdm::ProgressBar
This class implements a progress bar that can be used to track the progress of a simulation.
Definition: progress_bar.h:40
bdm::Scheduler::ScheduleOps
void ScheduleOps()
Definition: scheduler.cc:505
bdm::Scheduler::total_steps_
uint64_t total_steps_
Definition: scheduler.h:115
bdm::Scheduler::simulated_time_
real_t simulated_time_
Definition: scheduler.h:116
bdm::Scheduler::schedule_ops_
std::vector< std::pair< OpType, Operation * > > schedule_ops_
List of operations that are to be added in the upcoming timestep.
Definition: scheduler.h:138
param.h
bdm::Operation
Definition: operation.h:99
bdm::Scheduler::SetUpOps
void SetUpOps()
Definition: scheduler.cc:263
bdm::Scheduler
Definition: scheduler.h:45
bdm::Scheduler::root_visualization_
RootAdaptor * root_visualization_
Definition: scheduler.h:129
functor.h
bdm::Scheduler::ScheduleOp
void ScheduleOp(Operation *op, OpType op_type=OpType::kSchedule)
Definition: scheduler.cc:164
bdm::Scheduler::UnscheduleOp
void UnscheduleOp(Operation *op)
Definition: scheduler.cc:174
bdm::TimingAggregator
Definition: timing_aggregator.h:30
bdm::Scheduler::GetSimulatedSteps
uint64_t GetSimulatedSteps() const
This function returns the number of simulated steps (=iterations).
Definition: scheduler.cc:158
progress_bar.h