Pakman
MPIMaster.h
1 #ifndef MPIMASTER_H
2 #define MPIMASTER_H
3 
4 #include <queue>
5 #include <vector>
6 #include <set>
7 #include <string>
8 
9 #include <mpi.h>
10 
11 #include "core/common.h"
12 
13 #include "AbstractMaster.h"
14 
15 class LongOptions;
16 class Arguments;
17 
38 class MPIMaster : public AbstractMaster
39 {
40  public:
41 
47  MPIMaster(bool *p_program_terminated);
48 
50  virtual ~MPIMaster() override;
51 
53  virtual bool isActive() const override;
54 
56  virtual bool needMorePendingTasks() const override;
57 
62  virtual void pushPendingTask(const std::string& input_string) override;
63 
65  virtual bool finishedTasksEmpty() const override;
66 
68  virtual TaskHandler& frontFinishedTask() override;
69 
71  virtual void popFinishedTask() override;
72 
74  virtual void flush() override;
75 
77  virtual void terminate() override;
78 
80  static std::string help();
81 
86  static void addLongOptions(LongOptions& lopts);
87 
96  static void run(controller_t controller, const Arguments& args);
97 
105  static void cleanup();
106 
107  protected:
108 
110  virtual void iterate() override;
111 
112  private:
113 
123  enum state_t { normal, flushing, terminated };
124 
126  // Do normal stuff
127  void doNormalStuff();
128 
129  // Do flushing stuff
130  void doFlushingStuff();
131 
132  // Listen to messages from Managers.
133  void listenToManagers();
134 
135  // Pop finished tasks from busy queue and insert into finished queue
136  void popBusyQueue();
137 
138  // Delegate to Managers
139  void delegateToManagers();
140 
141  // Flush all task queues (finished, busy, pending)
142  void flushQueues();
143 
144  // Discard any messages, error codes and signals until all Managers are
145  // idle
146  void discardMessagesErrorCodesAndSignals();
147 
148  // Probe for message
149  bool probeMessage() const;
150 
151  // Probe for signal
152  bool probeSignal() const;
153 
154  // Probe for Manager rank of incoming message
155  int probeMessageManager() const;
156 
157  // Probe for Manager rank of incoming signal
158  int probeSignalManager() const;
159 
160  // Receive message from Manager
161  std::string receiveMessage(int manager_rank) const;
162 
163  // Receive signal from Manager
164  int receiveSignal(int manager_rank) const;
165 
166  // Receive error cdoe from Manager
167  int receiveErrorCode(int manager_rank) const;
168 
169  // Send message to a Manager
170  void sendMessageToManager(int manager_rank,
171  const std::string& message_string);
172 
173  // Send signal to all Managers
174  void sendSignalToAllManagers(int signal);
175 
177  // Initial state is normal
178  state_t m_state = normal;
179 
180  // Communicator size
181  const int m_comm_size;
182 
183  // Flag for terminating Master and Managers
184  bool m_master_manager_terminated = false;
185 
186  // Flag for flushing Workers
187  bool m_worker_flushed = false;
188 
189  // Set of idle managers
190  std::set<int> m_idle_managers;
191 
192  // Mapping from Manager to corresponding task
193  std::vector<TaskHandler*> m_map_manager_to_task;
194 
195  // Finished tasks
196  std::queue<TaskHandler> m_finished_tasks;
197 
198  // Busy tasks
199  std::queue<TaskHandler> m_busy_tasks;
200 
201  // Pending tasks
202  std::queue<TaskHandler> m_pending_tasks;
203 
204  // Message buffers
205  std::vector<std::string> m_message_buffers;
206 
207  // Message requests
208  std::vector<MPI_Request> m_message_requests;
209 
210  // Signal buffer (assumption: every signal goes to all Managers, so
211  // only one signal buffer is required)
212  int m_signal_buffer;
213 
214  // Signal requests
215  std::vector<MPI_Request> m_signal_requests;
216 
217  // Entered iterate()
218  bool m_entered = false;
219 };
220 
221 #endif // MPIMASTER_H
virtual void terminate() override
Definition: MPIMaster.cc:234
virtual void iterate() override
Definition: MPIMaster.cc:62
virtual TaskHandler & frontFinishedTask() override
Definition: MPIMaster.cc:213
static void cleanup()
virtual void pushPendingTask(const std::string &input_string) override
Definition: MPIMaster.cc:201
controller_t
Definition: common.h:45
virtual ~MPIMaster() override
Definition: MPIMaster.cc:36
virtual bool isActive() const override
Definition: MPIMaster.cc:56
virtual void popFinishedTask() override
Definition: MPIMaster.cc:219
static void run(controller_t controller, const Arguments &args)
virtual bool needMorePendingTasks() const override
Definition: MPIMaster.cc:91
virtual bool finishedTasksEmpty() const override
Definition: MPIMaster.cc:207
virtual void flush() override
Definition: MPIMaster.cc:225
static std::string help()
MPIMaster(bool *p_program_terminated)
Definition: MPIMaster.cc:19
static void addLongOptions(LongOptions &lopts)