processorLduInterfaceTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include "processorLduInterface.H"
27 #include "IPstream.H"
28 #include "OPstream.H"
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  const Pstream::commsTypes commsType,
36  const UList<Type>& f
37 ) const
38 {
39  label nBytes = f.byteSize();
40 
41  if
42  (
43  commsType == Pstream::commsTypes::blocking
44  || commsType == Pstream::commsTypes::scheduled
45  )
46  {
48  (
49  commsType,
50  neighbProcNo(),
51  reinterpret_cast<const char*>(f.begin()),
52  nBytes,
53  tag(),
54  comm()
55  );
56  }
57  else if (commsType == Pstream::commsTypes::nonBlocking)
58  {
59  resizeBuf(receiveBuf_, nBytes);
60 
62  (
63  commsType,
64  neighbProcNo(),
65  receiveBuf_.begin(),
66  nBytes,
67  tag(),
68  comm()
69  );
70 
71  resizeBuf(sendBuf_, nBytes);
72  memcpy(sendBuf_.begin(), f.begin(), nBytes);
73 
75  (
76  commsType,
77  neighbProcNo(),
78  sendBuf_.begin(),
79  nBytes,
80  tag(),
81  comm()
82  );
83  }
84  else
85  {
87  << "Unsupported communications type " << int(commsType)
88  << exit(FatalError);
89  }
90 }
91 
92 
93 template<class Type>
95 (
96  const Pstream::commsTypes commsType,
97  UList<Type>& f
98 ) const
99 {
100  if
101  (
102  commsType == Pstream::commsTypes::blocking
103  || commsType == Pstream::commsTypes::scheduled
104  )
105  {
107  (
108  commsType,
109  neighbProcNo(),
110  reinterpret_cast<char*>(f.begin()),
111  f.byteSize(),
112  tag(),
113  comm()
114  );
115  }
116  else if (commsType == Pstream::commsTypes::nonBlocking)
117  {
118  memcpy(f.begin(), receiveBuf_.begin(), f.byteSize());
119  }
120  else
121  {
123  << "Unsupported communications type " << int(commsType)
124  << exit(FatalError);
125  }
126 }
127 
128 
129 template<class Type>
131 (
132  const Pstream::commsTypes commsType,
133  const label size
134 ) const
135 {
136  tmp<Field<Type>> tf(new Field<Type>(size));
137  receive(commsType, tf.ref());
138  return tf;
139 }
140 
141 
142 template<class Type>
144 (
145  const Pstream::commsTypes commsType,
146  const UList<Type>& f
147 ) const
148 {
149  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
150  {
151  static const label nCmpts = sizeof(Type)/sizeof(scalar);
152  label nm1 = (f.size() - 1)*nCmpts;
153  label nlast = sizeof(Type)/sizeof(float);
154  label nFloats = nm1 + nlast;
155  label nBytes = nFloats*sizeof(float);
156 
157  const scalar *sArray = reinterpret_cast<const scalar*>(f.begin());
158  const scalar *slast = &sArray[nm1];
159  resizeBuf(sendBuf_, nBytes);
160  float *fArray = reinterpret_cast<float*>(sendBuf_.begin());
161 
162  for (label i=0; i<nm1; i++)
163  {
164  fArray[i] = sArray[i] - slast[i%nCmpts];
165  }
166 
167  reinterpret_cast<Type&>(fArray[nm1]) = f.last();
168 
169  if
170  (
171  commsType == Pstream::commsTypes::blocking
172  || commsType == Pstream::commsTypes::scheduled
173  )
174  {
176  (
177  commsType,
178  neighbProcNo(),
179  sendBuf_.begin(),
180  nBytes,
181  tag(),
182  comm()
183  );
184  }
185  else if (commsType == Pstream::commsTypes::nonBlocking)
186  {
187  resizeBuf(receiveBuf_, nBytes);
188 
190  (
191  commsType,
192  neighbProcNo(),
193  receiveBuf_.begin(),
194  nBytes,
195  tag(),
196  comm()
197  );
198 
200  (
201  commsType,
202  neighbProcNo(),
203  sendBuf_.begin(),
204  nBytes,
205  tag(),
206  comm()
207  );
208  }
209  else
210  {
212  << "Unsupported communications type " << int(commsType)
213  << exit(FatalError);
214  }
215  }
216  else
217  {
218  this->send(commsType, f);
219  }
220 }
221 
222 template<class Type>
224 (
225  const Pstream::commsTypes commsType,
226  UList<Type>& f
227 ) const
228 {
229  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
230  {
231  static const label nCmpts = sizeof(Type)/sizeof(scalar);
232  label nm1 = (f.size() - 1)*nCmpts;
233  label nlast = sizeof(Type)/sizeof(float);
234  label nFloats = nm1 + nlast;
235  label nBytes = nFloats*sizeof(float);
236 
237  if
238  (
239  commsType == Pstream::commsTypes::blocking
240  || commsType == Pstream::commsTypes::scheduled
241  )
242  {
243  resizeBuf(receiveBuf_, nBytes);
244 
246  (
247  commsType,
248  neighbProcNo(),
249  receiveBuf_.begin(),
250  nBytes,
251  tag(),
252  comm()
253  );
254  }
255  else if (commsType != Pstream::commsTypes::nonBlocking)
256  {
258  << "Unsupported communications type " << int(commsType)
259  << exit(FatalError);
260  }
261 
262  const float *fArray =
263  reinterpret_cast<const float*>(receiveBuf_.begin());
264  f.last() = reinterpret_cast<const Type&>(fArray[nm1]);
265  scalar *sArray = reinterpret_cast<scalar*>(f.begin());
266  const scalar *slast = &sArray[nm1];
267 
268  for (label i=0; i<nm1; i++)
269  {
270  sArray[i] = fArray[i] + slast[i%nCmpts];
271  }
272  }
273  else
274  {
275  this->receive<Type>(commsType, f);
276  }
277 }
278 
279 template<class Type>
281 (
282  const Pstream::commsTypes commsType,
283  const label size
284 ) const
285 {
286  tmp<Field<Type>> tf(new Field<Type>(size));
287  compressedReceive(commsType, tf.ref());
288  return tf;
289 }
290 
291 
292 // ************************************************************************* //
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
commsTypes
Types of communications.
Definition: UPstream.H:64
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
void compressedSend(const Pstream::commsTypes commsType, const UList< Type > &) const
Raw field send function with data compression.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
const tensorField & tf
Pre-declare SubField and related Field type.
Definition: Field.H:56
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
void receive(const Pstream::commsTypes commsType, UList< Type > &) const
Raw field receive function.
void send(const Pstream::commsTypes commsType, const UList< Type > &) const
Raw send function.
labelList f(nPoints)
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
A class for managing temporary objects.
Definition: PtrList.H:53
T & last()
Return the last element of the list.
Definition: UListI.H:128
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
void compressedReceive(const Pstream::commsTypes commsType, UList< Type > &) const
Raw field receive function with data compression.