If I am doing the following copy in Fortran
arr(i1:i2) = u(1:n)
where SIZE(arr(i1:i2)) == SIZE(u(1:n))
and the size is relative big, e.g 2M elements of double precision.
If variable arr
is a pointer alias to another allocatable array. Would Fortran use the stack or heap memory to handle the copy assignment.?
If it is using the stack, is there any specific reason for that choice.?
How could one possibly avoid the compiler using the stack to not get a stackoverflow, without having to run ulimit -s unlimited
on the Linux terminal.?
如果变量是指针,则编译器几乎肯定会使用堆栈为副本创建一个临时文件,因为它必须假定可能存在重叠。对于不连续的段,也可能不是简单的重叠,因此按不同顺序进行复制并非总是可行。
Fortran的语义是赋值的右侧在更改左侧之前已完全评估。除非编译器能证明没有重叠(使用ALLOCATABLE
就足够了),否则通常将使用堆栈临时进行复制。英特尔®Fortran有一个选项-heap-arrays
,可以告诉它在堆上分配这些临时文件,从而避免堆栈溢出。