FreeFOAM The Cross-Platform CFD Toolkit
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) 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 \*---------------------------------------------------------------------------*/
25 
26 #include "processorLduInterface.H"
27 #include <OpenFOAM/IPstream.H>
28 #include <OpenFOAM/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  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
40  {
41  OPstream::write
42  (
43  commsType,
44  neighbProcNo(),
45  reinterpret_cast<const char*>(f.begin()),
46  f.byteSize()
47  );
48  }
49  else if (commsType == Pstream::nonBlocking)
50  {
51  resizeBuf(receiveBuf_, f.size()*sizeof(Type));
52 
53  IPstream::read
54  (
55  commsType,
56  neighbProcNo(),
57  receiveBuf_.begin(),
58  receiveBuf_.size()
59  );
60 
61  resizeBuf(sendBuf_, f.byteSize());
62  memcpy(sendBuf_.begin(), f.begin(), f.byteSize());
63 
64  OPstream::write
65  (
66  commsType,
67  neighbProcNo(),
68  sendBuf_.begin(),
69  f.byteSize()
70  );
71  }
72  else
73  {
74  FatalErrorIn("processorLduInterface::send")
75  << "Unsupported communications type " << commsType
76  << exit(FatalError);
77  }
78 }
79 
80 
81 template<class Type>
83 (
84  const Pstream::commsTypes commsType,
85  UList<Type>& f
86 ) const
87 {
88  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
89  {
90  IPstream::read
91  (
92  commsType,
93  neighbProcNo(),
94  reinterpret_cast<char*>(f.begin()),
95  f.byteSize()
96  );
97  }
98  else if (commsType == Pstream::nonBlocking)
99  {
100  memcpy(f.begin(), receiveBuf_.begin(), f.byteSize());
101  }
102  else
103  {
104  FatalErrorIn("processorLduInterface::receive")
105  << "Unsupported communications type " << commsType
106  << exit(FatalError);
107  }
108 }
109 
110 
111 template<class Type>
113 (
114  const Pstream::commsTypes commsType,
115  const label size
116 ) const
117 {
118  tmp<Field<Type> > tf(new Field<Type>(size));
119  receive(commsType, tf());
120  return tf;
121 }
122 
123 
124 template<class Type>
126 (
127  const Pstream::commsTypes commsType,
128  const UList<Type>& f
129 ) const
130 {
131  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
132  {
133  static const label nCmpts = sizeof(Type)/sizeof(scalar);
134  label nm1 = (f.size() - 1)*nCmpts;
135  label nlast = sizeof(Type)/sizeof(float);
136  label nFloats = nm1 + nlast;
137  label nBytes = nFloats*sizeof(float);
138 
139  const scalar *sArray = reinterpret_cast<const scalar*>(f.begin());
140  const scalar *slast = &sArray[nm1];
141  resizeBuf(sendBuf_, nBytes);
142  float *fArray = reinterpret_cast<float*>(sendBuf_.begin());
143 
144  for (register label i=0; i<nm1; i++)
145  {
146  fArray[i] = sArray[i] - slast[i%nCmpts];
147  }
148 
149  reinterpret_cast<Type&>(fArray[nm1]) = f[f.size() - 1];
150 
151  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
152  {
153  OPstream::write
154  (
155  commsType,
156  neighbProcNo(),
157  sendBuf_.begin(),
158  nBytes
159  );
160  }
161  else if (commsType == Pstream::nonBlocking)
162  {
163  resizeBuf(receiveBuf_, nBytes);
164 
165  IPstream::read
166  (
167  commsType,
168  neighbProcNo(),
169  receiveBuf_.begin(),
170  receiveBuf_.size()
171  );
172 
173  OPstream::write
174  (
175  commsType,
176  neighbProcNo(),
177  sendBuf_.begin(),
178  nBytes
179  );
180  }
181  else
182  {
183  FatalErrorIn("processorLduInterface::compressedSend")
184  << "Unsupported communications type " << commsType
185  << exit(FatalError);
186  }
187  }
188  else
189  {
190  this->send(commsType, f);
191  }
192 }
193 
194 template<class Type>
196 (
197  const Pstream::commsTypes commsType,
198  UList<Type>& f
199 ) const
200 {
201  if (sizeof(scalar) != sizeof(float) && Pstream::floatTransfer && f.size())
202  {
203  static const label nCmpts = sizeof(Type)/sizeof(scalar);
204  label nm1 = (f.size() - 1)*nCmpts;
205  label nlast = sizeof(Type)/sizeof(float);
206  label nFloats = nm1 + nlast;
207  label nBytes = nFloats*sizeof(float);
208 
209  if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
210  {
211  resizeBuf(receiveBuf_, nBytes);
212 
213  IPstream::read
214  (
215  commsType,
216  neighbProcNo(),
217  receiveBuf_.begin(),
218  nBytes
219  );
220  }
221  else if (commsType != Pstream::nonBlocking)
222  {
223  FatalErrorIn("processorLduInterface::compressedReceive")
224  << "Unsupported communications type " << commsType
225  << exit(FatalError);
226  }
227 
228  const float *fArray =
229  reinterpret_cast<const float*>(receiveBuf_.begin());
230  f[f.size() - 1] = reinterpret_cast<const Type&>(fArray[nm1]);
231  scalar *sArray = reinterpret_cast<scalar*>(f.begin());
232  const scalar *slast = &sArray[nm1];
233 
234  for (register label i=0; i<nm1; i++)
235  {
236  sArray[i] = fArray[i] + slast[i%nCmpts];
237  }
238  }
239  else
240  {
241  this->receive<Type>(commsType, f);
242  }
243 }
244 
245 template<class Type>
247 (
248  const Pstream::commsTypes commsType,
249  const label size
250 ) const
251 {
252  tmp<Field<Type> > tf(new Field<Type>(size));
253  compressedReceive(commsType, tf());
254  return tf;
255 }
256 
257 
258 // ************************ vim: set sw=4 sts=4 et: ************************ //