-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from kidder/feature/error_handling
Add namespace Parallel and functions for enabling FPEs
- Loading branch information
Showing
12 changed files
with
216 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
add_subdirectory(ErrorHandling) | ||
add_subdirectory(Executables) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
set(library ErrorHandling) | ||
|
||
set(library_sources | ||
FloatingPointExceptions.cpp | ||
) | ||
|
||
add_library(${library} ${library_sources}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
#include "ErrorHandling/FloatingPointExceptions.hpp" | ||
|
||
#include "Parallel/Abort.hpp" | ||
|
||
#include <csignal> | ||
|
||
#ifdef __APPLE__ | ||
#include <xmmintrin.h> | ||
#else | ||
#include <fenv.h> | ||
#endif | ||
|
||
namespace { | ||
|
||
#ifdef __APPLE__ | ||
auto old_mask = _mm_getcsr(); | ||
#endif | ||
|
||
[[noreturn]] void fpe_signal_handler(int /*signal*/) { | ||
Parallel::abort("Floating point exception!"); // LCOV_EXCL_LINE | ||
} | ||
} // namespace | ||
|
||
void enable_floating_point_exceptions() { | ||
#ifdef __APPLE__ | ||
_mm_setcsr(_MM_MASK_MASK & | ||
~(_MM_MASK_OVERFLOW | _MM_MASK_INVALID | _MM_MASK_DIV_ZERO)); | ||
#else | ||
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); | ||
#endif | ||
signal(SIGFPE, fpe_signal_handler); | ||
} | ||
|
||
void disable_floating_point_exceptions() { | ||
#ifdef __APPLE__ | ||
_mm_setcsr(old_mask); | ||
#else | ||
fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
/// \file | ||
/// Functions to enable/disable termination on floating point exceptions | ||
|
||
#pragma once | ||
|
||
/// \ingroup ErrorHandling | ||
/// After a call to this function, the code will terminate with a floating | ||
/// point exception on overflow, divide-by-zero, and invalid operations. | ||
void enable_floating_point_exceptions(); | ||
|
||
/// \ingroup ErrorHandling | ||
/// After a call to this function, the code will NOT terminate with a floating | ||
/// point exception on overflow, divide-by-zero, and invalid operations. | ||
void disable_floating_point_exceptions(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
/// \file | ||
/// Defines function Parallel::abort. | ||
|
||
#pragma once | ||
|
||
#include <charm++.h> | ||
#include <exception> | ||
#include <string> | ||
|
||
namespace Parallel { | ||
|
||
/// \ingroup Parallel | ||
/// Abort the program with an error message. | ||
[[noreturn]] inline void abort(const std::string& message) { | ||
CkAbort(message.c_str()); | ||
// the following call is never reached, but suppresses the warning that | ||
// a 'noreturn' functions does return | ||
std::terminate(); // LCOV_EXCL_LINE | ||
} | ||
|
||
} // namespace Parallel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
/// \file | ||
/// Defines function Parallel::exit. | ||
|
||
#pragma once | ||
|
||
#include <charm++.h> | ||
#include <exception> | ||
|
||
/// Contains functions that forward to Charm++ parallel functions. | ||
namespace Parallel { | ||
|
||
/// \ingroup Parallel | ||
/// \brief Exit the program normally. | ||
/// This should only be called once over all processors. | ||
[[noreturn]] inline void exit() { | ||
CkExit(); | ||
// the following call is never reached, but suppresses the warning that | ||
// a 'noreturn' function does return | ||
std::terminate(); // LCOV_EXCL_LINE | ||
} | ||
|
||
} // namespace Parallel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Distributed under the MIT License. | ||
# See LICENSE.txt for details. | ||
|
||
list(APPEND ERROR_HANDLING_TESTS ErrorHandling/FloatingPointExceptions.cpp) | ||
|
||
# CMake cannot append to a list in the parent scope | ||
list(APPEND ERROR_HANDLING_TESTS ${SPECTRE_TESTS}) | ||
set(SPECTRE_TESTS "${ERROR_HANDLING_TESTS}" PARENT_SCOPE) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Distributed under the MIT License. | ||
// See LICENSE.txt for details. | ||
|
||
#include <catch.hpp> | ||
#include <cmath> | ||
#include <limits> | ||
|
||
#include "ErrorHandling/FloatingPointExceptions.hpp" | ||
|
||
// [[OutputRegex, Floating point exception!]] | ||
TEST_CASE("Unit.ErrorHandling.FloatingPointExceptions.Invalid", | ||
"[ErrorHandling][Unit]") { | ||
enable_floating_point_exceptions(); | ||
double x = -1.0; | ||
double invalid = sqrt(x); | ||
CHECK(true); | ||
} | ||
|
||
// [[OutputRegex, Floating point exception!]] | ||
TEST_CASE("Unit.ErrorHandling.FloatingPointExceptions.Overflow", | ||
"[ErrorHandling][Unit]") { | ||
enable_floating_point_exceptions(); | ||
volatile double overflow = std::numeric_limits<double>::max(); | ||
overflow *= 1.0e300; | ||
CHECK(true); | ||
} | ||
|
||
// [[OutputRegex, Floating point exception!]] | ||
TEST_CASE("Unit.ErrorHandling.FloatingPointExceptions.DivByZero", | ||
"[ErrorHandling][Unit]") { | ||
enable_floating_point_exceptions(); | ||
volatile double div_by_zero = 1.0; | ||
div_by_zero /= 0.0; | ||
CHECK(true); | ||
} | ||
|
||
TEST_CASE("Unit.ErrorHandling.FloatingPointExceptions.Disable", | ||
"[ErrorHandling][Unit]") { | ||
enable_floating_point_exceptions(); | ||
disable_floating_point_exceptions(); | ||
double x = -1.0; | ||
double invalid = sqrt(x); | ||
volatile double overflow = std::numeric_limits<double>::max(); | ||
overflow *= 1.0e300; | ||
volatile double div_by_zero = 1.0; | ||
div_by_zero /= 0.0; | ||
CHECK(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters