-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
64 lines (57 loc) · 1.85 KB
/
utils.h
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
/**
* @file utils.h
* @author Patrick Flick <[email protected]>
*
* Utility functions.
*
* Copyright (c) 2016 Georgia Institute of Technology. All Rights Reserved.
*/
/*********************************************************************
* !! DO NOT CHANGE THIS FILE !! *
*********************************************************************/
#ifndef UTILS_H
#define UTILS_H
#include <mpi.h>
#include <vector>
#include <iostream>
// output a std::vector
template <typename T>
std::ostream& operator <<(std::ostream& stream, const std::vector<T>& vec) {
stream << "[";
for (size_t i = 0; i < vec.size(); ++i) {
if (i > 0)
stream << ", ";
stream << vec[i];
}
stream << "]";
return stream;
}
template <typename T>
std::vector<T> gather(const std::vector<T>& in, MPI_Datatype dt, MPI_Comm comm) {
int p;
MPI_Comm_size(comm, &p);
std::vector<T> result(in.size()*p);
MPI_Gather(&in[0], in.size(), dt, &result[0], in.size(), dt, 0, comm);
return result;
}
template <typename T>
std::vector<T> scatter(const std::vector<T>& in, MPI_Datatype dt, MPI_Comm comm) {
int p, rank;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &rank);
if (rank != 0 && in.size() > 0) {
std::cerr << "std::vector scatter only scatters from rank 0" << std::endl;
MPI_Abort(comm, -1);
}
if (rank == 0 && in.size() % p != 0) {
std::cerr << "std::vector scatter assumes the number of elements is divisable by the number of processes." << std::endl;
MPI_Abort(comm, -1);
}
int scatter_size = in.size() / p;
MPI_Bcast(&scatter_size, 1, MPI_INT, 0, comm);
std::vector<T> result(scatter_size);
MPI_Scatter((void*)&in[0], scatter_size, dt,
(void*)&result[0], scatter_size, dt, 0, comm);
return result;
}
#endif