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 argListRunControl_H
41 #define argListRunControl_H
42 
43 #include "Pstream.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  int dryRun_;
58  int verbose_;
59  bool parallel_;
60  bool distributed_;
61 
62 public:
63 
64  //- Default construct
66  :
67  dryRun_(0),
68  verbose_(0),
69  parallel_(false),
70  distributed_(false)
71  {}
72 
73  //- Destructor. Shutdown (finalize) MPI as required
75  {
76  if (parallel_)
77  {
78  Info<< "Finalising parallel run" << endl;
79  }
81  }
82 
83 
84  // Parallel Control
85 
86  //- Initialize Pstream for a parallel run
87  void runPar(int& argc, char**& argv, bool needsThread)
88  {
89  if (!UPstream::init(argc, argv, needsThread))
90  {
91  Info<< "Failed to start parallel run" << endl;
92  UPstream::exit(1);
93  }
94  parallel_ = true;
95  }
96 
97 
98  //- True if this is a parallel run
99  bool parRun() const noexcept
100  {
101  return parallel_;
102  }
103 
104  //- True if this is a parallel run and uses distributed roots.
105  bool distributed() const noexcept
106  {
107  return (parallel_ && distributed_);
108  }
109 
110  //- Set use of distributed roots, but only if actually parallel
111  void distributed(bool on) noexcept
112  {
113  distributed_ = (parallel_ && on);
114  }
115 
116 
117  // General Control
118 
119  //- Non-zero if set as 'dry-run'
120  int dryRun() const noexcept
121  {
122  return dryRun_;
123  }
124 
125  //- Change 'dry-run', return old value
126  int dryRun(const int level) noexcept
127  {
128  int old(dryRun_);
129  dryRun_ = level;
130  return old;
131  }
132 
133  //- Non-zero if set as 'verbose'
134  int verbose() const noexcept
135  {
136  return verbose_;
137  }
138 
139  //- Change 'verbose', return old value
140  int verbose(const int level) noexcept
141  {
142  int old(verbose_);
143  verbose_ = level;
144  return old;
145  }
146 };
147 
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 } // End namespace Foam
152 
153 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
154 
155 #endif
156 
157 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::ParRunControl::dryRun
int dryRun(const int level) noexcept
Change 'dry-run', return old value.
Definition: parRun.H:125
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::ParRunControl::~ParRunControl
~ParRunControl()
Destructor. Shutdown (finalize) MPI as required.
Definition: parRun.H:73
Foam::ParRunControl::ParRunControl
ParRunControl()
Default construct.
Definition: parRun.H:64
Foam::ParRunControl::parRun
bool parRun() const noexcept
True if this is a parallel run.
Definition: parRun.H:98
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::ParRunControl::verbose
int verbose() const noexcept
Non-zero if set as 'verbose'.
Definition: parRun.H:133
Foam::ParRunControl::distributed
bool distributed() const noexcept
True if this is a parallel run and uses distributed roots.
Definition: parRun.H:104
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ParRunControl::verbose
int verbose(const int level) noexcept
Change 'verbose', return old value.
Definition: parRun.H:139
Foam::ParRunControl::distributed
void distributed(bool on) noexcept
Set use of distributed roots, but only if actually parallel.
Definition: parRun.H:110
Foam::UPstream::shutdown
static void shutdown(int errNo=0)
Shutdown (finalize) MPI as required.
Definition: UPstream.C:59
Foam::ParRunControl
Helper class for initializing parallel jobs from the command arguments, storing 'dry-run' state etc....
Definition: parRun.H:54
Foam::ParRunControl::runPar
void runPar(int &argc, char **&argv, bool needsThread)
Initialize Pstream for a parallel run.
Definition: parRun.H:86
Foam::ParRunControl::dryRun
int dryRun() const noexcept
Non-zero if set as 'dry-run'.
Definition: parRun.H:119
Foam::UPstream::init
static bool init(int &argc, char **&argv, const bool needsThread)
Initialisation function called from main.
Definition: UPstream.C:48
Foam::UPstream::exit
static void exit(int errNo=1)
Shutdown (finalize) MPI as required and exit program with errNo.
Definition: UPstream.C:63