我的主代码有一些问题,所以我尝试隔离问题。 因此,我有这个小代码:
MODULE Param
IMPLICIT NONE
integer, parameter :: dr = SELECTED_REAL_KIND(15, 307)
integer :: D =3
integer :: Q=10
integer :: mmo=16
integer :: n=2
integer :: x=80
integer :: y=70
integer :: z=20
integer :: tMax=8
END MODULE Param
module m
contains
subroutine compute(f, r)
USE Param, ONLY: dr, mmo, x, y, z, n
IMPLICIT NONE
real (kind=dr), intent(in) :: f(x,y,z, 0:mmo, n)
real (kind=dr), intent(out) :: r(x, y, z, n)
real (kind=dr) :: fGlob(x,y,z, 0:mmo)
!-------------------------------------------------------------------------
print*, 'We are in compute subroutine'
r= 00.0
fGlob=sum(f,dim=5)
r=sum(f, dim=4)
print*, 'fGlob=', fGlob(1,1,1, 1)
print*, 'f=', f(1,1,1, 0,1)
print*, 'r=', r(1,1,1, 1)
end subroutine compute
end module m
PROGRAM test_prog
USE Param
USE m
Implicit None
integer :: tStep
real (kind=dr), dimension(:,:,:, :,:), allocatable :: f
real (kind=dr), dimension(:,:,:,:), allocatable :: r
!----------------------------------------------------------------------------
! Initialise the parameters.
print*, 'beginning of the test'
! Allocate
allocate(f(x,y,z, 0:mmo,n))
allocate(r(x,y,z, n))
f=1.0_dr
! ---------------------------------------------------------
! Iteration over time
! ---------------------------------------------------------
do tStep = 1, tMax
print *, tStep
call compute(f,r)
f=f+1
print *, 'tStep', tStep
enddo
print*, 'f=', f(1,1,1, 0,1)
print*, 'r=', r(1,1,1, 1)
! Deallacation
deallocate(f)
deallocate(r)
print*, 'End of the test program'
END PROGRAM test_prog
目前,我无法理解为什么当我使用 ifort
进行编译时,我有一个segmentation fault
,当我用 gfortran
编译时它就可以工作了。 。最糟糕的是,当我使用 ifort
进行编译时和gfortran
与他们的fast
选项,我再次得到 segmentation fault (core dumped)
错误。更令人困惑的是,当我还尝试使用两个编译器来使用 traceback
进行编译时选项,一切正常。
我知道segmentation fault (core dumped)
错误通常意味着我尝试在错误的位置读取或写入(矩阵索引等...);但在这里,通过这个小代码,我看不到这样的错误。
有人可以帮助我理解为什么会出现这些错误吗?
最佳答案
问题来自于某些编译器默认使用的堆栈大小 ( ifort
) 或其他一些编译器在优化编译时使用的堆栈大小 ( gfortran -Ofast
)。在这里,我们的书写超出了堆栈的大小。
为了解决这个问题,我使用选项 -heap-arrays
对于 ifort
编译器和-fno-stack-arrays
对于 gfortran
编译器。
关于Fortran 奇怪的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633519/