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

Stack overflow

Svein-Atle_Engeseth
1,370 Views

Hi,

I have a program using an array of user defined type. The total size of the array is about 8MB.
The array does not have allocatable components.

This array is passed to a subroutine as an INOUT argument and I also made a version of the program where the array is global.

In both versions, calling the subroutine causes stack overflow.

When the array has a size of about 6MB  there is no stack overflow.

I have set the Properties in Visual Studio to use heap arrays
and have used both ifort and ifx compilers, latest Windows versions.

Any suggestion to why such a small array causes stack overflow?

Thanks.

Sincerely,

Svein

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,343 Views

The default stack size on Windows is, I think, 10MB. You can set a larger value in the linker property System > Stack Reserve Size. You can also set the compiler option Fortran > Optimization > Heap Arrays > 0 - this will use dynamic allocation instead of the stack for temporary values.

For further reading, see Doctor Fortran in "Don't Blow Your Stack!" - Doctor Fortran (stevelionel.com)

View solution in original post

4 Replies
andrew_4619
Honored Contributor III
1,354 Views

It is maybe pointless to speculate on the many possible causes. You need to follow the code in the debugger and find the code line that causes the overflow. One could then more usefully home in on the actual cause based on what the code is doing at the time of overflow. 

0 Kudos
Steve_Lionel
Honored Contributor III
1,344 Views

The default stack size on Windows is, I think, 10MB. You can set a larger value in the linker property System > Stack Reserve Size. You can also set the compiler option Fortran > Optimization > Heap Arrays > 0 - this will use dynamic allocation instead of the stack for temporary values.

For further reading, see Doctor Fortran in "Don't Blow Your Stack!" - Doctor Fortran (stevelionel.com)

Svein-Atle_Engeseth
1,255 Views

Hi Steve,

Thanks.

I used heap arrays first, but that did not help.

Setting the Stack Reserve Size helped.

Regards,

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,312 Views

@Svein-Atle_Engeseth the Heap Arrays option that @Steve_Lionel suggest should correct your problem.

From your description, it looks like your subroutine is performing an array operation that is requiring a temporary. Example:

A = B + C

 

As opposed to

do i=1,N

  A(i) = B(i) + C(i)

end do

 

Note, in the first case, the compiler might figure out that an array temporary was not necessary, but in other cases it may generate an array temporary. If this be the case, and if your array temporary is too large to fit on stack, then you will get the stack overflow.

 

Increasing the stack size is not necessarily a fix all solution.

 

Jim Dempsey

Reply