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 | 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 \*---------------------------------------------------------------------------*/
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 (commsType == Pstream::blocking || commsType == Pstream::scheduled)
42  {
44  (
45  commsType,
46  neighbProcNo(),
47  reinterpret_cast<const char*>(f.begin()),
48  nBytes,
49  tag(),
50  comm()
51  );
52  }
53  else if (commsType == Pstream::nonBlocking)
54  {
55  resizeBuf(receiveBuf_, nBytes);
56 
58  (
59  commsType,
60  neighbProcNo(),
61  receiveBuf_.begin(),
62  nBytes,
63  tag(),
64  comm()
65  );
66 
67  resizeBuf(sendBuf_, nBytes);
68  memcpy(sendBuf_.begin(), f.begin(), nBytes);
69 
71  (
72  commsType,
73  neighbProcNo(),
74  sendBuf_.begin(),
75  nBytes,
76  tag(),
77  comm()
78  );
79  }
80  else
81  {
83  << "Unsupported communications type " << commsType
84  << exit(FatalError);
85  }
86 }
87 
88 
89 template<class Type>
91 (
92  const Pstream::commsTypes commsType,
93  UList<Type>& f
94 ) const
95 {
96  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
97  {
99  (
100  commsType,
101  neighbProcNo(),
102  reinterpret_cast<char*>(f.begin()),
103  f.byteSize(),
104  tag(),
105  comm()
106  );
107  }
108  else if (commsType == Pstream::nonBlocking)
109  {
110  memcpy(f.begin(), receiveBuf_.begin(), f.byteSize());
111  }
112  else
113  {
115  << "Unsupported communications type " << commsType
116  << exit(FatalError);
117  }
118 }
119 
120 
121 template<class Type>
123 (
124  const Pstream::commsTypes commsType,
125  const label size
126 ) const
127 {
128  tmp<Field<Type>> tf(new Field<Type>(size));
129  receive(commsType, tf.ref());
130  return tf;
131 }
132 
133 
134 template<class Type>
136 (
137  const Pstream::commsTypes commsType,
138  const UList<Type>& f
139 ) const
140 {
141  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
142  {
143  static const label nCmpts = sizeof(Type)/sizeof(scalar);
144  label nm1 = (f.size() - 1)*nCmpts;
145  label nlast = sizeof(Type)/sizeof(float);
146  label nFloats = nm1 + nlast;
147  label nBytes = nFloats*sizeof(float);
148 
149  const scalar *sArray = reinterpret_cast<const scalar*>(f.begin());
150  const scalar *slast = &sArray[nm1];
151  resizeBuf(sendBuf_, nBytes);
152  float *fArray = reinterpret_cast<float*>(sendBuf_.begin());
153 
154  for (label i=0; i<nm1; i++)
155  {
156  fArray[i] = sArray[i] - slast[i%nCmpts];
157  }
158 
159  reinterpret_cast<Type&>(fArray[nm1]) = f.last();
160 
161  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
162  {
164  (
165  commsType,
166  neighbProcNo(),
167  sendBuf_.begin(),
168  nBytes,
169  tag(),
170  comm()
171  );
172  }
173  else if (commsType == Pstream::nonBlocking)
174  {
175  resizeBuf(receiveBuf_, nBytes);
176 
178  (
179  commsType,
180  neighbProcNo(),
181  receiveBuf_.begin(),
182  nBytes,
183  tag(),
184  comm()
185  );
186 
188  (
189  commsType,
190  neighbProcNo(),
191  sendBuf_.begin(),
192  nBytes,
193  tag(),
194  comm()
195  );
196  }
197  else
198  {
200  << "Unsupported communications type " << commsType
201  << exit(FatalError);
202  }
203  }
204  else
205  {
206  this->send(commsType, f);
207  }
208 }
209 
210 template<class Type>
212 (
213  const Pstream::commsTypes commsType,
214  UList<Type>& f
215 ) const
216 {
217  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
218  {
219  static const label nCmpts = sizeof(Type)/sizeof(scalar);
220  label nm1 = (f.size() - 1)*nCmpts;
221  label nlast = sizeof(Type)/sizeof(float);
222  label nFloats = nm1 + nlast;
223  label nBytes = nFloats*sizeof(float);
224 
225  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
226  {
227  resizeBuf(receiveBuf_, nBytes);
228 
230  (
231  commsType,
232  neighbProcNo(),
233  receiveBuf_.begin(),
234  nBytes,
235  tag(),
236  comm()
237  );
238  }
239  else if (commsType != Pstream::nonBlocking)
240  {
242  << "Unsupported communications type " << commsType
243  << exit(FatalError);
244  }
245 
246  const float *fArray =
247  reinterpret_cast<const float*>(receiveBuf_.begin());
248  f.last() = reinterpret_cast<const Type&>(fArray[nm1]);
249  scalar *sArray = reinterpret_cast<scalar*>(f.begin());
250  const scalar *slast = &sArray[nm1];
251 
252  for (label i=0; i<nm1; i++)
253  {
254  sArray[i] = fArray[i] + slast[i%nCmpts];
255  }
256  }
257  else
258  {
259  this->receive<Type>(commsType, f);
260  }
261 }
262 
263 template<class Type>
265 (
266  const Pstream::commsTypes commsType,
267  const label size
268 ) const
269 {
270  tmp<Field<Type>> tf(new Field<Type>(size));
271  compressedReceive(commsType, tf.ref());
272  return tf;
273 }
274 
275 
276 // ************************************************************************* //
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:319
commsTypes
Types of communications.
Definition: UPstream.H:64
void compressedReceive(const Pstream::commsTypes commsType, UList< Type > &) const
Raw field receive function with data compression.
const tensorField & tf
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
bool read(const char *, int32_t &)
Definition: int32IO.C:85
Pre-declare SubField and related Field type.
Definition: Field.H:57
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
void receive(const Pstream::commsTypes commsType, UList< Type > &) const
Raw field receive function.
labelList f(nPoints)
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
void compressedSend(const Pstream::commsTypes commsType, const UList< Type > &) const
Raw field send function with data compression.
void send(const Pstream::commsTypes commsType, const UList< Type > &) const
Raw send function.
A class for managing temporary objects.
Definition: PtrList.H:54
T & last()
Return the last element of the list.
Definition: UListI.H:128
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
runTime write()