Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

stack overflow problem

mybiandou
Beginner
692 Views

Hello, I found a strange problem when I use Intel Fortran 

Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.10.0 Build 20230609_000000

The minimal work source code is as the below,

subroutine foo(adim1,bdim1,adim2,bdim2,a,b,abdist)
    implicit none
    integer,parameter::nmax=1000
    integer ,intent(in):: adim1,bdim1,adim2,bdim2
    double precision ,intent(in)::a(adim1,adim2),b(bdim1,bdim2)
    double precision ,intent(out)::abdist(adim2,bdim2)
    double precision :: c(nmax,nmax)   !OK
    !double precision :: c(adim2,bdim2)     !NG
    print*,"adim1,bdim1,adim2,bdim2:",adim1,bdim1,adim2,bdim2
    
    c=0.0
    abdist=0.0

    return
end subroutine
      
      

      
program main
    implicit none
    integer,parameter::nmax=1000,ndim=2
    integer na,nb      
    double precision a(ndim,nmax),b(ndim,nmax)
    integer i
    
    double precision abdist_(nmax,nmax)
    double precision ,allocatable :: abdist(:,:)
    print*,'test:'
    !OK case   
    na = 500 ;  nb = 250    !(1)
    ! NG case
    na = 500 ;  nb = 300    !(2)
    do i=1,na
      a(:,i) = i*1.0
    end do      
    do i=1,nb
      b(:,i) = i*1.0
    end do      
      
    !reshape abdist according to the current size
    abdist = RESHAPE(abdist_,(/na,nb/))
    
    call foo(ndim,ndim,na,nb,a ,b,abdist)      
    print*,"final abdist:",abdist(na,nb)
      
    stop
end 

 

In the subroutine foo, if I declare array c 

double precision :: c(adim2,bdim2) 

 

Then, in my test main program,

when I let na=500;nb=250, it is OK.

But when I let na=500;nb=300, it gave me error.

forrtl: severe (170): Program Exception - stack overflow

 

But if I declare c in subroutine foo as 

double precision :: c(nmax,nmax)  !nmax=1000

it will also be OK.

 

I compile the above code using command

ifort test.f90 -check -traceback  -o test.exe

 

gfortran has no such problems.

 

Please help me to solve this problem. Thank you in advance.

0 Kudos
3 Replies
andrew_4619
Honored Contributor III
662 Views

You are exceeding the stack size. There are compiler options to change the stack size and also the heaparrays option to make certain things be created on heap rather than stack. it gets a bit complicated and is effort! As a principal it is usually better to make larger arrays allocatable and allocate them and they are then always on the heap.   See if that fixes the problem.

 

Steve_Lionel
Honored Contributor III
648 Views

gfortran uses the equivalent of Intel's -heap-arrays option by default.

mybiandou
Beginner
628 Views

Thanks for your answers, andrew_461 and Steve_Lionel.
Following your help, I use -heap-arrays option and get it run ok.

ifort test.f90 -heap-arrays  -check -traceback  -o test.exe

 

0 Kudos
Reply