BioDynaMo
v1.05.120-25dc9790
|
Go to the documentation of this file.
17 #include <cpptoml/cpptoml.h>
31 #include "bdm_version.h"
51 #include "memory_usage.h"
73 const std::vector<std::string>& config_files)
75 argc, argv, [](auto* param) {}, config_files) {}
78 const std::vector<std::string>& config_files)
80 simulation_name, [](auto* param) {}, config_files) {}
83 const std::vector<std::string>& config_files) {
85 clo, [](
auto* param) {}, config_files);
89 const std::function<
void(
Param*)>& set_param,
90 const std::vector<std::string>& config_files) {
95 const std::function<
void(
Param*)>& set_param,
96 const std::vector<std::string>& config_files) {
102 const std::function<
void(
Param*)>& set_param,
103 const std::vector<std::string>& config_files) {
104 const char* argv[1] = {simulation_name.c_str()};
106 Initialize(&options, set_param, config_files);
111 if (
random_.size() != restored.random_.size()) {
113 ") was run with a different number of threads. Can't restore "
114 "complete random number generator state.");
115 uint64_t min = std::min(
random_.size(), restored.random_.size());
116 for (uint64_t i = 0; i < min; i++) {
117 *(
random_[i]) = *(restored.random_[i]);
120 for (uint64_t i = 0; i <
random_.size(); i++) {
121 *(
random_[i]) = *(restored.random_[i]);
127 restored.param_ =
nullptr;
128 *
rm_ = std::move(*restored.rm_);
129 restored.rm_ =
nullptr;
141 os <<
"***********************************************" << std::endl;
142 os <<
"***********************************************" << std::endl;
143 os <<
"\033[1mSimulation Metadata:\033[0m" << std::endl;
144 os <<
"***********************************************" << std::endl;
146 os <<
"\033[1mGeneral\033[0m" << std::endl;
150 os <<
"Simulation name\t\t\t: " << sim.
GetUniqueName() << std::endl;
151 os <<
"Total simulation runtime\t: " << (sim.
dtor_ts_ - sim.
ctor_ts_) <<
" ms"
153 os <<
"Peak memory usage (MB)\t\t: " << (getPeakRSS() / 1048576.0)
155 os <<
"Number of iterations executed\t: "
157 os <<
"Number of agents\t\t: " << sim.
rm_->
GetNumAgents() << std::endl;
161 os <<
"Output directory\t\t: " << sim.
GetOutputDir() << std::endl;
162 os <<
" size\t\t\t\t: "
163 << gSystem->GetFromPipe(
166 os <<
"BioDynaMo version:\t\t: " << Version::String() << std::endl;
167 os <<
"BioDynaMo real type:\t\t: " <<
kRealtName << std::endl;
169 os <<
"***********************************************" << std::endl;
171 os <<
"***********************************************" << std::endl;
173 os <<
"\033[1mThread Info\033[0m" << std::endl;
176 os <<
"***********************************************" << std::endl;
180 os <<
"***********************************************" << std::endl;
182 os <<
"\033[1mParameters\033[0m" << std::endl;
185 os <<
"***********************************************" << std::endl;
186 os <<
"***********************************************" << std::endl;
195 std::stringstream sstr;
196 sstr << *
this << std::endl;
197 std::cout << sstr.str() << std::endl;
200 ofs << sstr.str() << std::endl;
205 git_tracker.SaveGitDetails();
207 #endif // USE_LIBGIT2
295 const std::function<
void(
Param*)>& set_param,
296 const std::vector<std::string>& config_files) {
298 TROOT(
name_.c_str(),
"BioDynaMo");
299 ROOT::EnableThreadSafety();
306 "CommandLineOptions argument was a nullptr!");
325 random_.resize(omp_get_max_threads());
326 #pragma omp parallel for schedule(static, 1)
327 for (uint64_t i = 0; i <
random_.size(); i++) {
332 auto map = std::make_shared<
334 #pragma omp parallel for schedule(static, 1)
335 for (uint64_t i = 0; i <
exec_ctxt_.size(); i++) {
348 Log::Error(
"Simulation::Initialize",
"No such neighboring method '",
364 const std::vector<ExecutionContext*>& exec_ctxts) {
366 Log::Error(
"Simulation::SetAllExecCtxts",
"Size of exec_ctxts (",
367 exec_ctxts.size(),
") does not match the expected size (",
371 for (uint64_t i = 0; i <
exec_ctxt_.size(); ++i) {
373 if (ctxt !=
nullptr && ctxt != exec_ctxts[i]) {
382 const std::vector<std::string>& ctor_config_files) {
387 std::stringstream sstr;
394 if (std::getenv(
"BDMSYS") ==
nullptr) {
396 "Simulation::InitializeRuntimeParams",
397 "The BioDynaMo environment is not set up correctly. Please execute "
398 "'source <path-to-bdm-installation>/bin/thisbdm.sh' and retry this "
402 static bool read_env =
false;
405 std::stringstream os;
406 os << std::getenv(
"BDMSYS") <<
"/etc/bdm.rootrc";
407 gEnv->ReadFile(os.str().c_str(), kEnvUser);
413 clo->Get<std::vector<std::string>>(
"config"));
416 auto inline_configs = clo->Get<std::vector<std::string>>(
"inline-config");
417 if (inline_configs.size()) {
418 for (
auto& inline_config : inline_configs) {
423 if (clo->Get<std::string>(
"backup") !=
"") {
426 if (clo->Get<std::string>(
"restore") !=
"") {
431 if (clo->Get<
bool>(
"cuda")) {
435 if (clo->Get<
bool>(
"opencl")) {
441 if (clo->Get<
bool>(
"visualize")) {
456 Log::Info(
"",
"Initialize new simulation using BioDynaMo ",
461 const std::vector<std::string>& cli_configs) {
462 constexpr
auto kTomlConfigFile =
"bdm.toml";
463 constexpr
auto kJsonConfigFile =
"bdm.json";
464 constexpr
auto kTomlConfigFileParentDir =
"../bdm.toml";
465 constexpr
auto kJsonConfigFileParentDir =
"../bdm.json";
467 std::vector<std::string> configs = {};
468 if (ctor_configs.size()) {
469 for (
auto& ctor_config : ctor_configs) {
471 configs.push_back(ctor_config);
473 Log::Fatal(
"Simulation::LoadConfigFiles",
"The config file ",
475 " specified in the constructor of bdm::Simulation "
476 "could not be found.");
480 if (cli_configs.size()) {
481 for (
auto& cli_config : cli_configs) {
483 configs.push_back(cli_config);
485 Log::Fatal(
"Simulation::LoadConfigFiles",
"The config file ",
487 " specified as command line argument "
488 "could not be found.");
494 if (!configs.size()) {
496 configs.push_back(kTomlConfigFile);
497 }
else if (
FileExists(kTomlConfigFileParentDir)) {
498 configs.push_back(kTomlConfigFileParentDir);
500 configs.push_back(kJsonConfigFile);
501 }
else if (
FileExists(kJsonConfigFileParentDir)) {
502 configs.push_back(kJsonConfigFileParentDir);
507 if (configs.size()) {
508 for (
auto& config : configs) {
510 auto toml = cpptoml::parse_file(config);
512 }
else if (
EndsWith(config,
".json")) {
513 std::ifstream ifs(config);
514 std::stringstream buffer;
515 buffer << ifs.rdbuf();
519 "Processed config file: ", config);
522 Log::Info(
"Simulation::LoadConfigFiles",
"Default config file ",
523 kTomlConfigFile,
" or ", kJsonConfigFile,
524 " not found in `.` or `..` directory. No other config file was "
525 "specified as command line parameter or passed to the "
526 "constructor of bdm::Simulation.");
531 name_ = simulation_name;
532 std::stringstream stream;
553 timeinfo = localtime(&rawtime);
554 strftime(buffer,
sizeof(buffer),
"/%Y-%m-%d-%H:%M:%S", timeinfo);
570 "Simulation::InitializeOutputDir",
"Output dir (",
output_dir_,
571 ") is not empty. Previous result files would be overridden. Abort."
572 "Please set Param::remove_output_dir_contents to true to remove "
574 " automatically or clear the output directory by hand.");
void InitializeUniqueName(const std::string &simulation_name)
This function initializes unique_name_
bool remove_output_dir_contents
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.
Environment * environment_
std::string GetSimulationName() const
Return the simulation name that was parsed from argv[0].
static GpuHelper * GetInstance()
size_t GetNumAgents(int numa_node=-1) const
void Initialize(CommandLineOptions *clo, const std::function< void(Param *)> &set_param, const std::vector< std::string > &config_files)
Initialize Simulation.
static ThreadInfo * GetInstance()
experimental::TimeSeries * GetTimeSeries()
void Simulate(uint64_t steps)
std::string output_dir_
cached value where unique_name_ is appended to Param::output_dir
void SetIgnoreDelete(bool value)
std::string ToJsonString() const
uint64_t mem_mgr_aligned_pages_shift
TimingAggregator * GetOpTimes()
MemoryManager * mem_mgr_
BioDynaMo memory manager. If nullptr, default allocator will be used.
void InitializeMembers()
Initialize data members that have a dependency on Simulation.
const std::string & GetUniqueName() const
Returns the name of the simulation.
void Activate()
Activates this simulation.
bool export_visualization
Random * GetRandom()
Returns a thread local random number generator.
std::vector< ExecutionContext * > exec_ctxt_
Execution Context for each thread.
uint64_t mem_mgr_max_mem_per_thread_factor
bool is_gpu_environment_initialized_
void SetEnvironment(Environment *env)
std::vector< Random * > random_
random number generator for each thread
Scheduler * GetScheduler()
uint64_t id_
This id is unique for each simulation within the same process.
std::vector< Random * > & GetAllRandom()
Returns all thread local random number generator.
std::string compute_target
real_t mem_mgr_growth_rate
static void Warning(const std::string &location, const Args &... parts)
Prints warning message.
experimental::TimeSeries * time_series_
Collects time series information during the simulation.
uint64_t RemoveDirectoryContents(const std::string &directory)
void InitializeGPUEnvironment()
static void Info(const std::string &location, const Args &... parts)
Prints information message.
ResourceManager * GetResourceManager()
Returns the ResourceManager instance.
void InitializeOutputDir()
Initializes output_dir_ and creates dir if it does not exist.
void Simulate(uint64_t steps)
Simulate steps number of iterations.
constexpr const char * kRealtName
bool FileExists(const std::string &file_name)
static std::atomic< uint64_t > counter_
Number of simulations in this process.
bool EndsWith(const std::string &str, const std::string &suffix)
int64_t dtor_ts_
Timestep when destructor was called.
void SetAllExecCtxts(const std::vector< ExecutionContext * > &exec_ctxts)
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
void MergeJsonPatch(const std::string &patch)
void Restore(Simulation &&restored)
OpenCLState * GetOpenCLState()
Return helper class for OpenCL environment.
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
static void Error(const std::string &location, const Args &... parts)
Prints error message.
std::string command_line_parameter_str_
AgentUidGenerator * GetAgentUidGenerator()
This class generates unique ids for agents.
void AssignFromConfig(const std::shared_ptr< cpptoml::table > &)
Assign values from config file to variables.
Simulation(TRootIOCtor *p)
static Simulation * active_
Currently active simulation.
void ForEachDiffusionGrid(TFunctor &&f) const
int64_t ctor_ts_
Timestep when constructor was called.
std::ostream & operator<<(std::ostream &o, const MathArray< T, N > &arr)
ExecutionContext * GetExecutionContext()
Returns a thread local execution context.
Environment * GetEnvironment()
void ReplaceScheduler(Scheduler *scheduler)
uint32_t visualization_interval
const Param * GetParam() const
Returns the simulation parameters.
void SetResourceManager(ResourceManager *rm)
std::vector< ExecutionContext * > & GetAllExecCtxts()
Returns all thread local execution contexts.
static int64_t Timestamp()
void LoadConfigFiles(const std::vector< std::string > &ctor_configs, const std::vector< std::string > &cli_configs)
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Class to contain and parse command line options.
const std::string & GetOutputDir() const
Returns the output directory for this specific simulation.
void Restore(Param &&other)
uint64_t GetSimulatedSteps() const
This function returns the number of simulated steps (=iterations).
AgentUidGenerator * agent_uid_generator_