processorLduInterfaceTemplates.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "processorLduInterface.H"
30 #include "IPstream.H"
31 #include "OPstream.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
35 template<class Type>
37 (
38  const Pstream::commsTypes commsType,
39  const UList<Type>& f
40 ) const
41 {
42  label nBytes = f.byteSize();
43 
44  if
45  (
46  commsType == Pstream::commsTypes::blocking
47  || commsType == Pstream::commsTypes::scheduled
48  )
49  {
51  (
52  commsType,
53  neighbProcNo(),
54  f.cdata_bytes(),
55  nBytes,
56  tag(),
57  comm()
58  );
59  }
60  else if (commsType == Pstream::commsTypes::nonBlocking)
61  {
62  resizeBuf(receiveBuf_, nBytes);
63 
65  (
66  commsType,
67  neighbProcNo(),
68  receiveBuf_.data(),
69  nBytes,
70  tag(),
71  comm()
72  );
73 
74  resizeBuf(sendBuf_, nBytes);
75  std::memcpy
76  (
77  static_cast<void*>(sendBuf_.data()), f.cdata(), nBytes
78  );
79 
81  (
82  commsType,
83  neighbProcNo(),
84  sendBuf_.cdata(),
85  nBytes,
86  tag(),
87  comm()
88  );
89  }
90  else
91  {
93  << "Unsupported communications type " << int(commsType)
94  << exit(FatalError);
95  }
96 }
97 
98 
99 template<class Type>
101 (
102  const Pstream::commsTypes commsType,
103  UList<Type>& f
104 ) const
105 {
106  if
107  (
108  commsType == Pstream::commsTypes::blocking
109  || commsType == Pstream::commsTypes::scheduled
110  )
111  {
113  (
114  commsType,
115  neighbProcNo(),
116  f.data_bytes(),
117  f.byteSize(),
118  tag(),
119  comm()
120  );
121  }
122  else if (commsType == Pstream::commsTypes::nonBlocking)
123  {
124  std::memcpy
125  (
126  static_cast<void*>(f.data()), receiveBuf_.cdata(), f.byteSize()
127  );
128  }
129  else
130  {
132  << "Unsupported communications type " << int(commsType)
133  << exit(FatalError);
134  }
135 }
136 
137 
138 template<class Type>
140 (
141  const Pstream::commsTypes commsType,
142  const label size
143 ) const
144 {
145  auto tfld = tmp<Field<Type>>::New(size);
146  receive(commsType, tfld.ref());
147  return tfld;
148 }
149 
150 
151 template<class Type>
153 (
154  const Pstream::commsTypes commsType,
155  const UList<Type>& f
156 ) const
157 {
158  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
159  {
160  static const label nCmpts = sizeof(Type)/sizeof(scalar);
161  label nm1 = (f.size() - 1)*nCmpts;
162  label nlast = sizeof(Type)/sizeof(float);
163  label nFloats = nm1 + nlast;
164  label nBytes = nFloats*sizeof(float);
165 
166  const scalar *sArray = reinterpret_cast<const scalar*>(f.cdata());
167  const scalar *slast = &sArray[nm1];
168  resizeBuf(sendBuf_, nBytes);
169  float *fArray = reinterpret_cast<float*>(sendBuf_.data());
170 
171  for (label i=0; i<nm1; i++)
172  {
173  fArray[i] = sArray[i] - slast[i%nCmpts];
174  }
175 
176  reinterpret_cast<Type&>(fArray[nm1]) = f.last();
177 
178  if
179  (
180  commsType == Pstream::commsTypes::blocking
181  || commsType == Pstream::commsTypes::scheduled
182  )
183  {
185  (
186  commsType,
187  neighbProcNo(),
188  sendBuf_.cdata(),
189  nBytes,
190  tag(),
191  comm()
192  );
193  }
194  else if (commsType == Pstream::commsTypes::nonBlocking)
195  {
196  resizeBuf(receiveBuf_, nBytes);
197 
199  (
200  commsType,
201  neighbProcNo(),
202  receiveBuf_.data(),
203  nBytes,
204  tag(),
205  comm()
206  );
207 
209  (
210  commsType,
211  neighbProcNo(),
212  sendBuf_.cdata(),
213  nBytes,
214  tag(),
215  comm()
216  );
217  }
218  else
219  {
221  << "Unsupported communications type " << int(commsType)
222  << exit(FatalError);
223  }
224  }
225  else
226  {
227  this->send(commsType, f);
228  }
229 }
230 
231 
232 template<class Type>
234 (
235  const Pstream::commsTypes commsType,
236  UList<Type>& f
237 ) const
238 {
239  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
240  {
241  static const label nCmpts = sizeof(Type)/sizeof(scalar);
242  label nm1 = (f.size() - 1)*nCmpts;
243  label nlast = sizeof(Type)/sizeof(float);
244  label nFloats = nm1 + nlast;
245  label nBytes = nFloats*sizeof(float);
246 
247  if
248  (
249  commsType == Pstream::commsTypes::blocking
250  || commsType == Pstream::commsTypes::scheduled
251  )
252  {
253  resizeBuf(receiveBuf_, nBytes);
254 
256  (
257  commsType,
258  neighbProcNo(),
259  receiveBuf_.data(),
260  nBytes,
261  tag(),
262  comm()
263  );
264  }
265  else if (commsType != Pstream::commsTypes::nonBlocking)
266  {
268  << "Unsupported communications type " << int(commsType)
269  << exit(FatalError);
270  }
271 
272  const float *fArray =
273  reinterpret_cast<const float*>(receiveBuf_.cdata());
274  f.last() = reinterpret_cast<const Type&>(fArray[nm1]);
275  scalar *sArray = reinterpret_cast<scalar*>(f.data());
276  const scalar *slast = &sArray[nm1];
277 
278  for (label i=0; i<nm1; i++)
279  {
280  sArray[i] = fArray[i] + slast[i%nCmpts];
281  }
282  }
283  else
284  {
285  this->receive<Type>(commsType, f);
286  }
287 }
288 
289 
290 template<class Type>
292 (
293  const Pstream::commsTypes commsType,
294  const label size
295 ) const
296 {
297  auto tfld = tmp<Field<Type>>::New(size);
298  compressedReceive(commsType, tfld.ref());
299  return tfld;
300 }
301 
302 
303 // ************************************************************************* //
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::processorLduInterface::compressedSend
void compressedSend(const Pstream::commsTypes commsType, const UList< Type > &f) const
Raw send function with data compression.
Definition: processorLduInterfaceTemplates.C:153
OPstream.H
Foam::processorLduInterface::compressedReceive
void compressedReceive(const Pstream::commsTypes commsType, UList< Type > &f) const
Raw receive function with data compression.
Definition: processorLduInterfaceTemplates.C:234
Foam::processorLduInterface::receive
void receive(const Pstream::commsTypes commsType, UList< Type > &f) const
Raw receive function.
Definition: processorLduInterfaceTemplates.C:101
processorLduInterface.H
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
IPstream.H
Foam::FatalError
error FatalError
Foam::processorLduInterface::send
void send(const Pstream::commsTypes commsType, const UList< Type > &f) const
Raw send function.
Definition: processorLduInterfaceTemplates.C:37
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
f
labelList f(nPoints)
Foam::UList< Type >
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36