hostCollatedFileOperation.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017-2018 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
30 #include "bitSet.H"
31 
32 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
33 
34 namespace Foam
35 {
36 namespace fileOperations
37 {
38  defineTypeNameAndDebug(hostCollatedFileOperation, 0);
40  (
41  fileOperation,
42  hostCollatedFileOperation,
43  word
44  );
45 
46  // Register initialisation routine. Signals need for threaded mpi and
47  // handles command line arguments
49  (
51  hostCollatedFileOperationInitialise,
52  word,
53  hostCollated
54  );
55 }
56 }
57 
58 
59 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
60 
61 Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
62 (
63  const label n
64 )
65 {
66  DynamicList<label> subRanks(64);
67 
68  string ioRanksString(getEnv("FOAM_IORANKS"));
69  if (!ioRanksString.empty())
70  {
71  IStringStream is(ioRanksString);
72  labelList ioRanks(is);
73 
74  if (!ioRanks.found(0))
75  {
77  << "Rank 0 (master) should be in the IO ranks. Currently "
78  << ioRanks << exit(FatalError);
79  }
80 
81  // The lowest numbered rank is the IO rank
82  const bitSet isIOrank(n, ioRanks);
83 
84  for (label proci = Pstream::myProcNo(); proci >= 0; --proci)
85  {
86  if (isIOrank[proci])
87  {
88  // Found my master. Collect all processors with same master
89  subRanks.append(proci);
90  for
91  (
92  label rank = proci+1;
93  rank < n && !isIOrank[rank];
94  ++rank
95  )
96  {
97  subRanks.append(rank);
98  }
99  break;
100  }
101  }
102  }
103  else
104  {
105  // Normal operation: one lowest rank per hostname is the writer
106  const string myHostName(hostName());
107 
108  stringList hosts(Pstream::nProcs());
109  hosts[Pstream::myProcNo()] = myHostName;
110  Pstream::gatherList(hosts);
111  Pstream::scatterList(hosts);
112 
113  // Collect procs with same hostname
114  forAll(hosts, proci)
115  {
116  if (hosts[proci] == myHostName)
117  {
118  subRanks.append(proci);
119  }
120  }
121  }
122  return subRanks;
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
127 
129 (
130  bool verbose
131 )
132 :
134  (
136  (
138  subRanks(Pstream::nProcs())
139  ),
140  (Pstream::parRun() ? labelList(0) : ioRanks()), // processor dirs
141  typeName,
142  verbose
143  )
144 {
145  verbose = (verbose && Foam::infoDetailLevel > 0);
146 
147  if (verbose)
148  {
149  // Print a bit of information
150  stringList ioRanks(Pstream::nProcs());
151  if (Pstream::master(comm_))
152  {
153  ioRanks[Pstream::myProcNo()] = hostName()+"."+name(pid());
154  }
155  Pstream::gatherList(ioRanks);
156 
157  Info<< " IO nodes:" << nl;
158  for (const string& ranks : ioRanks)
159  {
160  if (!ranks.empty())
161  {
162  Info<< " " << ranks << nl;
163  }
164  }
165  }
166 }
167 
168 
169 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
170 
172 {
173  if (comm_ != -1 && comm_ != UPstream::worldComm)
174  {
176  }
177 }
178 
179 
180 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
hostCollatedFileOperation(const bool verbose)
Construct null.
Definition: hostCollatedFileOperation.C:129
Foam::fileOperations::collatedFileOperation::ioRanks
static labelList ioRanks()
Definition: collatedFileOperation.C:79
Foam::Pstream::scatterList
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
Definition: gatherScatterList.C:215
Foam::UPstream::parRun
static bool & parRun()
Test if this a parallel run, or allow modify access.
Definition: UPstream.H:434
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
Foam::fileOperations::addNamedToRunTimeSelectionTable
addNamedToRunTimeSelectionTable(fileOperationInitialise, collatedFileOperationInitialise, word, collated)
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::stringList
List< string > stringList
A List of strings.
Definition: stringList.H:58
hostCollatedFileOperation.H
bitSet.H
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::UPstream::allocateCommunicator
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:100
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::fileOperations::hostCollatedFileOperation::~hostCollatedFileOperation
virtual ~hostCollatedFileOperation()
Destructor.
Definition: hostCollatedFileOperation.C:171
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
Foam::fileOperations::addToRunTimeSelectionTable
addToRunTimeSelectionTable(fileOperation, collatedFileOperation, word)
Foam::UPstream::freeCommunicator
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:166
Foam::FatalError
error FatalError
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fileOperations::collatedFileOperation
Version of masterUncollatedFileOperation that collates regIOobjects into a container in the processor...
Definition: collatedFileOperation.H:61
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
fileOperationInitialise
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:464
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:295
Foam::List< label >
Foam::hostName
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:446
Foam::fileOperations::defineTypeNameAndDebug
defineTypeNameAndDebug(collatedFileOperation, 0)