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  Copyright (C) 2021-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
31 #include "bitSet.H"
32 
33 /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
34 
35 namespace Foam
36 {
37 namespace fileOperations
38 {
39  defineTypeNameAndDebug(hostCollatedFileOperation, 0);
41  (
42  fileOperation,
43  hostCollatedFileOperation,
44  word
45  );
46 
47  // Register initialisation routine. Signals need for threaded mpi and
48  // handles command line arguments
50  (
51  fileOperationInitialise,
52  hostCollatedFileOperationInitialise,
53  word,
54  hostCollated
55  );
56 }
57 }
58 
59 
60 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
61 
62 Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
63 (
64  const label n
65 )
66 {
67  DynamicList<label> subRanks(64);
68 
69  labelList mainRanks(fileOperation::ioRanks());
70  if (!mainRanks.empty())
71  {
72  if (!mainRanks.found(0))
73  {
75  << "Rank 0 (master) should be in the IO ranks. Currently "
76  << mainRanks << nl
77  << exit(FatalError);
78  }
79 
80  // The lowest numbered rank is the IO rank
81  const bitSet isIOrank(n, mainRanks);
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::allGatherList(hosts);
110 
111  // Collect procs with same hostname
112  forAll(hosts, proci)
113  {
114  if (hosts[proci] == myHostName)
115  {
116  subRanks.append(proci);
117  }
118  }
119  }
120  return subRanks;
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
125 
126 void Foam::fileOperations::hostCollatedFileOperation::init(bool verbose)
127 {
128  verbose = (verbose && Foam::infoDetailLevel > 0);
129 
130  if (verbose)
131  {
132  this->printBanner(ioRanks_.size());
133  }
134 }
135 
136 
138 (
139  bool verbose
140 )
141 :
142  collatedFileOperation
143  (
144  UPstream::allocateCommunicator
145  (
146  UPstream::worldComm,
147  subRanks(Pstream::nProcs())
148  ),
149  (Pstream::parRun() ? labelList() : ioRanks()), // processor dirs
150  typeName,
151  false // verbose
152  ),
153  managedComm_(comm_)
154 {
155  init(verbose);
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
160 
162 {
163  if (UPstream::isUserComm(managedComm_))
164  {
165  UPstream::freeCommunicator(managedComm_);
166  }
167 }
168 
169 
170 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:491
int infoDetailLevel
Global for selective suppression of Info output.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:688
Macros for easy insertion into run-time selection tables.
addNamedToRunTimeSelectionTable(fileOperationInitialise, collatedFileOperationInitialise, word, collated)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:413
addToRunTimeSelectionTable(fileOperation, collatedFileOperation, word)
static void allGatherList(List< T > &values, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Gather data, but keep individual values separate. Uses linear/tree communication. ...
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator) is 1 for serial run.
Definition: UPstream.H:656
Inter-processor communications stream.
Definition: Pstream.H:56
string hostName()
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:324
static bool isUserComm(label communicator) noexcept
True if communicator appears to be user-allocated.
Definition: UPstream.H:386
hostCollatedFileOperation(const bool verbose)
Default construct.
List< string > stringList
A List of strings.
Definition: stringList.H:54
label n
defineTypeNameAndDebug(collatedFileOperation, 0)
List< label > labelList
A List of labels.
Definition: List.H:62
static labelList ioRanks()
Retrieve list of IO ranks from FOAM_IORANKS env variable.
Inter-processor communications stream.
Definition: UPstream.H:54
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:239
Namespace for OpenFOAM.