R. N
R. N

Reputation: 759

Fortran strange segmentation fault

I have some a problem with my main code, so I tried to isolate the problem. Therefore, I have this small code :

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

For now, I am not able to understand why when I compile with ifort, I have a segmentation fault, and it works when I compile with gfortran. And worst, when I compile with both ifort and gfortran with their fast options, I get again a segmentation fault (core dumped) error. And more confusing, when I also tried with both compilers to compile with traceback options, everything works fine.

I know that segmentation fault (core dumped) error usually means that I try to read or write in a wrong location (matrix indices etc...); but here with this small code, I see no mistake like this.

Does anyone can help me to understand why theses errors occur?

Upvotes: 2

Views: 2960

Answers (1)

R. N
R. N

Reputation: 759

The problem comes from the size of the stack used by some compilers by default (ifort) or by some others when they optimise the compilation (gfortran -Ofast). Here, our writings exceed the size of the stack.

To solve this, I use the options -heap-arrays for ifort compiler and -fno-stack-arrays for gfortran compiler.

Upvotes: 3

Related Questions