且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是什么"分段错误 - 无效的内存引用错误"在FORTRAN

更新时间:2023-01-05 21:06:15

的问题1的答案
分割错误是由于内存冲突。在这种情况下,由于变量错分配所产生的错误。此外,我们应该设置变量绑定的限制。

错误:

  CnewV = CnewV +α2 * R

修正:

  CnewV(0:IG,0:JG)= CnewV(0:IG,0:JG)+α2 * R(0:IG,0:JG)

回答问题2
我相信这是一个编译器错误。我检查了其他编译​​器也和没有一个是能够​​解决这种类型的错误。

回答问题3
在函数内部数组中元素的数量很少/子程序调用该函数/子程序期间,由于不正确的变量设置。如果你得到这个错误,你应该增加该变量列出的变量,应该被调用。

还有指针assignemnt不能正常工作:
我已经设法克服分段错误,整个code编译罚款。然而,我第三次迭代后得到NaN的数字。

的下面测试code为旨在显示的问题的来源。我曾尝试以下解决方案。但是,我不能够在每次迭代的向量'先生'写列的目标矩阵熔点的!

 程序ptrtest 真实的,指针,连片::先生(:)
 真实的,指针,连片::熔点(:, :)
 实,外形尺寸(9):: ABC
 整数:: N = 2
 ITER = 3 我是否= 1,ITER α2 = 2
 分配(先生(N ** 2))
 ABC = 42 先生(1:N ** 2)= 0.5 * ABC(1:N ** 2)
 写(*,*)MR ='
 写(* 555)先生 先生(1:N ** 2)=>熔点(1:N ** 2,1:1) WRITE(*,*)MP ='
 WRITE(* 555),熔点 做到底 555 FORMAT(F12.4,1X)
 程序结束ptrtest

错误:

  gfortran -Wall -fcheck =所有Pointer.f95(目录:/家居/瓦希德/ Dropbox的/到移动文件夹/ Geany / Test_Pointer应用程序)
 Pointer.f95:25.3:
 先生(1:N ** 2)=>熔点(1:N ** 2,1:I)
 1
 错误:不兼容行列1和2分配在(1)
 编译失败。

我曾尝试万像素(1:N ** 2,1:I)=>先生(1:N ** 2)。这克服编译错误。然而,MP不是靶向和向量的元素被重写为在每次迭代矩阵的第1列。

