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 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 
33  This class also handles cleanup of parallel or serial jobs in a
34  uniform manner.
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef parRun_H
39 #define parRun_H
40 
41 #include "Pstream.H"
42 #include "IOstreams.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class ParRunControl Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class ParRunControl
54 {
55  bool parallel_;
56  bool distributed_;
57 
58 public:
59 
60  //- Construct null
62  :
63  parallel_(false),
64  distributed_(false)
65  {}
66 
67  //- Destructor, triggers Pstream::exit
69  {
70  if (parallel_)
71  {
72  Info<< "Finalising parallel run" << endl;
73  }
74 
75  // Handles serial and parallel modes.
76  Pstream::exit(0);
77  }
78 
79 
80  //- Initialize Pstream for a parallel run
81  void runPar(int& argc, char**& argv, bool needsThread)
82  {
83  parallel_ = true;
84 
85  if (!Pstream::init(argc, argv, needsThread))
86  {
87  Info<< "Failed to start parallel run" << endl;
88  Pstream::exit(1);
89  }
90  }
91 
92  //- True if this is parallel run.
93  bool parRun() const
94  {
95  return parallel_;
96  }
97 
98  //- True if this is a parallel run and uses distributed roots.
99  bool distributed() const
100  {
101  return parallel_ && distributed_;
102  }
103 
104  //- Set use of distributed roots.
105  void distributed(bool on)
106  {
107  distributed_ = (parallel_ ? on : false);
108  }
109 };
110 
111 
112 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
113 
114 } // End namespace Foam
115 
116 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
117 
118 #endif
119 
120 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::ParRunControl::distributed
void distributed(bool on)
Set use of distributed roots.
Definition: parRun.H:104
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, triggers Pstream::exit.
Definition: parRun.H:67
Foam::ParRunControl::ParRunControl
ParRunControl()
Construct null.
Definition: parRun.H:60
Foam::UPstream::exit
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:59
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
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:98
Foam::ParRunControl
Helper class for initializing parallel jobs from the command arguments.
Definition: parRun.H:52
Foam::ParRunControl::runPar
void runPar(int &argc, char **&argv, bool needsThread)
Initialize Pstream for a parallel run.
Definition: parRun.H:80
Foam::ParRunControl::parRun
bool parRun() const
True if this is parallel run.
Definition: parRun.H:92
Foam::UPstream::init
static bool init(int &argc, char **&argv, const bool needsThread)
Initialisation function called from main.
Definition: UPstream.C:48