Test-parallel-nonBlocking.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) 2012-2015 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 Application
25  Test-parallel-nonBlocking
26 
27 Description
28  Test for various non-blocking parallel routines.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "List.H"
33 #include "mapDistribute.H"
34 #include "argList.H"
35 #include "Time.H"
36 #include "IPstream.H"
37 #include "OPstream.H"
38 #include "vector.H"
39 #include "IOstreams.H"
40 #include "Random.H"
41 #include "Tuple2.H"
42 #include "PstreamBuffers.H"
43 
44 using namespace Foam;
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 int main(int argc, char *argv[])
49 {
50 
51  #include "setRootCase.H"
52  #include "createTime.H"
53 
54 
55  // Test PstreamBuffers
56  // ~~~~~~~~~~~~~~~~~~~
57  if (false)
58  {
59  Perr<< "\nStarting transfers\n" << endl;
60 
61  vector data
62  (
66  );
67 
69 
71  {
72  Perr<< "slave sending to master "
73  << Pstream::masterNo() << endl;
74  UOPstream toMaster(Pstream::masterNo(), pBufs);
75  toMaster << data;
76  }
77 
78  // Start sending and receiving and block
79  pBufs.finishedSends();
80 
81  // Consume
82  DynamicList<vector> allData;
84  {
85  // Collect my own data
86  allData.append(data);
87 
88  for
89  (
90  int slave=Pstream::firstSlave();
91  slave<=Pstream::lastSlave();
92  slave++
93  )
94  {
95  Perr << "master receiving from slave " << slave << endl;
96  UIPstream fromSlave(slave, pBufs);
97  allData.append(vector(fromSlave));
98  }
99  }
100 
101 
102  // Send allData back
105  {
106  for
107  (
108  int slave=Pstream::firstSlave();
109  slave<=Pstream::lastSlave();
110  slave++
111  )
112  {
113  Perr << "master sending to slave " << slave << endl;
114  UOPstream toSlave(slave, pBufs2);
115  toSlave << allData;
116  }
117  }
118 
119  // Start sending and receiving and block
120  pBufs2.finishedSends();
121 
122  // Consume
124  {
125  Perr<< "slave receiving from master "
126  << Pstream::masterNo() << endl;
127  UIPstream fromMaster(Pstream::masterNo(), pBufs2);
128  fromMaster >> allData;
129  Perr<< allData << endl;
130  }
131  }
132 
133 
134  // Test non-blocking reductions
135  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136 
137  scalar data1 = 1.0;
138  label request1 = -1;
139  {
140  Foam::reduce(data1, sumOp<scalar>(), Pstream::msgType(), request1);
141  }
142 
143  scalar data2 = 0.1;
144  label request2 = -1;
145  {
146  Foam::reduce(data2, sumOp<scalar>(), Pstream::msgType(), request2);
147  }
148 
149 
150  // Do a non-blocking send inbetween
151  {
153 
154  for (label procI = 0; procI < Pstream::nProcs(); procI++)
155  {
156  UOPstream toProc(procI, pBufs);
157  toProc << Pstream::myProcNo();
158  }
159 
160  // Start sending and receiving and block
161  pBufs.finishedSends();
162 
163  // Consume
164  for (label procI = 0; procI < Pstream::nProcs(); procI++)
165  {
166  UIPstream fromProc(procI, pBufs);
167  label data;
168  fromProc >> data;
169 
170  if (data != procI)
171  {
173  << "From processor " << procI << " received " << data
174  << " but expected " << procI
175  << exit(FatalError);
176  }
177  }
178  }
179 
180 
181  if (request1 != -1)
182  {
183  Pout<< "Waiting for non-blocking reduce with request " << request1
184  << endl;
185  Pstream::waitRequest(request1);
186  }
187  Info<< "Reduced data1:" << data1 << endl;
188 
189  if (request2 != -1)
190  {
191  Pout<< "Waiting for non-blocking reduce with request " << request1
192  << endl;
193  Pstream::waitRequest(request2);
194  }
195  Info<< "Reduced data2:" << data2 << endl;
196 
197 
198  // Clear any outstanding requests
200 
201  Info<< "End\n" << endl;
202 
203  return 0;
204 }
205 
206 
207 // ************************************************************************* //
Foam::UPstream::resetRequests
static void resetRequests(const label sz)
Truncate number of outstanding requests.
Definition: UPstream.C:102
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
List.H
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
Tuple2.H
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
main
int main(int argc, char *argv[])
Definition: Test-parallel-nonBlocking.C:45
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:85
Foam::Perr
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
OPstream.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:43
IPstream.H
Foam::UPstream::nonBlocking
@ nonBlocking
Definition: UPstream.H:68
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::UPstream::lastSlave
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:428
Foam::Info
messageStream Info
argList.H
Foam::UPstream::waitRequest
static void waitRequest(const label i)
Wait until request i has finished.
Definition: UPstream.C:110
Foam::UPstream::masterNo
static int masterNo()
Process index of the master.
Definition: UPstream.H:393
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
Definition: PstreamBuffers.C:82
Foam::FatalError
error FatalError
Foam::UPstream::firstSlave
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:422
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Random.H
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
setRootCase.H
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::sumOp
Definition: ops.H:162
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
mapDistribute.H
Foam::Vector< scalar >
PstreamBuffers.H
createTime.H
vector.H
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52