-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_4.cpp
68 lines (54 loc) · 1.83 KB
/
Lab_4.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
60
61
62
63
64
65
66
67
68
// Purpose: MPI program to demonstrate ring communication
#include <iostream>
#include <mpi.h>
using namespace std;
int main()
{
// initialize MPI
MPI_Init(NULL, NULL);
// get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// get the rank of the process
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0)
{
// send message to process 1
int number = 100;
MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
// receive message from process 3
MPI_Recv(&number, 1, MPI_INT, 3, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << "Process 0 received number " << number << " from process 3" << endl;
}
if (rank == 1)
{
// receive message from process 0
int number;
MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << "Process 1 received number " << number << " from process 0" << endl;
// send message to process 2
MPI_Send(&number, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
}
if (rank == 2)
{
// receive message from process 1
int number;
MPI_Recv(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << "Process 2 received number " << number << " from process 1" << endl;
// send message to process 3
MPI_Send(&number, 1, MPI_INT, 3, 0, MPI_COMM_WORLD);
}
if (rank == 3)
{
// receive message from process 2
int number;
MPI_Recv(&number, 1, MPI_INT, 2, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << "Process 3 received number " << number << " from process 2" << endl;
// send message to process 0
MPI_Send(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}
// finalize MPI
MPI_Finalize();
return 0;
}