Newer
Older
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <exception>
#include <string>
#include <cstring>
using namespace std;
#include <stdlib.h>
#include <mpi.h>
#include <unistd.h>
//
//
// rotate exercies from nci
// passes a number around the ranks of a MPI program
// used to test connectivity. We use non block primitives just because we can
//
void do_work_non_blocking();
int mpirank=0;
const int MES_TAG=1;
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<sstream>
std::string ssystem (const char *command) {
char tmpname [L_tmpnam];
std::tmpnam ( tmpname );
std::string scommand = command;
std::string cmd = scommand + " >> " + tmpname;
std::system(cmd.c_str());
std::ifstream file(tmpname, std::ios::in );
std::string result;
if (file) {
while (!file.eof()) result.push_back(file.get());
file.close();
}
remove(tmpname);
return result;
}
int main( int argc, char* argv[])
{
try
{
cout <<"rotate program called on host ";
size_t BUFFLEN= 256;
char hostBuffer[BUFFLEN];
hostBuffer[0]='\0';
gethostname(hostBuffer, BUFFLEN);
cout<<hostBuffer<<endl;
int mpierror, mpisize;
cout<<"Before MPI_Init"<<endl;
mpierror = MPI_Init(&argc,&argv);
if (mpierror != MPI_SUCCESS)
{
cerr <<"Error in mpi init "<<mpierror<<endl;
exit(mpierror);
}
cout<<"Before MPI_Comm_size"<<endl;
mpierror = MPI_Comm_size(MPI_COMM_WORLD,&mpisize);
if (mpierror != MPI_SUCCESS)
{
cerr <<"Error in mpi comm size "<<mpierror<<endl;
exit(mpierror);
}
cout<< "Mpi size is "<<mpisize<<endl;
mpierror = MPI_Comm_rank(MPI_COMM_WORLD,&mpirank);
if (mpierror != MPI_SUCCESS)
{
cerr <<"Error in mpi rank size "<<mpierror<<endl;
exit(mpierror);
}
//std::string CPU_Affinity = ssystem("cat /proc/self/status | grep -i cpus_allowed_list");
//std::string CPU_Affinity = ssystem("grep -i cpus_allowed_list /proc/self/status");
//cout<<"Hostname="<<hostBuffer<< ": Mpi rank is "<<mpirank<<" and mpusize is "<<mpisize<< " and CPU Affinity is "<<CPU_Affinity<<endl;
cout<<"Hostname="<<hostBuffer<< ": Mpi rank is "<<mpirank<<" and mpusize is "<<mpisize<< endl;
//
//a do work here
//
do_work_non_blocking();
cout <<"Before MPI_Finalize from rank "<<mpirank<<endl;
MPI_Finalize();
cout <<"Exit from rank "<<mpirank<<endl;
}
catch (exception& e)
{
cerr<<"Exception caught " << e.what()<< "from mpirank "<<mpirank << endl;
}
}//main
//------------------------------------------
void sendString(char* s, int destRank)
{
if (s==NULL)
{
cerr<<"MPI_Send. error null pointer sent!\n";
return;
}
cout<<"MPI_Send::["<<mpirank<<"=>"<<destRank<< "] string is \""<<s<<"\""<<endl;
MPI_Status status;
int error=MPI_Ssend(s,strlen(s)+1,MPI_CHAR,destRank,MES_TAG,MPI_COMM_WORLD);
if (error != MPI_SUCCESS)
{
cout<<"error MPI_Ssend: from "<<mpirank<<" to "<<destRank<<endl;
return;
}
cout<<"........MPI_Send::["<<mpirank<<"=>"<<destRank<< "] Successful send\n";
}//sendString
//------------------------------------------
void recvString(char* buffer, int MAX_BUFFER, int destRank)
{
cout<<"........MPI_Recv: rank "<<mpirank<< "<="<<destRank<<endl;
MPI_Status status;
int error=MPI_Recv(buffer,MAX_BUFFER,MPI_CHAR,destRank, MPI_ANY_TAG,MPI_COMM_WORLD,&status);
if (error != MPI_SUCCESS)
{
cout<<"error MPI_Recv: from "<<mpirank<<endl;
return;
}
int received;
MPI_Get_count(&status,MPI_CHAR,&received);
if (strlen(buffer)==0)
{
cerr<<"Error from rank "<<mpirank<<" no string found! "<<endl;
return;
}
if (strlen(buffer)<MAX_BUFFER)
{
cout <<"MPI_Recv["<<mpirank<< "<=" <<status.MPI_SOURCE << "]: Successful receive of \""<<buffer<<"\" bytes\n";
}
}//recvString
void do_work_non_blocking()
{
cout<< "do_work_non_blocking: "<<mpirank<<endl;
int error;
double start = MPI_Wtime();
bool finish=false;
int MPI_SIZE;
MPI_Comm_size(MPI_COMM_WORLD,&MPI_SIZE);
char my_cpu_name[BUFSIZ];
int my_name_length;
MPI_Request requestSend;
MPI_Request requestReceive;
MPI_Status status;
int sendBuffer;
int receiveBuffer;
int nextRank;
if (mpirank==MPI_SIZE-1)
{
nextRank=0;
}
else
{
nextRank=mpirank+1;
}
MPI_Get_processor_name(my_cpu_name, &my_name_length);
cout<<"Rank "<<mpirank<<" Processor name is "<<my_cpu_name<<endl;
bool finished=false;
int currentRank=mpirank;
while (!finished)
{
//send rank
sendBuffer=currentRank;
cout<<"Send from rank "<<mpirank<<"=>"<<nextRank<<" sendBuffer "<<sendBuffer<<endl;
error =MPI_Isend(&sendBuffer,1,MPI_INT,nextRank, MES_TAG,MPI_COMM_WORLD,&requestSend);
if (error != MPI_SUCCESS)
{
cout<<"error MPI_Isend(int): from "<<mpirank<<endl;
return;
}
//receive rank
error=MPI_Irecv(&receiveBuffer,1,MPI_INT,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&requestReceive);
if (error != MPI_SUCCESS)
{
cout<<"error MPI_Irecv(int): from "<<mpirank<<endl;
return;
}
//wait
MPI_Status status;
cout<<"Before MPI_Wait for rank "<<mpirank<<endl;
MPI_Wait(&requestSend,&status);
MPI_Wait(&requestReceive,&status);
cout<<"2*waits finished for rank "<<mpirank<<" and received value is "<<receiveBuffer<<endl;
currentRank=receiveBuffer;
if (currentRank==mpirank)
{
finished=true;
cout<<"SUCCESS from "<<mpirank<<endl;
}
}//while
double end = MPI_Wtime();
cout<<"Time of work["<< mpirank << "] "<< (end-start) << " seconds "<<endl;
}
//------------------------------------------
//------------------------------------------