TRIQS/mpi 1.3.0
C++ interface to MPI
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-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: Nils Wentzell
16
21
22#pragma once
23
24#include "./communicator.hpp"
26#include "./macros.hpp"
27
28#include <mpi.h>
29
30#include <optional>
31#include <concepts>
32
33namespace mpi {
34
39
52 template <std::default_initializable T>
53 void mpi_broadcast(std::optional<T> &opt, communicator c = {}, int root = 0) {
54 bool has_val = opt.has_value();
55 broadcast(has_val, c, root);
56
57 if (has_val) {
58 if (!opt.has_value()) opt.emplace();
59 broadcast(*opt, c, root);
60 } else {
61 opt.reset();
62 }
63 }
64
82 template <typename T1, std::default_initializable T2>
83 void mpi_reduce_into(std::optional<T1> const &opt_in, std::optional<T2> &opt_out, communicator c = {}, int root = 0, bool all = false,
84 MPI_Op op = MPI_SUM) {
85 // Verify consistency
86 EXPECTS_WITH_MESSAGE(all_equal<int>(opt_in.has_value(), c),
87 "mpi::reduce_into for std::optional requires all ranks to have consistent has_value state");
88
89 bool const receives = (c.rank() == root || all);
90
91 if (opt_in.has_value()) {
92 if (receives) {
93 if (!opt_out.has_value()) opt_out.emplace();
94 reduce_into(*opt_in, *opt_out, c, root, all, op);
95 } else {
96 T2 dummy;
97 reduce_into(*opt_in, dummy, c, root, all, op);
98 }
99 } else if (receives) {
100 opt_out.reset();
101 }
102 }
103
105
106} // namespace mpi
C++ wrapper around MPI_Comm providing various convenience functions.
Provides a C++ wrapper class for an MPI_Comm object.
Provides generic implementations for a subset of collective MPI communications (broadcast,...
void mpi_reduce_into(std::array< T1, N1 > const &arr_in, std::array< T2, N2 > &arr_out, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for a std::array that reduces directly into an existing output array.
Definition array.hpp:99
void reduce_into(T1 &&x_in, T2 &&x_out, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Generic MPI reduce that reduces directly into an existing output object.
void mpi_broadcast(std::array< T, N > &arr, communicator c={}, int root=0)
Implementation of an MPI broadcast for a std::array.
Definition array.hpp:53
bool all_equal(T const &x, communicator c={})
Checks if a given object is equal across all ranks in the given communicator.
void broadcast(T &&x, communicator c={}, int root=0)
Generic MPI broadcast.
Macros used in the mpi library.