I am receiving the following error in my Fortran code :

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

  Backtrace for this error:
  #0  0x7F80C7B46777
  #1  0x7F80C7B46D7E
  #2  0x7F80C7282D3F
  #3  0x402D1A in __mymodule_MOD_gcr_mfree
  #4  0x412175 in MAIN__ at HUHSI1.f90:?
  Segmentation fault (core dumped)
  ------------------
  (program exited with code: 139)

  1. I don't understand this error completely. What is this error?

    Number 3 indicates the following subroutine :

    SUBROUTINE gcr_Mfree(F2,Cnew,Cold,C_Fold,xm,converged)
    
        !**** FUNCTIONS TO BE SOLVED ****
        ! Generalized Conjugate Residual Algorithm
        ! Solves M*x=b (J*dx=-F)
    
        IMPLICIT REAL*8 (A-H,O-Z)
    
        INTEGER, intent(out)                :: converged  
        REAL*8, DIMENSION(:,:), ALLOCATABLE :: F2,p,Mp             
        REAL*8, DIMENSION(:),   ALLOCATABLE :: F2V,F22V,CnewV,ColdV,C_FoldV,alpha2,r,b,Mr,xv
    
        ! 2D INPUT VARIABLE DIMENSIONS:
        DIMENSION F22(-2:IG+2,-2:JG+2)
        DIMENSION Cnew(-2:IG+2,-2:JG+2)
        DIMENSION Cold(-2:IG+2,-2:JG+2)
        DIMENSION C_Fold(-2:IG+2,-2:JG+2)
        DIMENSION xm(-2:IG+2,-2:JG+2)
    
        integer :: j,maxiter,iter
        real*8 :: tol,normr  
        !===================================================================           
    
        MASK = SIZE(F2)                 ! Size of F2 : (IG+5)*(JG+5)
        Print*, 'MASK IS EQUAL TO',MASK
        ALLOCATE(F2V(1:MASK))
        ALLOCATE(CnewV(1:MASK))
        ALLOCATE(ColdV(1:MASK))
        ALLOCATE(C_FoldV(1:MASK))
        ALLOCATE(xv(1:MASK))
    
        ALLOCATE(r(1:MASK))
        ALLOCATE(b(1:MASK))
        ALLOCATE(p(1:IG,1:JG))
        ALLOCATE(Mp(1:IG,1:JG))
    
        !************* RESHAPING MATRICES TO VECTORS **************
        F2V     = RESHAPE(F2    ,(/MASK/))
        ColdV   = RESHAPE(Cold  ,(/MASK/))
        CnewV   = RESHAPE(Cnew  ,(/MASK/))
        C_FoldV = RESHAPE(C_Fold,(/MASK/))
    
       !            Write(*,*) shape(CnewV), shape(Cnew)
       !            WRITE(*,*) 'Cold='
       !            WRITE(*,554) Cold
       !            WRITE(*,*) 'Cnew='
       !            WRITE(*,554) Cnew
       !554         FORMAT(F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F4.2,1X,F5.3,1X,F5.3,1X)     
    
        b       = -F2V(1:MASK)                !(why minus?)
    
        tol     = 5.E-2
        alpha2  = 1e-6 
        maxiter = MASK
        r       = b(1:MASK)               ! we need this when calculating r_new
        normr    = sqrt(sum( r(:)**2 ))                  !!!! Norm               
    
        557         FORMAT(1X,F5.2,2X,F5.2,10X,F5.2,4X,F5.2,10X,F5.2,2X,F5.2)
        WRITE(*,*) 'ColdV,CnewV'
        WRITE(*,557) ColdV,CnewV,F2V,C_FoldV,b,r
        PRINT*, 'normr=', nomr
    
        !************* MAIN CALCULATION ****************
        DO iter=1,maxiter
    
            !Get preliminary search direction
            p(:,iter) = r(:)
    
                            WRITE(*,*) 'p='
                            WRITE(*,558) ,p
            558             FORMAT(1X,F4.2) 
    
            ! Approximate the Jacobian(M) residual product
            CnewV = CnewV + alpha2 * r
    
            WRITE(*,*), 'CnewV='
            WRITE(*,559) ,CnewV
            559             FORMAT(1X,F4.2)
    
            Cnew = RESHAPE(CnewV,(/IG+5,JG+5/))                 
    
            !********** Call Crank-Nicolson Function               
    
            F22V= RESHAPE(F22,(/MASK/))
    
            Mr(:) = (1/alpha2)*(F22V-F2V)      !GUIDE: (The apporximated Jacobian matrix)
    
            Mp(:,iter) = Mr(:)
    
            !! Orthogonalize search direction
            do j = 1, iter-1
                p(:,iter)  = p(:,iter)  - sum( Mp(:,j) * Mp(:,iter) ) * p(:,j)
                Mp(:,iter) = Mp(:,iter) - sum( Mp(:,j) * Mp(:,iter) ) * Mp(:,j)
            enddo
    
            !Normalize search direction              
            p(:,iter)  =  p(:,iter) / (norm2(Mp(:,iter))) 
            Mp(:,iter) = Mp(:,iter) / (norm2(Mp(:,iter)))
    
            !Update solution and residual
            alpha2 = sum( r(:) * Mp(:,iter) ) / sum( Mp(:,iter)**2 )                            
            xv = xv + alpha2 * p(:,iter)
            r = r - alpha2 * Mp(:,iter)      ! where is the *(Cnew - C)?
    
            xm = RESHAPE(xv,(/IG+5,JG+5/)) 
    
            !Check convergence
            normr=sqrt(sum( r(:)**2 ))                               !!!! norm
            !fprintf('norm(r) = !g iter = !gNewLine',normr,iter+1);
            if (normr.LT.tol) then
                converged=1
                exit
            end if
    
    
        END DO 
    
        if (normr > tol) then
            write(*,*) 'GCR SOLUTION DID NOT CONVERGE!'
            converged=0
        end if
    
        RETURN
    END subroutine gcr_Mfree
    

    Here are a couple of things I have done to figure out why I am receiving this error:

    I have traced the values of the variables in my code by printing them step by step. I have reached this formula where I am not able to print the value for CnewV anymore.

    CnewV = CnewV + alpha2 * r
    

  2. This formula is in "mymodule_MOD_gcr_mfree" which is indicated by #3 in the error message. Why the error is not indicating any row number?

    "r" is a vector. "alpha2" is scalar. CnewV at the RHS is a vector. Size and rank of "r" and "CnewV" are the same and the value of only one element in "CnewV" vector is NaN. Also, two or three elements are printed "*****" when I am trying to print them at screen.

  3. when printing the matrix "p" (the format number is 558), there are much less elements in the array p. There are 221 elements in array "r", however, there are 96 in "p"

This is a complicated problem. I hope the given information is enough to understand the error.

The answer to question 1 : Segmentation error is due to memory conflicts. In this case, the error is generated due to wrong allocation of variables. Also, we should set variable bound limits.

Wrong :

CnewV = CnewV + alpha2 * r

Corrected:

CnewV(0:IG,0:JG) = CnewV(0:IG,0:JG) + alpha2 * r(0:IG,0:JG)

The answer to question 2 : I believe this is a compiler bug. I checked other compilers too and none were able to address this type of error.

The answer to question 3 : The low number of elements in an array inside a function/subroutine is due to improperly variable setting during calling the function/subroutine. If you get this error you should add that variable to list of variables which should be called.

Still Pointer assignemnt is not working properly: I have managed to overcome segmentation error and the whole code compiles fine. However, I receive NaN numbers after 3rd iteration.

The below test code is intended to show the source of problem. I have tried the following solution. however, I am not able to write the vector 'Mr' to column's of target matrix 'Mp' at each iteration!

 program ptrtest

 real, pointer, CONTIGUOUS :: Mr(:)
 real, pointer, CONTIGUOUS :: Mp(:,:)
 real, DIMENSION(9) ::abc


 integer :: n = 2
 iter=3

 Do i=1,iter

 alpha2 = 2
 allocate(Mr(n**2))
 abc= 42

 Mr(1:n**2) = 0.5 * abc(1:n**2)
 write(*,*) 'Mr='
 write(*,555) Mr

 Mr(1:n**2) => Mp(1:n**2,1:1) 

 WRITE(*,*) 'Mp='
 WRITE(*,555) Mp

 end do

 555  FORMAT(F12.4,1X) 


 end program ptrtest

Error :

 gfortran -Wall -fcheck=all "Pointer.f95" (in directory: /home/vahid/Dropbox/To Move folder/Geany/Test_Pointer application)
 Pointer.f95:25.3:
 Mr(1:n**2) => Mp(1:n**2,1:i)
 1
 Error: Incompatible ranks 1 and 2 in assignment at (1)
 Compilation failed.

I have tried Mp(1:n**2,1:i) => Mr(1:n**2). this overcomes compilation error. however, Mp is not target and the elements of vector are rewritten to 1st column of the matrix at each iteration.