OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

mapDistributeTemplates.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Pstream.H"
00028 
00029 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00030 
00031 // Distribute list.
00032 template<class T>
00033 void Foam::mapDistribute::distribute
00034 (
00035     const Pstream::commsTypes commsType,
00036     const List<labelPair>& schedule,
00037     const label constructSize,
00038     const labelListList& subMap,
00039     const labelListList& constructMap,
00040     List<T>& field
00041 )
00042 {
00043     if (commsType == Pstream::blocking)
00044     {
00045         // Since buffered sending can reuse the field to collect the
00046         // received data.
00047 
00048         // Send sub field to neighbour
00049         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00050         {
00051             const labelList& map = subMap[domain];
00052 
00053             if (domain != Pstream::myProcNo() && map.size())
00054             {
00055                 OPstream toNbr(Pstream::blocking, domain);
00056                 toNbr << UIndirectList<T>(field, map);
00057             }
00058         }
00059 
00060         // Subset myself
00061         const labelList& mySubMap = subMap[Pstream::myProcNo()];
00062 
00063         List<T> subField(mySubMap.size());
00064         forAll(mySubMap, i)
00065         {
00066             subField[i] = field[mySubMap[i]];
00067         }
00068 
00069         // Receive sub field from myself (subField)
00070         const labelList& map = constructMap[Pstream::myProcNo()];
00071 
00072         field.setSize(constructSize);
00073 
00074         forAll(map, i)
00075         {
00076             field[map[i]] = subField[i];
00077         }
00078 
00079         // Receive sub field from neighbour
00080         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00081         {
00082             const labelList& map = constructMap[domain];
00083 
00084             if (domain != Pstream::myProcNo() && map.size())
00085             {
00086                 IPstream fromNbr(Pstream::blocking, domain);
00087                 List<T> subField(fromNbr);
00088 
00089                 if (subField.size() != map.size())
00090                 {
00091                     FatalErrorIn
00092                     (
00093                         "template<class T>\n"
00094                         "void mapDistribute::distribute\n"
00095                         "(\n"
00096                         "    const Pstream::commsTypes commsType,\n"
00097                         "    const List<labelPair>& schedule,\n"
00098                         "    const label constructSize,\n"
00099                         "    const labelListList& subMap,\n"
00100                         "    const labelListList& constructMap,\n"
00101                         "    List<T>& field\n"
00102                         ")\n"
00103                     )   << "Expected from processor " << domain
00104                         << " " << map.size() << " but received "
00105                         << subField.size() << " elements."
00106                         << abort(FatalError);
00107                 }
00108 
00109                 forAll(map, i)
00110                 {
00111                     field[map[i]] = subField[i];
00112                 }
00113             }
00114         }
00115     }
00116     else if (commsType == Pstream::scheduled)
00117     {
00118         // Need to make sure I don't overwrite field with received data
00119         // since the data might need to be sent to another processor. So
00120         // allocate a new field for the results.
00121         List<T> newField(constructSize);
00122 
00123         // Subset myself
00124         UIndirectList<T> subField(field, subMap[Pstream::myProcNo()]);
00125 
00126         // Receive sub field from myself (subField)
00127         const labelList& map = constructMap[Pstream::myProcNo()];
00128 
00129         forAll(map, i)
00130         {
00131             newField[map[i]] = subField[i];
00132         }
00133 
00134         // Schedule will already have pruned 0-sized comms
00135         forAll(schedule, i)
00136         {
00137             const labelPair& twoProcs = schedule[i];
00138             label sendProc = twoProcs[0];
00139             label recvProc = twoProcs[1];
00140 
00141             if (Pstream::myProcNo() == sendProc)
00142             {
00143                 // I am sender. Send to recvProc.
00144                 OPstream toNbr(Pstream::scheduled, recvProc);
00145                 toNbr << UIndirectList<T>(field, subMap[recvProc]);
00146             }
00147             else
00148             {
00149                 // I am receiver. Receive from sendProc.
00150                 IPstream fromNbr(Pstream::scheduled, sendProc);
00151                 List<T> subField(fromNbr);
00152 
00153                 const labelList& map = constructMap[sendProc];
00154 
00155                 if (subField.size() != map.size())
00156                 {
00157                     FatalErrorIn
00158                     (
00159                         "template<class T>\n"
00160                         "void mapDistribute::distribute\n"
00161                         "(\n"
00162                         "    const Pstream::commsTypes commsType,\n"
00163                         "    const List<labelPair>& schedule,\n"
00164                         "    const label constructSize,\n"
00165                         "    const labelListList& subMap,\n"
00166                         "    const labelListList& constructMap,\n"
00167                         "    List<T>& field\n"
00168                         ")\n"
00169                     )   << "Expected from processor " << sendProc
00170                         << " " << map.size() << " but received "
00171                         << subField.size() << " elements."
00172                         << abort(FatalError);
00173                 }
00174 
00175                 forAll(map, i)
00176                 {
00177                     newField[map[i]] = subField[i];
00178                 }
00179             }
00180         }
00181         field.transfer(newField);
00182     }
00183     else if (commsType == Pstream::nonBlocking)
00184     {
00185         if (!contiguous<T>())
00186         {
00187             FatalErrorIn
00188             (
00189                 "template<class T>\n"
00190                 "void mapDistribute::distribute\n"
00191                 "(\n"
00192                 "    const Pstream::commsTypes commsType,\n"
00193                 "    const List<labelPair>& schedule,\n"
00194                 "    const label constructSize,\n"
00195                 "    const labelListList& subMap,\n"
00196                 "    const labelListList& constructMap,\n"
00197                 "    List<T>& field\n"
00198                 ")\n"
00199             )   << "Non-blocking only supported for contiguous data."
00200                 << exit(FatalError);
00201         }
00202 
00203         // Set up sends to neighbours
00204 
00205         List<List<T > > sendFields(Pstream::nProcs());
00206 
00207         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00208         {
00209             const labelList& map = subMap[domain];
00210 
00211             if (domain != Pstream::myProcNo() && map.size())
00212             {
00213                 List<T>& subField = sendFields[domain];
00214                 subField.setSize(map.size());
00215                 forAll(map, i)
00216                 {
00217                     subField[i] = field[map[i]];
00218                 }
00219 
00220                 OPstream::write
00221                 (
00222                     Pstream::nonBlocking,
00223                     domain,
00224                     reinterpret_cast<const char*>(subField.begin()),
00225                     subField.size()*sizeof(T)
00226                 );
00227             }
00228         }
00229 
00230         // Set up receives from neighbours
00231 
00232         List<List<T > > recvFields(Pstream::nProcs());
00233 
00234         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00235         {
00236             const labelList& map = constructMap[domain];
00237 
00238             if (domain != Pstream::myProcNo() && map.size())
00239             {
00240                 recvFields[domain].setSize(map.size());
00241                 IPstream::read
00242                 (
00243                     Pstream::nonBlocking,
00244                     domain,
00245                     reinterpret_cast<char*>(recvFields[domain].begin()),
00246                     recvFields[domain].size()*sizeof(T)
00247                 );
00248             }
00249         }
00250 
00251 
00252         // Set up 'send' to myself
00253 
00254         {
00255             const labelList& map = subMap[Pstream::myProcNo()];
00256 
00257             List<T>& subField = sendFields[Pstream::myProcNo()];
00258             subField.setSize(map.size());
00259             forAll(map, i)
00260             {
00261                 subField[i] = field[map[i]];
00262             }
00263         }
00264 
00265 
00266         // Combine bits. Note that can reuse field storage
00267 
00268         field.setSize(constructSize);
00269 
00270 
00271         // Receive sub field from myself (sendFields[Pstream::myProcNo()])
00272         {
00273             const labelList& map = constructMap[Pstream::myProcNo()];
00274             const List<T>& subField = sendFields[Pstream::myProcNo()];
00275 
00276             forAll(map, i)
00277             {
00278                 field[map[i]] = subField[i];
00279             }
00280         }
00281 
00282 
00283         // Wait for all to finish
00284 
00285         OPstream::waitRequests();
00286         IPstream::waitRequests();
00287 
00288         // Collect neighbour fields
00289 
00290         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00291         {
00292             const labelList& map = constructMap[domain];
00293 
00294             if (domain != Pstream::myProcNo() && map.size())
00295             {
00296                 if (recvFields[domain].size() != map.size())
00297                 {
00298                     FatalErrorIn
00299                     (
00300                         "template<class T>\n"
00301                         "void mapDistribute::distribute\n"
00302                         "(\n"
00303                         "    const Pstream::commsTypes commsType,\n"
00304                         "    const List<labelPair>& schedule,\n"
00305                         "    const label constructSize,\n"
00306                         "    const labelListList& subMap,\n"
00307                         "    const labelListList& constructMap,\n"
00308                         "    List<T>& field\n"
00309                         ")\n"
00310                     )   << "Expected from processor " << domain
00311                         << " " << map.size() << " but received "
00312                         << recvFields[domain].size() << " elements."
00313                         << abort(FatalError);
00314                 }
00315 
00316                 forAll(map, i)
00317                 {
00318                     field[map[i]] = recvFields[domain][i];
00319                 }
00320             }
00321         }
00322     }
00323     else
00324     {
00325         FatalErrorIn("mapDistribute::distribute(..)")
00326             << "Unknown communication schedule " << commsType
00327             << abort(FatalError);
00328     }
00329 }
00330 
00331 
00332 // Distribute list.
00333 template<class T, class CombineOp>
00334 void Foam::mapDistribute::distribute
00335 (
00336     const Pstream::commsTypes commsType,
00337     const List<labelPair>& schedule,
00338     const label constructSize,
00339     const labelListList& subMap,
00340     const labelListList& constructMap,
00341     List<T>& field,
00342     const CombineOp& cop,
00343     const T& nullValue
00344 )
00345 {
00346     if (commsType == Pstream::blocking)
00347     {
00348         // Since buffered sending can reuse the field to collect the
00349         // received data.
00350 
00351         // Send sub field to neighbour
00352         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00353         {
00354             const labelList& map = subMap[domain];
00355 
00356             if (domain != Pstream::myProcNo() && map.size())
00357             {
00358                 OPstream toNbr(Pstream::blocking, domain);
00359                 toNbr << UIndirectList<T>(field, map);
00360             }
00361         }
00362 
00363         // Subset myself
00364         const labelList& mySubMap = subMap[Pstream::myProcNo()];
00365 
00366         List<T> subField(mySubMap.size());
00367         forAll(mySubMap, i)
00368         {
00369             subField[i] = field[mySubMap[i]];
00370         }
00371 
00372         // Receive sub field from myself (subField)
00373         const labelList& map = constructMap[Pstream::myProcNo()];
00374 
00375         field.setSize(constructSize);
00376         field = nullValue;
00377 
00378         forAll(map, i)
00379         {
00380             cop(field[map[i]], subField[i]);
00381         }
00382 
00383         // Receive sub field from neighbour
00384         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00385         {
00386             const labelList& map = constructMap[domain];
00387 
00388             if (domain != Pstream::myProcNo() && map.size())
00389             {
00390                 IPstream fromNbr(Pstream::blocking, domain);
00391                 List<T> subField(fromNbr);
00392 
00393                 if (subField.size() != map.size())
00394                 {
00395                     FatalErrorIn
00396                     (
00397                         "template<class T>\n"
00398                         "void mapDistribute::distribute\n"
00399                         "(\n"
00400                         "    const Pstream::commsTypes commsType,\n"
00401                         "    const List<labelPair>& schedule,\n"
00402                         "    const label constructSize,\n"
00403                         "    const labelListList& subMap,\n"
00404                         "    const labelListList& constructMap,\n"
00405                         "    List<T>& field\n"
00406                         ")\n"
00407                     )   << "Expected from processor " << domain
00408                         << " " << map.size() << " but received "
00409                         << subField.size() << " elements."
00410                         << abort(FatalError);
00411                 }
00412 
00413                 forAll(map, i)
00414                 {
00415                     cop(field[map[i]], subField[i]);
00416                 }
00417             }
00418         }
00419     }
00420     else if (commsType == Pstream::scheduled)
00421     {
00422         // Need to make sure I don't overwrite field with received data
00423         // since the data might need to be sent to another processor. So
00424         // allocate a new field for the results.
00425         List<T> newField(constructSize, nullValue);
00426 
00427         // Subset myself
00428         UIndirectList<T> subField(field, subMap[Pstream::myProcNo()]);
00429 
00430         // Receive sub field from myself (subField)
00431         const labelList& map = constructMap[Pstream::myProcNo()];
00432 
00433         forAll(map, i)
00434         {
00435             cop(newField[map[i]], subField[i]);
00436         }
00437 
00438         // Schedule will already have pruned 0-sized comms
00439         forAll(schedule, i)
00440         {
00441             const labelPair& twoProcs = schedule[i];
00442             label sendProc = twoProcs[0];
00443             label recvProc = twoProcs[1];
00444 
00445             if (Pstream::myProcNo() == sendProc)
00446             {
00447                 // I am sender. Send to recvProc.
00448                 OPstream toNbr(Pstream::scheduled, recvProc);
00449                 toNbr << UIndirectList<T>(field, subMap[recvProc]);
00450             }
00451             else
00452             {
00453                 // I am receiver. Receive from sendProc.
00454                 IPstream fromNbr(Pstream::scheduled, sendProc);
00455                 List<T> subField(fromNbr);
00456 
00457                 const labelList& map = constructMap[sendProc];
00458 
00459                 if (subField.size() != map.size())
00460                 {
00461                     FatalErrorIn
00462                     (
00463                         "template<class T>\n"
00464                         "void mapDistribute::distribute\n"
00465                         "(\n"
00466                         "    const Pstream::commsTypes commsType,\n"
00467                         "    const List<labelPair>& schedule,\n"
00468                         "    const label constructSize,\n"
00469                         "    const labelListList& subMap,\n"
00470                         "    const labelListList& constructMap,\n"
00471                         "    List<T>& field\n"
00472                         ")\n"
00473                     )   << "Expected from processor " << sendProc
00474                         << " " << map.size() << " but received "
00475                         << subField.size() << " elements."
00476                         << abort(FatalError);
00477                 }
00478 
00479                 forAll(map, i)
00480                 {
00481                     cop(newField[map[i]], subField[i]);
00482                 }
00483             }
00484         }
00485         field.transfer(newField);
00486     }
00487     else if (commsType == Pstream::nonBlocking)
00488     {
00489         if (!contiguous<T>())
00490         {
00491             FatalErrorIn
00492             (
00493                 "template<class T>\n"
00494                 "void mapDistribute::distribute\n"
00495                 "(\n"
00496                 "    const Pstream::commsTypes commsType,\n"
00497                 "    const List<labelPair>& schedule,\n"
00498                 "    const label constructSize,\n"
00499                 "    const labelListList& subMap,\n"
00500                 "    const labelListList& constructMap,\n"
00501                 "    List<T>& field\n"
00502                 ")\n"
00503             )   << "Non-blocking only supported for contiguous data."
00504                 << exit(FatalError);
00505         }
00506 
00507         // Set up sends to neighbours
00508 
00509         List<List<T > > sendFields(Pstream::nProcs());
00510 
00511         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00512         {
00513             const labelList& map = subMap[domain];
00514 
00515             if (domain != Pstream::myProcNo() && map.size())
00516             {
00517                 List<T>& subField = sendFields[domain];
00518                 subField.setSize(map.size());
00519                 forAll(map, i)
00520                 {
00521                     subField[i] = field[map[i]];
00522                 }
00523 
00524                 OPstream::write
00525                 (
00526                     Pstream::nonBlocking,
00527                     domain,
00528                     reinterpret_cast<const char*>(subField.begin()),
00529                     subField.size()*sizeof(T)
00530                 );
00531             }
00532         }
00533 
00534         // Set up receives from neighbours
00535 
00536         List<List<T > > recvFields(Pstream::nProcs());
00537 
00538         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00539         {
00540             const labelList& map = constructMap[domain];
00541 
00542             if (domain != Pstream::myProcNo() && map.size())
00543             {
00544                 recvFields[domain].setSize(map.size());
00545                 IPstream::read
00546                 (
00547                     Pstream::nonBlocking,
00548                     domain,
00549                     reinterpret_cast<char*>(recvFields[domain].begin()),
00550                     recvFields[domain].size()*sizeof(T)
00551                 );
00552             }
00553         }
00554 
00555         // Set up 'send' to myself
00556 
00557         {
00558             const labelList& map = subMap[Pstream::myProcNo()];
00559 
00560             List<T>& subField = sendFields[Pstream::myProcNo()];
00561             subField.setSize(map.size());
00562             forAll(map, i)
00563             {
00564                 subField[i] = field[map[i]];
00565             }
00566         }
00567 
00568 
00569         // Combine bits. Note that can reuse field storage
00570 
00571         field.setSize(constructSize);
00572         field = nullValue;
00573 
00574         // Receive sub field from myself (subField)
00575         {
00576             const labelList& map = constructMap[Pstream::myProcNo()];
00577             const List<T>& subField = sendFields[Pstream::myProcNo()];
00578 
00579             forAll(map, i)
00580             {
00581                 cop(field[map[i]], subField[i]);
00582             }
00583         }
00584 
00585 
00586         // Wait for all to finish
00587 
00588         OPstream::waitRequests();
00589         IPstream::waitRequests();
00590 
00591         // Collect neighbour fields
00592 
00593         for (label domain = 0; domain < Pstream::nProcs(); domain++)
00594         {
00595             const labelList& map = constructMap[domain];
00596 
00597             if (domain != Pstream::myProcNo() && map.size())
00598             {
00599                 if (recvFields[domain].size() != map.size())
00600                 {
00601                     FatalErrorIn
00602                     (
00603                         "template<class T>\n"
00604                         "void mapDistribute::distribute\n"
00605                         "(\n"
00606                         "    const Pstream::commsTypes commsType,\n"
00607                         "    const List<labelPair>& schedule,\n"
00608                         "    const label constructSize,\n"
00609                         "    const labelListList& subMap,\n"
00610                         "    const labelListList& constructMap,\n"
00611                         "    List<T>& field\n"
00612                         ")\n"
00613                     )   << "Expected from processor " << domain
00614                         << " " << map.size() << " but received "
00615                         << recvFields[domain].size() << " elements."
00616                         << abort(FatalError);
00617                 }
00618 
00619                 forAll(map, i)
00620                 {
00621                     cop(field[map[i]], recvFields[domain][i]);
00622                 }
00623             }
00624         }
00625     }
00626     else
00627     {
00628         FatalErrorIn("mapDistribute::distribute(..)")
00629             << "Unknown communication schedule " << commsType
00630             << abort(FatalError);
00631     }
00632 }
00633 
00634 
00635 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd