From a477f26477fa2729ea92078eccefe13d5ddb5084 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 21 Jul 2017 15:37:40 -0400 Subject: [PATCH] add support for trapping floating point exception as an optional compile time feature we may make this a run time setting by connecting this code to a command. --- src/main.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index cc8f8be906..7401183fea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,10 @@ #include #include +#if defined(LAMMPS_TRAP_FPE) && defined(_GNU_SOURCE) +#include +#endif + using namespace LAMMPS_NS; /* ---------------------------------------------------------------------- @@ -28,6 +32,18 @@ int main(int argc, char **argv) { MPI_Init(&argc,&argv); +// enable trapping selected floating point exceptions. +// this uses GNU extensions and is only tested on Linux +// therefore we make it depend on -D_GNU_SOURCE, too. + +#if defined(LAMMPS_TRAP_FPE) && defined(_GNU_SOURCE) + fesetenv(FE_NOMASK_ENV); + fedisableexcept(FE_ALL_EXCEPT); + feenableexcept(FE_DIVBYZERO); + feenableexcept(FE_INVALID); + feenableexcept(FE_OVERFLOW); +#endif + #ifdef LAMMPS_EXCEPTIONS try { LAMMPS *lammps = new LAMMPS(argc,argv,MPI_COMM_WORLD);