-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvectorUtils.hpp
More file actions
executable file
·66 lines (56 loc) · 1.95 KB
/
vectorUtils.hpp
File metadata and controls
executable file
·66 lines (56 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2019, ATA Engineering, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef VECTOR_UTILS
#define VECTOR_UTILS
#include <Loci.h>
#include "mpi.h"
#include <iostream>
// function to gather vector and broadcast it to all processors
template <class T>
void AllGatherVector(std::vector<T> &p0v, const std::vector<T> &v,
const MPI_Comm &comm) {
int rank = 0;
MPI_Comm_rank(comm, &rank);
int procs = 1;
MPI_Comm_size(comm, &procs);
// Gather sizes
int local_size = v.size();
std::vector<int> recv_sizes(procs);
MPI_Allgather(&local_size, 1, MPI_INT, &recv_sizes[0], 1, MPI_INT, comm);
// Allocate receive array
int tot_size = recv_sizes[0];
for (int i = 1; i < procs; ++i) {
tot_size += recv_sizes[i];
}
if (tot_size != int(p0v.size())) {
p0v.resize(tot_size);
}
// Compute sizes in bytes
const int bsz = sizeof(T);
for (int i = 0; i < procs; ++i) {
recv_sizes[i] *= bsz;
}
// Bookkeeping for gatherv call
std::vector<int> displs(procs);
displs[0] = 0;
for (int i = 1; i < procs; ++i) {
displs[i] = displs[i - 1] + recv_sizes[i - 1];
}
// Gather data
MPI_Allgatherv(&v[0], local_size * bsz, MPI_BYTE, &p0v[0], &recv_sizes[0],
&displs[0], MPI_BYTE, comm);
}
#endif