program main implicit none #include "finclude/petscsys.h" #include "finclude/petscvec.h" #include "finclude/petscmat.h" #include "finclude/petscksp.h" #include "finclude/petscpc.h" #define xx_a(ib) xx_v(i_x+(ib)) Vec b PetscErrorCode ierr PetscInt rank, istart, iend PetscInt n, i PetscScalar c PetscOffset i_x PetscScalar xx_v(1) call PetscInitialize(PETSC_NULL_CHARACTER,ierr) call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr) n = 8 !Create vector call VecCreate(PETSC_COMM_WORLD,b,ierr) call VecSetSizes(b,PETSC_DECIDE,n,ierr) call VecSetFromOptions(b,ierr) !Set vector do i = 0, n-1 c = i+5 call VecSetValues(b,1,i,c,INSERT_VALUES,ierr) end do call VecAssemblyBegin(b,ierr) call VecAssemblyEnd(b,ierr) !print vector b write(*,*) 'b:' call VecView(b,PETSC_VIEWER_STDOUT_WORLD,ierr) call VecGetOwnershipRange(b,istart,iend,ierr) write(*, *) 'process:', rank, istart, '---',iend !get values of vector b call VecGetArray(b, xx_v, i_x, ierr) do i = istart, iend-1 ! c = xx_v(i) xx_a(i) = xx_a(i) + 200.0D0 write(*,*) '#:',xx_a(i) end do call VecRestoreArray(b, xx_v, i_x, ierr) call VecView(b,PETSC_VIEWER_STDOUT_WORLD,ierr) call VecDestroy(b,ierr) call PetscFinalize(ierr) end
什么? VecGetArray()?我也试过了哦~
错误如下:
[0]PETSC ERROR: ------------------------------------------------------------------------ [0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range [0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger [0]PETSC ERROR: or see http://www.mcs.anl.gov/petsc/petsc-as/documentation/troubleshooting.html#Signal[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors [0]PETSC ERROR: likely location of problem given in stack below [0]PETSC ERROR: --------------------- Stack Frames ------------------------------------ [0]PETSC ERROR: Note: The EXACT line numbers in the stack are not available, [0]PETSC ERROR: INSTEAD the line number of the start of the function [0]PETSC ERROR: is given. [0]PETSC ERROR: --------------------- Error Message ------------------------------------ [0]PETSC ERROR: Signal received! [0]PETSC ERROR: ------------------------------------------------------------------------ [0]PETSC ERROR: Petsc Release Version 3.1.0, Patch 7, Mon Dec 20 14:26:37 CST 2010 [0]PETSC ERROR: See docs/changes/index.html for recent updates. [0]PETSC ERROR: See docs/faq.html for hints about trouble shooting. [0]PETSC ERROR: See docs/index.html for manual pages. [0]PETSC ERROR: ------------------------------------------------------------------------ [0]PETSC ERROR: ./test on a linux-gnu named c0109 by root Tue Apr 19 18:44:43 2011 [0]PETSC ERROR: Libraries linked from /opt/petsc-3.1-p7/lib [0]PETSC ERROR: Configure run at Mon Mar 28 16:37:34 2011 [0]PETSC ERROR: Configure options --download-f-blas-lapack=../fblaslapack-3.1.1.tar.gz --prefix=/opt/petsc-3.1-p7 [0]PETSC ERROR: ------------------------------------------------------------------------ [0]PETSC ERROR: User provided function() line 0 in unknown directory unknown file aborting job: application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
纠结了一段时间,在高人指点下,终于发现了问题所在。
原来VecGetOwnershipRange()这个函数返回的[start,end),用 VecSetValues()对向量进行赋值时,索引值下界为0,但对向量进行取值时,索引下界是1。上面的代码忽略了这个因素,所以会出现内存访问越界。
比如说:
do 103, I = start,end-1 c = xx_v(i) write(*,*) '#:',c 103 continue
start = 0时,会访问xx_v(0),这个必然会出错。只需把
最后一个do i = istart, iend-1 改为do i = istart+1, iend 即可
下面给出正确的代码:
program main implicit none #include "finclude/petscsys.h" #include "finclude/petscvec.h" #include "finclude/petscmat.h" #include "finclude/petscksp.h" #include "finclude/petscpc.h" #define xx_a(ib) xx_v(i_x+(ib)) Vec b PetscErrorCode ierr PetscInt rank, istart, iend PetscInt n, i PetscScalar c PetscOffset i_x PetscScalar xx_v(1) call PetscInitialize(PETSC_NULL_CHARACTER,ierr) call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr) n = 8 !Create vector call VecCreate(PETSC_COMM_WORLD,b,ierr) call VecSetSizes(b,PETSC_DECIDE,n,ierr) call VecSetFromOptions(b,ierr) !Set vector do i = 0, n-1 c = i+5 call VecSetValues(b,1,i,c,INSERT_VALUES,ierr) end do call VecAssemblyBegin(b,ierr) call VecAssemblyEnd(b,ierr) !print vector b write(*,*) 'b:' call VecView(b,PETSC_VIEWER_STDOUT_WORLD,ierr) call VecGetOwnershipRange(b,istart,iend,ierr) write(*, *) 'process:', rank, istart, '---',iend !get values of vector b call VecGetArray(b, xx_v, i_x, ierr) write(*,*) 'i_x:',i_x !do i = istart,iend do i = istart+1, iend ! c = xx_v(i) xx_a(i) = xx_a(i) + 200.0D0 write(*,*) '#:',xx_a(i) end do call VecRestoreArray(b, xx_v, i_x, ierr) call VecView(b,PETSC_VIEWER_STDOUT_WORLD,ierr) call VecDestroy(b,ierr) call PetscFinalize(ierr) end