exchange.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-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 Description
25  Exchange data.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "Pstream.H"
30 #include "contiguous.H"
32 #include "UPstream.H"
33 
34 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35 
36 template<class Container, class T>
38 (
39  const List<Container>& sendBufs,
40  List<Container>& recvBufs,
41  labelListList& sizes,
42  const int tag,
43  const label comm,
44  const bool block
45 )
46 {
47  if (!contiguous<T>())
48  {
50  << "Continuous data only." << sizeof(T) << Foam::abort(FatalError);
51  }
52 
53  if (sendBufs.size() != UPstream::nProcs(comm))
54  {
56  << "Size of list:" << sendBufs.size()
57  << " does not equal the number of processors:"
58  << UPstream::nProcs(comm)
60  }
61 
62  sizes.setSize(UPstream::nProcs(comm));
63  labelList& nsTransPs = sizes[UPstream::myProcNo(comm)];
64  nsTransPs.setSize(UPstream::nProcs(comm));
65 
66  forAll(sendBufs, procI)
67  {
68  nsTransPs[procI] = sendBufs[procI].size();
69  }
70 
71  // Send sizes across. Note: blocks.
72  combineReduce(sizes, UPstream::listEq(), tag, comm);
73 
74  recvBufs.setSize(sendBufs.size());
75 
76  if (UPstream::parRun() && UPstream::nProcs(comm) > 1)
77  {
78  label startOfRequests = Pstream::nRequests();
79 
80  // Set up receives
81  // ~~~~~~~~~~~~~~~
82 
83  forAll(sizes, procI)
84  {
85  label nRecv = sizes[procI][UPstream::myProcNo(comm)];
86 
87  if (procI != Pstream::myProcNo(comm) && nRecv > 0)
88  {
89  recvBufs[procI].setSize(nRecv);
91  (
92  UPstream::nonBlocking,
93  procI,
94  reinterpret_cast<char*>(recvBufs[procI].begin()),
95  nRecv*sizeof(T),
96  tag,
97  comm
98  );
99  }
100  }
101 
102 
103  // Set up sends
104  // ~~~~~~~~~~~~
105 
106  forAll(sendBufs, procI)
107  {
108  if (procI != Pstream::myProcNo(comm) && sendBufs[procI].size() > 0)
109  {
110  if
111  (
113  (
114  UPstream::nonBlocking,
115  procI,
116  reinterpret_cast<const char*>(sendBufs[procI].begin()),
117  sendBufs[procI].size()*sizeof(T),
118  tag,
119  comm
120  )
121  )
122  {
124  << "Cannot send outgoing message. "
125  << "to:" << procI << " nBytes:"
126  << label(sendBufs[procI].size()*sizeof(T))
128  }
129  }
130  }
131 
132 
133  // Wait for all to finish
134  // ~~~~~~~~~~~~~~~~~~~~~~
135 
136  if (block)
137  {
138  Pstream::waitRequests(startOfRequests);
139  }
140  }
141 
142  // Do myself
143  recvBufs[Pstream::myProcNo(comm)] = sendBufs[Pstream::myProcNo(comm)];
144 }
145 
146 
147 // ************************************************************************* //
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:63
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
UPstream.H
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::combineReduce
void combineReduce(const List< UPstream::commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Definition: PstreamCombineReduceOps.H:52
PstreamCombineReduceOps.H
Combination-Reduction operation for a parallel run. The information from all nodes is collected on th...
Foam::label
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
Foam::Pstream::exchange
static void exchange(const List< Container > &, List< Container > &, labelListList &sizes, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, const bool block=true)
Exchange data. Sends sendData, receives into recvData, sets.
Definition: exchange.C:38
Foam::FatalError
error FatalError
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::UPstream::listEq
combineReduce operator for lists. Used for counting.
Definition: UPstream.H:160
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
contiguous.H
Template function to specify if the data of a type are contiguous.
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
write
Tcoeff write()