-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_scatter_gather.c
68 lines (54 loc) · 1.57 KB
/
mpi_scatter_gather.c
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
/*
MPI Scatter Gather testing program
main process allocate table with ones
sends this table to all
each process multiply table part by rank
main process gather and print table
*/
#include "mpi.h"
#include <stdio.h>
#define NUMBER_OF_PROCESSES 4
int main(int argc, char **argv) {
int rank, size, i, j;
int table[NUMBER_OF_PROCESSES][3];
int table_recv[NUMBER_OF_PROCESSES][3];
int row[3];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
/* If I'm the root (process 0), then fill out the big table */
if (rank == 0) {
for (j = 0; j < NUMBER_OF_PROCESSES; j++) {
for (i = 0; i < 3; i++) {
table[j][i] = 1;
}
}
for (j = 0; j < NUMBER_OF_PROCESSES; j++) {
for (i = 0; i < 3; i++) {
printf("%d, ", table[j][i]);
}
printf("\n");
}
printf("\n");
}
/* Scatter the big table to everybody's little table */
MPI_Scatter(&table[0][0], 3, MPI_INT,
&row, 3, MPI_INT, 0, MPI_COMM_WORLD);
for (i = 0; i < 3; i++) {
// printf("%d, ", row[i]);
row[i] *= rank;
}
// printf("\n\n");
MPI_Gather(&row, 3, MPI_INT,
&table_recv[0][0], 3, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (j = 0; j < NUMBER_OF_PROCESSES; j++) {
for (i = 0; i < 3; i++) {
printf("%d, ", table_recv[j][i]);
}
printf("\n");
}
}
MPI_Finalize();
return 0;
}