TRIQS/mpi
2.0.0
C++ interface to MPI
Toggle main menu visibility
Loading...
Searching...
No Matches
communicator.hpp
Go to the documentation of this file.
1
// Copyright (c) 2024 Simons Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0.txt
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// Authors: Thomas Hahn, Alexander Hampel, Olivier Parcollet, Nils Wentzell
16
21
22
#pragma once
23
24
#include "
./environment.hpp
"
25
#include "
./utils.hpp
"
26
27
#include <mpi.h>
28
29
#include <cstdlib>
30
#include <unistd.h>
31
32
namespace
mpi {
33
34
// Forward declaration.
35
class
shared_communicator
;
36
51
class
communicator
{
52
public
:
54
communicator
() =
default
;
55
60
communicator
(MPI_Comm c) : comm_(c) {}
61
63
[[nodiscard]] MPI_Comm
get
() const noexcept {
return
comm_; }
64
66
[[nodiscard]]
bool
is_null
() const noexcept {
return
comm_ == MPI_COMM_NULL; }
67
72
[[nodiscard]]
int
rank
()
const
{
73
int
r = 0;
74
if
(
has_env
)
check_mpi_call
(MPI_Comm_rank(comm_, &r),
"MPI_Comm_rank"
);
75
return
r;
76
}
77
82
[[nodiscard]]
int
size
()
const
{
83
int
s = 1;
84
if
(
has_env
)
check_mpi_call
(MPI_Comm_size(comm_, &s),
"MPI_Comm_size"
);
85
return
s;
86
}
87
103
[[nodiscard]]
communicator
split
(
int
color,
int
key = 0)
const
{
104
communicator
c{};
105
if
(
has_env
)
check_mpi_call
(MPI_Comm_split(comm_, color, key, &c.comm_),
"MPI_Comm_split"
);
106
return
c;
107
}
108
123
[[nodiscard]]
shared_communicator
split_shared
(
int
split_type = MPI_COMM_TYPE_SHARED,
int
key = 0)
const
;
124
137
[[nodiscard]]
communicator
duplicate
()
const
{
138
communicator
c{};
139
if
(
has_env
)
check_mpi_call
(MPI_Comm_dup(comm_, &c.comm_),
"MPI_Comm_dup"
);
140
return
c;
141
}
142
152
void
free
() {
153
if
(
has_env
&& !
is_null
())
check_mpi_call
(MPI_Comm_free(&comm_),
"MPI_Comm_free"
);
154
}
155
160
void
abort
(
int
error_code)
const
{
161
if
(
has_env
) {
162
check_mpi_call
(MPI_Abort(comm_, error_code),
"MPI_Abort"
);
163
}
else
{
164
std::abort();
165
}
166
}
167
168
#ifdef BOOST_MPI_HPP
169
// Conversion to and from boost communicator, Keep for backward compatibility
170
inline
operator
boost::mpi::communicator()
const
{
return
boost::mpi::communicator(comm_, boost::mpi::comm_duplicate); }
171
inline
communicator
(boost::mpi::communicator c) : comm_(c) {}
172
#endif
// BOOST_MPI_HPP
173
190
void
barrier
(
long
poll_msec = 1)
const
{
191
if
(
has_env
) {
192
if
(poll_msec == 0) {
193
check_mpi_call
(MPI_Barrier(comm_),
"MPI_Barrier"
);
194
}
else
{
195
MPI_Request req{};
196
int
flag = 0;
197
check_mpi_call
(MPI_Ibarrier(comm_, &req),
"MPI_Ibarrier"
);
198
while
(!flag) {
199
check_mpi_call
(MPI_Test(&req, &flag, MPI_STATUS_IGNORE),
"MPI_Test"
);
200
usleep(poll_msec * 1000);
201
}
202
}
203
}
204
}
205
206
private
:
207
MPI_Comm comm_ = MPI_COMM_WORLD;
208
};
209
218
class
shared_communicator
:
public
communicator
{
219
public
:
220
// Make the constructors of mpi::communicator accessible.
221
using
communicator::communicator
;
222
224
shared_communicator
() :
communicator
(MPI_COMM_NULL) {}
225
};
226
227
[[nodiscard]]
inline
shared_communicator
communicator::split_shared
(
int
split_type,
int
key)
const
{
228
shared_communicator
c{};
229
if
(
has_env
)
check_mpi_call
(MPI_Comm_split_type(comm_, split_type, key, MPI_INFO_NULL, &c.comm_),
"MPI_Comm_split_type"
);
230
return
c;
231
}
232
233
}
// namespace mpi
mpi::communicator::size
int size() const
Get the size of the communicator.
Definition
communicator.hpp:82
mpi::communicator::communicator
communicator(MPI_Comm c)
Construct a communicator by wrapping a given MPI_Comm object.
Definition
communicator.hpp:60
mpi::communicator::split_shared
shared_communicator split_shared(int split_type=MPI_COMM_TYPE_SHARED, int key=0) const
Partition the communicator into subcommunicators according to their type.
Definition
communicator.hpp:227
mpi::communicator::rank
int rank() const
Get the rank of the calling process in the communicator.
Definition
communicator.hpp:72
mpi::communicator::duplicate
communicator duplicate() const
Duplicate the communicator.
Definition
communicator.hpp:137
mpi::communicator::free
void free()
Free the communicator.
Definition
communicator.hpp:152
mpi::communicator::split
communicator split(int color, int key=0) const
Split the communicator into disjoint subgroups.
Definition
communicator.hpp:103
mpi::communicator::get
MPI_Comm get() const noexcept
Get the wrapped MPI_Comm object.
Definition
communicator.hpp:63
mpi::communicator::is_null
bool is_null() const noexcept
Check if the contained MPI_Comm is MPI_COMM_NULL.
Definition
communicator.hpp:66
mpi::communicator::barrier
void barrier(long poll_msec=1) const
Barrier synchronization.
Definition
communicator.hpp:190
mpi::communicator::abort
void abort(int error_code) const
If mpi::has_env is true, MPI_Abort is called with the given error code, otherwise it calls std::abort...
Definition
communicator.hpp:160
mpi::communicator::communicator
communicator()=default
Construct a communicator with MPI_COMM_WORLD.
mpi::shared_communicator
C++ wrapper around MPI_Comm that is a result of the mpi::communicator::split_shared operation.
Definition
communicator.hpp:218
mpi::shared_communicator::shared_communicator
shared_communicator()
Construct a shared communicator with MPI_COMM_NULL.
Definition
communicator.hpp:224
mpi::shared_communicator::communicator
communicator()=default
Construct a communicator with MPI_COMM_WORLD.
environment.hpp
Provides an MPI environment for initializing and finalizing an MPI program.
mpi::has_env
static const bool has_env
Boolean variable that checks if there is an active MPI runtime environment.
Definition
environment.hpp:65
mpi::check_mpi_call
void check_mpi_call(int errcode, const std::string &mpi_routine)
Check the success of an MPI call.
Definition
utils.hpp:48
utils.hpp
Provides general utilities related to MPI.
mpi
communicator.hpp
Generated by
1.17.0