ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / applications / test / parallel / Test-parallel.C
blob2292e031f4c7c6256a965997ac8ba32479e32b4f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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/>.
24 Application
25     parallelTest
27 Description
28     Test for various parallel routines.
30 \*---------------------------------------------------------------------------*/
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"
43 using namespace Foam;
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 int main(int argc, char *argv[])
50 #   include "setRootCase.H"
51 #   include "createTime.H"
54     // Test mapDistribute
55     // ~~~~~~~~~~~~~~~~~~
57     if (false)
58     {
59         Random rndGen(43544*Pstream::myProcNo());
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         }
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)
78         {
79             label procI = complexData[i].first();
80             nSend[procI]++;
81         }
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)
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         }
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         }
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         }
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;
135     }
138 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140     Perr<< "\nStarting transfers\n" << endl;
142     vector data(0, 1, 2);
144     if (Pstream::parRun())
145     {
146         if (Pstream::myProcNo() != Pstream::masterNo())
147         {
148             {
149                 Perr<< "slave sending to master "
150                     << Pstream::masterNo() << endl;
151                 OPstream toMaster(Pstream::blocking, Pstream::masterNo());
152                 toMaster << data;
153             }
155             Perr<< "slave receiving from master "
156                 << Pstream::masterNo() << endl;
157             IPstream fromMaster(Pstream::blocking, Pstream::masterNo());
158             fromMaster >> data;
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;
175                 Perr<< data << endl;
176             }
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     }
192     Info<< "End\n" << endl;
194     return 0;
198 // ************************************************************************* //