Gather.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "Gather.H"
29 #include "IPstream.H"
30 #include "OPstream.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 
39 // Construct from component
40 template<class T0>
41 Gather<T0>::Gather(const T0& localData, const bool redistribute)
42 :
43  List<T0>(0),
44  nProcs_(max(1, Pstream::nProcs()))
45 {
46  this->setSize(nProcs_);
47 
48  //
49  // Collect sizes on all processor
50  //
51 
52  if (Pstream::parRun())
53  {
54  if (Pstream::master())
55  {
56  this->operator[](0) = localData;
57 
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  }
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(Pstream::scheduled, slave);
79 
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  {
96  toMaster << localData;
97  }
98 
99  // Receive data from master
100  {
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  }
118 }
119 
120 
121 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
122 
123 } // End namespace Foam
124 
125 // ************************************************************************* //
Foam::Gather::nProcs_
label nProcs_
Number of processors (1 for sequential)
Definition: Gather.H:59
Foam::UPstream::scheduled
@ scheduled
Definition: UPstream.H:67
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:50
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
OPstream.H
IPstream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::UPstream::lastSlave
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:428
Foam::UPstream::masterNo
static int masterNo()
Process index of the master.
Definition: UPstream.H:393
Foam::UPstream::firstSlave
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:422
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::List< T0 >::setSize
void setSize(const label)
Reset size of List.
Foam::Pstream
Inter-processor communications stream.
Definition: Pstream.H:53
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Gather.H
Foam::Gather::Gather
Gather(const T0 &, const bool redistribute=true)
Storage of type 0.
Definition: Gather.C:41
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:50