Here is a piece of code that needs some debugging... This program prints norm is -nan
.
static char help[] = "Floating point exception.\n\n";
#include <petscvec.h>
#undef __FUNCT__
#define __FUNCT__ "ShouldTriggerFloatingPointException"
extern PetscErrorCode ShouldTriggerFloatingPointException(){
PetscErrorCode ierr;
PetscFunctionBegin;
PetscScalar a=0.0;
PetscScalar b=0.0;
PetscScalar c=a/b;
Vec x;
ierr=VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);
ierr= VecSetSizes(x,PETSC_DECIDE,1000);CHKERRQ(ierr);
ierr= VecSetFromOptions(x);CHKERRQ(ierr);
ierr=VecScale(x,c);CHKERRQ(ierr);
PetscScalar norm;
ierr=VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
PetscPrintf(PETSC_COMM_WORLD,"norm is %g\n",norm);
PetscFunctionReturn(0);
}
#undef __FUNCT__
#define __FUNCT__ "main"
int main(int argc,char **argv)
{
PetscErrorCode ierr;
PetscInitialize(&argc,&argv,(char*)0,help);
ierr=ShouldTriggerFloatingPointException();CHKERRQ(ierr);
ierr = PetscFinalize();
return 0;
}
The following makefile is used to build the executable main
https://scicomp.stackexchange.com/questions/905/compiling-and-running-a-hello-world-program-in-petsc
include ${PETSC_DIR}/conf/variables
include ${PETSC_DIR}/conf/rules
main: main.o chkopts
-${CLINKER} -o main main.o ${PETSC_LIB}
${RM} main.o
The option -fp_trap
of Petsc helps detecting the origin of this issue :
mpirun -np 2 main -fp_trap
will print a warning with the name of the function where the problem was detected :
[0]PETSC ERROR: [0] ShouldTriggerFloatingPointException line 14 main.c
[0]PETSC ERROR: ------- Error Message----------
[0]PETSC ERROR: Floating point exception!
[0]PETSC ERROR: trapped floating point error!
Hence -fp_trap
is an option of the petsc program, nothing to do with the debugger.
Using a debugger is required to find the exact line where the issue occured. For instance, with gdb (and petsc configure --with-debugging) :
gdb --args main -fp_trap
(gdb) run
args
states that Arguments after executable-file are passed to the executable. It produces :
Program received signal SIGFPE, Arithmetic exception.
0x00000000004011a2 in ShouldTriggerFloatingPointException () at main.c:18
18 PetscScalar c=a/b;
The issue is now detected !
Further information about debugging and mpi : How do I debug an MPI program?