-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreetings2.c
More file actions
54 lines (47 loc) · 1.34 KB
/
Copy pathgreetings2.c
File metadata and controls
54 lines (47 loc) · 1.34 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
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc , char * argv[])
{
int my_rank; /* rank of process */
int p; /* number of process */
int dest; /* rank of reciever */
int tag = 0; /* tag for messages */
char message[100]; /* storage for message */
MPI_Status status; /* return status for */
/* recieve */
int num = 5;
/* Start up MPI */
MPI_Init( &argc , &argv );
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out number of process */
MPI_Comm_size(MPI_COMM_WORLD, &p);
if( my_rank == 0)
{
for( dest = 1; dest < p ; dest++)
{
/*process 0 send 5 to all other processes*/
MPI_Send( &num, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
printf("send to %d\n",dest);
}
for( dest = 1; dest < p ; dest++)
{
/*process 0 receives num from all other processes */
MPI_Recv(&num , 1, MPI_INT, dest, tag, MPI_COMM_WORLD, &status );
printf("process number %d sent %d\n" , dest , num);
}
}
else
{
/*other processes receive 5 and add it's rank to it*/
MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status );
//printf("%d is received\n" , source);
num = num + my_rank;
/*other processes send the result to process 0*/
MPI_Send( &num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
}
/* shutdown MPI */
MPI_Finalize();
return 0;
}