TRIQS/mpi 1.3.0
C++ interface to MPI
Loading...
Searching...
No Matches
group.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 "./communicator.hpp"
25#include "./environment.hpp"
26#include "./utils.hpp"
27
28#include <mpi.h>
29
30#include <utility>
31#include <vector>
32
33namespace mpi {
34
46 class group {
47 public:
49 group() = default;
50
52 group(group const &) = delete;
53
55 group &operator=(group const &) = delete;
56
58 group(group &&other) noexcept : grp_{std::exchange(other.grp_, MPI_GROUP_NULL)} {}
59
61 group &operator=(group &&rhs) noexcept {
62 if (this != std::addressof(rhs)) {
63 free();
64 grp_ = std::exchange(rhs.grp_, MPI_GROUP_NULL);
65 }
66 return *this;
67 }
68
70 ~group() { free(); }
71
76 explicit group(MPI_Group grp) : grp_(grp) {}
77
82 explicit group(communicator c) {
83 if (has_env) check_mpi_call(MPI_Comm_group(c.get(), &grp_), "MPI_Comm_group");
84 }
85
87 [[nodiscard]] MPI_Group get() const noexcept { return grp_; }
88
90 [[nodiscard]] bool is_null() const noexcept { return grp_ == MPI_GROUP_NULL; }
91
96 [[nodiscard]] int rank() const {
97 int r = 0;
98 if (has_env) check_mpi_call(MPI_Group_rank(grp_, &r), "MPI_Group_rank");
99 return r;
100 }
101
106 [[nodiscard]] int size() const {
107 int s = 1;
108 if (has_env) check_mpi_call(MPI_Group_size(grp_, &s), "MPI_Group_size");
109 return s;
110 }
111
120 [[nodiscard]] group include(std::vector<int> const &ranks) const {
121 MPI_Group newgroup = MPI_GROUP_NULL;
122 if (has_env) check_mpi_call(MPI_Group_incl(grp_, static_cast<int>(ranks.size()), ranks.data(), &newgroup), "MPI_Group_incl");
123 return group{newgroup};
124 }
125
127 void free() noexcept {
128 if (has_env && !is_null()) MPI_Group_free(&grp_);
129 }
130
131 private:
132 MPI_Group grp_ = MPI_GROUP_NULL;
133 };
134
135} // namespace mpi
C++ wrapper around MPI_Comm providing various convenience functions.
MPI_Comm get() const noexcept
Get the wrapped MPI_Comm object.
bool is_null() const noexcept
Check if the contained MPI_Group is MPI_GROUP_NULL.
Definition group.hpp:90
void free() noexcept
Free the group by calling MPI_Group_free (if it is not is_null()).
Definition group.hpp:127
group include(std::vector< int > const &ranks) const
Create a new group by calling MPI_Group_incl.
Definition group.hpp:120
~group()
Destructor calls free() to release the group.
Definition group.hpp:70
int rank() const
Get the rank of the calling process in the group.
Definition group.hpp:96
group(group &&other) noexcept
Move constructor leaves moved-from object with MPI_GROUP_NULL.
Definition group.hpp:58
group(communicator c)
Create a group from a communicator by calling MPI_Comm_group.
Definition group.hpp:82
group()=default
Construct a group with MPI_GROUP_NULL.
group(MPI_Group grp)
Take ownership of an existing MPI_Group object.
Definition group.hpp:76
group & operator=(group &&rhs) noexcept
Move assignment operator leaves moved-from object with MPI_GROUP_NULL.
Definition group.hpp:61
group(group const &)=delete
Deleted copy constructor.
MPI_Group get() const noexcept
Get the wrapped MPI_Group object.
Definition group.hpp:87
int size() const
Get the size of the group.
Definition group.hpp:106
group & operator=(group const &)=delete
Deleted copy assignment operator.
Provides a C++ wrapper class for an MPI_Comm object.
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:51
Provides general utilities related to MPI.