这是一段需要调试的代码......这个程序打印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;
}
以下 makefile 用于构建可执行文件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
Petsc的选项-fp_trap
有助于检测此问题的根源:
mpirun -np 2 main -fp_trap
将打印带有检测到问题的函数名称的警告:
[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!
因此-fp_trap
是 petsc 程序的一个选项,与调试器无关。
需要使用调试器来查找发生问题的确切行。例如,使用 gdb(和 petsc 配置 --with-debugging):
gdb --args main -fp_trap
(gdb) run
args
声明可执行文件后的参数传递给可执行文件。它产生:
Program received signal SIGFPE, Arithmetic exception.
0x00000000004011a2 in ShouldTriggerFloatingPointException () at main.c:18
18 PetscScalar c=a/b;
现在发现问题了!
有关调试和 mpi的更多信息:如何调试 MPI 程序?