-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_collective_scatterv.cpp
59 lines (47 loc) · 1.18 KB
/
mpi_collective_scatterv.cpp
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
# include <iostream>
# include <cstdlib> // has exit(), etc.
# include <ctime>
# include <stdio.h>
# include "mpi.h" // MPI header file
#define SIZE 4
using namespace std;
int main (int argc, char **argv)
{
int nprocs, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// the data to be distributed
char data[SIZE][SIZE] = {
{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p'}
};
char rec_buf[100];
int sendcounts[nprocs];
int displs[nprocs];
// calculate send counts and displacements
// Your code
for (int i = 0; i < nprocs; i++)
{
sendcounts[i] = i+1;
displs[i] = i*nprocs;
}
// print calculated send counts and displacements for each process
if (rank == 0) {
for (int i = 0; i < nprocs; i++) {
printf("sendcounts[%d] = %d\tdispls[%d] = %d\n", i, sendcounts[i], i, displs[i]);
}
}
// MPI_Scatterv
// Your code
MPI_Scatterv(&data, sendcounts, displs, MPI_CHAR, &rec_buf, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
printf("%d: ", rank);
for (int i = 0; i < sendcounts[rank]; i++) {
printf("%c\t", rec_buf[i]);
}
printf("\n");
MPI_Finalize();
return 0;
}