1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
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
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/>.
28 Inter-processor communications stream
35 combineGatherScatter.C
38 \*---------------------------------------------------------------------------*/
43 #include "labelList.H"
44 #include "DynamicList.H"
45 #include "HashTable.H"
47 #include "NamedEnum.H"
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 /*---------------------------------------------------------------------------*\
55 Class UPstream Declaration
56 \*---------------------------------------------------------------------------*/
63 //- Types of communications
71 static const NamedEnum<commsTypes, 3> commsTypeNames;
75 //- Structure for communicating between processors
80 //- procID of above processor
83 //- procIDs of processors directly below me
86 //- procIDs of all processors below (so not just directly below)
89 //- procIDs of all processors not below. (inverse set of
90 // allBelow_ and minus myProcNo)
91 labelList allNotBelow_;
101 //- Construct from components
110 //- Construct from components; construct allNotBelow_
114 const label myProcID,
130 const labelList& below() const
135 const labelList& allBelow() const
140 const labelList& allNotBelow() const
148 bool operator==(const commsStruct&) const;
150 bool operator!=(const commsStruct&) const;
155 friend Ostream& operator<<(Ostream&, const commsStruct&);
159 //- combineReduce operator for lists. Used for counting.
166 void operator()(T& x, const T& y) const
183 static int myProcNo_;
186 static List<int> procIDs_;
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
209 const List<DynamicList<label> >& receives,
210 DynamicList<label>& allReceives
213 //- Initialize all communication schedules. Callback from
215 static void initCommunicationSchedule();
222 //- Communications type of this stream
223 commsTypes commsType_;
227 // Declare name of the class and its debug switch
228 ClassName("UPstream");
233 //- Should compact transfer be used in which floats replace doubles
234 // reducing the bandwidth requirement at the expense of some loss
236 static bool floatTransfer;
238 //- Number of processors at which the sum algorithm changes from linear
240 static int nProcsSimpleSum;
242 //- Default commsType
243 static commsTypes defaultCommsType;
248 //- Construct given optional buffer size
249 UPstream(const commsTypes commsType)
251 commsType_(commsType)
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?
277 //- Number of processes in parallel run
278 static label nProcs()
280 return procIDs_.size();
283 //- Am I the master process
286 return myProcNo_ == 0;
289 //- Process index of the master
290 static int masterNo()
295 //- Number of this process (starting from masterNo() = 0)
296 static int myProcNo()
302 static const List<int>& procIDs()
307 //- Process ID of given process index
308 static int procID(int procNo)
310 return procIDs_[procNo];
313 //- Process index of first slave
314 static int firstSlave()
319 //- Process index of last slave
320 static int lastSlave()
325 //- Communication schedule for linear all-to-master (proc 0)
326 static const List<commsStruct>& linearCommunication()
328 return linearCommunication_;
331 //- Communication schedule for tree all-to-master (proc 0)
332 static const List<commsStruct>& treeCommunication()
334 return treeCommunication_;
337 //- Message tag of standard messages
338 static int& msgType()
344 //- Get the communications type of the stream
345 commsTypes commsType() const
350 //- Set the communications type of the stream
351 commsTypes commsType(const commsTypes ct)
353 commsTypes oldCommsType = commsType_;
360 static void exit(int errnum = 1);
369 Ostream& operator<<(Ostream&, const UPstream::commsStruct&);
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 } // End namespace Foam
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 // ************************************************************************* //