BioDynaMo  v1.05.119-a4ff3934
multi_simulation_manager.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_MULTI_SIMULATION_MULTI_SIMULATION_MANAGER_H_
16 #define CORE_MULTI_SIMULATION_MULTI_SIMULATION_MANAGER_H_
17 
18 #ifdef USE_MPI
19 
20 #include <algorithm>
21 #include <fstream>
22 #include <functional>
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
32 
33 using std::cout;
34 using std::endl;
35 using std::string;
36 using std::to_string;
37 using std::vector;
38 
39 namespace bdm {
40 namespace experimental {
41 
42 static const unsigned int kMaster = 0;
43 
44 enum Status { kBusy, kAvail };
45 enum Tag { kReady, kResult, kTask, kKill };
46 
49 class MultiSimulationManager {
50  public:
51  void Log(string s);
52 
53  explicit MultiSimulationManager(
54  int ws, Param *default_params,
55  std::function<void(Param *, TimeSeries *)> simulate);
56 
57  void WriteTimingsToFile();
58 
59  void IngestData(const std::string &data_file);
60 
61  ~MultiSimulationManager();
62 
63  // Copy the timing results of the specified worker
64  void RecordTiming(int worker, TimingAggregator *agg);
65 
66  // Send kill message to all workers
67  void KillAllWorkers();
68 
69  // Receive timing objects of all workers
70  void GetTimingsFromWorkers();
71 
72  int Start();
73 
74  private:
75  friend struct ParticleSwarm;
76  // Returns the ID of the first available worker in the list. Returns -1 if
77  // there is no available worker.
78  int GetFirstAvailableWorker();
79 
80  // Changes the status
81  void ChangeStatusWorker(int worker, Status s);
82 
83  // Executes the specified function for all workers. Starting from index 1,
84  // because 0 is the master's ID.
85  void ForAllWorkers(const std::function<void(int w)> &lambda);
86 
87  vector<Status> availability_;
88  int worldsize_;
89  TimingAggregator ta_;
90  Param *default_params_;
91  std::function<void(Param *, TimeSeries *)> simulate_;
92  std::vector<TimingAggregator> timings_;
93 };
94 
97 class Worker {
98  public:
99  explicit Worker(int myrank,
100  std::function<void(Param *, TimeSeries *)> simulate);
101 
102  template <typename T>
103  void Log(T s) {
104  Log::Info("MultiSimulationManager", "[W", myrank_, "]: ", s);
105  }
106 
107  ~Worker();
108 
109  int Start();
110 
111  private:
112  void IncrementTaskCount();
113 
114  int myrank_;
115  unsigned int task_count_ = 0;
116  std::function<void(Param *, TimeSeries *)> simulate_;
117  TimingAggregator ta_;
118 };
119 
120 } // namespace experimental
121 } // namespace bdm
122 
123 #endif // USE_MPI
124 
125 #endif // CORE_MULTI_SIMULATION_MULTI_SIMULATION_MANAGER_H_
timing_aggregator.h
bdm
Definition: agent.cc:39
algorithm_registry.h
time_series.h
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
dynamic_loop.h