CommunicationUtils.hpp
Go to the documentation of this file.
1/*
2 Copyright 2020,2021 OPM-OP AS
3
4 This file is part of the Open Porous Media Project (OPM).
5
6 OPM 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 OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef OPM_COMMUNICATION_UTILS_HPP
20#define OPM_COMMUNICATION_UTILS_HPP
21
22#include<vector>
23#include<numeric>
24#include<tuple>
25#include<utility> // Should be included via tuple but you never know.
26
27namespace Opm
28{
42template<class T, class A, class C>
43std::pair<std::vector<T, A>,
44 std::vector<int>>
45allGatherv(const std::vector<T,A>& input, const C& comm)
46{
47 std::vector<int> sizes(comm.size());
48 std::vector<int> displ(comm.size() + 1, 0);
49 int mySize = input.size();
50 comm.allgather(&mySize, 1, sizes.data());
51 std::partial_sum(sizes.begin(), sizes.end(), displ.begin()+1);
52 std::vector<T,A> output(displ.back());
53 comm.allgatherv(input.data(), input.size(), output.data(), sizes.data(), displ.data());
54 return {output, displ};
55}
71
72template<class T, class A, class C>
73std::pair<std::vector<T, A>,
74 std::vector<int>>
75gatherv(const std::vector<T,A>& input, const C& comm, int root)
76{
77 bool isRoot = (comm.rank() == root);
78 std::vector<int> sizes;
79 std::vector<int> displ;
80 std::vector<T,A> output;
81
82 if (isRoot)
83 {
84 sizes.resize(comm.size());
85 displ.resize(comm.size() + 1);
86 }
87 int mySize = input.size();
88 comm.gather(&mySize, sizes.data(), 1, root);
89
90 if (isRoot)
91 {
92 std::partial_sum(sizes.begin(), sizes.end(),
93 displ.begin()+1);
94 output.resize(displ.back());
95 }
96
97 comm.gatherv(input.data(), input.size(), output.data(), sizes.data(), displ.data(), root);
98 return {output, displ};
99}
100}
101#endif
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.hpp:26
std::pair< std::vector< T, A >, std::vector< int > > allGatherv(const std::vector< T, A > &input, const C &comm)
Gathers vectors from all processes on all processes.
Definition: CommunicationUtils.hpp:45
std::pair< std::vector< T, A >, std::vector< int > > gatherv(const std::vector< T, A > &input, const C &comm, int root)
Gathers vectors from all processes on a root process.
Definition: CommunicationUtils.hpp:75