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

allocatable automatic stack heap

Alessandro_D_
新分销商 I
2,847 次查看

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 项奖励
1 解答
Steve_Lionel
名誉分销商 III
2,832 次查看

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.

在原帖中查看解决方案

5 回复数
Alessandro_D_
新分销商 I
2,845 次查看

(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 项奖励
Steve_Lionel
名誉分销商 III
2,833 次查看

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_
新分销商 I
2,807 次查看

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 项奖励
Steve_Lionel
名誉分销商 III
2,789 次查看

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_
新分销商 I
2,781 次查看

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

0 项奖励
回复