Test-parallel-communicators.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) 2013-2015 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 Application
25  Test-parallel-communicators
26 
27 Description
28  Checks communication using user-defined communicators
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "Time.H"
34 #include "IPstream.H"
35 #include "OPstream.H"
36 #include "vector.H"
37 #include "IOstreams.H"
38 #include "PstreamReduceOps.H"
39 
40 using namespace Foam;
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 scalar sumReduce
45 (
46  const label comm,
47  const scalar localValue
48 )
49 {
50  scalar sum = 0;
51  if (Pstream::parRun())
52  {
53  if (UPstream::master(comm))
54  {
55  // Add master value and all slaves
56  sum = localValue;
57 
58  for
59  (
60  int slave=Pstream::firstSlave();
61  slave<=Pstream::lastSlave(comm);
62  slave++
63  )
64  {
65  scalar slaveValue;
67  (
69  slave,
70  reinterpret_cast<char*>(&slaveValue),
71  sizeof(scalar),
72  UPstream::msgType(), // tag
73  comm // communicator
74  );
75 
76  sum += slaveValue;
77  }
78 
79  // Send back to slaves
80 
81  for
82  (
83  int slave=UPstream::firstSlave();
84  slave<=UPstream::lastSlave(comm);
85  slave++
86  )
87  {
89  (
91  slave,
92  reinterpret_cast<const char*>(&sum),
93  sizeof(scalar),
94  UPstream::msgType(), // tag
95  comm // communicator
96  );
97  }
98  }
99  else
100  {
101  {
103  (
106  reinterpret_cast<const char*>(&localValue),
107  sizeof(scalar),
108  UPstream::msgType(), // tag
109  comm // communicator
110  );
111  }
112 
113  {
115  (
118  reinterpret_cast<char*>(&sum),
119  sizeof(scalar),
120  UPstream::msgType(), // tag
121  comm // communicator
122  );
123  }
124  }
125  }
126  return sum;
127 }
128 
129 
130 int main(int argc, char *argv[])
131 {
132 
133  #include "setRootCase.H"
134  #include "createTime.H"
135 
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
137 
138  // Allocate a communicator
140 
141  DynamicList<label> bottom;
142  DynamicList<label> top;
143 
144  for (label i = 0; i < n/2; i++)
145  {
146  bottom.append(i);
147  }
148  for (label i = n/2; i < n; i++)
149  {
150  top.append(i);
151  }
152 
153  //Pout<< "bottom:" << bottom << endl;
154  Pout<< "top :" << top << endl;
155 
156 
157  scalar localValue = 111*UPstream::myProcNo(UPstream::worldComm);
158  Pout<< "localValue :" << localValue << endl;
159 
160 
162  (
164  top
165  );
166 
167  Pout<< "allocated comm :" << comm << endl;
168  Pout<< "comm myproc :" << Pstream::myProcNo(comm)
169  << endl;
170 
171 
172  if (Pstream::myProcNo(comm) != -1)
173  {
174  //scalar sum = sumReduce(comm, localValue);
175  //scalar sum = localValue;
176  //reduce
177  //(
178  // UPstream::treeCommunication(comm),
179  // sum,
180  // sumOp<scalar>(),
181  // Pstream::msgType(),
182  // comm
183  //);
184  scalar sum = returnReduce
185  (
186  localValue,
187  sumOp<scalar>(),
189  comm
190  );
191  Pout<< "sum :" << sum << endl;
192  }
193 
195 
196 
197  Pout<< "End\n" << endl;
198 
199  return 0;
200 }
201 
202 
203 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
Foam::DynamicList< label >
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
OPstream.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::UIPstream::read
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Read into given buffer from given processor and return the.
Definition: UIPread.C:79
Foam::UPstream::blocking
@ blocking
Definition: UPstream.H:66
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Write given buffer to given processor.
Definition: UOPwrite.C:34
IPstream.H
Foam::UPstream::allocateCommunicator
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:248
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
argList.H
Foam::UPstream::freeCommunicator
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:314
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
PstreamReduceOps.H
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
setRootCase.H
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
Foam::sumOp
Definition: ops.H:162
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:258
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:333
createTime.H
vector.H
Foam::sumReduce
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:125
main
int main(int argc, char *argv[])
Definition: Test-parallel-communicators.C:127