Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
manager.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/parallel/manager.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/parallel/manager.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/parallel/manager.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17
41
42#pragma once
43
44#ifdef BERTINI2_HAVE_MPI
45
48
50
51#include <functional>
52#include <limits>
53#include <map>
54#include <queue>
55
56namespace bertini {
57namespace parallel {
58
59
69template<typename TaskT, typename ResultT>
70void RunManagerLoop(
71 MPI_Comm comm,
72 std::queue<TaskT>& tasks,
73 std::function<void(ResultT const&)> store_fn)
74{
75 int world_size = 0;
76 MPI_Comm_size(comm, &world_size);
77 const int num_workers = world_size - 1;
78 if (num_workers <= 0)
79 return;
80
81 TaskT sentinel_value = detail::make_sentinel(TaskT{});
82
83 // Each worker announces how many tasks it can have in flight at once
84 // (1 for a serial worker, n_threads for a threaded one).
85 std::map<int, int> capacity; // rank -> max tasks in flight
86 for (int ii = 0; ii < num_workers; ++ii)
87 {
88 int cap = 0;
89 MPI_Status status;
90 MPI_Recv(&cap, 1, MPI_INT, MPI_ANY_SOURCE, TAG_CAPACITY, comm, &status);
91 capacity[status.MPI_SOURCE] = (cap >= 1) ? cap : 1;
92 }
93
94 std::map<int, int> outstanding; // rank -> tasks dispatched but not yet returned
95 int active_workers = num_workers;
96
97 // Seed each worker up to its capacity. A rank seeded with zero tasks
98 // (queue exhausted) is immediately sent the sentinel.
99 for (auto const& [rank, cap] : capacity)
100 {
101 outstanding[rank] = 0;
102 for (int ii = 0; ii < cap && !tasks.empty(); ++ii)
103 {
104 TaskT task = tasks.front(); tasks.pop();
105 mpi_send_serialized(comm, rank, TAG_WORK_ITEM, task);
106 ++outstanding[rank];
107 }
108 if (outstanding[rank] == 0)
109 {
110 mpi_send_serialized(comm, rank, TAG_WORK_ITEM, sentinel_value);
111 --active_workers;
112 }
113 }
114
115 // Drain: refill a rank as each result arrives; retire it with a sentinel
116 // once the queue is empty and its last outstanding task has come back.
117 while (active_workers > 0)
118 {
119 ResultT result;
120 int worker = mpi_recv_serialized_any(comm, TAG_RESULT, result);
121
122 store_fn(result);
123 --outstanding[worker];
124
125 if (!tasks.empty())
126 {
127 TaskT task = tasks.front(); tasks.pop();
128 mpi_send_serialized(comm, worker, TAG_WORK_ITEM, task);
129 ++outstanding[worker];
130 }
131 else if (outstanding[worker] == 0)
132 {
133 mpi_send_serialized(comm, worker, TAG_WORK_ITEM, sentinel_value);
134 --active_workers;
135 }
136 }
137}
138
139} // namespace parallel
140} // namespace bertini
141
142#endif // BERTINI2_HAVE_MPI
Single include point for the MPI C API.
Serialization-aware MPI send/recv helpers using plain C MPI + Boost.Serialization.
std::size_t make_sentinel(std::size_t)
Make the sentinel index value.
Definition path_result.hpp:192
Definition initialize_finalize.hpp:43
constexpr int TAG_CAPACITY
MPI message tag: worker -> manager, max tasks in flight on that rank.
Definition path_result.hpp:49
constexpr int TAG_RESULT
MPI message tag: worker -> manager result.
Definition path_result.hpp:48
constexpr int TAG_WORK_ITEM
MPI message tag: manager -> worker work item.
Definition path_result.hpp:47
Definition algorithm_builder.hpp:36
Result struct used in the manager-worker MPI protocol for ZeroDim.