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-2016 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. Note reverse order (compared to
189  // receiving). This is to make sure to send to the critical path
190  // (only when using a tree schedule!) first.
191  forAllReverse(myComm.below(), belowI)
192  {
193  if (contiguous<T>())
194  {
196  (
198  myComm.below()[belowI],
199  reinterpret_cast<const char*>(&Value),
200  sizeof(T),
201  tag,
202  comm
203  );
204  }
205  else
206  {
207  OPstream toBelow
208  (
210  myComm.below()[belowI],
211  0,
212  tag,
213  comm
214  );
215  toBelow << Value;
216  }
217  }
218  }
219 }
220 
221 
222 template<class T>
223 void Pstream::scatter(T& Value, const int tag, const label comm)
224 {
226  {
227  scatter(UPstream::linearCommunication(comm), Value, tag, comm);
228  }
229  else
230  {
231  scatter(UPstream::treeCommunication(comm), Value, tag, comm);
232  }
233 }
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 } // End namespace Foam
239 
240 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
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
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:417
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:265
Template function to specify if the data of a type are contiguous.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:440
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:447
Input inter-processor communications stream.
Definition: IPstream.H:50
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:456
Structure for communicating between processors.
Definition: UPstream.H:76
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
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
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
Output inter-processor communications stream.
Definition: OPstream.H:50
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:393
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:399
const labelList & below() const
Definition: UPstream.H:130
label above() const
Definition: UPstream.H:125
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
Namespace for OpenFOAM.