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-2023 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"
29 
30 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
31 
32 namespace Foam
33 {
34 namespace fileOperations
35 {
38  (
41  word
42  );
43 
44  // Register initialisation routine. Signals need for threaded mpi and
45  // handles command line arguments
47  (
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 
122  return subRanks;
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
127 
129 (
130  const bool verbose
131 )
132 :
134  (
135  UPstream::allocateCommunicator
136  (
137  UPstream::worldComm,
138  subRanks(Pstream::nProcs())
139  ),
140  (Pstream::parRun() ? labelList(0) : ioRanks()), // processor dirs
141  typeName,
142  verbose
143  )
144 {
145  if (verbose)
146  {
147  // Print a bit of information
149  if (Pstream::master(comm_))
150  {
151  ioRanks[Pstream::myProcNo()] = hostName()+"."+name(pid());
152  }
154 
155  Info<< " IO nodes:" << endl;
156  forAll(ioRanks, proci)
157  {
158  if (!ioRanks[proci].empty())
159  {
160  Info<< " " << ioRanks[proci] << endl;
161  }
162  }
163  }
164 }
165 
166 
167 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
168 
170 {
171  if (comm_ != -1)
172  {
174  }
175 }
176 
177 
178 // ************************************************************************* //
label n
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
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:314
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
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
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