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-------------------------------------------------------------------------------
11License
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
30#include "IPstream.H"
31#include "OPstream.H"
32
33// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34
35template<class Type>
37(
38 const Pstream::commsTypes commsType,
39 const UList<Type>& f
40) const
41{
42 const label nBytes = f.byteSize();
43
44 if
45 (
47 || commsType == Pstream::commsTypes::scheduled
48 )
49 {
51 (
52 commsType,
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,
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,
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
99template<class Type>
101(
102 const Pstream::commsTypes commsType,
104) const
105{
106 if
107 (
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
138template<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
151template<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 (
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
232template<class Type>
234(
235 const Pstream::commsTypes commsType,
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 (
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
290template<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// ************************************************************************* //
virtual bool read()
Re-read model coefficients if they have changed.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:230
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
std::streamsize byteSize() const
Definition: UList.C:199
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
T & last()
Return the last element of the list.
Definition: UListI.H:216
commsTypes
Types of communications.
Definition: UPstream.H:67
@ nonBlocking
"nonBlocking"
static bool floatTransfer
Definition: UPstream.H:275
virtual bool write()
Write the output fields.
virtual label comm() const =0
Return communicator used for parallel communication.
void compressedSend(const Pstream::commsTypes commsType, const UList< Type > &f) const
Raw send function with data compression.
virtual int neighbProcNo() const =0
Return neighbour processor number (rank in communicator)
void receive(const Pstream::commsTypes commsType, UList< Type > &f) const
Raw receive function.
void compressedReceive(const Pstream::commsTypes commsType, UList< Type > &f) const
Raw receive function with data compression.
virtual int tag() const =0
Return message tag used for sending.
void send(const Pstream::commsTypes commsType, const UList< Type > &f) const
Raw send function.
A class for managing temporary objects.
Definition: tmp.H:65
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
labelList f(nPoints)