sigFpe.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-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 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::sigFpe
29 
30 Description
31  Set up trapping for floating point exceptions (signal FPE).
32 
33  Defined by controlDict InfoSwitch entries:
34  - \par trapFpe
35  Enable floating point exception trapping.
36 
37  - \par setNaN
38  Initialization all malloced memory to NaN.
39  Combined with \c trapFpe, this causes usage of uninitialized scalars
40  to trigger an abort.
41 
42  Environment variables:
43  - \par FOAM_SIGFPE (true|false)
44  overrides \c trapFpe
45  - \par FOAM_SETNAN (true|false)
46  overrides \c setNaN
47 
48  Note that trapping can be set/removed through the static member functions
49  or through the scope of the object (constructor sets trapping; destructor
50  restores original). The class behaves as a singleton.
51 
52 SourceFiles
53  sigFpe.C
54 
55 \*---------------------------------------------------------------------------*/
56 
57 #ifndef Foam_sigFpe_H
58 #define Foam_sigFpe_H
59 
60 #include <cstddef> // For std::size_t
61 #include "scalarFwd.H"
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 // Forward Declarations
69 template<class T> class UList;
70 
71 /*---------------------------------------------------------------------------*\
72  Class sigFpe Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class sigFpe
76 {
77  // Private Data
78 
79  //- Flag that floating point trapping should be used.
80  // Can override with FOAM_SIGFPE env variable
81  static bool switchFpe_;
82 
83  //- Flag that NaN initialisation should be used.
84  // Can override with FOAM_SETNAN env variable
85  static bool switchNan_;
86 
87  //- Floating point trapping currently active?
88  static bool sigActive_;
89 
90  //- Is NaN memory initialisation currently active?
91  static bool nanActive_;
92 
93 
94  // Private Member Functions
95 
96  //- Handler for caught signals - ends job and prints stack
97  static void sigHandler(int);
98 
99 
100 public:
101 
102  // Constructors
103 
104  //- Constructor calls set() to activate the FPE signal handler if it
105  //- was was not previously activate and requested() returns true.
106  sigFpe();
107 
108 
109  //- Destructor calls unset() to deactivate the FPE signal handler
110  //- as required.
111  ~sigFpe();
112 
113 
114  // Static Member Functions
115 
116  //- Check if SIGFPE signals handler is to be enabled.
117  // This is controlled by the trapFpe entry or the FOAM_SIGFPE
118  // environment variable
119  static bool requested();
120 
121  //- True if SIGFPE handling is currently active.
122  static bool active() noexcept { return sigActive_; }
123 
124  //- True if NaN memory initialisation is currently active.
125  static bool nanActive() noexcept { return nanActive_; }
126 
127  //- Activate SIGFPE handler when FOAM_SIGFPE is enabled.
128  //- Activate fill memory with signaling_NaN when FOAM_SETNAN is enabled
129  static void set(bool verbose=false);
130 
131  //- Deactivate SIGFPE handler and NaN memory initialisation
132  static void unset(bool verbose=false);
133 
134  //- Fill data block with signaling_NaN values
135  static void fillNan(char* buf, size_t count);
136 
137  //- Fill data block with signaling_NaN values
138  static void fillNan(UList<scalar>& list);
139 
140 
141  // Helpers
142 
143  //- Helper to locally ignore SIGFPE handling.
144  // Restores the original state of the SIGFPE handler on destruction.
145  class ignore
146  {
147  //- The signal handler state when entering
148  bool wasActive_;
149 
150  public:
151 
152  //- No copy construct
153  ignore(const ignore&) = delete;
154 
155  //- No move construct
156  ignore(ignore&&) = delete;
157 
158  //- No copy assignment
159  void operator=(const ignore&) = delete;
160 
161  //- No move assignment
162  void operator=(ignore&&) = delete;
163 
164  //- Constructor deactivates any previously active SIGFPE handler
165  ignore()
166  :
167  wasActive_(Foam::sigFpe::active())
168  {
169  if (wasActive_)
170  {
172  }
173  }
175  //- Destructor restores the original state of SIGFPE handler
176  ~ignore() { reset(); }
177 
178  //- Restore the original state of SIGFPE handler
179  void reset()
180  {
181  if (wasActive_)
182  {
183  wasActive_ = false;
185  }
186  }
187 
188  //- Same as reset()
189  void restore() { reset(); }
190  };
191 };
192 
193 
194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 
196 } // End namespace Foam
197 
198 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
199 
200 #endif
201 
202 // ************************************************************************* //
static void unset(bool verbose=false)
Deactivate SIGFPE handler and NaN memory initialisation.
Definition: sigFpe.C:208
static bool active() noexcept
True if SIGFPE handling is currently active.
Definition: sigFpe.H:138
ignore()
Constructor deactivates any previously active SIGFPE handler.
Definition: sigFpe.H:206
~sigFpe()
Destructor calls unset() to deactivate the FPE signal handler as required.
Definition: sigFpe.C:136
static void set(bool verbose=false)
Activate SIGFPE handler when FOAM_SIGFPE is enabled. Activate fill memory with signaling_NaN when FOA...
Definition: sigFpe.C:150
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
Set up trapping for floating point exceptions (signal FPE).
Definition: sigFpe.H:70
~ignore()
Destructor restores the original state of SIGFPE handler.
Definition: sigFpe.H:219
Helper to locally ignore SIGFPE handling.
Definition: sigFpe.H:174
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const direction noexcept
Definition: Scalar.H:258
static bool nanActive() noexcept
True if NaN memory initialisation is currently active.
Definition: sigFpe.H:143
Typedefs for float/double/scalar without requiring scalar.H.
void operator=(const ignore &)=delete
No copy assignment.
void reset()
Restore the original state of SIGFPE handler.
Definition: sigFpe.H:224
static void fillNan(char *buf, size_t count)
Fill data block with signaling_NaN values.
Definition: sigFpe.C:244
static bool requested()
Check if SIGFPE signals handler is to be enabled.
Definition: sigFpe.C:144
sigFpe()
Constructor calls set() to activate the FPE signal handler if it was was not previously activate and ...
Definition: sigFpe.C:128
void restore()
Same as reset()
Definition: sigFpe.H:236
Namespace for OpenFOAM.