程序接收信号SIGSEGV:分段故障 - 无效的存储器参考。回溯此错误:

时间:2014-09-27 16:08:28

标签: segmentation-fault fortran gfortran fortran95

我在Ubuntu 14.04中编译了一个fortran 95程序。运行时,我收到以下错误: 这与:(1)

不同
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
^Z
[9]+  Stopped                 ./a.out

我甚至尝试过

  

gfortran -g -fcheck = all -Wall DoubExchange.f95

但得到了同样的错误信息。

我的节目:

! HAMILTONIAN Diagonalization for a 2D Lattice of N sites or (N_X) X (N_Y) size, with DOUBLE EXCHANGE.
!                                                       !
            !           D O U B L E    E X C H A N G E                   !
!                                                       !
 PROGRAM DOUBLE_EXCHANGE
   implicit none
   integer:: I, J, K, M, Z, P, L, SIZ, CONT, POINT, S
   integer,parameter :: seed = 8645642
!   character:: N, U
   real*8:: H
   real :: start, finish
!     .. Parameters ..
      INTEGER          N, N_X, N_Y
      INTEGER          LDA
      INTEGER          LWMAX
      PARAMETER        ( LWMAX = 1000 )
!
!     .. Local Scalars ..
!
      INTEGER          INFO, LWORK
!
!     .. Local Arrays ..
!     RWORK dimension should be at least MAX(1,3*N-2)
!
      DOUBLE PRECISION,ALLOCATABLE,DIMENSION(:):: EIG
      REAL*8,ALLOCATABLE,DIMENSION(:,:):: F1
      REAL*8,ALLOCATABLE,DIMENSION(:):: WORK
        call cpu_time(start)
        open(1,file='DOUBLE_EXCHANGE.dat')
        open(2,file='DOUBLE_EXCHANGE.txt')
!
!   We are considering a 2D square lattice of 3 X 3
!
    CALL srand(seed)
    !!  LATTICE DESCRIPTION  !!
    N_X = 2
    N_Y = 2
    SIZ = 2 * N_X * N_Y
    N = SIZ*SIZ
    LDA = N
    !   PROGRAM STARTING POINT  !
    ALLOCATE(F1( SIZ, SIZ ), EIG(SIZ), WORK(3*siz-1))
    CONT = 1
    S = (N_X*2)-1
    M = N_Y-1
DO P=1,1
    DO I=0,M
       DO J=0,S
         POINT = 1
          DO K=0,M
            DO L=0,S
                IF(I==K .AND. ABS(J-L)==2) THEN
                  Z = -1

                ELSE IF(J==L .AND. ABS(I-K)==1) THEN
                  Z = -1

                ELSE IF(J==L .AND. ABS(I-K)==M) THEN
                  Z = -1

                ELSE IF(I==K .AND. ABS(J-L)==S-1) THEN
                  Z = -1

                ELSE IF(I==K .AND. J==L) THEN
                  Z = (-1)**POINT

                ELSE
                  Z = 0                                 !       Potential     0
                ENDIF
            F1(CONT,POINT) = Z
                POINT = POINT + 1
            ENDDO
         ENDDO
            CONT = CONT + 1
       ENDDO
    ENDDO

    WRITE(1,*)
!
       WRITE(1,*),'Displaying Matrix for the given Hamiltonian'
!
       DO i=1,SIZ
            WRITE(1,'(18g15.1)') (F1(i,j), j=1,SIZ)
       ENDDO
!
!
!       DSYEV Example Program Results
!
!   ALLOCATE(WORK(SIZ))
       WRITE(1,*)
       LWORK = -1
       CALL DSYEV( 'Vectors', 'Upper', SIZ, F1, LDA, Eig, WORK, LWORK, INFO )

       LWORK = INT( WORK( 1 ) )

!   DEALLOCATE(WORK)

!   ALLOCATE(WORK(LWORK))
!
!   Solve eigenproblem.
!
       CALL DSYEV( 'Vectors', 'Upper', SIZ, F1, LDA, Eig, WORK, LWORK, INFO )
!
!   Checking for convergence.
!
       IF( INFO.GT.0 ) THEN
          WRITE(*,*)'The algorithm failed to compute eigenvalues.'
          STOP
       END IF
!
!
!       Computes all eigenvalues and, optionally, eigenvectors of an
!       n x n real symmetric matrix A. The eigenvector v(j) of A satisfies
!
!             A*v_i(j) = λ(j)*v_i(j)
!
!
        !!   Print eigenvalues. !!
!
        write(1,*)
        write(1,*)'Eigenvalues'
        write(1,*)
            write(1,'(100g15.5)') (Eig(j), j=1,SIZ)
!
        !!     Print eigenvectors. Where ψ_(i)(eqv. F1(i,j)), i-label is for energy and j-label is for site.   !!
!
        write(1,*)
        write(1,*)'Orthonormal Eigenvectors (stored columnwise)'
        write(1,*)
        do i=1,SIZ
            write(1,'(100g15.5)') (F1(i,j), j=1,SIZ)
        end do
        write(1,*)

!
!        Calculation of number operator, ρ(i) at i-th site for n number of electrons
!
        WRITE(1,*)'Calculation of number operator, ρ(i) at i-th site'
!
!            Considering 3 electrons in the problem
!
    k = INT(SIZ/2)
       CONT = 1
       L    = 1
    DO I=1,SIZ
       H = 0D0
        DO J=1,K
           H = H + F1(I,J)*F1(I,J)     !  WE ARE CALCULATING Σ_n|ψ_n(i)|^2, Where ψ_n(i) are eigenvectors of i-th site with n-component.
        ENDDO
          IF(MOD(I,S) .NE. 0) THEN
             WRITE(2,'(100g15.5)')L,CONT,H
             CONT =CONT + 1

          ELSE
             WRITE(2,'(100g15.5)')L,CONT,H
             WRITE(2,*)
             CONT = 1
             L    = L + 1
          END IF
    ENDDO

        WRITE(1,*)
        WRITE(1,*)
        WRITE(1,*)
ENDDO
! 900    format (F8.3,F8.3)
          call cpu_time(finish)
              print '("Time = ",f6.3," seconds.")',finish-start
              write(1, '("Time = ",f6.3," seconds.")'),finish-start
       STOP
END PROGRAM

因此,错误主要是关于,正确初始化WORK。

2 个答案:

答案 0 :(得分:3)

是的,错误大约是WORK。您在LWORK工作空间查询中请求并使用DSYEV的最佳值,但您并未相应调整WORK的大小。 (你有调整大小的代码,但它被注释掉了。)

我在启用-C=all -C=undefined的情况下浏览了NAG Fortran编译器:

Runtime Error: dsyev.f90, line 1: Invalid reference to procedure DSYEV - Dummy array WORK (number 7) has 80 elements but actual argument only has 23 elements

(我不得不链接一个也是用-C=undefined构建的LAPACK。)

答案 1 :(得分:0)

感谢您了解 WORK

但我在我的程序中解决了错误。问题是因为初始化 LDA = N(其中,N = siz * siz),结果我的矩阵F1(siz,siz * siz),超过分配。

所以,我只需要初始化 LDA = siz

问题已解决。