Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
mpi_utils.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/parallel/mpi_utils.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/mpi_utils.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/mpi_utils.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17
33
34#pragma once
35
36#ifdef BERTINI2_HAVE_MPI
37
39
40#include <sstream>
41#include <string>
42
43#include <boost/archive/binary_iarchive.hpp>
44#include <boost/archive/binary_oarchive.hpp>
45
46namespace bertini {
47namespace parallel {
48
49
53template<typename T>
54void mpi_send_serialized(MPI_Comm comm, int dest, int tag, T const& obj)
55{
56 std::ostringstream oss;
57 {
58 boost::archive::binary_oarchive oa(oss);
59 oa << obj;
60 }
61 std::string const buf = oss.str();
62 MPI_Send(buf.data(), static_cast<int>(buf.size()), MPI_BYTE, dest, tag, comm);
63}
64
65
74template<typename T>
75int mpi_recv_serialized_any(MPI_Comm comm, int tag, T& obj)
76{
77 MPI_Status probe_status;
78 MPI_Probe(MPI_ANY_SOURCE, tag, comm, &probe_status);
79
80 int source = probe_status.MPI_SOURCE;
81 int count = 0;
82 MPI_Get_count(&probe_status, MPI_BYTE, &count);
83
84 std::string buf(static_cast<std::size_t>(count), '\0');
85 MPI_Status recv_status;
86 MPI_Recv(buf.data(), count, MPI_BYTE, source, tag, comm, &recv_status);
87
88 std::istringstream iss(buf);
89 boost::archive::binary_iarchive ia(iss);
90 ia >> obj;
91
92 return source;
93}
94
95
99template<typename T>
100void mpi_recv_serialized(MPI_Comm comm, int source, int tag, T& obj)
101{
102 MPI_Status probe_status;
103 MPI_Probe(source, tag, comm, &probe_status);
104
105 int count = 0;
106 MPI_Get_count(&probe_status, MPI_BYTE, &count);
107
108 std::string buf(static_cast<std::size_t>(count), '\0');
109 MPI_Status recv_status;
110 MPI_Recv(buf.data(), count, MPI_BYTE, source, tag, comm, &recv_status);
111
112 std::istringstream iss(buf);
113 boost::archive::binary_iarchive ia(iss);
114 ia >> obj;
115}
116
117
124inline void mpi_broadcast_string(MPI_Comm comm, std::string& s, int root)
125{
126 int len = static_cast<int>(s.size());
127 MPI_Bcast(&len, 1, MPI_INT, root, comm);
128 s.resize(static_cast<std::size_t>(len));
129 MPI_Bcast(s.data(), len, MPI_CHAR, root, comm);
130}
131
132
140template<typename T>
141void mpi_broadcast_serialized(MPI_Comm comm, T& obj, int root)
142{
143 int rank = 0;
144 MPI_Comm_rank(comm, &rank);
145
146 std::string s;
147 if (rank == root)
148 {
149 std::ostringstream oss;
150 boost::archive::binary_oarchive oa(oss);
151 oa << obj;
152 s = oss.str();
153 }
154
155 mpi_broadcast_string(comm, s, root);
156
157 if (rank != root)
158 {
159 std::istringstream iss(s);
160 boost::archive::binary_iarchive ia(iss);
161 ia >> obj;
162 }
163}
164
165
166} // namespace parallel
167} // namespace bertini
168
169#endif // BERTINI2_HAVE_MPI
Single include point for the MPI C API.
Definition initialize_finalize.hpp:43
Definition algorithm_builder.hpp:36