1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
28 Test for various parallel routines.
30 \*---------------------------------------------------------------------------*/
33 #include "mapDistribute.H"
39 #include "IOstreams.H"
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 int main(int argc, char *argv[])
50 # include "setRootCase.H"
51 # include "createTime.H"
59 Random rndGen(43544*Pstream::myProcNo());
61 // Generate random data.
62 List<Tuple2<label, List<scalar> > > complexData(100);
63 forAll(complexData, i)
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;
72 // Send all ones to processor indicated by .first()
75 // Count how many to send
76 labelList nSend(Pstream::nProcs(), 0);
77 forAll(complexData, i)
79 label procI = complexData[i].first();
83 // Sync how many to send
84 labelListList allNTrans(Pstream::nProcs());
85 allNTrans[Pstream::myProcNo()] = nSend;
86 combineReduce(allNTrans, UPstream::listEq());
88 // Collect items to be sent
89 labelListList sendMap(Pstream::nProcs());
90 forAll(sendMap, procI)
92 sendMap[procI].setSize(nSend[procI]);
95 forAll(complexData, i)
97 label procI = complexData[i].first();
98 sendMap[procI][nSend[procI]++] = i;
101 // Collect items to be received
102 labelListList recvMap(Pstream::nProcs());
103 forAll(recvMap, procI)
105 recvMap[procI].setSize(allNTrans[procI][Pstream::myProcNo()]);
108 label constructSize = 0;
109 // Construct with my own elements first
110 forAll(recvMap[Pstream::myProcNo()], i)
112 recvMap[Pstream::myProcNo()][i] = constructSize++;
114 // Construct from other processors
115 forAll(recvMap, procI)
117 if (procI != Pstream::myProcNo())
119 forAll(recvMap[procI], i)
121 recvMap[procI][i] = constructSize++;
128 // Construct distribute map (destructively)
129 mapDistribute map(constructSize, sendMap.xfer(), recvMap.xfer());
131 // Distribute complexData
132 map.distribute(complexData);
134 Pout<< "complexData:" << complexData << endl;
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 Perr<< "\nStarting transfers\n" << endl;
142 vector data(0, 1, 2);
144 if (Pstream::parRun())
146 if (Pstream::myProcNo() != Pstream::masterNo())
149 Perr<< "slave sending to master "
150 << Pstream::masterNo() << endl;
151 OPstream toMaster(Pstream::blocking, Pstream::masterNo());
155 Perr<< "slave receiving from master "
156 << Pstream::masterNo() << endl;
157 IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
166 int slave=Pstream::firstSlave();
167 slave<=Pstream::lastSlave();
171 Perr << "master receiving from slave " << slave << endl;
172 IPstream fromSlave(Pstream::blocking, slave);
180 int slave=Pstream::firstSlave();
181 slave<=Pstream::lastSlave();
185 Perr << "master sending to slave " << slave << endl;
186 OPstream toSlave(Pstream::blocking, slave);
192 Info<< "End\n" << endl;
198 // ************************************************************************* //