Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132944 views
License: OTHER
1
// Quelle: Klausur vom SS 2013 am KIT bei
2
// Prof. Dr.-Ing. Gregor Snelting
3
void my_int_sum_reduce(int *sendbuf,
4
int *recvbuf, int count,
5
int root, MPI_Comm comm)
6
{
7
int size, rank;
8
MPI_Comm_size(comm, &size);
9
MPI_Comm_rank(comm, &rank);
10
if (rank == root) {
11
/* Initialize recvbuf with our own values. */
12
for (int i = 0; i < count; ++i) {
13
recvbuf[i] = sendbuf[i];
14
}
15
16
/* Receive values from every other node
17
and accumulate. */
18
for (int i = 0; i < size; ++i) {
19
if (i == root)
20
continue;
21
22
int other[count];
23
MPI_Recv(other, count, MPI_INT,
24
i, 0, comm, MPI_STATUS_IGNORE);
25
26
for (int j = 0; j < count; ++j) {
27
recvbuf[j] += other[j];
28
}
29
}
30
} else {
31
/* Send our values to root. */
32
MPI_Send(sendbuf, count, MPI_INT,
33
root, 0, comm);
34
}
35
}
36