Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/paramin-beta/slavecommunication.cc
[mareframe] / trunk / paramin-beta / slavecommunication.cc Repository:
ViewVC logotype

Annotation of /trunk/paramin-beta/slavecommunication.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (view) (download)

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 :     NetDataResult* sendData = new NetDataResult;
128 :     sendData->who = myID;
129 :     sendData->result = res;
130 :     sendData->x_id = netDataVar->x_id;
131 :     sendData->tag = netDataVar->tag;
132 :     info = send(sendData);
133 :     delete sendData;
134 :     if (info > 0)
135 :     {
136 :     netDataVar->tag = -1;
137 :     netDataVar->x_id = -1;
138 :     return 1;
139 :     }
140 :     else
141 :     return 0;
142 :     }
143 :     return 0;
144 :     }
145 :    
146 :     int SlaveCommunication::send(NetDataResult* sendData)
147 :     {
148 :     int info;
149 :     MPI_Comm parentcomm;
150 :     MPI_Comm_get_parent(&parentcomm);
151 :     MPI_Send(&sendData->tag,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
152 :     MPI_Send(&sendData->result,1,MPI_DOUBLE, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
153 :     MPI_Send(&sendData->who,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
154 :     MPI_Send(&sendData->x_id,1,MPI_INT, 0, pvmConst->getMasterReceiveDataTag(),parentcomm);
155 :     return 1;
156 :     }
157 :    
158 :     int SlaveCommunication::receiveFromMaster()
159 :     {
160 :     /*
161 :     Þetta fall er komið í bili, þarf samt að skoða með þetta TIMEOUT fall...
162 :     */
163 :     int bufID = 0;
164 :     int OK = 1;
165 :     int info, bytes, source, type;
166 :     typeReceived = -1;
167 :     MPI_Status status;
168 :     MPI_Comm parentcomm;
169 :     MPI_Comm_get_parent(&parentcomm);
170 :    
171 :     // Hér er hægt að nota non-blocking probe til að komast eitthvað til móts við þetta timeout, hægt
172 :     // 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
173 :     // sjálfur Timout fall sem testar á fullu í Maxwait tíma og skilar ef flag er true... Ætla að breyta
174 :     // þessu í bili, en ræð við Bjarka upp á framhaldið...
175 :     // Update: Þetta virkar svona, svo það er e.t.v. ekki nein ástæða til að breyta þessu.
176 :    
177 :     MPI_Probe(0, MPI_ANY_TAG, parentcomm, &status);
178 :     //bufID = pvm_trecv(parenttid, -1, &tmout);
179 :     if (status.MPI_TAG == pvmConst->getStopTag())
180 :     {
181 :     //receive information from master to quit
182 :     int stopMessage;
183 :     MPI_Recv(&stopMessage, 1, MPI_INT, 0, MPI_ANY_TAG, parentcomm, &status);
184 :     typeReceived = pvmConst->getStopTag();
185 :     return !OK;
186 :     }
187 :     else if (status.MPI_TAG == pvmConst->getMasterSendStringTag())
188 :     {
189 :     // There is an incoming message of data type stringDataVariables
190 :     info = receiveString();
191 :     typeReceived = pvmConst->getMasterSendStringTag();
192 :     if (info > 0)
193 :     return 1;
194 :     return 0;
195 :     }
196 :     else if (status.MPI_TAG == pvmConst->getMasterSendBoundTag())
197 :     {
198 :     // There is an incoming message of data type double for the bounds
199 :     info = receiveBound();
200 :     typeReceived = pvmConst->getMasterSendBoundTag();
201 :     if (info > 0)
202 :     return 1;
203 :     return 0;
204 :     }
205 :     else if (status.MPI_TAG == pvmConst->getMasterSendVarTag())
206 :     {
207 :     //There is an incoming message of data type NetDataVariables
208 :     info = receive();
209 :     if (info > 0)
210 :     {
211 :     typeReceived = pvmConst->getMasterSendVarTag();
212 :     return 1;
213 :     }
214 :     return 0;
215 :     }
216 :     else
217 :     {
218 :     cerr << "Error in slavecommunication - received unrecognised tag of type " << type << endl;
219 :     return !OK;
220 :     }
221 :    
222 :     }
223 :    
224 :     int SlaveCommunication::receiveBound()
225 :     {
226 :     int i;
227 :     double* temp = new double[numVar];
228 :     MPI_Status status;
229 :     MPI_Comm parentcomm;
230 :     MPI_Comm_get_parent(&parentcomm);
231 :     MPI_Recv(temp,numVar,MPI_DOUBLE,0,MPI_ANY_TAG,parentcomm,&status);
232 :     netDataDouble.Reset();
233 :     netDataDouble.resize(numVar, 0.0);
234 :     for (i = 0; i < numVar; i++)
235 :     {
236 :     netDataDouble[i] = temp[i];
237 :     }
238 :     delete [] temp;
239 :     return 1;
240 :     }
241 :    
242 :     int SlaveCommunication::receive()
243 :     {
244 :     int info, i;
245 :     MPI_Status status;
246 :     MPI_Comm parentcomm;
247 :     MPI_Comm_get_parent(&parentcomm);
248 :     MPI_Recv(&netDataVar->tag,1,MPI_INT, 0,MPI_ANY_TAG, parentcomm,&status);
249 :     MPI_Recv(&netDataVar->x_id, 1, MPI_INT, 0,MPI_ANY_TAG, parentcomm, &status);
250 :     MPI_Recv(netDataVar->x, numVar,MPI_DOUBLE,0,MPI_ANY_TAG, parentcomm, &status);
251 :     return 1;
252 :     }
253 :    
254 :     int SlaveCommunication::receivedVector()
255 :     {
256 :     if (pvmConst->getMasterSendVarTag() == typeReceived)
257 :     return 1;
258 :     return 0;
259 :     }
260 :    
261 :     // Þetta var ekki að virka rétt þar sem kallað var á þetta í Gadget.
262 :     //void SlaveCommunication::getVector(DoubleVector& vec)
263 :     //{
264 :     // /*
265 :     // Senda út villu ef þetta if condition er ekki uppfyllt... G.E.
266 :     // */
267 :     // int i;
268 :     // if (vec.Size() != numVar)
269 :     // {
270 :     // // error....
271 :     // }
272 :     //
273 :     // for (i = 0; i < numVar; i++)
274 :     // vec[i] = netDataVar->x[i];
275 :     //}
276 :    
277 :     void SlaveCommunication::getVector(double* vec) {
278 :     int i;
279 :     for (i = 0; i < numVar; i++)
280 :     vec[i] = netDataVar->x[i];
281 :     }
282 :    
283 :     int SlaveCommunication::getReceiveType()
284 :     {
285 :     return typeReceived;
286 :     }
287 :    
288 :     int SlaveCommunication::receiveString()
289 :     {
290 :     int OK = 1;
291 :     int i, info;
292 :     char* tempString = new char[MaxStrLength + 1];
293 :     strncpy(tempString, "", MaxStrLength);
294 :     tempString[MaxStrLength] = '\0';
295 :     MPI_Status status;
296 :     MPI_Comm parentcomm;
297 :     MPI_Comm_get_parent(&parentcomm);
298 :    
299 :     for (i = 0; i < numVar; i++)
300 :     {
301 : agomez 9 MPI_Recv(tempString, MaxStrLength, MPI_BYTE, 0,MPI_ANY_TAG, parentcomm, &status);
302 : agomez 1 Parameter pm(tempString);
303 :     netDataStr.resize(pm);
304 :     }
305 :     delete [] tempString;
306 :     return OK;
307 :     }
308 :    
309 :     int SlaveCommunication::receivedString()
310 :     {
311 :     if (pvmConst->getMasterSendStringTag() == typeReceived)
312 :     return 1;
313 :     return 0;
314 :     }
315 :    
316 :     int SlaveCommunication::receivedBounds()
317 :     {
318 :     if (pvmConst->getMasterSendBoundTag() == typeReceived)
319 :     return 1;
320 :     return 0;
321 :     }
322 :    
323 :     const ParameterVector& SlaveCommunication::getStringVector()
324 :     {
325 :     return netDataStr;
326 :     }
327 :    
328 :     const Parameter& SlaveCommunication::getString(int num)
329 :     {
330 :     assert(num >= 0);
331 :     assert(netDataStr.Size() == numVar);
332 :     return netDataStr[num];
333 :     }
334 :    
335 :     // Þetta var ekki að virka rétt þar sem kallað var á þetta í Gadget.
336 :     //void SlaveCommunication::getBound(DoubleVector& vec)
337 :     //{
338 :     // int i;
339 :     // if (vec.Size() != numVar)
340 :     // // errror...
341 :     // /*
342 :     // for (i = 0; i < numVar; i++)
343 :     // vec[i] = netDataDouble[i];
344 :     // */
345 :     // vec = netDataDouble;
346 :     //}
347 :    
348 :     void SlaveCommunication::getBound(double* vec) {
349 :     int i;
350 :     for (i = 0; i < numVar; i++)
351 :     vec[i] = netDataDouble[i];
352 : agomez 6 }

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge