gatherScatter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25  Gather data from all processors onto single processor according to some
26  communication schedule (usually linear-to-master or tree-to-master).
27  The gathered data will be a single value constructed from the values
28  on individual processors using a user-specified operator.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "UOPstream.H"
33 #include "OPstream.H"
34 #include "UIPstream.H"
35 #include "IPstream.H"
36 #include "contiguous.H"
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
45 template<class T, class BinaryOp>
46 void Pstream::gather
47 (
48  const List<UPstream::commsStruct>& comms,
49  T& Value,
50  const BinaryOp& bop,
51  const int tag,
52  const label comm
53 )
54 {
55  if (UPstream::parRun() && UPstream::nProcs(comm) > 1)
56  {
57  // Get my communication order
58  const commsStruct& myComm = comms[UPstream::myProcNo(comm)];
59 
60  // Receive from my downstairs neighbours
61  forAll(myComm.below(), belowI)
62  {
63  T value;
64 
65  if (contiguous<T>())
66  {
68  (
70  myComm.below()[belowI],
71  reinterpret_cast<char*>(&value),
72  sizeof(T),
73  tag,
74  comm
75  );
76  }
77  else
78  {
79  IPstream fromBelow
80  (
82  myComm.below()[belowI],
83  0,
84  tag,
85  comm
86  );
87  fromBelow >> value;
88  }
89 
90  Value = bop(Value, value);
91  }
92 
93  // Send up Value
94  if (myComm.above() != -1)
95  {
96  if (contiguous<T>())
97  {
99  (
101  myComm.above(),
102  reinterpret_cast<const char*>(&Value),
103  sizeof(T),
104  tag,
105  comm
106  );
107  }
108  else
109  {
110  OPstream toAbove
111  (
113  myComm.above(),
114  0,
115  tag,
116  comm
117  );
118  toAbove << Value;
119  }
120  }
121  }
122 }
123 
124 
125 template <class T, class BinaryOp>
126 void Pstream::gather
127 (
128  T& Value,
129  const BinaryOp& bop,
130  const int tag,
131  const label comm
132 )
133 {
135  {
136  gather(UPstream::linearCommunication(comm), Value, bop, tag, comm);
137  }
138  else
139  {
140  gather(UPstream::treeCommunication(comm), Value, bop, tag, comm);
141  }
142 }
143 
144 
145 template<class T>
146 void Pstream::scatter
147 (
148  const List<UPstream::commsStruct>& comms,
149  T& Value,
150  const int tag,
151  const label comm
152 )
153 {
154  if (UPstream::parRun() && UPstream::nProcs(comm) > 1)
155  {
156  // Get my communication order
157  const commsStruct& myComm = comms[UPstream::myProcNo(comm)];
158 
159  // Reveive from up
160  if (myComm.above() != -1)
161  {
162  if (contiguous<T>())
163  {
165  (
167  myComm.above(),
168  reinterpret_cast<char*>(&Value),
169  sizeof(T),
170  tag,
171  comm
172  );
173  }
174  else
175  {
176  IPstream fromAbove
177  (
179  myComm.above(),
180  0,
181  tag,
182  comm
183  );
184  fromAbove >> Value;
185  }
186  }
187 
188  // Send to my downstairs neighbours
189  forAll(myComm.below(), belowI)
190  {
191  if (contiguous<T>())
192  {
194  (
196  myComm.below()[belowI],
197  reinterpret_cast<const char*>(&Value),
198  sizeof(T),
199  tag,
200  comm
201  );
202  }
203  else
204  {
205  OPstream toBelow
206  (
208  myComm.below()[belowI],
209  0,
210  tag,
211  comm
212  );
213  toBelow << Value;
214  }
215  }
216  }
217 }
218 
219 
220 template <class T>
221 void Pstream::scatter(T& Value, const int tag, const label comm)
222 {
224  {
225  scatter(UPstream::linearCommunication(comm), Value, tag, comm);
226  }
227  else
228  {
229  scatter(UPstream::treeCommunication(comm), Value, tag, comm);
230  }
231 }
232 
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 } // End namespace Foam
237 
238 // ************************************************************************* //
Foam::UPstream::scheduled
@ scheduled
Definition: UPstream.H:67
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:50
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
OPstream.H
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:147
Foam::Pstream::gather
static void gather(const List< commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Gather data. Apply bop to combine Value.
Definition: gatherScatter.C:47
Foam::UIPstream::read
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Read into given buffer from given processor and return the.
Definition: UIPread.C:79
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Write given buffer to given processor.
Definition: UOPwrite.C:34
IPstream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UPstream::commsStruct::above
label above() const
Definition: UPstream.H:125
Foam::UPstream::commsStruct
Structure for communicating between processors.
Definition: UPstream.H:76
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::UPstream::nProcsSimpleSum
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:249
UOPstream.H
contiguous.H
Template function to specify if the data of a type are contiguous.
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:50
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:435
UIPstream.H
Foam::UPstream::commsStruct::below
const labelList & below() const
Definition: UPstream.H:130
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:444