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.?
If the variables are pointers, then the compiler almost certainly used the stack to create a temp for the copy because it has to assume there may be overlap. It might not be simple overlap either, with discontiguous segments, so doing the copy in different orders doesn't always work.
The semantics of Fortran are that the right side of an assignment is completely evaluated before the left is changed. Unless the compiler can prove that there is no overlap (use of ALLOCATABLE
will suffice), it will typically use a stack temp for the copy. Intel Fortran has an option -heap-arrays
that tells it to allocate these temps on the heap, avoiding stack overflow.