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 #include "OPstream.H"
00038 #include "IPstream.H"
00039 #include "IOstreams.H"
00040 #include "contiguous.H"
00041
00042
00043
00044 namespace Foam
00045 {
00046
00047
00048
00049 template <class T, class CombineOp>
00050 void Pstream::combineGather
00051 (
00052 const List<Pstream::commsStruct>& comms,
00053 T& Value,
00054 const CombineOp& cop
00055 )
00056 {
00057 if (Pstream::parRun())
00058 {
00059
00060 const commsStruct& myComm = comms[Pstream::myProcNo()];
00061
00062
00063 forAll(myComm.below(), belowI)
00064 {
00065 label belowID = myComm.below()[belowI];
00066
00067 if (contiguous<T>())
00068 {
00069 T value;
00070 IPstream::read
00071 (
00072 Pstream::scheduled,
00073 belowID,
00074 reinterpret_cast<char*>(&value),
00075 sizeof(T)
00076 );
00077
00078 if (debug & 2)
00079 {
00080 Pout<< " received from "
00081 << belowID << " data:" << value << endl;
00082 }
00083
00084 cop(Value, value);
00085 }
00086 else
00087 {
00088 IPstream fromBelow(Pstream::scheduled, belowID);
00089 T value(fromBelow);
00090
00091 if (debug & 2)
00092 {
00093 Pout<< " received from "
00094 << belowID << " data:" << value << endl;
00095 }
00096
00097 cop(Value, value);
00098 }
00099 }
00100
00101
00102 if (myComm.above() != -1)
00103 {
00104 if (debug & 2)
00105 {
00106 Pout<< " sending to " << myComm.above()
00107 << " data:" << Value << endl;
00108 }
00109
00110 if (contiguous<T>())
00111 {
00112 OPstream::write
00113 (
00114 Pstream::scheduled,
00115 myComm.above(),
00116 reinterpret_cast<const char*>(&Value),
00117 sizeof(T)
00118 );
00119 }
00120 else
00121 {
00122 OPstream toAbove(Pstream::scheduled, myComm.above());
00123 toAbove << Value;
00124 }
00125 }
00126 }
00127 }
00128
00129
00130 template <class T, class CombineOp>
00131 void Pstream::combineGather(T& Value, const CombineOp& cop)
00132 {
00133 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00134 {
00135 combineGather(Pstream::linearCommunication(), Value, cop);
00136 }
00137 else
00138 {
00139 combineGather(Pstream::treeCommunication(), Value, cop);
00140 }
00141 }
00142
00143
00144 template <class T>
00145 void Pstream::combineScatter(const List<Pstream::commsStruct>& comms, T& Value)
00146 {
00147 if (Pstream::parRun())
00148 {
00149
00150 const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00151
00152
00153 if (myComm.above() != -1)
00154 {
00155 if (contiguous<T>())
00156 {
00157 IPstream::read
00158 (
00159 Pstream::scheduled,
00160 myComm.above(),
00161 reinterpret_cast<char*>(&Value),
00162 sizeof(T)
00163 );
00164 }
00165 else
00166 {
00167 IPstream fromAbove(Pstream::scheduled, myComm.above());
00168 Value = T(fromAbove);
00169 }
00170
00171 if (debug & 2)
00172 {
00173 Pout<< " received from "
00174 << myComm.above() << " data:" << Value << endl;
00175 }
00176 }
00177
00178
00179 forAll(myComm.below(), belowI)
00180 {
00181 label belowID = myComm.below()[belowI];
00182
00183 if (debug & 2)
00184 {
00185 Pout<< " sending to " << belowID << " data:" << Value << endl;
00186 }
00187
00188 if (contiguous<T>())
00189 {
00190 OPstream::write
00191 (
00192 Pstream::scheduled,
00193 belowID,
00194 reinterpret_cast<const char*>(&Value),
00195 sizeof(T)
00196 );
00197 }
00198 else
00199 {
00200 OPstream toBelow(Pstream::scheduled, belowID);
00201 toBelow << Value;
00202 }
00203 }
00204 }
00205 }
00206
00207
00208 template <class T>
00209 void Pstream::combineScatter(T& Value)
00210 {
00211 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00212 {
00213 combineScatter(Pstream::linearCommunication(), Value);
00214 }
00215 else
00216 {
00217 combineScatter(Pstream::treeCommunication(), Value);
00218 }
00219 }
00220
00221
00222
00223
00224
00225
00226 template <class T, class CombineOp>
00227 void Pstream::listCombineGather
00228 (
00229 const List<Pstream::commsStruct>& comms,
00230 List<T>& Values,
00231 const CombineOp& cop
00232 )
00233 {
00234 if (Pstream::parRun())
00235 {
00236
00237 const commsStruct& myComm = comms[Pstream::myProcNo()];
00238
00239
00240 forAll(myComm.below(), belowI)
00241 {
00242 label belowID = myComm.below()[belowI];
00243
00244 if (contiguous<T>())
00245 {
00246 List<T> receivedValues(Values.size());
00247
00248 IPstream::read
00249 (
00250 Pstream::scheduled,
00251 belowID,
00252 reinterpret_cast<char*>(receivedValues.begin()),
00253 receivedValues.byteSize()
00254 );
00255
00256 if (debug & 2)
00257 {
00258 Pout<< " received from "
00259 << belowID << " data:" << receivedValues << endl;
00260 }
00261
00262 forAll(Values, i)
00263 {
00264 cop(Values[i], receivedValues[i]);
00265 }
00266 }
00267 else
00268 {
00269 IPstream fromBelow(Pstream::scheduled, belowID);
00270 List<T> receivedValues(fromBelow);
00271
00272 if (debug & 2)
00273 {
00274 Pout<< " received from "
00275 << belowID << " data:" << receivedValues << endl;
00276 }
00277
00278 forAll(Values, i)
00279 {
00280 cop(Values[i], receivedValues[i]);
00281 }
00282 }
00283 }
00284
00285
00286 if (myComm.above() != -1)
00287 {
00288 if (debug & 2)
00289 {
00290 Pout<< " sending to " << myComm.above()
00291 << " data:" << Values << endl;
00292 }
00293
00294 if (contiguous<T>())
00295 {
00296 OPstream::write
00297 (
00298 Pstream::scheduled,
00299 myComm.above(),
00300 reinterpret_cast<const char*>(Values.begin()),
00301 Values.byteSize()
00302 );
00303 }
00304 else
00305 {
00306 OPstream toAbove(Pstream::scheduled, myComm.above());
00307 toAbove << Values;
00308 }
00309 }
00310 }
00311 }
00312
00313
00314 template <class T, class CombineOp>
00315 void Pstream::listCombineGather(List<T>& Values, const CombineOp& cop)
00316 {
00317 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00318 {
00319 listCombineGather(Pstream::linearCommunication(), Values, cop);
00320 }
00321 else
00322 {
00323 listCombineGather(Pstream::treeCommunication(), Values, cop);
00324 }
00325 }
00326
00327
00328 template <class T>
00329 void Pstream::listCombineScatter
00330 (
00331 const List<Pstream::commsStruct>& comms,
00332 List<T>& Values
00333 )
00334 {
00335 if (Pstream::parRun())
00336 {
00337
00338 const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00339
00340
00341 if (myComm.above() != -1)
00342 {
00343 if (contiguous<T>())
00344 {
00345 IPstream::read
00346 (
00347 Pstream::scheduled,
00348 myComm.above(),
00349 reinterpret_cast<char*>(Values.begin()),
00350 Values.byteSize()
00351 );
00352 }
00353 else
00354 {
00355 IPstream fromAbove(Pstream::scheduled, myComm.above());
00356 fromAbove >> Values;
00357 }
00358
00359 if (debug & 2)
00360 {
00361 Pout<< " received from "
00362 << myComm.above() << " data:" << Values << endl;
00363 }
00364 }
00365
00366
00367 forAll(myComm.below(), belowI)
00368 {
00369 label belowID = myComm.below()[belowI];
00370
00371 if (debug & 2)
00372 {
00373 Pout<< " sending to " << belowID << " data:" << Values << endl;
00374 }
00375
00376 if (contiguous<T>())
00377 {
00378 OPstream::write
00379 (
00380 Pstream::scheduled,
00381 belowID,
00382 reinterpret_cast<const char*>(Values.begin()),
00383 Values.byteSize()
00384 );
00385 }
00386 else
00387 {
00388 OPstream toBelow(Pstream::scheduled, belowID);
00389 toBelow << Values;
00390 }
00391 }
00392 }
00393 }
00394
00395
00396 template <class T>
00397 void Pstream::listCombineScatter(List<T>& Values)
00398 {
00399 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00400 {
00401 listCombineScatter(Pstream::linearCommunication(), Values);
00402 }
00403 else
00404 {
00405 listCombineScatter(Pstream::treeCommunication(), Values);
00406 }
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416 template <class Container, class CombineOp>
00417 void Pstream::mapCombineGather
00418 (
00419 const List<Pstream::commsStruct>& comms,
00420 Container& Values,
00421 const CombineOp& cop
00422 )
00423 {
00424 if (Pstream::parRun())
00425 {
00426
00427 const commsStruct& myComm = comms[Pstream::myProcNo()];
00428
00429
00430 forAll(myComm.below(), belowI)
00431 {
00432 label belowID = myComm.below()[belowI];
00433
00434 IPstream fromBelow(Pstream::scheduled, belowID);
00435 Container receivedValues(fromBelow);
00436
00437 if (debug & 2)
00438 {
00439 Pout<< " received from "
00440 << belowID << " data:" << receivedValues << endl;
00441 }
00442
00443 for
00444 (
00445 typename Container::const_iterator slaveIter =
00446 receivedValues.begin();
00447 slaveIter != receivedValues.end();
00448 ++slaveIter
00449 )
00450 {
00451 typename Container::iterator
00452 masterIter = Values.find(slaveIter.key());
00453
00454 if (masterIter != Values.end())
00455 {
00456 cop(masterIter(), slaveIter());
00457 }
00458 else
00459 {
00460 Values.insert(slaveIter.key(), slaveIter());
00461 }
00462 }
00463 }
00464
00465
00466 if (myComm.above() != -1)
00467 {
00468 if (debug & 2)
00469 {
00470 Pout<< " sending to " << myComm.above()
00471 << " data:" << Values << endl;
00472 }
00473
00474 OPstream toAbove(Pstream::scheduled, myComm.above());
00475 toAbove << Values;
00476 }
00477 }
00478 }
00479
00480
00481 template <class Container, class CombineOp>
00482 void Pstream::mapCombineGather(Container& Values, const CombineOp& cop)
00483 {
00484 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00485 {
00486 mapCombineGather(Pstream::linearCommunication(), Values, cop);
00487 }
00488 else
00489 {
00490 mapCombineGather(Pstream::treeCommunication(), Values, cop);
00491 }
00492 }
00493
00494
00495 template <class Container>
00496 void Pstream::mapCombineScatter
00497 (
00498 const List<Pstream::commsStruct>& comms,
00499 Container& Values
00500 )
00501 {
00502 if (Pstream::parRun())
00503 {
00504
00505 const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
00506
00507
00508 if (myComm.above() != -1)
00509 {
00510 IPstream fromAbove(Pstream::scheduled, myComm.above());
00511 fromAbove >> Values;
00512
00513 if (debug & 2)
00514 {
00515 Pout<< " received from "
00516 << myComm.above() << " data:" << Values << endl;
00517 }
00518 }
00519
00520
00521 forAll(myComm.below(), belowI)
00522 {
00523 label belowID = myComm.below()[belowI];
00524
00525 if (debug & 2)
00526 {
00527 Pout<< " sending to " << belowID << " data:" << Values << endl;
00528 }
00529
00530 OPstream toBelow(Pstream::scheduled, belowID);
00531 toBelow << Values;
00532 }
00533 }
00534 }
00535
00536
00537 template <class Container>
00538 void Pstream::mapCombineScatter(Container& Values)
00539 {
00540 if (Pstream::nProcs() < Pstream::nProcsSimpleSum)
00541 {
00542 mapCombineScatter(Pstream::linearCommunication(), Values);
00543 }
00544 else
00545 {
00546 mapCombineScatter(Pstream::treeCommunication(), Values);
00547 }
00548 }
00549
00550
00551
00552
00553
00554
00555 }
00556
00557