Dear all,
I have a question of how to heap arrays or increase stack size in gfortran in the best way.
I know Intel Fortran has -heap-arrays
option to heap stuff on heap. So no worry about stack size any more. My personal experience is that, if we compile a program with multiple f90 or f files, this -heap-arrays
should only be apply on those files which really need to heap arrays. For files which do not need this flag, just do not add this flag.
Anyway, now for gfortran, I wanted to know what flags can best mimic Intelâs -heap-arrays
. After some searching, from a warning from my code by gfortran, I found that frecursive
seems kind of similar with Intelâs -heap-arrays
, it says,
Consider increasing the â-fmax-stack-var-size=â limit (or use â-frecursiveâ, which implies unlimited â-fmax-stack-var-sizeâ) - or change the code to use an ALLOCATABLE array. If the variable is never accessed concurrently, this warning can be ignored, and the variable could also be declared with the SAVE attribute. [-Wsurprising]
see it seems either -fmax-stack-var-size=xxx
and -frecursiveâ
should be fine.
Have anyone used -frecursiveâ
and is it good?
I am asking this because, like, I define a random number generator function to generate a size(n) random number array, like
function rand(n)
integer :: n
real :: rand(n)
some stuff
return
end function rand
So I use it even for just 1 element random number, so rand(1). I mean I can define rand
as allocatable array, but then if I call the function frequently, the frequent allocate and deallocate may cause some performance issues perhaps. So I just define it is rand(n)
, however if n
is big it will casue stackoverflow. So for the file contain this function and those will use this function, it seems I need to use -heap-arrays
for those files.
On the other hand, I remember at least both @certik and @shahmoradi recommended
-unlimit -s
But, uhm, how to use this -unlimit -s
?
Like, do I put this as a flag somewhere at the Fortran linking stage? Or, just type it in the terminal? Is it possible to apply -unlimit -s
to my code only? So that the OS stack limit is still its default value.
Thank you very much in advance!