BioDynaMo
v1.03.58-27764645
|
Go to the documentation of this file.
17 #include <cpptoml/cpptoml.h>
22 #include <experimental/filesystem>
31 #include "bdm_version.h"
51 #include "memory_usage.h"
56 namespace fs = std::experimental::filesystem;
72 const std::vector<std::string>& config_files)
74 argc, argv, [](auto* param) {}, config_files) {}
77 const std::vector<std::string>& config_files)
79 simulation_name, [](auto* param) {}, config_files) {}
82 const std::vector<std::string>& config_files) {
84 clo, [](
auto* param) {}, config_files);
88 const std::function<
void(
Param*)>& set_param,
89 const std::vector<std::string>& config_files) {
94 const std::function<
void(
Param*)>& set_param,
95 const std::vector<std::string>& config_files) {
101 const std::function<
void(
Param*)>& set_param,
102 const std::vector<std::string>& config_files) {
103 const char* argv[1] = {simulation_name.c_str()};
105 Initialize(&options, set_param, config_files);
110 if (
random_.size() != restored.random_.size()) {
112 ") was run with a different number of threads. Can't restore "
113 "complete random number generator state.");
114 uint64_t min = std::min(
random_.size(), restored.random_.size());
115 for (uint64_t i = 0; i < min; i++) {
116 *(
random_[i]) = *(restored.random_[i]);
119 for (uint64_t i = 0; i <
random_.size(); i++) {
120 *(
random_[i]) = *(restored.random_[i]);
126 restored.param_ =
nullptr;
127 *
rm_ = std::move(*restored.rm_);
128 restored.rm_ =
nullptr;
138 std::vector<std::string> dgrid_names;
139 std::vector<size_t> dgrid_resolutions;
140 std::vector<std::array<int32_t, 3>> dgrid_dimensions;
141 std::vector<uint64_t> dgrid_voxels;
144 dgrid_names.push_back(dgrid->GetSubstanceName());
145 dgrid_resolutions.push_back(dgrid->GetResolution());
146 dgrid_dimensions.push_back(dgrid->GetGridSize());
147 dgrid_voxels.push_back(dgrid->GetNumBoxes());
152 os <<
"***********************************************" << std::endl;
153 os <<
"***********************************************" << std::endl;
154 os <<
"\033[1mSimulation Metadata:\033[0m" << std::endl;
155 os <<
"***********************************************" << std::endl;
157 os <<
"\033[1mGeneral\033[0m" << std::endl;
161 os <<
"Simulation name\t\t\t: " << sim.
GetUniqueName() << std::endl;
162 os <<
"Total simulation runtime\t: " << (sim.
dtor_ts_ - sim.
ctor_ts_) <<
" ms"
164 os <<
"Peak memory usage (MB)\t\t: " << (getPeakRSS() / 1048576.0)
166 os <<
"Number of iterations executed\t: "
168 os <<
"Number of agents\t\t: " << sim.
rm_->
GetNumAgents() << std::endl;
170 if (dgrid_names.size() != 0) {
171 os <<
"Diffusion grids" << std::endl;
172 for (
size_t i = 0; i < dgrid_names.size(); ++i) {
173 os <<
" " << dgrid_names[i] <<
":" << std::endl;
174 auto& dim = dgrid_dimensions[i];
176 <<
"Resolution\t\t: " << dgrid_resolutions[i] << std::endl;
178 <<
"Size\t\t\t: " << dim[0] <<
" x " << dim[1] <<
" x " << dim[2]
180 <<
"\tVoxels\t\t\t: " << dgrid_voxels[i] << std::endl;
184 os <<
"Output directory\t\t: " << sim.
GetOutputDir() << std::endl;
185 os <<
" size\t\t\t\t: "
186 << gSystem->GetFromPipe(
189 os <<
"BioDynaMo version:\t\t: " << Version::String() << std::endl;
190 os <<
"BioDynaMo real type:\t\t: " <<
kRealtName << std::endl;
192 os <<
"***********************************************" << std::endl;
194 os <<
"***********************************************" << std::endl;
196 os <<
"\033[1mThread Info\033[0m" << std::endl;
199 os <<
"***********************************************" << std::endl;
203 os <<
"***********************************************" << std::endl;
205 os <<
"\033[1mParameters\033[0m" << std::endl;
208 os <<
"***********************************************" << std::endl;
209 os <<
"***********************************************" << std::endl;
218 std::stringstream sstr;
219 sstr << *
this << std::endl;
220 std::cout << sstr.str() << std::endl;
223 ofs << sstr.str() << std::endl;
312 const std::function<
void(
Param*)>& set_param,
313 const std::vector<std::string>& config_files) {
315 TROOT(
name_.c_str(),
"BioDynaMo");
316 ROOT::EnableThreadSafety();
323 "CommandLineOptions argument was a nullptr!");
342 random_.resize(omp_get_max_threads());
343 #pragma omp parallel for schedule(static, 1)
344 for (uint64_t i = 0; i <
random_.size(); i++) {
349 auto map = std::make_shared<
351 #pragma omp parallel for schedule(static, 1)
352 for (uint64_t i = 0; i <
exec_ctxt_.size(); i++) {
365 Log::Error(
"Simulation::Initialize",
"No such neighboring method '",
381 const std::vector<ExecutionContext*>& exec_ctxts) {
383 Log::Error(
"Simulation::SetAllExecCtxts",
"Size of exec_ctxts (",
384 exec_ctxts.size(),
") does not match the expected size (",
388 for (uint64_t i = 0; i <
exec_ctxt_.size(); ++i) {
390 if (ctxt !=
nullptr && ctxt != exec_ctxts[i]) {
399 const std::vector<std::string>& ctor_config_files) {
404 std::stringstream sstr;
411 if (std::getenv(
"BDMSYS") ==
nullptr) {
413 "Simulation::InitializeRuntimeParams",
414 "The BioDynaMo environment is not set up correctly. Please execute "
415 "'source <path-to-bdm-installation>/bin/thisbdm.sh' and retry this "
419 static bool read_env =
false;
422 std::stringstream os;
423 os << std::getenv(
"BDMSYS") <<
"/etc/bdm.rootrc";
424 gEnv->ReadFile(os.str().c_str(), kEnvUser);
430 clo->Get<std::vector<std::string>>(
"config"));
433 auto inline_configs = clo->Get<std::vector<std::string>>(
"inline-config");
434 if (inline_configs.size()) {
435 for (
auto& inline_config : inline_configs) {
440 if (clo->Get<std::string>(
"backup") !=
"") {
443 if (clo->Get<std::string>(
"restore") !=
"") {
448 if (clo->Get<
bool>(
"cuda")) {
452 if (clo->Get<
bool>(
"opencl")) {
458 if (clo->Get<
bool>(
"visualize")) {
473 Log::Info(
"",
"Initialize new simulation using BioDynaMo ",
478 const std::vector<std::string>& cli_configs) {
479 constexpr
auto kTomlConfigFile =
"bdm.toml";
480 constexpr
auto kJsonConfigFile =
"bdm.json";
481 constexpr
auto kTomlConfigFileParentDir =
"../bdm.toml";
482 constexpr
auto kJsonConfigFileParentDir =
"../bdm.json";
484 std::vector<std::string> configs = {};
485 if (ctor_configs.size()) {
486 for (
auto& ctor_config : ctor_configs) {
488 configs.push_back(ctor_config);
490 Log::Fatal(
"Simulation::LoadConfigFiles",
"The config file ",
492 " specified in the constructor of bdm::Simulation "
493 "could not be found.");
497 if (cli_configs.size()) {
498 for (
auto& cli_config : cli_configs) {
500 configs.push_back(cli_config);
502 Log::Fatal(
"Simulation::LoadConfigFiles",
"The config file ",
504 " specified as command line argument "
505 "could not be found.");
511 if (!configs.size()) {
513 configs.push_back(kTomlConfigFile);
514 }
else if (
FileExists(kTomlConfigFileParentDir)) {
515 configs.push_back(kTomlConfigFileParentDir);
517 configs.push_back(kJsonConfigFile);
518 }
else if (
FileExists(kJsonConfigFileParentDir)) {
519 configs.push_back(kJsonConfigFileParentDir);
524 if (configs.size()) {
525 for (
auto& config : configs) {
527 auto toml = cpptoml::parse_file(config);
529 }
else if (
EndsWith(config,
".json")) {
530 std::ifstream ifs(config);
531 std::stringstream buffer;
532 buffer << ifs.rdbuf();
536 "Processed config file: ", config);
539 Log::Info(
"Simulation::LoadConfigFiles",
"Default config file ",
540 kTomlConfigFile,
" or ", kJsonConfigFile,
541 " not found in `.` or `..` directory. No other config file was "
542 "specified as command line parameter or passed to the "
543 "constructor of bdm::Simulation.");
548 name_ = simulation_name;
549 std::stringstream stream;
570 timeinfo = localtime(&rawtime);
571 strftime(buffer,
sizeof(buffer),
"/%Y-%m-%d-%H:%M:%S", timeinfo);
586 "Simulation::InitializeOutputDir",
"Output dir (",
output_dir_,
587 ") is not empty. Previous result files would be overriden. Abort."
588 "Please set Param::remove_output_dir_contents to true to remove files"
589 " automatically or clear the output directory by hand.");
void InitializeUniqueName(const std::string &simulation_name)
This function initialzes 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_
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.
std::string GetSimulationName()
Return the simulation name that was parsed from argv[0].
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 numer of simulated steps (=iterations).
AgentUidGenerator * agent_uid_generator_