hostCollatedFileOperation.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 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 \*---------------------------------------------------------------------------*/
25 
28 #include "PackedBoolList.H"
29 
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
31 
32 namespace Foam
33 {
34 namespace fileOperations
35 {
36  defineTypeNameAndDebug(hostCollatedFileOperation, 0);
38  (
39  fileOperation,
40  hostCollatedFileOperation,
41  word
42  );
43 
44  // Register initialisation routine. Signals need for threaded mpi and
45  // handles command line arguments
47  (
48  fileOperationInitialise,
49  hostCollatedFileOperationInitialise,
50  word,
51  hostCollated
52  );
53 }
54 }
55 
56 
57 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
58 
59 Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
60 (
61  const label n
62 )
63 {
64  DynamicList<label> subRanks(64);
65 
66  string ioRanksString(getEnv("FOAM_IORANKS"));
67  if (!ioRanksString.empty())
68  {
69  IStringStream is(ioRanksString);
70  labelList ioRanks(is);
71 
72  if (findIndex(ioRanks, 0) == -1)
73  {
75  << "Rank 0 (master) should be in the IO ranks. Currently "
76  << ioRanks << exit(FatalError);
77  }
78 
79  // The lowest numbered rank is the IO rank
80  PackedBoolList isIOrank(n);
81  isIOrank.set(ioRanks);
82 
83  for (label proci = Pstream::myProcNo(); proci >= 0; --proci)
84  {
85  if (isIOrank[proci])
86  {
87  // Found my master. Collect all processors with same master
88  subRanks.append(proci);
89  for
90  (
91  label rank = proci+1;
92  rank < n && !isIOrank[rank];
93  ++rank
94  )
95  {
96  subRanks.append(rank);
97  }
98  break;
99  }
100  }
101  }
102  else
103  {
104  // Normal operation: one lowest rank per hostname is the writer
105  const string myHostName(hostName());
106 
107  stringList hosts(Pstream::nProcs());
108  hosts[Pstream::myProcNo()] = myHostName;
109  Pstream::gatherList(hosts);
110  Pstream::scatterList(hosts);
111 
112  // Collect procs with same hostname
113  forAll(hosts, proci)
114  {
115  if (hosts[proci] == myHostName)
116  {
117  subRanks.append(proci);
118  }
119  }
120  }
121  return subRanks;
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
126 
128 (
129  const bool verbose
130 )
131 :
133  (
135  (
137  subRanks(Pstream::nProcs())
138  ),
139  (Pstream::parRun() ? labelList(0) : ioRanks()), // processor dirs
140  typeName,
141  verbose
142  )
143 {
144  if (verbose)
145  {
146  // Print a bit of information
147  stringList ioRanks(Pstream::nProcs());
148  if (Pstream::master(comm_))
149  {
150  ioRanks[Pstream::myProcNo()] = hostName()+"."+name(pid());
151  }
152  Pstream::gatherList(ioRanks);
153 
154  Info<< " IO nodes:" << endl;
155  forAll(ioRanks, proci)
156  {
157  if (!ioRanks[proci].empty())
158  {
159  Info<< " " << ioRanks[proci] << endl;
160  }
161  }
162  }
163 }
164 
165 
166 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
167 
169 {
170  if (comm_ != -1)
171  {
173  }
174 }
175 
176 
177 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:96
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:427
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:421
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:278
Macros for easy insertion into run-time selection tables.
addNamedToRunTimeSelectionTable(fileOperationInitialise, collatedFileOperationInitialise, word, collated)
addToRunTimeSelectionTable(fileOperation, collatedFileOperation, word)
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:124
List< label > labelList
A List of labels.
Definition: labelList.H:56
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:72
hostCollatedFileOperation(const bool verbose)
Construct null.
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
List< string > stringList
A List of strings.
Definition: stringList.H:50
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:397
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:409
Version of masterUncollatedFileOperation that collates regIOobjects into a container in the processor...
messageStream Info
defineTypeNameAndDebug(collatedFileOperation, 0)
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:253
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:319
Namespace for OpenFOAM.