BioDynaMo  v1.03.58-27764645
simulation.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 #ifndef CORE_SIMULATION_H_
15 #define CORE_SIMULATION_H_
16 
17 #include <functional>
18 #include <string>
19 #include <vector>
20 
21 #include "core/agent/agent_uid.h"
22 #include "core/gpu/opencl_state.h"
24 #include "core/util/random.h"
25 #include "core/util/root.h"
26 
27 namespace bdm {
28 
29 // forward declarations
30 class ResourceManager;
31 class Environment;
32 class Grid;
33 class Scheduler;
34 struct Param;
35 class ExecutionContext;
36 class CommandLineOptions;
37 class AgentUidGenerator;
38 
39 class SimulationTest;
40 class ParaviewAdaptorTest;
41 
42 namespace experimental {
43 class TimeSeries;
44 } // namespace experimental
45 
50 class Simulation {
51  public:
53  static Simulation* GetActive();
54 
55  explicit Simulation(TRootIOCtor* p);
59  Simulation(int argc, const char** argv,
60  const std::vector<std::string>& config_files = {});
61 
62  explicit Simulation(CommandLineOptions* clo,
63  const std::vector<std::string>& config_files = {});
64 
71  explicit Simulation(const std::string& simulation_name,
72  const std::vector<std::string>& config_files = {});
73 
74  Simulation(int argc, const char** argv,
75  const std::function<void(Param*)>& set_param,
76  const std::vector<std::string>& config_files = {});
77 
79  const std::function<void(Param*)>& set_param,
80  const std::vector<std::string>& config_files = {});
81 
82  Simulation(const std::string& simulation_name,
83  const std::function<void(Param*)>& set_param,
84  const std::vector<std::string>& config_files = {});
85 
86  ~Simulation();
87 
90  void Restore(Simulation&& restored);
91 
93  void Activate();
94 
96 
98 
99  const Param* GetParam() const;
100 
102 
104 
107  void SetEnvironment(Environment* env);
108 
110 
111  void Simulate(uint64_t steps);
112 
114  Random* GetRandom();
115 
117  std::vector<Random*>& GetAllRandom();
118 
121 
123  std::vector<ExecutionContext*>& GetAllExecCtxts();
124 
125  void SetAllExecCtxts(const std::vector<ExecutionContext*>& exec_ctxts);
126 
128 
131 
133  const std::string& GetUniqueName() const;
134 
136  const std::string& GetOutputDir() const;
137 
139 
144  void ReplaceScheduler(Scheduler* scheduler);
145 
146  private:
150  static std::atomic<uint64_t> counter_;
151 
153  std::vector<Random*> random_;
154 
156  std::vector<ExecutionContext*> exec_ctxt_;
157 
158  ResourceManager* rm_ = nullptr;
159  Param* param_ = nullptr;
161  std::string name_;
163  Scheduler* scheduler_ = nullptr;
164  OpenCLState* ocl_state_ = nullptr;
167  uint64_t id_ = 0;
168  std::string unique_name_;
173  std::string output_dir_;
175  std::string command_line_parameter_str_;
178  MemoryManager* mem_mgr_ = nullptr;
180  int64_t ctor_ts_ = 0;
182  int64_t dtor_ts_ = 0;
186 
188  void Initialize(CommandLineOptions* clo,
189  const std::function<void(Param*)>& set_param,
190  const std::vector<std::string>& config_files);
191 
193  void InitializeMembers();
194 
197  const std::function<void(Param*)>& set_param,
198  const std::vector<std::string>& ctor_config);
199 
200  void LoadConfigFiles(const std::vector<std::string>& ctor_configs,
201  const std::vector<std::string>& cli_configs);
202 
204  void InitializeUniqueName(const std::string& simulation_name);
205 
207  void InitializeOutputDir();
208 
212  friend std::ostream& operator<<(std::ostream& os, Simulation& sim);
213 
215 };
216 
217 } // namespace bdm
218 
219 #endif // CORE_SIMULATION_H_
bdm::Simulation::InitializeUniqueName
void InitializeUniqueName(const std::string &simulation_name)
This function initialzes unique_name_
Definition: simulation.cc:547
bdm::Simulation::InitializeRuntimeParams
void InitializeRuntimeParams(CommandLineOptions *clo, const std::function< void(Param *)> &set_param, const std::vector< std::string > &ctor_config)
This function parses command line parameters and the configuration file.
Definition: simulation.cc:397
opencl_state.h
bdm::Simulation::environment_
Environment * environment_
Definition: simulation.h:162
bdm::Simulation::Initialize
void Initialize(CommandLineOptions *clo, const std::function< void(Param *)> &set_param, const std::vector< std::string > &config_files)
Initialize Simulation.
Definition: simulation.cc:311
agent_uid.h
bdm::Simulation::GetTimeSeries
experimental::TimeSeries * GetTimeSeries()
Definition: simulation.cc:304
bdm::Simulation::Simulate
void Simulate(uint64_t steps)
Definition: simulation.cc:281
bdm::Simulation::output_dir_
std::string output_dir_
cached value where unique_name_ is appended to Param::output_dir
Definition: simulation.h:174
bdm
Definition: agent.cc:39
bdm::Simulation::ocl_state_
OpenCLState * ocl_state_
Definition: simulation.h:164
bdm::Simulation::name_
std::string name_
Definition: simulation.h:161
bdm::Simulation::mem_mgr_
MemoryManager * mem_mgr_
BioDynaMo memory manager. If nullptr, default allocator will be used.
Definition: simulation.h:179
bdm::Simulation::scheduler_
Scheduler * scheduler_
Definition: simulation.h:163
bdm::ExecutionContext
Definition: execution_context.h:31
bdm::Simulation::InitializeMembers
void InitializeMembers()
Initialize data members that have a dependency on Simulation.
Definition: simulation.cc:331
bdm::Simulation::GetUniqueName
const std::string & GetUniqueName() const
Returns the name of the simulation.
Definition: simulation.cc:299
bdm::Simulation::Activate
void Activate()
Activates this simulation.
Definition: simulation.cc:258
bdm::Simulation::unique_name_
std::string unique_name_
Definition: simulation.h:172
bdm::Simulation::GetRandom
Random * GetRandom()
Returns a thread local random number generator.
Definition: simulation.cc:284
bdm::Simulation::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(Simulation, 1)
bdm::Simulation::exec_ctxt_
std::vector< ExecutionContext * > exec_ctxt_
Execution Context for each thread.
Definition: simulation.h:156
bdm::Random
Definition: random.h:262
random.h
bdm::Simulation::rm_
ResourceManager * rm_
Definition: simulation.h:158
bdm::Simulation::is_gpu_environment_initialized_
bool is_gpu_environment_initialized_
Definition: simulation.h:165
bdm::Simulation::SetEnvironment
void SetEnvironment(Environment *env)
Definition: simulation.cc:373
bdm::Simulation::SimulationTest
friend SimulationTest
Definition: simulation.h:209
bdm::Simulation::random_
std::vector< Random * > random_
random number generator for each thread
Definition: simulation.h:153
bdm::Simulation::GetScheduler
Scheduler * GetScheduler()
Definition: simulation.cc:279
bdm::Simulation::id_
uint64_t id_
This id is unique for each simulation within the same process.
Definition: simulation.h:167
bdm::Simulation::GetAllRandom
std::vector< Random * > & GetAllRandom()
Returns all thread local random number generator.
Definition: simulation.cc:286
bdm::Simulation::time_series_
experimental::TimeSeries * time_series_
Collects time series information during the simulation.
Definition: simulation.h:185
bdm::MemoryManager
Definition: memory_manager.h:149
bdm::Simulation::GetMemoryManager
MemoryManager * GetMemoryManager()
Definition: simulation.h:127
bdm::Simulation::GetResourceManager
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
Definition: simulation.cc:261
bdm::Simulation::InitializeOutputDir
void InitializeOutputDir()
Initializes output_dir_ and creates dir if it does not exist.
Definition: simulation.cc:557
bdm::ResourceManager
Definition: resource_manager.h:52
root.h
bdm::OpenCLState
Definition: opencl_state.h:33
bdm::Simulation::counter_
static std::atomic< uint64_t > counter_
Number of simulations in this process.
Definition: simulation.h:150
bdm::Simulation::dtor_ts_
int64_t dtor_ts_
Timestep when destructor was called.
Definition: simulation.h:183
bdm::Simulation::SetAllExecCtxts
void SetAllExecCtxts(const std::vector< ExecutionContext * > &exec_ctxts)
Definition: simulation.cc:380
bdm::Simulation::Restore
void Restore(Simulation &&restored)
Definition: simulation.cc:108
bdm::Simulation::GetOpenCLState
OpenCLState * GetOpenCLState()
Return helper class for OpenCL environment.
Definition: simulation.cc:296
bdm::Simulation::command_line_parameter_str_
std::string command_line_parameter_str_
Definition: simulation.h:177
bdm::Simulation::GetAgentUidGenerator
AgentUidGenerator * GetAgentUidGenerator()
Definition: simulation.cc:273
bdm::experimental::TimeSeries
Definition: time_series.h:33
bdm::AgentUidGenerator
This class generates unique ids for agents.
Definition: agent_uid_generator.h:33
bdm::Simulation::Simulation
Simulation(TRootIOCtor *p)
Definition: simulation.cc:69
bdm::Simulation::operator<<
friend std::ostream & operator<<(std::ostream &os, Simulation &sim)
Definition: simulation.cc:137
memory_manager.h
bdm::Simulation::active_
static Simulation * active_
Currently active simulation.
Definition: simulation.h:148
bdm::Simulation::ctor_ts_
int64_t ctor_ts_
Timestep when constructor was called.
Definition: simulation.h:181
bdm::Simulation::DiffusionTest_CopyOldData_Test
friend class DiffusionTest_CopyOldData_Test
Definition: simulation.h:211
bdm::Simulation::GetExecutionContext
ExecutionContext * GetExecutionContext()
Returns a thread local execution context.
Definition: simulation.cc:288
bdm::Simulation::GetEnvironment
Environment * GetEnvironment()
Definition: simulation.cc:277
bdm::Simulation::ReplaceScheduler
void ReplaceScheduler(Scheduler *scheduler)
Definition: simulation.cc:306
bdm::Simulation::ParaviewAdaptorTest
friend ParaviewAdaptorTest
Definition: simulation.h:210
bdm::Simulation::GetParam
const Param * GetParam() const
Returns the simulation parameters.
Definition: simulation.cc:271
bdm::Simulation::SetResourceManager
void SetResourceManager(ResourceManager *rm)
Definition: simulation.cc:263
bdm::Simulation::GetAllExecCtxts
std::vector< ExecutionContext * > & GetAllExecCtxts()
Returns all thread local execution contexts.
Definition: simulation.cc:292
bdm::Environment
Definition: environment.h:30
bdm::Simulation::LoadConfigFiles
void LoadConfigFiles(const std::vector< std::string > &ctor_configs, const std::vector< std::string > &cli_configs)
Definition: simulation.cc:477
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:67
bdm::Param
Definition: param.h:35
bdm::Simulation::~Simulation
~Simulation()
Definition: simulation.cc:214
bdm::Simulation::param_
Param * param_
Definition: simulation.h:159
bdm::Scheduler
Definition: scheduler.h:45
bdm::CommandLineOptions
Class to contain and parse command line options.
Definition: command_line_options.h:37
bdm::Simulation::GetOutputDir
const std::string & GetOutputDir() const
Returns the output directory for this specific simulation.
Definition: simulation.cc:302
bdm::Simulation
Definition: simulation.h:50
bdm::Simulation::agent_uid_generator_
AgentUidGenerator * agent_uid_generator_
Definition: simulation.h:160