SpECTRE  v2026.11.17
Loading...
Searching...
No Matches
Error Handling

Classes

class  DeviceMessage
 A minimal output stream that can be used in device code. More...
class  SpectreAssert
 Exception indicating an ASSERT failed. More...
class  SpectreError
 Exception indicating an ERROR was triggered. More...
class  SpectreFpe
 Exception indicating an ERROR was triggered because of an FPE. More...
class  convergence_error
 Exception indicating convergence failure. More...
class  ScopedFpeState
 An RAII object to temporarily modify the handling of floating point exceptions. More...

Macros

#define SPECTRE_ASSERT_ON_HOST(a, m)
 Assert that an expression should be true.
#define SPECTRE_ABORT_ON_DEVICE(HEADING, DESCRIPTION, MESSAGE)
 Prints a failure message in device code and aborts the program.
#define SPECTRE_ERROR_ON_HOST(EXCEPTION_TYPE, m)
 prints an error message to the standard error stream and aborts the program.
#define ERROR_AS(m, EXCEPTION_TYPE)
 Same as ERROR but will throw EXCEPTION_TYPE instead of SpectreError.
#define SPECTRE_ERROR_NO_TRACE_ON_HOST(m)
 Same as ERROR but does not print a backtrace. Intended to be used for user errors, such as incorrect values in an input file.
#define Expects(cond)
 check expectation of pre-conditions of a function
#define Ensures(cond)
 Check that a post-condition of a function is true.
#define DEBUG_STATIC_ASSERT(...)
 A static_assert that is only checked in Debug builds.

Functions

void abort_with_error_message (const char *expression, const char *file, int line, const char *pretty_function, const std::string &message)
 Compose an error message with an expression and a backtrace, then abort the program.
template<typename ExceptionTypeToThrow>
void abort_with_error_message (const char *file, int line, const char *pretty_function, const std::string &message)
 Compose an error message including a backtrace and abort the program.
void abort_with_error_message_no_trace (const char *file, int line, const char *pretty_function, const std::string &message)
 Compose an error message without a backtrace and abort the program.
void enable_floating_point_exceptions ()
 After a call to this function, the code will terminate with a floating point exception on overflow, divide-by-zero, and invalid operations.
void disable_floating_point_exceptions ()
 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 enable_segfault_handler ()
 After a call to this function, the code will handle SIGSEGV segmentation faults by printing an error with a stacktrace.
void sys::abort (const std::string &message)
 Abort the program with an error message.

Detailed Description

Macros and functions used for handling errors

Macro Definition Documentation

◆ DEBUG_STATIC_ASSERT

#define DEBUG_STATIC_ASSERT ( ...)
Value:
static_assert(true)

A static_assert that is only checked in Debug builds.

◆ Ensures

#define Ensures ( cond)
Value:
if (false) { \
static_cast<void>(cond); \
} else \
static_cast<void>(0)

Check that a post-condition of a function is true.

The Ensures macro sets the postconditions of function, it is a contract (C++20) that must be satisfied. See the CppCoreGuidelines for details.

Parameters
condthe expression that is expected to be true

◆ ERROR_AS

#define ERROR_AS ( m,
EXCEPTION_TYPE )
Value:
do { \
KOKKOS_IF_ON_HOST((SPECTRE_ERROR_ON_HOST(EXCEPTION_TYPE, m);)) \
KOKKOS_IF_ON_DEVICE((SPECTRE_ABORT_ON_DEVICE("DEVICE ERROR", "", m);)) \
} while (false)
#define SPECTRE_ABORT_ON_DEVICE(HEADING, DESCRIPTION, MESSAGE)
Prints a failure message in device code and aborts the program.
Definition DeviceMessage.hpp:175
#define SPECTRE_ERROR_ON_HOST(EXCEPTION_TYPE, m)
prints an error message to the standard error stream and aborts the program.
Definition Error.hpp:72

Same as ERROR but will throw EXCEPTION_TYPE instead of SpectreError.

