KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
is_same_on_all_ranks.hpp
1// This file is part of KaMPIng.
2//
3// Copyright 2022 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.
13
14#pragma once
15
16#include <type_traits>
17
18#include <kassert/kassert.hpp>
19#include <mpi.h>
20
21#include "kamping/communicator.hpp"
23#include "kamping/mpi_ops.hpp"
25
26namespace kamping {
27
28/// @brief Checks if all ranks provide the same value to this collective.
29///
30/// This collective function checks if all ranks have called it with the same value. The result is returned on all
31/// ranks.
32/// @tparam Value Type of the value to check. Must be comparable with `operator==`.
33/// @param value The value of this rank. This value is compared with the ones provided by all other ranks.
34/// @return `true` if all ranks have provided the same value, `false` otherwise.
35template <
36 template <typename...>
37 typename DefaultContainerType,
38 template <typename, template <typename...> typename>
39 typename... Plugins>
40template <typename Value>
42 /// @todo How to handle more complex data types, e.g. std::pair<>, user defined classes, std::vector (here and
43 /// elsewhere)?
44 /// @todo Assert that two values are comparable.
45
46 /// @todo replace this with something more general
47 static_assert(!std::is_pointer_v<Value>, "Comparing pointers from different machines does not make sense.");
48
49 struct ValueEqual {
50 Value value; // The value to compare, init on each rank with the local value.
51 bool equal; // Have we seen only equal values in the reduction so far?
52 };
53 ValueEqual value_equal = {value, true};
55
56 // Build the operation for the reduction.
58 [](ValueEqual const& a, ValueEqual const& b) {
59 if (a.equal && b.equal && a.value == b.value) {
60 return ValueEqual{a.value, true};
61 } else {
62 return ValueEqual{a.value, false};
63 }
64 },
66 );
68
69 // Perform the reduction and return.
71 MPI_IN_PLACE, // sendbuf
72 &value_equal, // recvbuf
73 1, // count
74 datatype, // datatype,
75 operation.op(), // operation,
76 this->mpi_communicator() // communicator
77 );
78
79 return value_equal.equal;
80}
81
82} // namespace kamping
bool is_same_on_all_ranks(Value const &value) const
Checks if all ranks provide the same value to this collective.
Definition is_same_on_all_ranks.hpp:41
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
internal::OperationBuilder< Op, Commutative > op(Op &&op, Commutative commute=ops::internal::undefined_commutative_tag{})
Passes a reduction operation to ther underlying call. Accepts function objects, lambdas,...
Definition named_parameters.hpp:1155
Utility that maps C++ types to types that can be understood by MPI.
Definitions for builtin MPI operations.
Factory methods for buffer wrappers.
constexpr internal::commutative_tag commutative
global tag for commutativity
Definition mpi_ops.hpp:197