sicortex
[OpenFOAM-1.5.x.git] / applications / test / router / Gather / Gather.C
blob968f45b9361a9cb0d449e93217d2aff0a6468e7b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "Gather.H"
30 #include "IPstream.H"
31 #include "OPstream.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 namespace Foam
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 // Construct from component
41 template <class T0>
42 Gather<T0>::Gather(const T0& localData, const bool redistribute)
44     List<T0>(0),
45     nProcs_(max(1, Pstream::nProcs()))
47     this->setSize(nProcs_);
49     //
50     // Collect sizes on all processor
51     //
53     if (Pstream::parRun())
54     {
55         if (Pstream::master())
56         {
57             this->operator[](0) = localData;
59             // Receive data
60             for
61             (
62                 int slave=Pstream::firstSlave(), procIndex = 1;
63                 slave<=Pstream::lastSlave();
64                 slave++, procIndex++
65             )
66             {
67                 IPstream fromSlave(slave);
68                 fromSlave >> this->operator[](procIndex);
69             }
70             // Send data
71             for
72             (
73                 int slave=Pstream::firstSlave(), procIndex = 1;
74                 slave<=Pstream::lastSlave();
75                 slave++, procIndex++
76             )
77             {
78                 OPstream toSlave(slave);
80                 if (redistribute)
81                 {
82                     toSlave << *this;
83                 }
84                 else
85                 {
86                     // Dummy send just to balance sends/receives
87                     toSlave << 0;
88                 }
89             }
90         }
91         else
92         {
93             // Slave: send my local data to master
94             {
95                 OPstream toMaster(Pstream::masterNo());
96                 toMaster << localData;
97             }
98             // Receive data from master
99             {
100                 IPstream fromMaster(Pstream::masterNo());
101                 if (redistribute)
102                 {
103                     fromMaster >> *this;
104                 }
105                 else
106                 {
107                     label dummy;
108                     fromMaster >> dummy;
109                 }
110             }
111         }
112     }
113     else
114     {
115         this->operator[](0) = localData;
116     }
120 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 } // End namespace Foam
124 // ************************************************************************* //