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-2025 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 
27 #include "PackedBoolList.H"
28 #include "stringList.H"
30 
31 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
32 
33 namespace Foam
34 {
35 namespace fileOperations
36 {
39  (
42  word
43  );
44 
45  // Register initialisation routine. Signals need for threaded mpi and
46  // handles command line arguments
48  (
51  word,
52  hostCollated
53  );
54 }
55 }
56 
57 
58 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
59 
60 Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
61 (
62  const label n
63 )
64 {
65  DynamicList<label> subRanks(64);
66 
67  string ioRanksString(getEnv("FOAM_IORANKS"));
68  if (!ioRanksString.empty())
69  {
70  IStringStream is(ioRanksString);
71  labelList ioRanks(is);
72 
73  if (findIndex(ioRanks, 0) == -1)
74  {
76  << "Rank 0 (master) should be in the IO ranks. Currently "
77  << ioRanks << exit(FatalError);
78  }
79 
80  // The lowest numbered rank is the IO rank
81  PackedBoolList isIOrank(n);
82  isIOrank.set(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 
123  return subRanks;
124 }
125 
126 
127 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
128 
130 (
131  const bool verbose
132 )
133 :
135  (
136  UPstream::allocateCommunicator
137  (
138  UPstream::worldComm,
139  subRanks(Pstream::nProcs())
140  ),
141  (Pstream::parRun() ? labelList(0) : ioRanks()), // processor dirs
142  typeName,
143  verbose
144  )
145 {
146  if (verbose)
147  {
148  // Print a bit of information
150  if (Pstream::master(comm_))
151  {
152  ioRanks[Pstream::myProcNo()] = hostName()+"."+name(pid());
153  }
155 
156  Info<< " IO nodes:" << endl;
157  forAll(ioRanks, proci)
158  {
159  if (!ioRanks[proci].empty())
160  {
161  Info<< " " << ioRanks[proci] << endl;
162  }
163  }
164  }
165 }
166 
167 
168 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
169 
171 {
172  if (comm_ != -1)
173  {
175  }
176 }
177 
178 
179 // ************************************************************************* //
label n
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
Macros for easy insertion into run-time selection tables.
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
Inter-processor communications stream.
Definition: Pstream.H:56
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Inter-processor communications stream.
Definition: UPstream.H:59
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:311
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
const label comm_
Communicator to use.
Definition: fileOperation.H:96
Version of masterUncollatedFileOperation that collates regIOobjects into a container in the processor...
Version of collatedFileOperation with multiple read/write ranks.
hostCollatedFileOperation(const bool verbose)
Construct null.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
addToRunTimeSelectionTable(fileOperation, collatedFileOperation, word)
defineTypeNameAndDebug(collatedFileOperation, 0)
addNamedToRunTimeSelectionTable(fileOperationInitialise, collatedFileOperationInitialise, word, collated)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: POSIX.C:125
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
label findIndex(const ListType &, typename ListType::const_reference, const label start=0)
Find first occurrence of given element and return index,.
error FatalError
List< string > stringList
A List of strings.
Definition: stringList.H:50