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-2021 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
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  Info<< "Finalising parallel run" << endl;
96  }
98  }
99 
100 
101  // Member Functions - General Control
102 
103  //- Return the dry-run level (default: 0)
104  int dryRun() const noexcept { return dryRun_; }
105 
106  //- Increase the dry-run level
107  void incrDryRun(int level = 1) noexcept { dryRun_ += level; }
108 
109  //- Change dry-run level, returns old value
110  int dryRun(const int level) noexcept
111  {
112  int old(dryRun_);
113  dryRun_ = level;
114  return old;
115  }
116 
117  //- Return the verbosity level (default: 0)
118  int verbose() const noexcept { return verbose_; }
119 
120  //- Increase the verbosity level
121  void incrVerbose(int level = 1) noexcept { verbose_ += level; }
122 
123  //- Change verbosity level, returns old value
124  int verbose(const int level) noexcept
125  {
126  int old(verbose_);
127  verbose_ = level;
128  return old;
129  }
130 
131 
132  // Member Functions - Parallel Control
134  //- True if this is (or will be) a parallel run
135  bool parRun() const noexcept
136  {
137  return parallel_;
138  }
139 
140  //- Set as parallel run on/off, return the previous value.
141  // Use with \b extreme caution if runPar() has already been
142  // called.
143  bool parRun(const bool on) noexcept
144  {
145  bool old(parallel_);
146  parallel_ = on;
147  return old;
148  }
149 
150  //- True if a parallel run and uses distributed roots.
151  bool distributed() const noexcept
152  {
153  return (parallel_ && distributed_);
154  }
155 
156  //- Set use of distributed roots, but only if actually parallel
157  void distributed(bool on) noexcept
158  {
159  distributed_ = (parallel_ && on);
160  }
161 
162  //- True if MPI threads are desired (default: false)
163  bool threads() const noexcept
164  {
165  return needsThread_;
166  }
168  //- Set preference for use of MPI threads
169  void threads(bool on) noexcept
170  {
171  needsThread_ = on;
172  }
173 
174  //- Initialize UPstream for a parallel run
175  void runPar(int& argc, char**& argv)
176  {
177  if (!UPstream::init(argc, argv, needsThread_))
178  {
179  Info<< "Failed to start parallel run" << endl;
180  UPstream::exit(1);
181  }
182  parallel_ = true;
183  }
184 };
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 
189 } // End namespace Foam
190 
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
192 
193 #endif
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Helper class for initializing parallel jobs from the command arguments, storing &#39;dry-run&#39; state etc...
Definition: parRun.H:48
~ParRunControl()
Destructor. Shutdown (finalize) MPI as required.
Definition: parRun.H:98
int verbose() const noexcept
Return the verbosity level (default: 0)
Definition: parRun.H:133
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
void incrVerbose(int level=1) noexcept
Increase the verbosity level.
Definition: parRun.H:138
void runPar(int &argc, char **&argv)
Initialize UPstream for a parallel run.
Definition: parRun.H:209
static void shutdown(int errNo=0)
Shutdown (finalize) MPI as required.
Definition: UPstream.C:51
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:55
static bool init(int &argc, char **&argv, const bool needsThread)
Initialisation function called from main.
Definition: UPstream.C:40
const direction noexcept
Definition: Scalar.H:258
bool threads() const noexcept
True if MPI threads are desired (default: false)
Definition: parRun.H:193
ParRunControl() noexcept
Default construct.
Definition: parRun.H:85
int dryRun() const noexcept
Return the dry-run level (default: 0)
Definition: parRun.H:113
bool distributed() const noexcept
True if a parallel run and uses distributed roots.
Definition: parRun.H:177
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool parRun() const noexcept
True if this is (or will be) a parallel run.
Definition: parRun.H:156
void incrDryRun(int level=1) noexcept
Increase the dry-run level.
Definition: parRun.H:118
Namespace for OpenFOAM.