ENH: patchCloud: return pTraits<Type>::max for unfound points
[OpenFOAM-1.7.x.git] / applications / test / router / Gather / Gather.C
blob4133c281c9abfc0647cdbbd265caf6da3d0309e6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2010 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
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 Description
26 \*---------------------------------------------------------------------------*/
28 #include "Gather.H"
29 #include "IPstream.H"
30 #include "OPstream.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
37 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
39 // Construct from component
40 template <class T0>
41 Gather<T0>::Gather(const T0& localData, const bool redistribute)
43     List<T0>(0),
44     nProcs_(max(1, Pstream::nProcs()))
46     this->setSize(nProcs_);
48     //
49     // Collect sizes on all processor
50     //
52     if (Pstream::parRun())
53     {
54         if (Pstream::master())
55         {
56             this->operator[](0) = localData;
58             // Receive data
59             for
60             (
61                 int slave = Pstream::firstSlave(), procIndex = 1;
62                 slave <= Pstream::lastSlave();
63                 slave++, procIndex++
64             )
65             {
66                 IPstream fromSlave(Pstream::scheduled, slave);
67                 fromSlave >> this->operator[](procIndex);
68             }
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(Pstream::scheduled, 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::scheduled, Pstream::masterNo());
96                 toMaster << localData;
97             }
99             // Receive data from master
100             {
101                 IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
102                 if (redistribute)
103                 {
104                     fromMaster >> *this;
105                 }
106                 else
107                 {
108                     label dummy;
109                     fromMaster >> dummy;
110                 }
111             }
112         }
113     }
114     else
115     {
116         this->operator[](0) = localData;
117     }
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
123 } // End namespace Foam
125 // ************************************************************************* //