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-2020 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  Also handles cleanup of parallel (or serial) jobs.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef parRun_H
37 #define parRun_H
38 
39 #include "Pstream.H"
40 #include "IOstreams.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 /*---------------------------------------------------------------------------*\
48  Class ParRunControl Declaration
49 \*---------------------------------------------------------------------------*/
50 
51 class ParRunControl
52 {
53  bool parallel_;
54  bool distributed_;
55 
56 public:
57 
58  //- Default construct
60  :
61  parallel_(false),
62  distributed_(false)
63  {}
64 
65  //- Destructor. Shutdown (finalize) MPI as required
67  {
68  if (parallel_)
69  {
70  Info<< "Finalising parallel run" << endl;
71  }
72 
74  }
75 
76 
77  //- Initialize Pstream for a parallel run
78  void runPar(int& argc, char**& argv, bool needsThread)
79  {
80  if (!Pstream::init(argc, argv, needsThread))
81  {
82  Info<< "Failed to start parallel run" << endl;
83  Pstream::exit(1);
84  }
85  parallel_ = true;
86  }
87 
88  //- True if this is parallel run.
89  bool parRun() const
90  {
91  return parallel_;
92  }
93 
94  //- True if this is a parallel run and uses distributed roots.
95  bool distributed() const
96  {
97  return parallel_ && distributed_;
98  }
99 
100  //- Set use of distributed roots.
101  void distributed(bool on)
102  {
103  distributed_ = (parallel_ ? on : false);
104  }
105 };
106 
107 
108 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
109 
110 } // End namespace Foam
111 
112 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
113 
114 #endif
115 
116 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::ParRunControl::distributed
void distributed(bool on)
Set use of distributed roots.
Definition: parRun.H:100
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:65
Foam::ParRunControl::ParRunControl
ParRunControl()
Default construct.
Definition: parRun.H:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ParRunControl::distributed
bool distributed() const
True if this is a parallel run and uses distributed roots.
Definition: parRun.H:94
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. Also handles cleanup of paral...
Definition: parRun.H:50
Foam::ParRunControl::runPar
void runPar(int &argc, char **&argv, bool needsThread)
Initialize Pstream for a parallel run.
Definition: parRun.H:77
Foam::ParRunControl::parRun
bool parRun() const
True if this is parallel run.
Definition: parRun.H:88
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