BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / IOstreams / Pstreams / UPstream.H
blob6dbb5bddde74759e7f9e40d4705a188c19c0a145
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 Class
25     Foam::UPstream
27 Description
28     Inter-processor communications stream
30 SourceFiles
31     UPstream.C
32     UPstreamsPrint.C
33     UPstreamCommsStruct.C
34     gatherScatter.C
35     combineGatherScatter.C
36     gatherScatterList.C
38 \*---------------------------------------------------------------------------*/
40 #ifndef UPstream_H
41 #define UPstream_H
43 #include "labelList.H"
44 #include "DynamicList.H"
45 #include "HashTable.H"
46 #include "string.H"
47 #include "NamedEnum.H"
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 namespace Foam
54 /*---------------------------------------------------------------------------*\
55                           Class UPstream Declaration
56 \*---------------------------------------------------------------------------*/
58 class UPstream
61 public:
63     //- Types of communications
64     enum commsTypes
65     {
66         blocking,
67         scheduled,
68         nonBlocking
69     };
71     static const NamedEnum<commsTypes, 3> commsTypeNames;
73     // Public classes
75         //- Structure for communicating between processors
76         class commsStruct
77         {
78             // Private data
80                 //- procID of above processor
81                 label above_;
83                 //- procIDs of processors directly below me
84                 labelList below_;
86                 //- procIDs of all processors below (so not just directly below)
87                 labelList allBelow_;
89                 //- procIDs of all processors not below. (inverse set of
90                 //  allBelow_ and minus myProcNo)
91                 labelList allNotBelow_;
94         public:
96             // Constructors
98                 //- Construct null
99                 commsStruct();
101                 //- Construct from components
102                 commsStruct
103                 (
104                     const label,
105                     const labelList&,
106                     const labelList&,
107                     const labelList&
108                 );
110                 //- Construct from components; construct allNotBelow_
111                 commsStruct
112                 (
113                     const label nProcs,
114                     const label myProcID,
115                     const label,
116                     const labelList&,
117                     const labelList&
118                 );
121             // Member Functions
123                 // Access
125                     label above() const
126                     {
127                         return above_;
128                     }
130                     const labelList& below() const
131                     {
132                         return below_;
133                     }
135                     const labelList& allBelow() const
136                     {
137                         return allBelow_;
138                     }
140                     const labelList& allNotBelow() const
141                     {
142                         return allNotBelow_;
143                     }
146             // Member operators
148                 bool operator==(const commsStruct&) const;
150                 bool operator!=(const commsStruct&) const;
153              // Ostream Operator
155                 friend Ostream& operator<<(Ostream&, const commsStruct&);
156         };
159         //- combineReduce operator for lists. Used for counting.
160         class listEq
161         {
163         public:
165             template<class T>
166             void operator()(T& x, const T& y) const
167             {
168                 forAll(y, i)
169                 {
170                     if (y[i].size())
171                     {
172                         x[i] = y[i];
173                     }
174                 }
175             }
176         };
179 private:
181     // Private data
183         static int myProcNo_;
184         static bool parRun_;
186         static List<int> procIDs_;
187         static int msgType_;
189         static List<commsStruct> linearCommunication_;
190         static List<commsStruct> treeCommunication_;
193     // Private Member Functions
195         //- Set data for parallel running
196         static void setParRun();
198         //- Calculate linear communication schedule
199         static void calcLinearComm(const label nProcs);
201         //- Calculate tree communication schedule
202         static void calcTreeComm(const label nProcs);
204         //- Helper function for tree communication schedule determination
205         //  Collects all processorIDs below a processor
206         static void collectReceives
207         (
208             const label procID,
209             const List<DynamicList<label> >& receives,
210             DynamicList<label>& allReceives
211         );
213         //- Initialize all communication schedules. Callback from
214         //  UPstream::init()
215         static void initCommunicationSchedule();
218 protected:
220     // Protected data
222         //- Communications type of this stream
223         commsTypes commsType_;
225 public:
227     // Declare name of the class and its debug switch
228     ClassName("UPstream");
231     // Static data
233         //- Should compact transfer be used in which floats replace doubles
234         //  reducing the bandwidth requirement at the expense of some loss
235         //  in accuracy
236         static bool floatTransfer;
238         //- Number of processors at which the sum algorithm changes from linear
239         //  to tree
240         static int nProcsSimpleSum;
242         //- Default commsType
243         static commsTypes defaultCommsType;
246     // Constructors
248         //- Construct given optional buffer size
249         UPstream(const commsTypes commsType)
250         :
251             commsType_(commsType)
252         {}
255     // Member functions
257         //- Add the valid option this type of communications library
258         //  adds/requires on the command line
259         static void addValidParOptions(HashTable<string>& validParOptions);
261         //- Initialisation function called from main
262         //  Spawns slave processes and initialises inter-communication
263         static bool init(int& argc, char**& argv);
265         //- Non-blocking comms: wait until all have finished.
266         static void waitRequests();
268         //- Non-blocking comms: has request i finished?
269         static bool finishedRequest(const label i);
271         //- Is this a parallel run?
272         static bool parRun()
273         {
274             return parRun_;
275         }
277         //- Number of processes in parallel run
278         static label nProcs()
279         {
280             return procIDs_.size();
281         }
283         //- Am I the master process
284         static bool master()
285         {
286             return myProcNo_ == 0;
287         }
289         //- Process index of the master
290         static int masterNo()
291         {
292             return 0;
293         }
295         //- Number of this process (starting from masterNo() = 0)
296         static int myProcNo()
297         {
298             return myProcNo_;
299         }
301         //- Process IDs
302         static const List<int>& procIDs()
303         {
304             return procIDs_;
305         }
307         //- Process ID of given process index
308         static int procID(int procNo)
309         {
310             return procIDs_[procNo];
311         }
313         //- Process index of first slave
314         static int firstSlave()
315         {
316             return 1;
317         }
319         //- Process index of last slave
320         static int lastSlave()
321         {
322             return nProcs() - 1;
323         }
325         //- Communication schedule for linear all-to-master (proc 0)
326         static const List<commsStruct>& linearCommunication()
327         {
328             return linearCommunication_;
329         }
331         //- Communication schedule for tree all-to-master (proc 0)
332         static const List<commsStruct>& treeCommunication()
333         {
334             return treeCommunication_;
335         }
337         //- Message tag of standard messages
338         static int& msgType()
339         {
340             return msgType_;
341         }
344             //- Get the communications type of the stream
345             commsTypes commsType() const
346             {
347                 return commsType_;
348             }
350             //- Set the communications type of the stream
351             commsTypes commsType(const commsTypes ct)
352             {
353                 commsTypes oldCommsType = commsType_;
354                 commsType_ = ct;
355                 return oldCommsType;
356             }
359         //- Exit program
360         static void exit(int errnum = 1);
362         //- Abort program
363         static void abort();
369 Ostream& operator<<(Ostream&, const UPstream::commsStruct&);
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 } // End namespace Foam
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378 #endif
380 // ************************************************************************* //