include stdio include stdlib include mpi int main int argc char argv i

 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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int size, rank;
MPI_Status stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int root;
root = 0;
int r = 0;
srand(time(NULL));
if (rank == 0)
r = rand();
printf("Rank: %d; val: %d\n", rank, r);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == root)
{
int i;
for (i = 0; i < size; i++)
if (i != root)
MPI_Send(&r, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(&r, 1, MPI_INT, root, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
}
printf("\n");
MPI_Barrier(MPI_COMM_WORLD);
printf("Rank: %d; val: %d\n", rank, r);
MPI_Finalize();
return 0;
}