-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.cpp
More file actions
201 lines (163 loc) · 7.63 KB
/
Copy pathcommunication.cpp
File metadata and controls
201 lines (163 loc) · 7.63 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// Created by niko on 8/20/19.
//
#include <cstring>
#include "communication.h"
void bcast_vector(std::vector<double> &v, MPI_Comm comm, int root) {
int rank, comm_size;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &comm_size);
int size;
if (rank == root) {
size = v.size();
}
MPI_Bcast(&size, 1, MPI_INT, root, comm);
if (rank != root) {
v.clear();
v.resize(size);
}
MPI_Bcast(v.data(), size, MPI_DOUBLE, root, comm);
}
void get_rowcnt_start_row(MPI_Comm comm, unsigned num_rows, std::vector<int> &rowcnt, std::vector<int> &start_row) {
int comm_size;
MPI_Comm_size(comm, &comm_size);
rowcnt.clear();
start_row.clear();
start_row.push_back(0);
for (int i{0}; i < comm_size; ++i) {
unsigned start = (num_rows * i) / comm_size;
unsigned end = (num_rows * (i + 1)) / comm_size;
rowcnt.push_back(end - start);
const auto last_start = start_row.back();
start_row.push_back(last_start + rowcnt.back());
}
}
CSR distribute_matrix(const CSR &matrix, MPI_Comm comm, int root) {
int rank, comm_size;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &comm_size);
const int metadata_size = 3;
std::vector<int> r_num_rows;
std::vector<int> r_num_values;
std::vector<int> r_stretched_rowptr;
std::vector<unsigned> r_metadata;
std::vector<int> r_rowptr_displs;
std::vector<int> r_values_displs;
if (rank == root) {
r_num_rows.resize(comm_size);
r_num_values.resize(comm_size);
for (int i = 0; i < comm_size; ++i) {
unsigned start = (matrix.num_rows() * i) / comm_size;
unsigned end = (matrix.num_rows() * (i + 1)) / comm_size;
r_num_rows.at(i) = end - start;
r_num_values.at(i) = matrix.rowptr().at(end) - matrix.rowptr().at(start);
}
// compute prefix sums
r_rowptr_displs.resize(comm_size);
r_rowptr_displs.at(0) = 0;
std::partial_sum(r_num_rows.begin(), r_num_rows.end() - 1, r_rowptr_displs.begin() + 1);
r_values_displs.resize(comm_size);
r_values_displs.at(0) = 0;
std::partial_sum(r_num_values.begin(), r_num_values.end() - 1, r_values_displs.begin() + 1);
// The rowptr of a CSR matrix with n rows contains n+1 elements
for (auto &e : r_num_rows) {
e++;
}
// Duplicate indices, as MPI Scatterv does not guarantee correct behaviour
// in case of overlapping send buffers
r_stretched_rowptr = duplicate_indices(matrix.rowptr(), r_rowptr_displs);
for (size_t i = 0; i < r_rowptr_displs.size(); ++i) {
r_rowptr_displs.at(i) += i;
}
r_metadata.resize(comm_size * metadata_size);
for (int i = 0; i < comm_size; ++i) {
auto start = i * metadata_size;
r_metadata.at(start + 0) = matrix.num_cols();
r_metadata.at(start + 1) = r_num_rows.at(i);
r_metadata.at(start + 2) = r_num_values.at(i);
}
}
std::vector<unsigned> metadata(metadata_size);
MPI_Scatter(r_metadata.data(), 3, MPI_UNSIGNED, metadata.data(), 3, MPI_UNSIGNED, root, comm);
std::vector<int> rowptr(metadata.at(1));
std::vector<int> colidx(metadata.at(2));
std::vector<double> values(metadata.at(2));
MPI_Scatterv(r_stretched_rowptr.data(), r_num_rows.data(), r_rowptr_displs.data(), MPI_INT, rowptr.data(),
rowptr.size(), MPI_INT, root, comm);
MPI_Scatterv(matrix.colidx().data(), r_num_values.data(), r_values_displs.data(), MPI_INT, colidx.data(),
metadata.at(2), MPI_INT, root, comm);
MPI_Scatterv(matrix.values().data(), r_num_values.data(), r_values_displs.data(), MPI_DOUBLE, values.data(),
metadata.at(2), MPI_DOUBLE, root, comm);
// A node might own no row, if #nodes > #rows
if (!rowptr.empty()) {
int displacement = rowptr.at(0);
for (auto &e : rowptr) {
e -= displacement;
}
}
return CSR{values, colidx, rowptr, metadata.at(0)};
}
/*void
gather_results(char *new_partial, char *old_partial, std::int32_t num_values_partial, std::int32_t num_values_total,
std::int32_t bytes_per_val, char *out,
std::int32_t out_bytes, const std::vector<std::int32_t> &rowcnt, MPI_Comm comm) {
std::int32_t comm_size, rank;
MPI_Comm_size(comm, &comm_size);
MPI_Comm_rank(comm, &rank);
// ##########################################################
// ### Compute & distribute the node specific information ###
// ##########################################################
std::vector<std::int32_t> relevant_per_rank(comm_size);
std::int32_t needed_bytes{0};
for (std::int32_t i{0}; i < num_values_partial * bytes_per_val; ++i) {
if (new_partial[i] != old_partial[i]) {
const std::int32_t byte_idx = i % bytes_per_val; // Index within the currently looked at value
const std::int32_t affected_bytes =
bytes_per_val - byte_idx; // Look from the back end, as we assume little endian
needed_bytes = std::max(needed_bytes, affected_bytes);
}
}
MPI_Allgather(&needed_bytes, 1, MPI_INT32_T, relevant_per_rank.data(), 1, MPI_INT32_T, comm);
// ##########################################################
// ### Generate the data to distribute ######################
// ##########################################################
std::vector<char> sendbuf(num_values_partial * needed_bytes);
for (size_t i{0}; i < num_values_partial; ++i) {
const auto end = (i + 1) * bytes_per_val;
const auto start = end - needed_bytes;
std::memcpy(new_partial + start, sendbuf.data() + needed_bytes * i, needed_bytes);
}
// ##########################################################
// ### Generate Allgatherv metadata on each rank ############
// ##########################################################
const std::int32_t sendcount{needed_bytes * num_values_partial};
std::int32_t total_recvcount{0};
for (std::int32_t i{0}; i < comm_size; ++i) {
total_recvcount += rowcnt.at(i) * relevant_per_rank.at(i);
}
std::vector<char> recvbuf(total_recvcount);
std::vector<std::int32_t> recvcounts;
std::vector<std::int32_t> displs;
recvcounts.reserve(comm_size);
displs.reserve(comm_size);
for (std::int32_t i{0}; i < comm_size; ++i) {
recvcounts.push_back(relevant_per_rank.at(i) * rowcnt.at(i));
displs.push_back(displs.back() + recvcounts.back());
}
MPI_Allgatherv(sendbuf.data(), sendcount, MPI_CHAR, recvbuf.data(), recvcounts.data(), displs.data(), MPI_CHAR,
comm);
// ##########################################################
// ### Update the result buffer #############################
// ##########################################################
std::int32_t out_start_idx{0};
for (std::int32_t i{0}; i < comm_size; ++i) {
const auto curr_relevant_bytes{relevant_per_rank.at(i)};
const auto changed_start_idx{displs.at(i)}; // first byte belonging to the changed data from node i
for (std::int32_t k{0}; k < rowcnt.at(i); ++k) {
const auto changed_start{changed_start_idx + k * curr_relevant_bytes};
const auto out_start{out_start_idx + (k + 1) * bytes_per_val - curr_relevant_bytes}; // change index to fit little endian, i.e. we only want to overwrite the curr_relevant_bytes last bytes of a value
std::memcpy(recvbuf.data() + changed_start, out + out_start, curr_relevant_bytes);
}
out_start_idx += rowcnt.at(i) * bytes_per_val;
}
}*/