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-2024 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,
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();
59  void report_job_done();
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();
130  void check_workers();
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
pMPI::MPIWorker::receive_order
void receive_order()
Check if there is an outstanding order from the master.
pMPI::MPIWorker::current_job_
JobId current_job_
ID of the job currently assigned to this worker.
Definition: mpi_dispatcher.hpp:66
pMPI::MPIMaster::Comm
MPI_Comm Comm
MPI communicator.
Definition: mpi_dispatcher.hpp:80
pMPI::MPIWorker::req
MPI_Request req
An MPI request handle used for non-blocking communications.
Definition: mpi_dispatcher.hpp:72
pMPI::MPIWorker::is_working
bool is_working()
Is a job being processed by this worker?
pMPI::MPIMaster::task_numbers
std::vector< JobId > task_numbers
A list of IDs of all jobs to be completed.
Definition: mpi_dispatcher.hpp:94
pMPI::MPIWorker::MPIWorker
MPIWorker(MPI_Comm const &Comm, int Boss)
pMPI::MPIMaster::wait_statuses
std::vector< MPI_Request > wait_statuses
MPI request handles used to perform non-blocking communications with the workers.
Definition: mpi_dispatcher.hpp:101
pMPI::MPIMaster::JobStack
std::stack< JobId > JobStack
Stack of the jobs yet to be assigned to a worker.
Definition: mpi_dispatcher.hpp:87
pMPI::MPIMaster::Nprocs
int Nprocs
Total number of worker processes.
Definition: mpi_dispatcher.hpp:84
pMPI::MPIMaster::WorkerStack
std::stack< WorkerId > WorkerStack
Stack of currently pending workers.
Definition: mpi_dispatcher.hpp:89
pMPI::MPIMaster::Ntasks
int Ntasks
Total number of jobs.
Definition: mpi_dispatcher.hpp:82
pMPI::MPIWorker::boss
const int boss
Rank of the master process.
Definition: mpi_dispatcher.hpp:50
pMPI::MPIMaster::WorkerIndices
std::map< WorkerId, int > WorkerIndices
Worker IDs and their serial numbers from the [0; worker_pool.size()[ range.
Definition: mpi_dispatcher.hpp:98
pMPI::MPIMaster::order
void order()
Request the next available worker to perform the next job from the job stack.
pMPI::MPIWorker::current_job
JobId current_job() const
Get the ID of the job currently assigned to this worker.
Definition: mpi_dispatcher.hpp:66
pMPI::MPIWorker::Comm
MPI_Comm Comm
MPI communicator.
Definition: mpi_dispatcher.hpp:46
pMPI::WorkerTag
WorkerTag
MPI message tags used in communications between the master and its workers.
Definition: mpi_dispatcher.hpp:32
pMPI::Finish
@ Finish
Order a worker to shut down.
Definition: mpi_dispatcher.hpp:35
pMPI::MPIMaster::workers_finish
std::vector< bool > workers_finish
Flags to mark workers that have been shut down.
Definition: mpi_dispatcher.hpp:103
pMPI::MPIWorker::id
const WorkerId id
Worker ID of this process.
Definition: mpi_dispatcher.hpp:48
pMPI::MPIMaster
Abstraction of an MPI master process.
Definition: mpi_dispatcher.hpp:78
pMPI::MPIMaster::is_finished
bool is_finished() const
Have all the workers been shut down?
pMPI::MPIWorker::is_finished
bool is_finished()
Has this worker process finished execution.
pMPI::Work
@ Work
Request a worker to do a job.
Definition: mpi_dispatcher.hpp:34
pMPI::Pending
@ Pending
A worker is waiting for a new job.
Definition: mpi_dispatcher.hpp:33
pMPI::WorkerId
int WorkerId
ID of a worker process.
Definition: mpi_dispatcher.hpp:41
pMPI::MPIMaster::order_worker
void order_worker(WorkerId worker_id, JobId job)
pMPI::MPIMaster::worker_pool
std::vector< WorkerId > worker_pool
A list of IDs of all worker processes.
Definition: mpi_dispatcher.hpp:96
pMPI::MPIMaster::check_workers
void check_workers()
Check which workers have become available and which have been shut down.
pMPI::MPIWorker::Status
WorkerTag Status
Current state of this worker.
Definition: mpi_dispatcher.hpp:74
pMPI::MPIWorker
Abstraction of an MPI worker process.
Definition: mpi_dispatcher.hpp:44
pMPI::MPIMaster::MPIMaster
MPIMaster(MPI_Comm const &Comm, std::vector< WorkerId > worker_pool, std::vector< JobId > task_numbers)
pMPI::MPIMaster::DispatchMap
std::map< JobId, WorkerId > DispatchMap
A mapping from job IDs to IDs of the workers assigned to perform the jobs.
Definition: mpi_dispatcher.hpp:92
pMPI
A bunch of tools used for MPI-parallelization of computations.
Definition: misc.hpp:21
pMPI::JobId
int JobId
ID of a job.
Definition: mpi_dispatcher.hpp:39
pMPI::MPIWorker::report_job_done
void report_job_done()
Notify the master about a job's completion.