Test-parallel.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 |
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  parallelTest
26 
27 Description
28  Test for various 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 
43 using namespace Foam;
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 int main(int argc, char *argv[])
48 {
49 
50  #include "setRootCase.H"
51  #include "createTime.H"
52 
53 
54  // Test mapDistribute
55  // ~~~~~~~~~~~~~~~~~~
56 
57  if (false)
58  {
60 
61  // Generate random data.
62  List<Tuple2<label, List<scalar> > > complexData(100);
63  forAll(complexData, i)
64  {
65  complexData[i].first() = rndGen.integer(0, Pstream::nProcs()-1);
66  complexData[i].second().setSize(3);
67  complexData[i].second()[0] = 1;
68  complexData[i].second()[1] = 2;
69  complexData[i].second()[2] = 3;
70  }
71 
72  // Send all ones to processor indicated by .first()
73 
74 
75  // Count how many to send
76  labelList nSend(Pstream::nProcs(), 0);
77  forAll(complexData, i)
78  {
79  label procI = complexData[i].first();
80  nSend[procI]++;
81  }
82 
83  // Sync how many to send
84  labelListList allNTrans(Pstream::nProcs());
85  allNTrans[Pstream::myProcNo()] = nSend;
86  combineReduce(allNTrans, UPstream::listEq());
87 
88  // Collect items to be sent
89  labelListList sendMap(Pstream::nProcs());
90  forAll(sendMap, procI)
91  {
92  sendMap[procI].setSize(nSend[procI]);
93  }
94  nSend = 0;
95  forAll(complexData, i)
96  {
97  label procI = complexData[i].first();
98  sendMap[procI][nSend[procI]++] = i;
99  }
100 
101  // Collect items to be received
102  labelListList recvMap(Pstream::nProcs());
103  forAll(recvMap, procI)
104  {
105  recvMap[procI].setSize(allNTrans[procI][Pstream::myProcNo()]);
106  }
107 
108  label constructSize = 0;
109  // Construct with my own elements first
110  forAll(recvMap[Pstream::myProcNo()], i)
111  {
112  recvMap[Pstream::myProcNo()][i] = constructSize++;
113  }
114  // Construct from other processors
115  forAll(recvMap, procI)
116  {
117  if (procI != Pstream::myProcNo())
118  {
119  forAll(recvMap[procI], i)
120  {
121  recvMap[procI][i] = constructSize++;
122  }
123  }
124  }
125 
126 
127 
128  // Construct distribute map (destructively)
129  mapDistribute map(constructSize, sendMap.xfer(), recvMap.xfer());
130 
131  // Distribute complexData
132  map.distribute(complexData);
133 
134  Pout<< "complexData:" << complexData << endl;
135  }
136 
137 
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
139 
140  Perr<< "\nStarting transfers\n" << endl;
141 
142  vector data(0, 1, 2);
143 
144  if (Pstream::parRun())
145  {
147  {
148  {
149  Perr<< "slave sending to master "
150  << Pstream::masterNo() << endl;
152  toMaster << data;
153  }
154 
155  Perr<< "slave receiving from master "
156  << Pstream::masterNo() << endl;
158  fromMaster >> data;
159 
160  Perr<< data << endl;
161  }
162  else
163  {
164  for
165  (
166  int slave=Pstream::firstSlave();
167  slave<=Pstream::lastSlave();
168  slave++
169  )
170  {
171  Perr << "master receiving from slave " << slave << endl;
172  IPstream fromSlave(Pstream::blocking, slave);
173  fromSlave >> data;
174 
175  Perr<< data << endl;
176  }
177 
178  for
179  (
180  int slave=Pstream::firstSlave();
181  slave<=Pstream::lastSlave();
182  slave++
183  )
184  {
185  Perr << "master sending to slave " << slave << endl;
186  OPstream toSlave(Pstream::blocking, slave);
187  toSlave << data;
188  }
189  }
190  }
191 
192  Info<< "End\n" << endl;
193 
194  return 0;
195 }
196 
197 
198 // ************************************************************************* //
Foam::Random
Simple random number generator.
Definition: Random.H:49
main
int main(int argc, char *argv[])
Definition: Test-parallel.C:44
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
List.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Tuple2.H
Foam::List::xfer
Xfer< List< T > > xfer()
Transfer contents to the Xfer container.
Definition: ListI.H:90
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:50
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::Perr
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
OPstream.H
Foam::combineReduce
void combineReduce(const List< UPstream::commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Definition: PstreamCombineReduceOps.H:52
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::UPstream::blocking
@ blocking
Definition: UPstream.H:66
IPstream.H
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::mapDistribute
Class containing processor-to-processor mapping information.
Definition: mapDistribute.H:152
Foam::UPstream::masterNo
static int masterNo()
Process index of the master.
Definition: UPstream.H:393
Foam::mapDistribute::distribute
void distribute(List< T > &fld, const bool dummyTransform=true, const int tag=UPstream::msgType()) const
Distribute data using default commsType.
Definition: mapDistributeTemplates.C:155
Foam::UPstream::firstSlave
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:422
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Random.H
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::List::setSize
void setSize(const label)
Reset size of List.
setRootCase.H
Foam::UPstream::listEq
combineReduce operator for lists. Used for counting.
Definition: UPstream.H:160
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
mapDistribute.H
Foam::Vector< scalar >
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
createTime.H
vector.H
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:50
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
rndGen
cachedRandom rndGen(label(0), -1)