parRun.H
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) 2011-2018 OpenFOAM Foundation
9  Copyright (C) 2018-2025 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 Class
28  Foam::ParRunControl
29 
30 Description
31  Helper class for initializing parallel jobs from the command arguments,
32  storing 'dry-run' state etc.
33  Also handles cleanup of parallel (or serial) jobs.
34 
35 Note
36  In the meantime the class name may be slightly misleading.
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_argListRunControl_H
41 #define Foam_argListRunControl_H
42 
43 #include "UPstream.H"
44 #include "IOstreams.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class ParRunControl Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class ParRunControl
56 {
57  // Private Data
58 
59  //- The dry-run level
60  int dryRun_;
61 
62  //- The verbosity level
63  int verbose_;
64 
65  //- True if this is (or will be) a parallel run
66  bool parallel_;
67 
68  //- Uses distributed roots
69  bool distributed_;
70 
71  //- MPI threads are desired
72  bool needsThread_;
73 
74 
75 public:
76 
77  // Constructors
78 
79  //- Default construct
80  constexpr ParRunControl() noexcept
81  :
82  dryRun_(0),
83  verbose_(0),
84  parallel_(false),
85  distributed_(false),
86  needsThread_(false)
87  {}
88 
89 
90  //- Destructor. Shutdown (finalize) MPI as required
92  {
93  if (parallel_)
94  {
95  // Report shutdown (stdout or stderr)
97  << "Finalising parallel run" << endl;
98  }
100  }
101 
102 
103  // Member Functions - General Control
104 
105  //- Return the dry-run level (default: 0)
106  int dryRun() const noexcept { return dryRun_; }
107 
108  //- Increase the dry-run level
109  void incrDryRun(int i=1) noexcept { dryRun_ += i; }
110 
111  //- Change dry-run level, returns old value
112  int dryRun(int level) noexcept
113  {
114  int old(dryRun_);
115  dryRun_ = level;
116  return old;
117  }
118 
119  //- Return the verbosity level (default: 0)
120  int verbose() const noexcept { return verbose_; }
121 
122  //- Increase the verbosity level
123  void incrVerbose(int i=1) noexcept { verbose_ += i; }
124 
125  //- Change verbosity level, returns old value
126  int verbose(int level) noexcept
127  {
128  int old(verbose_);
129  verbose_ = level;
130  return old;
131  }
132 
133 
134  // Member Functions - Parallel Control
136  //- True if this is (or will be) a parallel run
137  bool parRun() const noexcept
138  {
139  return parallel_;
140  }
141 
142  //- Set as parallel run on/off, return the previous value.
143  // Use with \b extreme caution if runPar() has already been
144  // called.
145  bool parRun(bool on) noexcept
146  {
147  bool old(parallel_);
148  parallel_ = on;
149  return old;
150  }
151 
152  //- True if a parallel run and uses distributed roots.
153  bool distributed() const noexcept
154  {
155  return (parallel_ && distributed_);
156  }
157 
158  //- Set use of distributed roots, but only if actually parallel
159  void distributed(bool on) noexcept
160  {
161  distributed_ = (parallel_ && on);
162  }
163 
164  //- True if MPI threads are desired (default: false)
165  bool threads() const noexcept
166  {
167  return needsThread_;
168  }
170  //- Set preference for use of MPI threads
171  void threads(bool on) noexcept
172  {
173  needsThread_ = on;
174  }
175 
176  //- Initialize UPstream for a parallel run
177  void runPar(int& argc, char**& argv)
178  {
179  if (!UPstream::init(argc, argv, needsThread_))
180  {
181  // Report failure (stdout or stderr)
183  << "Failed to start parallel run" << endl;
184 
185  UPstream::exit(1);
186  }
187  parallel_ = true;
188  }
189 };
190 
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 } // End namespace Foam
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 #endif
199 
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Helper class for initializing parallel jobs from the command arguments, storing &#39;dry-run&#39; state etc...
Definition: parRun.H:48
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
~ParRunControl()
Destructor. Shutdown (finalize) MPI as required.
Definition: parRun.H:98
int verbose() const noexcept
Return the verbosity level (default: 0)
Definition: parRun.H:135
int infoDetailLevel
Global for selective suppression of Info output.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
constexpr ParRunControl() noexcept
Default construct.
Definition: parRun.H:85
void runPar(int &argc, char **&argv)
Initialize UPstream for a parallel run.
Definition: parRun.H:211
static void shutdown(int errNo=0)
Shutdown (finalize) MPI as required.
Definition: UPstream.C:57
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static void exit(int errNo=1)
Shutdown (finalize) MPI as required and exit program with errNo.
Definition: UPstream.C:61
static bool init(int &argc, char **&argv, const bool needsThread)
Initialisation function called from main.
Definition: UPstream.C:40
OSstream & stream(OSstream *alternative=nullptr, int communicator=-1)
Return OSstream for output operations.
const direction noexcept
Definition: scalarImpl.H:255
bool threads() const noexcept
True if MPI threads are desired (default: false)
Definition: parRun.H:195
void incrDryRun(int i=1) noexcept
Increase the dry-run level.
Definition: parRun.H:120
int dryRun() const noexcept
Return the dry-run level (default: 0)
Definition: parRun.H:115
bool distributed() const noexcept
True if a parallel run and uses distributed roots.
Definition: parRun.H:179
messageStream Info
Information stream (stdout output on master, null elsewhere)
void incrVerbose(int i=1) noexcept
Increase the verbosity level.
Definition: parRun.H:140
bool parRun() const noexcept
True if this is (or will be) a parallel run.
Definition: parRun.H:158
Namespace for OpenFOAM.