Note
Any exception types used must have an associated explicit instantiation in Utilities/ErrorHandling/AbortWithErrorMessage.cpp

◆ Expects

#define Expects ( cond)
Value:
if (false) { \
static_cast<void>(cond); \
} else \
static_cast<void>(0)

check expectation of pre-conditions of a function

The Expects macro sets the preconditions to a function's arguments, it is a contract (C++20) that must be satisfied. See the CppCoreGuidelines for details.

Parameters
condthe expression that is expected to be true

◆ SPECTRE_ABORT_ON_DEVICE

#define SPECTRE_ABORT_ON_DEVICE ( HEADING,
DESCRIPTION,
MESSAGE )
Value:
do { \
if (::DeviceMessage_detail::claim_failure_message()) { \
::DeviceMessage& device_message_ABORT_ON_DEVICE = \
::DeviceMessage_detail::start_failure_message(HEADING, DESCRIPTION, \
__FILE__, __LINE__); \
device_message_ABORT_ON_DEVICE << MESSAGE; /* NOLINT */ \
::DeviceMessage_detail::finish_failure_message( \
device_message_ABORT_ON_DEVICE, \
static_cast<const char*>(__PRETTY_FUNCTION__)); \
} \
::DeviceMessage_detail::end_other_failing_thread( \
HEADING \
" in " __FILE__ \
":" SPECTRE_DEVICE_MESSAGE_STRINGIFY(__LINE__) " " DESCRIPTION); \
} while (false)
A minimal output stream that can be used in device code.
Definition DeviceMessage.hpp:44

Prints a failure message in device code and aborts the program.

Used by ASSERT and ERROR in device code. HEADING and DESCRIPTION must be string literals, where the DESCRIPTION (e.g. the failed expression) can be empty. MESSAGE is an expression that can be streamed into a DeviceMessage.

Often many GPU threads fail at the same time. Only the first of them formats its message, in a single static buffer, and passes it to Kokkos::abort, which prints it with the indices of the GPU block and thread. Printing it at once keeps it from interleaving with other output, because GPUs print the output of separate printf calls in no guaranteed order. The message starts with the heading, file, line and description, and ends with the function name, so truncating a long message cuts the function name first.

On CUDA devices the other failing threads end right away without printing anything (with the PTX instruction exit, which ends only the calling thread), so the kernel keeps running until the first thread has printed its message. On HIP devices a single thread can't end by itself, so the others wait until the abort of the first thread ends the kernel. If that doesn't happen in time, e.g. because the GPU can't run the first thread while the others wait, they abort with a short message that contains the heading, file, line and description.

Warning
Kokkos warns that the SYCL backend does not terminate the program on an abort() call. For us this is undefined behavior and needs to be revisited before properly support SYCL.

◆ SPECTRE_ASSERT_ON_HOST

#define SPECTRE_ASSERT_ON_HOST ( a,
m )
Value:
do { \
if (false) { \
static_cast<void>(a); \
const ScopedFpeState disable_fpes_ASSERT(false); \
std::ostringstream avoid_name_collisions_ASSERT; \
/* clang-tidy: macro arg in parentheses */ \
avoid_name_collisions_ASSERT << std::setprecision(18) << std::scientific \
<< m; /* NOLINT */ \
static_cast<void>(avoid_name_collisions_ASSERT); \
} \
} while (false)
An RAII object to temporarily modify the handling of floating point exceptions.
Definition FloatingPointExceptions.hpp:38
T scientific(T... args)
T setprecision(T... args)

Assert that an expression should be true.

If the preprocessor macro SPECTRE_DEBUG is defined and the expression is false, an error message is printed to the standard error stream, and the program aborts. ASSERT should be used to catch coding errors as it does nothing in production code.

ASSERT can also be used in functions that run on a GPU (KOKKOS_FUNCTIONs). On the GPU the message is streamed into a DeviceMessage, which supports fewer types than std::ostream.

