TRIQS/mpi 1.3.0
C++ interface to MPI
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
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
32namespace mpi {
33
44 // Wrapped `MPI_Comm` object.
45 MPI_Comm _com = MPI_COMM_WORLD;
46
47 public:
49 communicator() = default;
50
55 communicator(MPI_Comm c) : _com(c) {}
56
58 [[nodiscard]] MPI_Comm get() const noexcept { return _com; }
59
64 [[nodiscard]] int rank() const {
65 if (has_env) {
66 int num = 0;
67 check_mpi_call(MPI_Comm_rank(_com, &num), "MPI_Comm_rank");
68 return num;
69 } else
70 return 0;
71 }
72
77 [[nodiscard]] int size() const {
78 if (has_env) {
79 int num = 0;
80 check_mpi_call(MPI_Comm_size(_com, &num), "MPI_Comm_size");
81 return num;
82 } else
83 return 1;
84 }
85
98 [[nodiscard]] communicator split(int color, int key = 0) const {
99 if (has_env) {
100 communicator c;
101 check_mpi_call(MPI_Comm_split(_com, color, key, &c._com), "MPI_Comm_split");
102 return c;
103 } else
104 return {};
105 }
106
119 [[nodiscard]] communicator duplicate() const {
120 if (has_env) {
121 communicator c;
122 check_mpi_call(MPI_Comm_dup(_com, &c._com), "MPI_Comm_dup");
123 return c;
124 } else
125 return {};
126 }
127
137 void free() {
138 if (has_env) { check_mpi_call(MPI_Comm_free(&_com), "MPI_Comm_free"); }
139 }
140
145 void abort(int error_code) const {
146 if (has_env)
147 check_mpi_call(MPI_Abort(_com, error_code), "MPI_Abort");
148 else
149 std::abort();
150 }
151
152#ifdef BOOST_MPI_HPP
153 // Conversion to and from boost communicator, Keep for backward compatibility
154 inline operator boost::mpi::communicator() const { return boost::mpi::communicator(_com, boost::mpi::comm_duplicate); }
155 inline communicator(boost::mpi::communicator c) : _com(c) {}
156#endif // BOOST_MPI_HPP
157
173 void barrier(long poll_msec = 1) const {
174 if (has_env) {
175 if (poll_msec == 0) {
176 check_mpi_call(MPI_Barrier(_com), "MPI_Barrier");
177 } else {
178 MPI_Request req{};
179 int flag = 0;
180 check_mpi_call(MPI_Ibarrier(_com, &req), "MPI_Ibarrier");
181 while (!flag) {
182 check_mpi_call(MPI_Test(&req, &flag, MPI_STATUS_IGNORE), "MPI_Test");
183 usleep(poll_msec * 1000);
184 }
185 }
186 }
187 }
188 };
189
190} // namespace mpi
C++ wrapper around MPI_Comm providing various convenience functions.
int size() const
Get the size of the communicator.
communicator(MPI_Comm c)
Construct a communicator with a given MPI_Comm object.
int rank() const
Get the rank of the calling process in the communicator.
communicator duplicate() const
Duplicate the communicator.
void free()
Free the communicator.
communicator split(int color, int key=0) const
Split the communicator into disjoint subgroups.
MPI_Comm get() const noexcept
Get the wrapped MPI_Comm object.
void barrier(long poll_msec=1) const
Barrier synchronization.
void abort(int error_code) const
If mpi::has_env is true, MPI_Abort is called with the given error code, otherwise std::abort is calle...
communicator()=default
Construct a communicator with MPI_COMM_WORLD.
Provides an MPI environment for initializing and finalizing an MPI program.
static const bool has_env
Boolean variable that is true, if one of the environment variables OMPI_COMM_WORLD_RANK,...
void check_mpi_call(int errcode, const std::string &mpi_routine)
Check the success of an MPI call.
Definition utils.hpp:72
Provides general utilities related to MPI.