Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Different behaviour for allocatable and automatic arrays

wayne_lewis
Beginner
414 Views
I am noticing different behaviour when I create a large array in two different ways.

If I use the allocatable method:

[cpp]subroutine array_function(array_size)

  implicit none

  integer :: array_size
  integer, allocatable :: array(:)

allocate(array(array_size))
...

end subroutine

[/cpp]
and call this with

[cpp]array_function(300000)[/cpp]

everything works fine.

If I define the function as:

[cpp]subroutine array_function(array_size)

  implicit none

  integer :: array_size
  integer :: array(array_size)
  ...

end subroutine[/cpp]

and call it with

[cpp]array_function(300000)[/cpp]

I get the following error at run time:

forrtl: severe (170): Program Exception - stack overflow
Image PC Routine Line Source
array_test.exe 00447897 Unknown Unknown Unknown
array_test.exe 0040116E Unknown Unknown Unknown
array_test.exe 004637C3 Unknown Unknown Unknown
array_test.exe 004527CE Unknown Unknown Unknown
kernel32.dll 7C816FD7 Unknown Unknown Unknown

I am using Intel Visual Fortran Compiler IA-32, Version 11.0, Build 20090318.

I am launching the compiler from a Cygwin terminal session, using the following command:

/usr/local/bin/ifort -c /QaxSSE4.1 /MT /fpp /Qvec-report0 /check:bounds /warn:all array_test.f90 -o
array_test.obj

Note that the file contains the function described above and a small test program to call the function.
0 Kudos
3 Replies
thomas_boehme
New Contributor II
414 Views
Quoting - wayne.lewis
I am noticing different behaviour when I create a large array in two different ways.

If I use the allocatable method:

[cpp]subroutine array_function(array_size)

  implicit none

  integer :: array_size
  integer, allocatable :: array(:)

allocate(array(array_size))
...

end subroutine

[/cpp]
and call this with

[cpp]array_function(300000)[/cpp]

everything works fine.

If I define the function as:

[cpp]subroutine array_function(array_size)

  implicit none

  integer :: array_size
  integer :: array(array_size)
  ...

end subroutine[/cpp]

and call it with

[cpp]array_function(300000)[/cpp]

I get the following error at run time:

forrtl: severe (170): Program Exception - stack overflow
Image PC Routine Line Source
array_test.exe 00447897 Unknown Unknown Unknown
array_test.exe 0040116E Unknown Unknown Unknown
array_test.exe 004637C3 Unknown Unknown Unknown
array_test.exe 004527CE Unknown Unknown Unknown
kernel32.dll 7C816FD7 Unknown Unknown Unknown

I am using Intel Visual Fortran Compiler IA-32, Version 11.0, Build 20090318.

I am launching the compiler from a Cygwin terminal session, using the following command:

/usr/local/bin/ifort -c /QaxSSE4.1 /MT /fpp /Qvec-report0 /check:bounds /warn:all array_test.f90 -o
array_test.obj

Note that the file contains the function described above and a small test program to call the function.


By default, automatic arrays are allocated on the stack, whereas the one you explicitly allocate are not. The stack size is not large enough to hold your 300000 element array.

You could either increase your stack size (don't remember the setting right now), or you could tell the compiler to allocate large temporary arrays on the heap rather than on the stack. For that, set the option Fortran->Optimization->Heap Arrays.

Best regards,
Thomas
0 Kudos
reinhold-bader
New Contributor II
414 Views
Hello,

in contrast to allocatable variables, which are allocated on the heap, automatic variables are taken from the stack. Apparently your default stack size is insufficient; to get around this problem, you can either use the /F option - in your case /F1200000 for 300000 * (size of integer) bytes - or specify /heap-arrays, which would enforce using the heap for automatic arrays as well.

Regards
Reinhold
0 Kudos
TimP
Honored Contributor III
414 Views
Quoting - reinhold-bader

in contrast to allocatable variables, which are allocated on the heap, automatic variables are taken from the stack. Apparently your default stack size is insufficient; to get around this problem, you can either use the /F option - in your case /F1200000 for 300000 * (size of integer) bytes - or specify /heap-arrays, which would enforce using the heap for automatic arrays as well.


These are among the arguments in favor of ALLOCATABLE rather than automatic array, taking advantage of the STAT and ERRMSG options to check for and inform about the location of any failure.
0 Kudos
Reply