-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocking_ring_performance.cpp
137 lines (83 loc) · 3.56 KB
/
blocking_ring_performance.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<mpi.h>
#include<fstream>
int main(int argc,char *argv[]){
//" This program use a blocking communication \n" ;
int numtasks,rank,next, prev, buf[2]={100,100}, itag_rank=1, itag_rec_prev=2, itag_rec_next=2;
// std::fstream myFile;
MPI_Status stats[2]; //
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
// myFile.open("result/result.csv",std::ios::app); // open in append mode
// if(myFile.is_open()){
double starttime, endtime, mean=0;
//determine the right and left neighbors
prev = rank -1;
next = rank + 1;
int msgleft = rank, msgright = -rank,sum=0, count=0; // sum collect the the sum of the
// rank and the receiving message at each step of the process.compt will compt the number of messages receive
// for each process.
if(rank==0) prev= numtasks-1;
if(rank== (numtasks-1)) next = 0;
int N = 3000;// number of iteration to get a runtime mean
for(int i = 0; i< N; i++)
{
starttime = MPI_Wtime();
int cond=1000; // in the cluster, we are allow to use 24 processes, using this number, we are sure
// that any process will have this rank. So it help to solve deadlock at the first step
// of the program
itag_rank = rank*10;
itag_rec_prev= prev*10;
itag_rec_next = next*10;
if(rank %2==0){ // this condition is used to avoid deadlock. at the begining
while(cond !=rank){
MPI_Recv(&buf[0],1,MPI_INT,next,itag_rec_next,MPI_COMM_WORLD,&stats[0]); // buf[0] -> msgleft
MPI_Send(&msgleft,1,MPI_INT,prev,itag_rank,MPI_COMM_WORLD);
// post non blocking receives and sends for neighbord
MPI_Recv(&buf[1],1,MPI_INT,prev, itag_rec_prev,MPI_COMM_WORLD, &stats[1]); //buf[1] -> msgright
MPI_Send(&msgright ,1,MPI_INT,next,itag_rank,MPI_COMM_WORLD);
// The message receive from the next process become the message to send to the previous process,inversely.
msgleft = buf[0];
msgright = buf[1];
sum = rank + msgleft + msgright;
count= count+ 2 ; // because at each step, process p receive message from right and left.
cond =msgleft;
}
// std::cout<<" I am process " << rank <<" and I have received " << count << " messages. " << " My final messages have tag " << itag_rank;
// std::cout <<" .My final msgleft and msgright are "<< msgleft << " and " << msgright <<std::endl;
// std::cout << std::endl;
}
else
{
while( cond!= rank){
MPI_Send(&msgleft,1,MPI_INT,prev,itag_rank,MPI_COMM_WORLD);
MPI_Recv(&buf[0],1,MPI_INT,next,itag_rec_next,MPI_COMM_WORLD,&stats[0]); // buf[0] -> msgleft
// post non blocking receives and sends for neighbord
MPI_Send(&msgright,1,MPI_INT,next,itag_rank,MPI_COMM_WORLD);
MPI_Recv(&buf[1],1,MPI_INT,prev, itag_rec_prev,MPI_COMM_WORLD, &stats[1]); // buef[1]-> msgright
// wait for all non-blocking operations to complete
msgleft=buf[0];
msgright= buf[1];
cond= msgleft;
sum = msgleft + msgright + rank;
count= count+ 2;
}
}
endtime = MPI_Wtime();
mean = mean + (endtime-starttime);
std::cout<<"\n"<< numtasks<<";"<<rank<<";"<<mean/N<<std::endl; //write on the file
}// iteration
//std::cout<<" I am process " << rank <<" and I have received " << count << " messages. " << " My final messages have tag " << itag_rank;
//std::cout <<" .My final msgleft and msgright are "<< msgleft<< " and "<< msgright <<std::endl;
//std::cout << std::endl;
// myFile.close();
MPI_Finalize();
return 0;
// }
// else{
// std:: cout<< "File not open. Please make sure you run the fille run.sh first \n";
// MPI_Finalize();
// return 1;
// }
}