问题描述
我收到以下错误在我的Fortran code:
计划接收信号SIGSEGV:段错误 - 无效的内存引用。 回溯跟踪此错误:
#0 0x7F80C7B46777
#1 0x7F80C7B46D7E
#2 0x7F80C7282D3F
#3 0x402D1A在__mymodule_MOD_gcr_mfree
#4 0x412175在MAIN__在HUHSI1.f90:?
分段错误(核心转储)
------------------
(程序退出,code:139)
-
我不完全理解这个错误。这是什么错误?
3号表示下面的子例程:
SUBROUTINE gcr_Mfree(F2,Cnew,冷,C_Fold,XM,融合) !****功能进行解决****
!广义共轭残差算法
!解决了M * X = B(Ĵ* DX = -F) 隐REAL * 8(A-H,O-Z) INTEGER,意图(出)::融合
REAL * 8,外形尺寸(:, :),可分配:: F2,P,熔点
REAL * 8,外形尺寸(:),可分配:: F2V,F22V,CnewV,ColdV,C_FoldV,α2,R,B先生,XV ! 2D输入变量尺寸:
DIMENSION F22(-2:IG + 2,-2:JG + 2)
DIMENSION Cnew(-2:IG + 2,-2:JG + 2)
DIMENSION冷(-2:IG + 2,-2:JG + 2)
DIMENSION C_Fold(-2:IG + 2,-2:JG + 2)
DIMENSION XM(-2:IG + 2,-2:JG + 2) 整数::Ĵ,MAXITER,ITER
真正的* 8 :: TOL,normr
!================================================= ================== MASK = SIZE(F2)! F2的大小:(IG + 5)*(JG + 5)
打印*,'MASK等于',MASK
ALLOCATE(F2V(1:MASK))
ALLOCATE(CnewV(1:MASK))
ALLOCATE(ColdV(1:MASK))
ALLOCATE(C_FoldV(1:MASK))
ALLOCATE(十五(1:MASK)) ALLOCATE(R(1:MASK))
ALLOCATE(二(1:MASK))
ALLOCATE(P(1:IG,1:JG))
ALLOCATE(熔点(1:IG,1:JG)) !*************特色重塑矩阵来向量**************
F2V = RESHAPE(F2,(/ MASK /))
ColdV = RESHAPE(冷,(/ MASK /))
CnewV = RESHAPE(Cnew,(/ MASK /))
C_FoldV = RESHAPE(C_Fold,(/ MASK /)) !写(*,*)形状(CnewV),形状(Cnew)
! WRITE(*,*)冷='
! WRITE(* 554)冷
! 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)(为什么减?)! TOL = 5.E-2
α2 = 1E-6
MAXITER = MASK
R = B(1:MASK)!我们需要这个计算r_new时
normr =开方(SUM(R(:)** 2))!!!!规范 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 !*************主要计算****************
DO ITER = 1,MAXITER !获得preliminary搜索方向
P(:,ITER)= R(:) WRITE(*,*)p值='
WRITE(*,558),P
558 FORMAT(1X,F4.2) !近似雅可比(M)剩余产品
CnewV = CnewV +α2 * R WRITE(*,*),'CnewV ='
WRITE(*,559),CnewV
559 FORMAT(1X,F4.2) Cnew = RESHAPE(CnewV,(/ IG + 5,+ JG 5 /)) !**********呼叫曲柄尼科尔森功能 F22V = RESHAPE(F22,(/ MASK /)) 先生(:) =(1 /α2)*(F22V-F2V)指南:!(该apporximated雅可比矩阵) 熔点(:,ITER)=先生(:) !正交搜索方向
做J = 1,ITER-1
P(:,ITER)= P(:,ITER) - 和(熔点(:,J)*熔点(:,ITER))* P(:,J)
熔点(:,ITER)= NP(:,ITER) - 和(MP(:,J)*熔点(:,ITER))*熔点(:,J)
ENDDO !规范化搜索方向
P(:,ITER)= P(:,ITER)/(NORM2(熔点(:,ITER)))
熔点(:,ITER)= NP(:,ITER)/(NORM2(MP(:,ITER))) !更新的解决方案和残余
α2 = SUM(R(:) *熔点(:,ITER))/总和(熔点(:,ITER)** 2)
XV = XV +α2 * P(:,ITER)
R = R - α2 *熔点(:,ITER)!哪里是*(Cnew - C)? XM = RESHAPE(十五,(/ IG + 5,+ JG 5 /)) !检查收敛
normr =开方(SUM(R(:)** 2))!!!!规范
!fprintf中(标准(R)= G = ITER gNewLine!',normr,ITER + 1);
如果(normr.LT.tol),然后
融合= 1
出口
万一
END DO 如果(normr> TOL),那么
写(*,*)'GCR SOLUTION没有收敛!
融合= 0
万一 返回
END子程序gcr_Mfree下面是几件事情我已经做弄清楚为什么我收到此错误:
我已经通过打印,一步步追踪我的code中的变量的值。我已经达到了这个公式在这里我不能打印值 CnewV 了。
CnewV = CnewV +α2 * R
-
本公式是在由#3错误消息指出mymodule_MOD_gcr_mfree。 为什么错误不表明任何行号?
R是一个载体。 α2是标量。 CnewV在RHS是一个向量。大小和R和CnewV的等级是一样的,只有一个元素的CnewV矢量值的为NaN 。此外,两个或三个元素都印有*****当我试图在屏幕上打印出来。
- 打印矩阵P(格式编号是558),当
,有在阵列p少得多的元件。有在阵列的r221的元素,然而,在P
有96
这是一个复杂的问题。我希望提供的信息足以理解错误。
的问题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)
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
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.
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.
这篇关于是什么"分段错误 - 无效的内存引用错误"在FORTRAN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!