1 : |
agomez |
1 |
#include "slavecommunication.h" |
2 : |
|
|
#include "gadget.h" |
3 : |
|
|
|
4 : |
|
|
SlaveCommunication::SlaveCommunication()
|
5 : |
|
|
{
|
6 : |
|
|
MAXWAIT = 30;
|
7 : |
|
|
pvmConst = new PVMConstants();
|
8 : |
|
|
typeReceived = -1;
|
9 : |
|
|
parenttid = -1;
|
10 : |
|
|
mytid = -1;
|
11 : |
|
|
myID = -1;
|
12 : |
|
|
numVar = 0;
|
13 : |
|
|
netDataVar = NULL;
|
14 : |
|
|
tmout.tv_sec = MAXWAIT;
|
15 : |
|
|
tmout.tv_usec = 0;
|
16 : |
|
|
}
|
17 : |
|
|
|
18 : |
|
|
SlaveCommunication::~SlaveCommunication()
|
19 : |
|
|
{
|
20 : |
|
|
delete pvmConst;
|
21 : |
|
|
if (netDataVar != NULL)
|
22 : |
|
|
{
|
23 : |
|
|
delete netDataVar;
|
24 : |
|
|
netDataVar = NULL;
|
25 : |
|
|
}
|
26 : |
|
|
}
|
27 : |
|
|
|
28 : |
|
|
void SlaveCommunication::printErrorMsg(const char* errorMsg)
|
29 : |
|
|
{
|
30 : |
|
|
/*
|
31 : |
|
|
Laga þetta þegar ég fer að pæla í error handler...
|
32 : |
|
|
*/
|
33 : |
|
|
char* msg;
|
34 : |
|
|
msg = new char[strlen(errorMsg) + 1];
|
35 : |
|
|
strcpy(msg, errorMsg);
|
36 : |
|
|
//Prófum þetta hér, var notað pvm_perror sem skrifar villuna sem síðasta pvm kallið olli.
|
37 : |
|
|
std::cout << msg << "\n";
|
38 : |
|
|
cerr << msg;
|
39 : |
|
|
delete[] msg;
|
40 : |
|
|
}
|
41 : |
|
|
|
42 : |
|
|
int SlaveCommunication::startNetCommunication()
|
43 : |
|
|
{
|
44 : |
|
|
/*
|
45 : |
|
|
Þetta fall er komið í bili!
|
46 : |
|
|
*/
|
47 : |
|
|
int info, bytes, type, source;
|
48 : |
|
|
int OK = 1;
|
49 : |
|
|
int bufID = 0;
|
50 : |
|
|
|
51 : |
|
|
//enroll in pvm and get identity of process for myself
|
52 : |
|
|
MPI_Init(NULL,NULL);
|
53 : |
|
|
MPI_Comm parentcomm;
|
54 : |
|
|
MPI_Status status;
|
55 : |
|
|
|
56 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
57 : |
|
|
if (parentcomm == MPI_COMM_NULL)
|
58 : |
|
|
{
|
59 : |
|
|
printErrorMsg("Error in slavecommunication - process has not been spawned");
|
60 : |
|
|
return 0;
|
61 : |
|
|
}
|
62 : |
|
|
int flag;
|
63 : |
|
|
// Þetta þarf að vera gert með timeout, prófum venjulegt blocking probe:
|
64 : |
|
|
//MPI_Iprobe(0, MPI_ANY_TAG, parentcomm, &flag,&status);
|
65 : |
|
|
MPI_Probe(0, MPI_ANY_TAG, parentcomm,&status);
|
66 : |
|
|
//if(flag == true)
|
67 : |
|
|
//{
|
68 : |
|
|
if (status.MPI_TAG == pvmConst->getStopTag())
|
69 : |
|
|
{
|
70 : |
|
|
int stopMessage;
|
71 : |
|
|
MPI_Recv(&stopMessage, 1, MPI_INT, 0, MPI_ANY_TAG, parentcomm, &status);
|
72 : |
|
|
typeReceived = pvmConst->getStopTag();
|
73 : |
|
|
return !OK;
|
74 : |
|
|
}
|
75 : |
|
|
else if (status.MPI_TAG == pvmConst->getStartTag())
|
76 : |
|
|
{
|
77 : |
|
|
MPI_Recv(&numVar, 1, MPI_INT, 0, MPI_ANY_TAG, parentcomm, &status);
|
78 : |
|
|
if (numVar <= 0)
|
79 : |
|
|
{
|
80 : |
|
|
cerr << "Error in slavecommunication - number of variables received from master is less than zero\n";
|
81 : |
|
|
return !OK;
|
82 : |
|
|
}
|
83 : |
|
|
MPI_Recv(&myID, 1, MPI_INT, 0, MPI_ANY_TAG, parentcomm, &status);
|
84 : |
|
|
if (myID < 0)
|
85 : |
|
|
{
|
86 : |
|
|
cerr << "Error in slavecommunication - received invalid id of " << myID << endl;
|
87 : |
|
|
return !OK;
|
88 : |
|
|
}
|
89 : |
|
|
netDataVar = new NetDataVariables(numVar);
|
90 : |
|
|
typeReceived = pvmConst->getStartTag();
|
91 : |
|
|
return numVar;
|
92 : |
|
|
}
|
93 : |
|
|
else
|
94 : |
|
|
{
|
95 : |
|
|
cerr << "Error in slavecommunication - received unrecognised tag of type " << status.MPI_TAG << endl;
|
96 : |
|
|
return !OK;
|
97 : |
|
|
}
|
98 : |
|
|
//}
|
99 : |
|
|
//else
|
100 : |
|
|
//{
|
101 : |
|
|
// cerr << "Error in slavecommunication - non-blocking-probe fail" << endl;
|
102 : |
|
|
// return !OK;
|
103 : |
|
|
//}
|
104 : |
|
|
}
|
105 : |
|
|
|
106 : |
|
|
void SlaveCommunication::stopNetCommunication()
|
107 : |
|
|
{
|
108 : |
|
|
MPI_Finalize();
|
109 : |
|
|
}
|
110 : |
|
|
|
111 : |
|
|
int SlaveCommunication::sendToMaster(double res)
|
112 : |
|
|
{
|
113 : |
|
|
int info;
|
114 : |
|
|
assert(netDataVar != NULL);
|
115 : |
|
|
if (netDataVar->x_id < 0 || netDataVar->tag < 0)
|
116 : |
|
|
{
|
117 : |
|
|
printErrorMsg("Error in slavecommunication - invalid id received\n");
|
118 : |
|
|
return 0;
|
119 : |
|
|
}
|
120 : |
|
|
else if (myID < 0)
|
121 : |
|
|
{
|
122 : |
|
|
printErrorMsg("Error in slavecommunication - invalid id received\n");
|
123 : |
|
|
return 0;
|
124 : |
|
|
}
|
125 : |
|
|
else
|
126 : |
|
|
{
|
127 : |
|
|
//cout << "Slave að skrifa result: " << res << "\n";
|
128 : |
|
|
NetDataResult* sendData = new NetDataResult;
|
129 : |
|
|
sendData->who = myID;
|
130 : |
|
|
sendData->result = res;
|
131 : |
|
|
sendData->x_id = netDataVar->x_id;
|
132 : |
|
|
sendData->tag = netDataVar->tag;
|
133 : |
|
|
info = send(sendData);
|
134 : |
|
|
delete sendData;
|
135 : |
|
|
if (info > 0)
|
136 : |
|
|
{
|
137 : |
|
|
netDataVar->tag = -1;
|
138 : |
|
|
netDataVar->x_id = -1;
|
139 : |
|
|
return 1;
|
140 : |
|
|
}
|
141 : |
|
|
else
|
142 : |
|
|
return 0;
|
143 : |
|
|
}
|
144 : |
|
|
return 0;
|
145 : |
|
|
}
|
146 : |
|
|
|
147 : |
|
|
int SlaveCommunication::send(NetDataResult* sendData)
|
148 : |
|
|
{
|
149 : |
|
|
int info;
|
150 : |
|
|
MPI_Comm parentcomm;
|
151 : |
|
|
//cout << "Slave að skrifa result: " << sendData->result << "\n";
|
152 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
153 : |
|
|
MPI_Send(&sendData->tag,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
|
154 : |
|
|
MPI_Send(&sendData->result,1,MPI_DOUBLE, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
|
155 : |
|
|
MPI_Send(&sendData->who,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
|
156 : |
|
|
MPI_Send(&sendData->x_id,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
|
157 : |
|
|
return 1;
|
158 : |
|
|
}
|
159 : |
|
|
|
160 : |
|
|
int SlaveCommunication::receiveFromMaster()
|
161 : |
|
|
{
|
162 : |
|
|
/*
|
163 : |
|
|
Þetta fall er komið í bili, þarf samt að skoða með þetta TIMEOUT fall...
|
164 : |
|
|
*/
|
165 : |
|
|
int bufID = 0;
|
166 : |
|
|
int OK = 1;
|
167 : |
|
|
int info, bytes, source, type;
|
168 : |
|
|
typeReceived = -1;
|
169 : |
|
|
MPI_Status status;
|
170 : |
|
|
MPI_Comm parentcomm;
|
171 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
172 : |
|
|
|
173 : |
|
|
// Hér er hægt að nota non-blocking probe til að komast eitthvað til móts við þetta timeout, hægt
|
174 : |
|
|
// að láta það bíða í einhvern tíma og probe-a aftur... Hérna gæti verið góð pæling að útfæra bara
|
175 : |
|
|
// sjálfur Timout fall sem testar á fullu í Maxwait tíma og skilar ef flag er true... Ætla að breyta
|
176 : |
|
|
// þessu í bili, en ræð við Bjarka upp á framhaldið...
|
177 : |
|
|
// Update: Þetta virkar svona, svo það er e.t.v. ekki nein ástæða til að breyta þessu.
|
178 : |
|
|
|
179 : |
|
|
MPI_Probe(0, MPI_ANY_TAG, parentcomm, &status);
|
180 : |
|
|
//bufID = pvm_trecv(parenttid, -1, &tmout);
|
181 : |
|
|
if (status.MPI_TAG == pvmConst->getStopTag())
|
182 : |
|
|
{
|
183 : |
|
|
//receive information from master to quit
|
184 : |
|
|
int stopMessage;
|
185 : |
|
|
MPI_Recv(&stopMessage, 1, MPI_INT, 0, MPI_ANY_TAG, parentcomm, &status);
|
186 : |
|
|
typeReceived = pvmConst->getStopTag();
|
187 : |
|
|
return !OK;
|
188 : |
|
|
}
|
189 : |
|
|
else if (status.MPI_TAG == pvmConst->getMasterSendStringTag())
|
190 : |
|
|
{
|
191 : |
|
|
// There is an incoming message of data type stringDataVariables
|
192 : |
|
|
info = receiveString();
|
193 : |
|
|
typeReceived = pvmConst->getMasterSendStringTag();
|
194 : |
|
|
if (info > 0)
|
195 : |
|
|
return 1;
|
196 : |
|
|
return 0;
|
197 : |
|
|
}
|
198 : |
|
|
else if (status.MPI_TAG == pvmConst->getMasterSendBoundTag())
|
199 : |
|
|
{
|
200 : |
|
|
// There is an incoming message of data type double for the bounds
|
201 : |
|
|
info = receiveBound();
|
202 : |
|
|
typeReceived = pvmConst->getMasterSendBoundTag();
|
203 : |
|
|
if (info > 0)
|
204 : |
|
|
return 1;
|
205 : |
|
|
return 0;
|
206 : |
|
|
}
|
207 : |
|
|
else if (status.MPI_TAG == pvmConst->getMasterSendVarTag())
|
208 : |
|
|
{
|
209 : |
|
|
//There is an incoming message of data type NetDataVariables
|
210 : |
|
|
info = receive();
|
211 : |
|
|
if (info > 0)
|
212 : |
|
|
{
|
213 : |
|
|
typeReceived = pvmConst->getMasterSendVarTag();
|
214 : |
|
|
return 1;
|
215 : |
|
|
}
|
216 : |
|
|
return 0;
|
217 : |
|
|
}
|
218 : |
|
|
else
|
219 : |
|
|
{
|
220 : |
|
|
cerr << "Error in slavecommunication - received unrecognised tag of type " << type << endl;
|
221 : |
|
|
return !OK;
|
222 : |
|
|
}
|
223 : |
|
|
|
224 : |
|
|
}
|
225 : |
|
|
|
226 : |
|
|
int SlaveCommunication::receiveBound()
|
227 : |
|
|
{
|
228 : |
|
|
int i;
|
229 : |
|
|
double* temp = new double[numVar];
|
230 : |
|
|
MPI_Status status;
|
231 : |
|
|
MPI_Comm parentcomm;
|
232 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
233 : |
|
|
MPI_Recv(temp,numVar,MPI_DOUBLE,0,MPI_ANY_TAG,parentcomm,&status);
|
234 : |
|
|
netDataDouble.Reset();
|
235 : |
|
|
netDataDouble.resize(numVar, 0.0);
|
236 : |
|
|
for (i = 0; i < numVar; i++)
|
237 : |
|
|
{
|
238 : |
|
|
netDataDouble[i] = temp[i];
|
239 : |
|
|
}
|
240 : |
|
|
delete [] temp;
|
241 : |
|
|
return 1;
|
242 : |
|
|
}
|
243 : |
|
|
|
244 : |
|
|
int SlaveCommunication::receive()
|
245 : |
|
|
{
|
246 : |
|
|
int info, i;
|
247 : |
|
|
MPI_Status status;
|
248 : |
|
|
MPI_Comm parentcomm;
|
249 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
250 : |
|
|
MPI_Recv(&netDataVar->tag,1,MPI_INT, 0,MPI_ANY_TAG, parentcomm,&status);
|
251 : |
|
|
MPI_Recv(&netDataVar->x_id, 1, MPI_INT, 0,MPI_ANY_TAG, parentcomm, &status);
|
252 : |
|
|
MPI_Recv(netDataVar->x, numVar,MPI_DOUBLE,0,MPI_ANY_TAG, parentcomm, &status);
|
253 : |
|
|
return 1;
|
254 : |
|
|
}
|
255 : |
|
|
|
256 : |
|
|
int SlaveCommunication::receivedVector()
|
257 : |
|
|
{
|
258 : |
|
|
if (pvmConst->getMasterSendVarTag() == typeReceived)
|
259 : |
|
|
return 1;
|
260 : |
|
|
return 0;
|
261 : |
|
|
}
|
262 : |
|
|
|
263 : |
|
|
// Þetta var ekki að virka rétt þar sem kallað var á þetta í Gadget.
|
264 : |
|
|
//void SlaveCommunication::getVector(DoubleVector& vec)
|
265 : |
|
|
//{
|
266 : |
|
|
// /*
|
267 : |
|
|
// Senda út villu ef þetta if condition er ekki uppfyllt... G.E.
|
268 : |
|
|
// */
|
269 : |
|
|
// int i;
|
270 : |
|
|
// if (vec.Size() != numVar)
|
271 : |
|
|
// {
|
272 : |
|
|
// // error....
|
273 : |
|
|
// }
|
274 : |
|
|
//
|
275 : |
|
|
// for (i = 0; i < numVar; i++)
|
276 : |
|
|
// vec[i] = netDataVar->x[i];
|
277 : |
|
|
//}
|
278 : |
|
|
|
279 : |
|
|
void SlaveCommunication::getVector(double* vec) {
|
280 : |
|
|
int i;
|
281 : |
|
|
for (i = 0; i < numVar; i++)
|
282 : |
|
|
vec[i] = netDataVar->x[i];
|
283 : |
|
|
}
|
284 : |
|
|
|
285 : |
|
|
int SlaveCommunication::getReceiveType()
|
286 : |
|
|
{
|
287 : |
|
|
return typeReceived;
|
288 : |
|
|
}
|
289 : |
|
|
|
290 : |
|
|
int SlaveCommunication::receiveString()
|
291 : |
|
|
{
|
292 : |
|
|
int OK = 1;
|
293 : |
|
|
int i, info;
|
294 : |
|
|
char* tempString = new char[MaxStrLength + 1];
|
295 : |
|
|
strncpy(tempString, "", MaxStrLength);
|
296 : |
|
|
tempString[MaxStrLength] = '\0';
|
297 : |
|
|
MPI_Status status;
|
298 : |
|
|
MPI_Comm parentcomm;
|
299 : |
|
|
MPI_Comm_get_parent(&parentcomm);
|
300 : |
|
|
|
301 : |
|
|
for (i = 0; i < numVar; i++)
|
302 : |
|
|
{
|
303 : |
ulcessvp |
8 |
MPI_Recv(tempString, MaxStrLength, MPI_BYTE, 0,MPI_ANY_TAG, parentcomm, &status);
|
304 : |
agomez |
1 |
Parameter pm(tempString);
|
305 : |
|
|
netDataStr.resize(pm);
|
306 : |
|
|
}
|
307 : |
|
|
delete [] tempString;
|
308 : |
|
|
return OK;
|
309 : |
|
|
}
|
310 : |
|
|
|
311 : |
|
|
int SlaveCommunication::receivedString()
|
312 : |
|
|
{
|
313 : |
|
|
if (pvmConst->getMasterSendStringTag() == typeReceived)
|
314 : |
|
|
return 1;
|
315 : |
|
|
return 0;
|
316 : |
|
|
}
|
317 : |
|
|
|
318 : |
|
|
int SlaveCommunication::receivedBounds()
|
319 : |
|
|
{
|
320 : |
|
|
if (pvmConst->getMasterSendBoundTag() == typeReceived)
|
321 : |
|
|
return 1;
|
322 : |
|
|
return 0;
|
323 : |
|
|
}
|
324 : |
|
|
|
325 : |
|
|
const ParameterVector& SlaveCommunication::getStringVector()
|
326 : |
|
|
{
|
327 : |
|
|
return netDataStr;
|
328 : |
|
|
}
|
329 : |
|
|
|
330 : |
|
|
const Parameter& SlaveCommunication::getString(int num)
|
331 : |
|
|
{
|
332 : |
|
|
assert(num >= 0);
|
333 : |
|
|
assert(netDataStr.Size() == numVar);
|
334 : |
|
|
return netDataStr[num];
|
335 : |
|
|
}
|
336 : |
|
|
|
337 : |
|
|
// Þetta var ekki að virka rétt þar sem kallað var á þetta í Gadget.
|
338 : |
|
|
//void SlaveCommunication::getBound(DoubleVector& vec)
|
339 : |
|
|
//{
|
340 : |
|
|
// int i;
|
341 : |
|
|
// if (vec.Size() != numVar)
|
342 : |
|
|
// // errror...
|
343 : |
|
|
// /*
|
344 : |
|
|
// for (i = 0; i < numVar; i++)
|
345 : |
|
|
// vec[i] = netDataDouble[i];
|
346 : |
|
|
// */
|
347 : |
|
|
// vec = netDataDouble;
|
348 : |
|
|
//}
|
349 : |
|
|
|
350 : |
|
|
void SlaveCommunication::getBound(double* vec) {
|
351 : |
|
|
int i;
|
352 : |
|
|
for (i = 0; i < numVar; i++)
|
353 : |
|
|
vec[i] = netDataDouble[i];
|
354 : |
ulcessvp |
11 |
}
|