FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 "OPstream.H"
33 #include "IPstream.H"
34 #include <OpenFOAM/contiguous.H>
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 
41 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
42 
43 template <class T, class BinaryOp>
44 void Pstream::gather
45 (
46  const List<Pstream::commsStruct>& comms,
47  T& Value,
48  const BinaryOp& bop
49 )
50 {
51  if (Pstream::parRun())
52  {
53  // Get my communication order
54  const commsStruct& myComm = comms[Pstream::myProcNo()];
55 
56  // Receive from my downstairs neighbours
57  forAll(myComm.below(), belowI)
58  {
59  T value;
60 
61  if (contiguous<T>())
62  {
64  (
66  myComm.below()[belowI],
67  reinterpret_cast<char*>(&value),
68  sizeof(T)
69  );
70  }
71  else
72  {
73  IPstream fromBelow(Pstream::scheduled, myComm.below()[belowI]);
74  fromBelow >> value;
75  }
76 
77  Value = bop(Value, value);
78  }
79 
80  // Send up Value
81  if (myComm.above() != -1)
82  {
83  if (contiguous<T>())
84  {
86  (
88  myComm.above(),
89  reinterpret_cast<const char*>(&Value),
90  sizeof(T)
91  );
92  }
93  else
94  {
95  OPstream toAbove(Pstream::scheduled, myComm.above());
96  toAbove << Value;
97  }
98  }
99  }
100 }
101 
102 
103 template <class T, class BinaryOp>
104 void Pstream::gather(T& Value, const BinaryOp& bop)
105 {
107  {
108  gather(Pstream::linearCommunication(), Value, bop);
109  }
110  else
111  {
112  gather(Pstream::treeCommunication(), Value, bop);
113  }
114 }
115 
116 
117 template <class T>
119 {
120  if (Pstream::parRun())
121  {
122  // Get my communication order
123  const commsStruct& myComm = comms[Pstream::myProcNo()];
124 
125  // Reveive from up
126  if (myComm.above() != -1)
127  {
128  if (contiguous<T>())
129  {
131  (
133  myComm.above(),
134  reinterpret_cast<char*>(&Value),
135  sizeof(T)
136  );
137  }
138  else
139  {
140  IPstream fromAbove(Pstream::scheduled, myComm.above());
141  fromAbove >> Value;
142  }
143  }
144 
145  // Send to my downstairs neighbours
146  forAll(myComm.below(), belowI)
147  {
148  if (contiguous<T>())
149  {
151  (
153  myComm.below()[belowI],
154  reinterpret_cast<const char*>(&Value),
155  sizeof(T)
156  );
157  }
158  else
159  {
160  OPstream toBelow(Pstream::scheduled,myComm.below()[belowI]);
161  toBelow << Value;
162  }
163  }
164  }
165 }
166 
167 
168 template <class T>
169 void Pstream::scatter(T& Value)
170 {
172  {
174  }
175  else
176  {
178  }
179 }
180 
181 
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
183 
184 } // End namespace Foam
185 
186 // ************************ vim: set sw=4 sts=4 et: ************************ //