All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2019 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 
122  return move(subRanks);
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
127 
129 (
130  const 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  if (verbose)
146  {
147  // Print a bit of information
148  stringList ioRanks(Pstream::nProcs());
149  if (Pstream::master(comm_))
150  {
151  ioRanks[Pstream::myProcNo()] = hostName()+"."+name(pid());
152  }
153  Pstream::gatherList(ioRanks);
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 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
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:434
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:59
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
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:125
List< label > labelList
A List of labels.
Definition: labelList.H:56
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
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:399
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
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:250
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:316
Namespace for OpenFOAM.