Pstream.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #include "Pstream.H"
00058 #include "PstreamReduceOps.H"
00059 #include "OSspecific.H"
00060 #include "PstreamGlobals.H"
00061
00062 #include <cstring>
00063 #include <cstdlib>
00064 #include <csignal>
00065
00066 extern "C"
00067 {
00068 # include <linux/gamma/libgamma.h>
00069 }
00070
00071
00072
00073
00074 namespace Foam
00075 {
00076
00077
00078 static void handler(void)
00079 {
00080 label current = PstreamGlobals::recvIndex[gamma_active_port];
00081
00082 List<char>& buf = PstreamGlobals::recvBuf[current][gamma_active_port];
00083 label bufLen = PstreamGlobals::recvBufLen[current][gamma_active_port];
00084
00085 if (bufLen != -1)
00086 {
00087 FatalErrorIn("Pstream::handler(void)")
00088 << "Buffer length not reset : "
00089 << bufLen
00090 << " when receiving message of size " << gamma_msglen
00091 << " from processor " << gamma_active_port << endl
00092 << "This means that the existing data has not been consumed yet"
00093 << " (by IPstream::read) and means your communication pattern"
00094 << " is probably not balanced (a receive for every send)"
00095 << endl
00096 << "This can happen if you have e.g. gather without scatter."
00097 << endl
00098 << "A workaround is to increase the depth of the circular"
00099 << " receive buffers in PstreamGlobals.H"
00100 << abort(FatalError);
00101 }
00102
00103
00104
00105 if
00106 (
00107 gamma_msglen < 0
00108 || gamma_msglen > buf.size()
00109 )
00110 {
00111 FatalErrorIn("Pstream::handler(void)")
00112 << "Received message of size " << gamma_msglen
00113 << " from processor " << gamma_active_port
00114 << Foam::endl
00115 << "but global receive buffer is only of size "
00116 << buf.size()
00117 << abort(FatalError);
00118 }
00119
00120
00121 label resizeLen = PstreamGlobals::getSizeFromHeader
00122 (
00123 buf.begin(),
00124 gamma_msglen
00125 );
00126
00127 if (resizeLen != -1)
00128 {
00129 if (Pstream::debug)
00130 {
00131 Pout<< "Pstream::handler : Resize message:" << resizeLen
00132 << " from proc " << gamma_active_port
00133 << " current size:"
00134 << PstreamGlobals::getMaxBufSize(gamma_active_port)
00135 << Foam::endl;
00136 }
00137
00138
00139 List<char> savedBuf;
00140
00141 if (resizeLen > PstreamGlobals::getMaxBufSize(gamma_active_port))
00142 {
00143 if (Pstream::debug)
00144 {
00145 Pout<< "Pstream::handler :"
00146 << " resizing receive buffer for processor "
00147 << gamma_active_port
00148 << " from "
00149 << PstreamGlobals::getMaxBufSize(gamma_active_port)
00150 << " to " << resizeLen << Foam::endl;
00151 }
00152
00153
00154
00155
00156 savedBuf.transfer(buf);
00157
00158
00159 forAll(PstreamGlobals::recvBuf, i)
00160 {
00161 List<char>& chars =
00162 PstreamGlobals::recvBuf[i][gamma_active_port];
00163
00164
00165 chars.setSize(resizeLen);
00166
00167 }
00168 }
00169
00170
00171 PstreamGlobals::recvBufLen[current][gamma_active_port] = -2;
00172 }
00173 else
00174 {
00175
00176 PstreamGlobals::recvBufLen[current][gamma_active_port] = gamma_msglen;
00177 }
00178
00179
00180 label next = PstreamGlobals::recvBuf.fcIndex(current);
00181 PstreamGlobals::recvIndex[gamma_active_port] = next;
00182
00183
00184 gamma_post_recv
00185 (
00186 gamma_active_port,
00187 PstreamGlobals::recvBuf[next][gamma_active_port].begin(),
00188 PstreamGlobals::recvBuf[next][gamma_active_port].size()
00189 );
00190 }
00191
00192
00193
00194
00195 void Pstream::addValidParOptions(HashTable<string>& validParOptions)
00196 {
00197 validParOptions.insert("np", "");
00198 validParOptions.insert("p4pg", "PI file");
00199 validParOptions.insert("p4wd", "directory");
00200 validParOptions.insert("p4amslave", "");
00201 validParOptions.insert("p4yourname", "hostname");
00202
00203 validParOptions.insert("machinefile", "machine file");
00204 validParOptions.insert("GAMMANP", "numProcs");
00205 validParOptions.insert("GAMMAHOME", "gamma cwd");
00206 validParOptions.insert("GAMMA", "1(enable) or 0(disable)");
00207 }
00208
00209
00210 bool Pstream::init(int& argc, char**& argv)
00211 {
00212 int numprocs = 0;
00213
00214 string npString("-GAMMANP");
00215
00216 for (label i = 0; i < argc; i++)
00217 {
00218 if (argv[i] == npString)
00219 {
00220 if (i+1 < argc)
00221 {
00222 numprocs = atoi(argv[i+1]);
00223 break;
00224 }
00225 }
00226 }
00227
00228
00229 unsigned char smallNumprocs = numprocs;
00230
00231 gamma_init(smallNumprocs, argc, argv);
00232
00233 myProcNo_ = gamma_my_node();
00234
00235
00236 setParRun();
00237
00238 procIDs_.setSize(numprocs);
00239
00240 forAll(procIDs_, procNo)
00241 {
00242 procIDs_[procNo] = procNo;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 PstreamGlobals::recvIndex.setSize(numprocs);
00254 PstreamGlobals::recvIndex = 0;
00255 PstreamGlobals::consumeIndex.setSize(numprocs);
00256 PstreamGlobals::consumeIndex = 0;
00257
00258 forAll(PstreamGlobals::recvBuf, i)
00259 {
00260 PstreamGlobals::recvBufLen[i].setSize(numprocs);
00261 PstreamGlobals::recvBufLen[i] = -1;
00262
00263 List<List<char> >& buffers = PstreamGlobals::recvBuf[i];
00264
00265 buffers.setSize(numprocs);
00266 forAll(buffers, procNo)
00267 {
00268 if (procNo != myProcNo_)
00269 {
00270 buffers[procNo].setSize(PstreamGlobals::initialBufferLen);
00271
00272
00273
00274
00275
00276
00277
00278
00279 }
00280 }
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 forAll(procIDs_, procNo)
00294 {
00295 if (procNo != myProcNo_)
00296 {
00297
00298 label current = PstreamGlobals::recvIndex[procNo];
00299
00300
00301 List<char>& buf = PstreamGlobals::recvBuf[current][procNo];
00302
00303 gamma_set_active_port
00304 (
00305 procNo,
00306 procNo,
00307 gamma_my_par_pid(),
00308 myProcNo_,
00309 handler,
00310 procNo,
00311 GO_BACK,
00312 buf.begin(),
00313 buf.size()
00314 );
00315 }
00316 }
00317
00318
00319
00320 gamma_sync();
00321
00322 Info<< "GAMMA Pstream initialized with:" << nl
00323 << " floatTransfer : " << floatTransfer << nl
00324 << " nProcsSimpleSum : " << nProcsSimpleSum << nl
00325 << " scheduledTransfer : " << Pstream::scheduledTransfer << nl
00326 << Foam::endl;
00327
00328
00329 initCommunicationSchedule();
00330
00331 return true;
00332 }
00333
00334
00335 void Pstream::exit(int errnum)
00336 {
00337
00338 gamma_exit();
00339
00340 }
00341
00342
00343 void Pstream::abort()
00344 {
00345 Pout<< "**Pstream::abort()**" << endl;
00346
00347 gamma_abort();
00348 }
00349
00350
00351 void reduce(scalar& Value, const sumOp<scalar>& bop)
00352 {
00353 if (!Pstream::parRun())
00354 {
00355 return;
00356 }
00357
00358 if (Pstream::debug)
00359 {
00360 Pout<< "**entering Pstream::reduce for " << Value << Foam::endl;
00361 }
00362
00363
00364 if (Pstream::master())
00365 {
00366 for
00367 (
00368 int slave=Pstream::firstSlave();
00369 slave<=Pstream::lastSlave();
00370 slave++
00371 )
00372 {
00373 scalar value;
00374
00375 if
00376 (
00377 !IPstream::read
00378 (
00379 slave,
00380 reinterpret_cast<char*>(&value),
00381 sizeof(Value)
00382 )
00383 )
00384 {
00385 FatalErrorIn
00386 (
00387 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00388 ) << "IPstream::read failed"
00389 << Foam::abort(FatalError);
00390 }
00391
00392 Value = bop(Value, value);
00393 }
00394 }
00395 else
00396 {
00397 if
00398 (
00399 !OPstream::write
00400 (
00401 Pstream::masterNo(),
00402 reinterpret_cast<const char*>(&Value),
00403 sizeof(Value),
00404 false
00405 )
00406 )
00407 {
00408 FatalErrorIn
00409 (
00410 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00411 ) << "OPstream::write failed"
00412 << Foam::abort(FatalError);
00413 }
00414 }
00415
00416 if (Pstream::master())
00417 {
00418 for
00419 (
00420 int slave=Pstream::firstSlave();
00421 slave<=Pstream::lastSlave();
00422 slave++
00423 )
00424 {
00425 if
00426 (
00427 !OPstream::write
00428 (
00429 slave,
00430 reinterpret_cast<const char*>(&Value),
00431 sizeof(Value),
00432 false
00433 )
00434 )
00435 {
00436 FatalErrorIn
00437 (
00438 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00439 ) << "OPstream::write failed"
00440 << Foam::abort(FatalError);
00441 }
00442 }
00443 }
00444 else
00445 {
00446 if
00447 (
00448 !IPstream::read
00449 (
00450 Pstream::masterNo(),
00451 reinterpret_cast<char*>(&Value),
00452 sizeof(Value)
00453 )
00454 )
00455 {
00456 FatalErrorIn
00457 (
00458 "reduce(scalar& Value, const sumOp<scalar>& sumOp)"
00459 ) << "IPstream::read failed"
00460 << Foam::abort(FatalError);
00461 }
00462 }
00463
00464 if (Pstream::debug)
00465 {
00466 Pout<< "**exiting Pstream::reduce with " << Value << Foam::endl;
00467 }
00468 }
00469
00470
00471
00472
00473 }
00474
00475
Copyright © 2000-2009 OpenCFD Ltd