initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / router / Gather / Gather.C
blob59529fc7bb6828e4dcacf4c1cfd8ab81c7a7c217
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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(Pstream::scheduled, slave);
68                 fromSlave >> this->operator[](procIndex);
69             }
71             // Send data
72             for
73             (
74                 int slave = Pstream::firstSlave(), procIndex = 1;
75                 slave <= Pstream::lastSlave();
76                 slave++, procIndex++
77             )
78             {
79                 OPstream toSlave(Pstream::scheduled, slave);
81                 if (redistribute)
82                 {
83                     toSlave << *this;
84                 }
85                 else
86                 {
87                     // Dummy send just to balance sends/receives
88                     toSlave << 0;
89                 }
90             }
91         }
92         else
93         {
94             // Slave: send my local data to master
95             {
96                 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
97                 toMaster << localData;
98             }
100             // Receive data from master
101             {
102                 IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
103                 if (redistribute)
104                 {
105                     fromMaster >> *this;
106                 }
107                 else
108                 {
109                     label dummy;
110                     fromMaster >> dummy;
111                 }
112             }
113         }
114     }
115     else
116     {
117         this->operator[](0) = localData;
118     }
122 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
124 } // End namespace Foam
126 // ************************************************************************* //