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-2014 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 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  (
51  "Pstream::exchange(..)"
52  ) << "Continuous data only." << sizeof(T) << Foam::abort(FatalError);
53  }
54 
55  if (sendBufs.size() != UPstream::nProcs(comm))
56  {
58  (
59  "Pstream::exchange(..)"
60  ) << "Size of list:" << sendBufs.size()
61  << " does not equal the number of processors:"
62  << UPstream::nProcs(comm)
64  }
65 
66  sizes.setSize(UPstream::nProcs(comm));
67  labelList& nsTransPs = sizes[UPstream::myProcNo(comm)];
68  nsTransPs.setSize(UPstream::nProcs(comm));
69 
70  forAll(sendBufs, procI)
71  {
72  nsTransPs[procI] = sendBufs[procI].size();
73  }
74 
75  // Send sizes across. Note: blocks.
76  combineReduce(sizes, UPstream::listEq(), tag, comm);
77 
78  if (UPstream::nProcs(comm) > 1)
79  {
80  label startOfRequests = Pstream::nRequests();
81 
82  // Set up receives
83  // ~~~~~~~~~~~~~~~
84 
85  recvBufs.setSize(sendBufs.size());
86  forAll(sizes, procI)
87  {
88  label nRecv = sizes[procI][UPstream::myProcNo(comm)];
89 
90  if (procI != Pstream::myProcNo(comm) && nRecv > 0)
91  {
92  recvBufs[procI].setSize(nRecv);
94  (
95  UPstream::nonBlocking,
96  procI,
97  reinterpret_cast<char*>(recvBufs[procI].begin()),
98  nRecv*sizeof(T),
99  tag,
100  comm
101  );
102  }
103  }
104 
105 
106  // Set up sends
107  // ~~~~~~~~~~~~
108 
109  forAll(sendBufs, procI)
110  {
111  if (procI != Pstream::myProcNo(comm) && sendBufs[procI].size() > 0)
112  {
113  if
114  (
116  (
117  UPstream::nonBlocking,
118  procI,
119  reinterpret_cast<const char*>(sendBufs[procI].begin()),
120  sendBufs[procI].size()*sizeof(T),
121  tag,
122  comm
123  )
124  )
125  {
126  FatalErrorIn("Pstream::exchange(..)")
127  << "Cannot send outgoing message. "
128  << "to:" << procI << " nBytes:"
129  << label(sendBufs[procI].size()*sizeof(T))
131  }
132  }
133  }
134 
135 
136  // Wait for all to finish
137  // ~~~~~~~~~~~~~~~~~~~~~~
138 
139  if (block)
140  {
141  Pstream::waitRequests(startOfRequests);
142  }
143  }
144 
145  // Do myself
146  recvBufs[Pstream::myProcNo(comm)] = sendBufs[Pstream::myProcNo(comm)];
147 }
148 
149 
150 // ************************************************************************* //
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
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
const volScalarField & T
Definition: createFields.H:25
runTime write()
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
void setSize(const label)
Reset size of List.
Definition: List.C:318
#define forAll(list, i)
Definition: UList.H:421
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Template function to specify if the data of a type are contiguous.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
bool read(const char *, int32_t &)
Definition: int32IO.C:87
combineReduce operator for lists. Used for counting.
Definition: UPstream.H:160
void combineReduce(const List< UPstream::commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Combination-Reduction operation for a parallel run. The information from all nodes is collected on th...