Parameters
athe expression that must be true
mthe error message as an ostream

◆ SPECTRE_ERROR_NO_TRACE_ON_HOST

#define SPECTRE_ERROR_NO_TRACE_ON_HOST ( m)
Value:
do { \
if (__builtin_is_constant_evaluated()) { \
throw std::runtime_error("Failed"); \
} else { \
const ScopedFpeState disable_fpes_ERROR(false); \
abort_with_error_message_no_trace( \
__FILE__, __LINE__, static_cast<const char*>(__PRETTY_FUNCTION__), \
} \
} while (false)
Make a string by streaming into object.
Definition MakeString.hpp:18

Same as ERROR but does not print a backtrace. Intended to be used for user errors, such as incorrect values in an input file.

◆ SPECTRE_ERROR_ON_HOST

#define SPECTRE_ERROR_ON_HOST ( EXCEPTION_TYPE,
m )
Value:
do { \
if (__builtin_is_constant_evaluated()) { \
throw std::runtime_error("Failed"); \
} else { \
Error_detail::abort_without_fpes<EXCEPTION_TYPE>( \
__FILE__, __LINE__, static_cast<const char*>(__PRETTY_FUNCTION__), \
[&]() -> std::string { \
<< m; \
}); \
} \
} while (false)

prints an error message to the standard error stream and aborts the program.

ERROR should not be used for coding errors, but instead for user errors or failure modes of numerical algorithms. An acceptable use for error is also in the default case of a switch statement.

Details

The implementation is specialized so that in compile time contexts, a short error message will be thrown, but in runtime contexts, a more verbose error will be printed. This specialization of throwing a short error at compile time greatly reduces the compile time and memory consumption during debug builds of deep and heavily inlined TensorExpression tree traversals.

To accomplish this, __builtin_is_constant_evaluated() is used directly instead of calling a wrapper function because calling a wrapper was found to slightly increase the compile time and memory usage of large TensorExpressions when compiling in debug mode.

ERROR can also be used in functions that run on a GPU (KOKKOS_FUNCTIONs). On the GPU the message is streamed into a DeviceMessage, which supports fewer types than std::ostream, and the program aborts with Kokkos::abort.

Parameters
man arbitrary output stream.

Function Documentation

◆ abort()

void sys::abort ( const std::string & message)

Abort the program with an error message.

Details

This function calls CkExit with a non-zero argument to indicate a failure, unless the SPECTRE_TRAP_ON_ERROR environmental variable is set, in which case it raises SIGTRAP.

◆ abort_with_error_message() [1/2]

void abort_with_error_message ( const char * expression,
const char * file,
int line,
const char * pretty_function,
const std::string & message )

Compose an error message with an expression and a backtrace, then abort the program.

We try to demangle and format the backtrace. Long symbol names are abbreviated, unless you set the SPECTRE_SHOW_FULL_BACKTRACE_SYMBOLS environment variable to a non-empty value (e.g. "1").

◆ abort_with_error_message() [2/2]

template<typename ExceptionTypeToThrow>
void abort_with_error_message ( const char * file,
int line,
const char * pretty_function,
const std::string & message )

Compose an error message including a backtrace and abort the program.

We try to demangle and format the backtrace. Long symbol names are abbreviated, unless you set the SPECTRE_SHOW_FULL_BACKTRACE_SYMBOLS environment variable to a non-empty value (e.g. "1").

◆ abort_with_error_message_no_trace()

void abort_with_error_message_no_trace ( const char * file,
int line,
const char * pretty_function,
const std::string & message )

Compose an error message without a backtrace and abort the program.

Note
This always throws SpectreError as the exception type.

◆ disable_floating_point_exceptions()

void disable_floating_point_exceptions ( )

After a call to this function, the code will NOT terminate with a floating point exception on overflow, divide-by-zero, and invalid operations.

Warning
Do not use this function to temporarily disable FPEs, because it will not interact correctly with C++ exceptions. Use ScopedFpeState instead.