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

Pstream.H

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 Class
00026     Foam::Pstream
00027 
00028 Description
00029     Inter-processor communications stream
00030 
00031 SourceFiles
00032     Pstream.C
00033     PstreamsPrint.C
00034     PstreamCommsStruct.C
00035     gatherScatter.C
00036     combineGatherScatter.C
00037     gatherScatterList.C
00038 
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #ifndef Pstream_H
00042 #define Pstream_H
00043 
00044 #include "labelList.H"
00045 #include "DynamicList.H"
00046 #include "HashTable.H"
00047 #include "string.H"
00048 #include "NamedEnum.H"
00049 
00050 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00051 
00052 namespace Foam
00053 {
00054 
00055 /*---------------------------------------------------------------------------*\
00056                            Class Pstream Declaration
00057 \*---------------------------------------------------------------------------*/
00058 
00059 class Pstream
00060 {
00061 
00062 public:
00063 
00064     //- Types of communications
00065     enum commsTypes
00066     {
00067         blocking,
00068         scheduled,
00069         nonBlocking
00070     };
00071 
00072     static const NamedEnum<commsTypes, 3> commsTypeNames;
00073 
00074     //- Structure for communicating between processors
00075     class commsStruct
00076     {
00077         // Private data
00078 
00079             //- procID of above processor
00080             label above_;
00081 
00082             //- procIDs of processors directly below me
00083             labelList below_;
00084 
00085             //- procIDs of all processors below (so not just directly below)
00086             labelList allBelow_;
00087 
00088             //- procIDs of all processors not below. (inverse set of allBelow_
00089             //  and minus myProcNo)
00090             labelList allNotBelow_;
00091 
00092 
00093     public:
00094 
00095         // Constructors
00096 
00097             //- Construct null
00098             commsStruct();
00099 
00100             //- Construct from components
00101             commsStruct
00102             (
00103                 const label,
00104                 const labelList&,
00105                 const labelList&,
00106                 const labelList&
00107             );
00108 
00109             //- Construct from components; construct allNotBelow_
00110             commsStruct
00111             (
00112                 const label nProcs,
00113                 const label myProcID,
00114                 const label,
00115                 const labelList&,
00116                 const labelList&
00117             );
00118 
00119 
00120         // Member Functions
00121 
00122             // Access
00123 
00124                 label above() const
00125                 {
00126                     return above_;
00127                 }
00128 
00129                 const labelList& below() const
00130                 {
00131                     return below_;
00132                 }
00133 
00134                 const labelList& allBelow() const
00135                 {
00136                     return allBelow_;
00137                 }
00138 
00139                 const labelList& allNotBelow() const
00140                 {
00141                     return allNotBelow_;
00142                 }
00143 
00144 
00145         // Member operators
00146 
00147             bool operator==(const commsStruct&) const;
00148 
00149             bool operator!=(const commsStruct&) const;
00150 
00151 
00152          // Ostream Operator
00153 
00154             friend Ostream& operator<<(Ostream&, const commsStruct&);
00155     };
00156 
00157 
00158 private:
00159 
00160     // Private data
00161 
00162         static int myProcNo_;
00163         static bool parRun_;
00164 
00165         static List<int> procIDs_;
00166         static const int msgType_;
00167 
00168         static List<commsStruct> linearCommunication_;
00169         static List<commsStruct> treeCommunication_;
00170 
00171 
00172     // Private member functions
00173 
00174         //- Set data for parallel running
00175         static void setParRun();
00176 
00177         //- Calculate linear communication schedule
00178         static void calcLinearComm(const label nProcs);
00179 
00180         //- Calculate tree communication schedule
00181         static void calcTreeComm(const label nProcs);
00182 
00183         //- Helper function for tree communication schedule determination
00184         //  Collects all processorIDs below a processor
00185         static void collectReceives
00186         (
00187             const label procID,
00188             const List<DynamicList<label> >& receives,
00189             DynamicList<label>& allReceives
00190         );
00191 
00192         //- Initialize all communication schedules. Callback from
00193         //  Pstream::init()
00194         static void initCommunicationSchedule();
00195 
00196 
00197 protected:
00198 
00199     // Protected data
00200 
00201         //- Communications type of this stream
00202         commsTypes commsType_;
00203 
00204         //- Transfer buffer
00205         List<char> buf_;
00206 
00207         //- Current buffer read/write location
00208         int bufPosition_;
00209 
00210 
00211     // Protected member functions
00212 
00213         //- Increase the size of the transfer buffer
00214         inline void enlargeBuffer(size_t count);
00215 
00216 
00217 public:
00218 
00219     // Declare name of the class and its debug switch
00220     ClassName("Pstream");
00221 
00222 
00223     // Static data
00224 
00225         //- Should compact transfer be used in which floats replace doubles
00226         //  reducing the bandwidth requirement at the expense of some loss
00227         //  in accuracy
00228         static bool floatTransfer;
00229 
00230         //- Number of processors at which the sum algorithm changes from linear
00231         //  to tree
00232         static int nProcsSimpleSum;
00233 
00234         //- Default commsType
00235         static commsTypes defaultCommsType;
00236 
00237 
00238     // Constructors
00239 
00240         //- Construct given optional buffer size
00241         Pstream
00242         (
00243             const commsTypes commsType,
00244             const label bufSize = 0
00245         )
00246         :
00247             commsType_(commsType),
00248             bufPosition_(0)
00249         {
00250             if (bufSize)
00251             {
00252                 buf_.setSize(bufSize + 2*sizeof(scalar) + 1);
00253             }
00254         }
00255 
00256 
00257     // Member functions
00258 
00259         //- Add the valid option this type of communications library
00260         //  adds/requires on the command line
00261         static void addValidParOptions(HashTable<string>& validParOptions);
00262 
00263         //- Initialisation function called from main
00264         //  Spawns slave processes and initialises inter-communication
00265         static bool init(int& argc, char**& argv);
00266 
00267         //- Is this a parallel run?
00268         static bool parRun()
00269         {
00270             return parRun_;
00271         }
00272 
00273         //- Number of processes in parallel run
00274         static label nProcs()
00275         {
00276             return procIDs_.size();
00277         }
00278 
00279         //- Am I the master process
00280         static bool master()
00281         {
00282             return myProcNo_ == 0;
00283         }
00284 
00285         //- Process index of the master
00286         static int masterNo()
00287         {
00288             return 0;
00289         }
00290 
00291         //- Number of this process (starting from masterNo() = 0)
00292         static int myProcNo()
00293         {
00294             return myProcNo_;
00295         }
00296 
00297         //- Process IDs
00298         static const List<int>& procIDs()
00299         {
00300             return procIDs_;
00301         }
00302 
00303         //- Process ID of given process index
00304         static int procID(int procNo)
00305         {
00306             return procIDs_[procNo];
00307         }
00308 
00309         //- Process index of first slave
00310         static int firstSlave()
00311         {
00312             return 1;
00313         }
00314 
00315         //- Process index of last slave
00316         static int lastSlave()
00317         {
00318             return nProcs() - 1;
00319         }
00320 
00321         //- Communication schedule for linear all-to-master (proc 0)
00322         static const List<commsStruct>& linearCommunication()
00323         {
00324             return linearCommunication_;
00325         }
00326 
00327         //- Communication schedule for tree all-to-master (proc 0)
00328         static const List<commsStruct>& treeCommunication()
00329         {
00330             return treeCommunication_;
00331         }
00332 
00333         //- Message tag of standard messages
00334         static int msgType()
00335         {
00336             return msgType_;
00337         }
00338 
00339             //- Get the communications type of the stream
00340             commsTypes commsType() const
00341             {
00342                 return commsType_;
00343             }
00344 
00345             //- Set the communications type of the stream
00346             commsTypes commsType(const commsTypes ct)
00347             {
00348                 commsTypes oldCommsType = commsType_;
00349                 commsType_ = ct;
00350                 return oldCommsType;
00351             }
00352 
00353         //- Exit program
00354         static void exit(int errnum = 1);
00355 
00356         //- Abort program
00357         static void abort();
00358 
00359 
00360         // Gather and scatter
00361 
00362             //- Gather data. Apply bop to combine Value
00363             //  from different processors
00364             template <class T, class BinaryOp>
00365             static void gather
00366             (
00367                 const List<commsStruct>& comms,
00368                 T& Value,
00369                 const BinaryOp& bop
00370             );
00371 
00372             //- Like above but switches between linear/tree communication
00373             template <class T, class BinaryOp>
00374             static void gather(T& Value, const BinaryOp& bop);
00375 
00376             //- Scatter data. Distribute without modification. Reverse of gather
00377             template <class T>
00378             static void scatter(const List<commsStruct>& comms, T& Value);
00379 
00380             //- Like above but switches between linear/tree communication
00381             template <class T>
00382             static void scatter(T& Value);
00383 
00384 
00385         // Combine variants. Inplace combine values from processors.
00386         // (Uses construct from Istream instead of <<)
00387 
00388             template <class T, class CombineOp>
00389             static void combineGather
00390             (
00391                 const List<commsStruct>& comms,
00392                 T& Value,
00393                 const CombineOp& cop
00394             );
00395 
00396             //- Like above but switches between linear/tree communication
00397             template <class T, class CombineOp>
00398             static void combineGather(T& Value, const CombineOp& cop);
00399 
00400             //- Scatter data. Reverse of combineGather
00401             template <class T>
00402             static void combineScatter
00403             (
00404                 const List<commsStruct>& comms,
00405                 T& Value
00406             );
00407 
00408             //- Like above but switches between linear/tree communication
00409             template <class T>
00410             static void combineScatter(T& Value);
00411 
00412         // Combine variants working on whole List at a time.
00413 
00414             template <class T, class CombineOp>
00415             static void listCombineGather
00416             (
00417                 const List<commsStruct>& comms,
00418                 List<T>& Value,
00419                 const CombineOp& cop
00420             );
00421 
00422             //- Like above but switches between linear/tree communication
00423             template <class T, class CombineOp>
00424             static void listCombineGather(List<T>& Value, const CombineOp& cop);
00425 
00426             //- Scatter data. Reverse of combineGather
00427             template <class T>
00428             static void listCombineScatter
00429             (
00430                 const List<commsStruct>& comms,
00431                 List<T>& Value
00432             );
00433 
00434             //- Like above but switches between linear/tree communication
00435             template <class T>
00436             static void listCombineScatter(List<T>& Value);
00437 
00438         // Combine variants working on whole map at a time. Container needs to
00439         // have iterators and find() defined.
00440 
00441             template <class Container, class CombineOp>
00442             static void mapCombineGather
00443             (
00444                 const List<commsStruct>& comms,
00445                 Container& Values,
00446                 const CombineOp& cop
00447             );
00448 
00449             //- Like above but switches between linear/tree communication
00450             template <class Container, class CombineOp>
00451             static void mapCombineGather
00452             (
00453                 Container& Values,
00454                 const CombineOp& cop
00455             );
00456 
00457             //- Scatter data. Reverse of combineGather
00458             template <class Container>
00459             static void mapCombineScatter
00460             (
00461                 const List<commsStruct>& comms,
00462                 Container& Values
00463             );
00464 
00465             //- Like above but switches between linear/tree communication
00466             template <class Container>
00467             static void mapCombineScatter(Container& Values);
00468 
00469 
00470 
00471         // Gather/scatter keeping the individual processor data separate.
00472         // Values is a List of size Pstream::nProcs() where
00473         // Values[Pstream::myProcNo()] is the data for the current processor.
00474 
00475             //- Gather data but keep individual values separate
00476             template <class T>
00477             static void gatherList
00478             (
00479                 const List<commsStruct>& comms,
00480                 List<T>& Values
00481             );
00482 
00483             //- Like above but switches between linear/tree communication
00484             template <class T>
00485             static void gatherList(List<T>& Values);
00486 
00487             //- Scatter data. Reverse of gatherList
00488             template <class T>
00489             static void scatterList
00490             (
00491                 const List<commsStruct>& comms,
00492                 List<T>& Values
00493             );
00494 
00495             //- Like above but switches between linear/tree communication
00496             template <class T>
00497             static void scatterList(List<T>& Values);
00498 };
00499 
00500 
00501 inline void Pstream::enlargeBuffer(size_t count)
00502 {
00503     buf_.setSize(max(int(buf_.size() + count), 2*buf_.size()));
00504 }
00505 
00506 
00507 Ostream& operator<<(Ostream&, const Pstream::commsStruct&);
00508 
00509 
00510 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00511 
00512 } // End namespace Foam
00513 
00514 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00515 
00516 #ifdef NoRepository
00517 #   include "gatherScatter.C"
00518 #   include "combineGatherScatter.C"
00519 #   include "gatherScatterList.C"
00520 #endif
00521 
00522 
00523 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00524 
00525 #endif
00526 
00527 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd