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

allocatable automatic stack heap

Alessandro_D_
New Contributor I
947 Views

Dear users, 

I would like to sort an array in ascending order and then sort another array according to the first. To clarify the problem, I post a simple example below. The problem is that if I need a large array (say n>=140000 in my case), I get a stack-overflow error at the line "arr2 = arr2(ind1)". Following a previous suggestion of Steve, I set the option Heap Array equal to 0 but I still get the same error. Of course I could replace the line "arr2 = arr2(ind1)" with a do-loop but I would like to avoid that.

Any comments or suggestions are welcome.

Code:

program main

use toolbox, only: sort
implicit none

integer :: n, i
real(8), allocatable :: arr1(:), arr2(:)
integer, allocatable :: ind1(:)

write(*,*) "Enter integer n, num. of elements of vectors"
read(*,*) n

allocate(arr1(n), arr2(n),ind1(n))

!arr1 = [12.0, 3.0, 14.0]
!arr2 = [10.0, 2.0, 5.0]

CALL RANDOM_NUMBER(arr1)
CALL RANDOM_NUMBER(arr2)

call sort(arr1,ind1)
arr2 = arr2(ind1)

deallocate(arr1,arr2,ind1)

end program main

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
932 Views

I took your code and made it executable. I determined that without /heap-arrays, I got a stack overflow, but with it, the program ran to completion. Please do a rebuild of the project with that setting. If the problem persists, put the buildlog.htm into a Zip file and attach it here.

View solution in original post

5 Replies
Alessandro_D_
New Contributor I
945 Views

(I do not include the file for the toolbox that contains the subroutine sort since it is very long and not really relevant for the problem)

0 Kudos
Steve_Lionel
Honored Contributor III
933 Views

I took your code and made it executable. I determined that without /heap-arrays, I got a stack overflow, but with it, the program ran to completion. Please do a rebuild of the project with that setting. If the problem persists, put the buildlog.htm into a Zip file and attach it here.

Alessandro_D_
New Contributor I
907 Views

Hi Steve,

thanks for your answer.  I managed to run the program successfully by setting /heap-arrays0 (which is equivalent to /heap-arrays, right?). I wonder why /heap-arrays0 is not the default option. Is there any performance penalty if you store all arrays on the heap?

0 Kudos
Steve_Lionel
Honored Contributor III
889 Views

Yes, /heap-arrays0 is the same as /heap-arrays. There is a small performance penalty, which is why it is not the default, though other compilers I know do make this the default. It matters only if array temps are created frequently in the program, but the alternative is that the program doesn't run at all.

Intel's attitude is that its compilers are for maximum performance, and a default that slows down benchmarks even a bit is frowned upon. This is not absolute - the Intel compiler did change the "realloc_lhs" default to match the standard after competing compilers made the same change.

Are you saying that putting in "0" in the property page for Heap Arrays didn't work?

Alessandro_D_
New Contributor I
881 Views

It did work, thanks! I also compiled it from the command line with either /heap-arrays or /heap-arrays0 and it works well.

0 Kudos
Reply