ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
all_compare.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef UTILS_MPI_ALL_COMPARED_HPP
21#define UTILS_MPI_ALL_COMPARED_HPP
22
23#include <boost/mpi/collectives/all_reduce.hpp>
24#include <boost/mpi/collectives/broadcast.hpp>
25
26namespace Utils {
27namespace Mpi {
28
29/**
30 * @brief Compare values on all nodes.
31 *
32 * @param comm The communicator to operate on.
33 * @param value the value to compare.
34 *
35 * @tparam T the type of value.
36 *
37 * Returns true iff all ranks in comm called
38 * the function with the same value.
39 * T has to be serializeable.
40 */
41template <typename T>
42bool all_compare(boost::mpi::communicator const &comm, T const &value) {
43 T root_value{};
44
45 /* Arbitrary pick the value on rank 0, and broadcast it */
46 if (comm.rank() == 0) {
47 root_value = value;
48 }
49
50 boost::mpi::broadcast(comm, root_value, 0);
51
52 bool is_same = false;
53 boost::mpi::all_reduce(comm, value == root_value, is_same,
54 std::logical_and<bool>());
55
56 return is_same;
57}
58} // namespace Mpi
59} // namespace Utils
60
61#endif
bool all_compare(boost::mpi::communicator const &comm, T const &value)
Compare values on all nodes.