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

-heap-arrays flag - does it work?

Mark_Mackey
Beginner
3,052 Views

Hi all.

If I compile the following program with

ifort -m32 -heap-arrays test.f

the array in mytest2() is allocated on the heap, but the one in mytest() is not. According to the documentation, the -heap-arrays flag should put all automatic arrays on the heap. Am I misunderstanding the documentation? Is there a way to force the array in mytest to be allocated on the heap instead of on the stack or in static data?

Regards,

Mark

        program test
        implicit none
        integer i,mytest,mytest2

        i=mytest()
        i=mytest2(100000)

        stop
        end

        function mytest
        implicit none
        integer mytest
        automatic bigarray
        integer bigarray(100000)
        integer i,j

        do i=1,100000
          bigarray(i)=i
        enddo
        do i=1,100000
          j=j+bigarray(i)
        enddo
        mytest=j
        
        return
        end

        function mytest2(isize)
        implicit none
        integer mytest2,isize
        integer bigarray(isize)
        integer i,j

        do i=1,100000
          bigarray(i)=i
        enddo
        do i=1,100000
          j=j+bigarray(i)
        enddo
        mytest2=j
        
        return
        end

4 Replies
Steven_L_Intel1
Employee
3,052 Views

An interesting question.

The array in mytest is not an automatic array - not by the definition of the language. The AUTOMATIC statement you used is an extension and effectively means "non-static".  I do see that, when compiled without optimization, this array is indeed put on the stack, even with -heap-arrays. With optimization, the compiler is able to compute the function value without creating the array at all.

The idea of -heap-arrays was to control allocation of temporaries and "automatic arrays" in the Fortran standard sense of local arrays whose bounds are determined by run-time expressions. I admit that the documentation isn't terribly clear on this and will discuss it with the development team.

Steven_L_Intel1
Employee
3,052 Views

I looked closer at the documentation and it does explain this behavior:

In Fortran, an automatic array gets its size from a run-time expression. For example:

RECURSIVE SUBROUTINE F( N )
INTEGER :: N
REAL :: X ( N )     ! an automatic array
REAL :: Y ( 1000 )  ! an explicit-shape local array on the stack 

Array X in the example above is affected by the heap-array option; array Y is not.

 

0 Kudos
Mark_Mackey
Beginner
3,052 Views

Ah, OK - I was assuming that the "automatic" keyword made an array automatic :).

So there is no way currently to ask that explicit-shape local arrays be allocated on the heap without doing it manually?

0 Kudos
Steven_L_Intel1
Employee
3,052 Views

Correct. You would make the array ALLOCATABLE and allocate it. This is better than depending on compiler switches, anyway.

0 Kudos
Reply