BioDynaMo  v1.03.61-fe34ced0
simulation_backup.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_SIMULATION_BACKUP_H_
16 #define CORE_SIMULATION_BACKUP_H_
17 
18 #include <functional>
19 #include <sstream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "core/simulation.h"
25 
26 #include "core/util/io.h"
27 #include "core/util/log.h"
28 
29 namespace bdm {
30 
34  public:
35  // object names for root file
36  static const std::string kSimulationName;
37  static const std::string kSimulationStepName;
38  static const std::string kRuntimeVariableName;
39 
47  static std::vector<std::function<void()>> after_restore_event_;
48 
51  SimulationBackup(const std::string& backup_file,
52  const std::string& restore_file);
53 
54  void Backup(size_t completed_simulation_steps) {
55  if (!backup_) {
56  Log::Fatal("SimulationBackup",
57  "Requested to backup data, but no backup file given.");
58  }
59 
60  // create temporary file
61  // if application crashes during backup; last backup is not corrupted
62  std::stringstream tmp_file;
63  tmp_file << "tmp_" << backup_file;
64 
65  // Backup
66  {
67  TFileRaii f(tmp_file.str(), "UPDATE");
68  auto* simulation = Simulation::GetActive();
69  f.Get()->WriteObject(simulation, kSimulationName.c_str());
70  IntegralTypeWrapper<size_t> wrapper(completed_simulation_steps);
71  f.Get()->WriteObject(&wrapper, kSimulationStepName.c_str());
73  f.Get()->WriteObject(&rv, kRuntimeVariableName.c_str());
74  // TODO(lukas) random number generator; all statics (e.g. Param)
75  }
76 
77  // remove last backup file
78  remove(backup_file.c_str());
79  // rename temporary file
80  rename(tmp_file.str().c_str(), backup_file.c_str());
81  }
82 
83  void Restore() {
84  if (!restore_) {
85  Log::Fatal("SimulationBackup",
86  "Requested to restore data, but no restore file given.");
87  }
88  after_restore_event_.clear();
89 
90  TFileRaii file(TFile::Open(restore_file.c_str()));
91  RuntimeVariables* restored_rv;
92  file.Get()->GetObject(kRuntimeVariableName.c_str(), restored_rv);
93  // check if runtime variables are the same
94  if (!(RuntimeVariables() == *restored_rv)) {
95  Log::Warning("SimulationBackup",
96  "Restoring simulation executed on a different system!");
97  }
98  Simulation* restored_simulation = nullptr;
99  file.Get()->GetObject(kSimulationName.c_str(), restored_simulation);
100  Simulation::GetActive()->Restore(std::move(*restored_simulation));
101  Log::Info("Scheduler", "Restored simulation from ", restore_file);
102  delete restored_simulation;
103 
104  // call all after restore events
105  for (auto&& event : after_restore_event_) {
106  event();
107  }
108  after_restore_event_.clear();
109  }
110 
112 
113  bool BackupEnabled();
114 
115  bool RestoreEnabled();
116 
117  private:
118  bool backup_ = false;
119  bool restore_ = true;
120  std::string backup_file;
121  std::string restore_file;
122 };
123 
124 } // namespace bdm
125 
126 #endif // CORE_SIMULATION_BACKUP_H_
bdm::SimulationBackup::kRuntimeVariableName
static const std::string kRuntimeVariableName
Definition: simulation_backup.h:38
bdm::RuntimeVariables
Definition: io.h:29
bdm::TFileRaii::Get
TFile * Get()
Definition: io.cc:76
bdm
Definition: agent.cc:39
bdm::IntegralTypeWrapper
Definition: io.h:64
bdm::SimulationBackup::SimulationBackup
SimulationBackup(const std::string &backup_file, const std::string &restore_file)
Definition: simulation_backup.cc:19
bdm::TFileRaii
Automatically close a TFile object using RAII pattern.
Definition: io.h:50
bdm::SimulationBackup::restore_
bool restore_
Definition: simulation_backup.h:119
bdm::SimulationBackup::GetSimulationStepsFromBackup
size_t GetSimulationStepsFromBackup()
Definition: simulation_backup.cc:42
bdm::SimulationBackup::RestoreEnabled
bool RestoreEnabled()
Definition: simulation_backup.cc:62
bdm::Log::Warning
static void Warning(const std::string &location, const Args &... parts)
Prints warning message.
Definition: log.h:67
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
bdm::Simulation::Restore
void Restore(Simulation &&restored)
Definition: simulation.cc:108
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
log.h
bdm::SimulationBackup
Definition: simulation_backup.h:33
io.h
bdm::SimulationBackup::BackupEnabled
bool BackupEnabled()
Definition: simulation_backup.cc:60
simulation.h
bdm::SimulationBackup::Restore
void Restore()
Definition: simulation_backup.h:83
bdm::SimulationBackup::backup_file
std::string backup_file
Definition: simulation_backup.h:120
bdm::SimulationBackup::kSimulationStepName
static const std::string kSimulationStepName
Definition: simulation_backup.h:37
bdm::SimulationBackup::kSimulationName
static const std::string kSimulationName
Definition: simulation_backup.h:36
bdm::SimulationBackup::Backup
void Backup(size_t completed_simulation_steps)
Definition: simulation_backup.h:54
bdm::SimulationBackup::restore_file
std::string restore_file
Definition: simulation_backup.h:121
bdm::Simulation::GetActive
static Simulation * GetActive()
This function returns the currently active Simulation simulation.
Definition: simulation.cc:67
bdm::SimulationBackup::backup_
bool backup_
Definition: simulation_backup.h:118
bdm::SimulationBackup::after_restore_event_
static std::vector< std::function< void()> > after_restore_event_
Definition: simulation_backup.h:47
bdm::Simulation
Definition: simulation.h:50