pomerol  2.1
mpi_dispatcher.hpp
Go to the documentation of this file.
1 //
2 // This file is part of pomerol, an exact diagonalization library aimed at
3 // solving condensed matter models of interacting fermions.
4 //
5 // Copyright (C) 2016-2025 A. Antipov, I. Krivenko and contributors
6 //
7 // This Source Code Form is subject to the terms of the Mozilla Public
8 // License, v. 2.0. If a copy of the MPL was not distributed with this
9 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
15 
16 #ifndef POMEROL_INCLUDE_MPI_DISPATCHER_MPI_DISPATCHER_HPP
17 #define POMEROL_INCLUDE_MPI_DISPATCHER_MPI_DISPATCHER_HPP
18 
19 #include <mpi.h>
20 
21 #include <cstddef>
22 #include <map>
23 #include <stack>
24 #include <vector>
25 
26 namespace pMPI {
27 
30 
32 enum WorkerTag : int {
34  Work,
35  Finish
36 };
37 
39 using JobId = int;
41 using WorkerId = int;
42 
44 struct MPIWorker {
46  MPI_Comm Comm;
48  WorkerId const id;
50  int const boss;
51 
55  MPIWorker(MPI_Comm const& Comm, int Boss);
57  void receive_order();
61  bool is_finished();
63  bool is_working();
64 
66  JobId current_job() const { return current_job_; };
67 
68 protected:
72  MPI_Request req;
75 };
76 
78 struct MPIMaster {
80  MPI_Comm Comm;
82  int Ntasks;
84  int Nprocs;
85 
87  std::stack<JobId> JobStack;
89  std::stack<WorkerId> WorkerStack;
90 
92  std::map<JobId, WorkerId> DispatchMap;
94  std::vector<JobId> task_numbers;
96  std::vector<WorkerId> worker_pool;
98  std::map<WorkerId, int> WorkerIndices;
99 
101  std::vector<MPI_Request> wait_statuses;
103  std::vector<bool> workers_finish;
104 
109  MPIMaster(MPI_Comm const& Comm, std::vector<WorkerId> worker_pool, std::vector<JobId> task_numbers);
115  MPIMaster(MPI_Comm const& Comm, std::vector<JobId> task_numbers, bool include_boss = true);
121  MPIMaster(MPI_Comm const& Comm, std::size_t ntasks, bool include_boss = true);
122 
126  void order_worker(WorkerId worker_id, JobId job);
128  void order();
132  bool is_finished() const;
133 
134 private:
135  // Implementation details
136  void fill_stack_();
137 };
138 
140 
141 } // namespace pMPI
142 
143 #endif // #ifndef POMEROL_INCLUDE_MPI_DISPATCHER_MPI_DISPATCHER_HPP
int JobId
ID of a job.
int WorkerId
ID of a worker process.
WorkerTag
MPI message tags used in communications between the master and its workers.
@ Pending
A worker is waiting for a new job.
@ Work
Request a worker to do a job.
@ Finish
Order a worker to shut down.
A bunch of tools used for MPI-parallelization of computations.
Definition: misc.hpp:21
Abstraction of an MPI master process.
void order()
Request the next available worker to perform the next job from the job stack.
MPI_Comm Comm
MPI communicator.
void order_worker(WorkerId worker_id, JobId job)
int Nprocs
Total number of worker processes.
std::map< JobId, WorkerId > DispatchMap
A mapping from job IDs to IDs of the workers assigned to perform the jobs.
int Ntasks
Total number of jobs.
std::vector< bool > workers_finish
Flags to mark workers that have been shut down.
std::vector< MPI_Request > wait_statuses
MPI request handles used to perform non-blocking communications with the workers.
MPIMaster(MPI_Comm const &Comm, std::vector< WorkerId > worker_pool, std::vector< JobId > task_numbers)
std::stack< JobId > JobStack
Stack of the jobs yet to be assigned to a worker.
std::vector< WorkerId > worker_pool
A list of IDs of all worker processes.
void check_workers()
Check which workers have become available and which have been shut down.
std::vector< JobId > task_numbers
A list of IDs of all jobs to be completed.
std::map< WorkerId, int > WorkerIndices
Worker IDs and their serial numbers from the [0; worker_pool.size()[ range.
MPIMaster(MPI_Comm const &Comm, std::vector< JobId > task_numbers, bool include_boss=true)
bool is_finished() const
Have all the workers been shut down?
std::stack< WorkerId > WorkerStack
Stack of currently pending workers.
MPIMaster(MPI_Comm const &Comm, std::size_t ntasks, bool include_boss=true)
Abstraction of an MPI worker process.
void receive_order()
Check if there is an outstanding order from the master.
MPI_Request req
An MPI request handle used for non-blocking communications.
WorkerTag Status
Current state of this worker.
MPI_Comm Comm
MPI communicator.
MPIWorker(MPI_Comm const &Comm, int Boss)
WorkerId const id
Worker ID of this process.
JobId current_job() const
Get the ID of the job currently assigned to this worker.
int const boss
Rank of the master process.
bool is_working()
Is a job being processed by this worker?
bool is_finished()
Has this worker process finished execution.
JobId current_job_
ID of the job currently assigned to this worker.
void report_job_done()
Notify the master about a job's completion.