How to prevent using /heap-arrays without stack overflow?

Dear all,

I have been puzzled by the heap-arrays flag in intel Fortran for some years. Like below,

I usually always set heap arrays size as 0, so always heap arrays, so that I can prevent stack overflow issue without change the code, and without too much thinking.

But recently, I more and more feel that there is something I am missing. Because sometime my code with or without this /heap-arrays0 flag, speed will change a lot.

As @dwwork points out in below thread
https://fortran-lang.discourse.group/t/why-stack-is-faster-than-heap-and-what-exactly-is-stack/2130/3?u=crquantum

that,

So perhaps no need to use this heap-array flag, just leave it blank as system default. Because enabling heap-arrays sometimes will cause some strange performance loss issue.

But then my question is, do I then have to put all the arrays allocable to prevent stack overflow?

Like, what do you guys do, if you do not use heap-arrays0 flag, and also want to prevent stack overflow?

Do you just put all the arrays as allocable?

If so, will you have a lot of allocate and deallocate stuff in your code?
Will that be very cumbersome and also slow down your code a lot? Especially if there are many allocate and deallocate in a big loop.

Thanks a lot in advance!

1 Like

As @mecej4 already mentioned, this really depends on the usage pattern. If your code has large automatic arrays (let’s say, more than 10,000 elements per array, but that is just simple figure that illustrates the size you can think of), then it pays off to consider using the /heap-arrays option. On the other hand, what you can also do is allocate such arrays explicitly and have them have the SAVE attribute. Then you only need to allocate them once, on the first call. Check that the given size is adequate on the next call though or trust automatic reallocation (that is what it is there for).
It is simply impossible to give general and precise guidelines. The only thing I can think of: do not use this option (other compilers may not have something similar!) and if you run into stack overflows try to solve them by manually managing the arrays.
Here the adagium visualised by xkcd comes into play: 1691: Optimization - explain xkcd

2 Likes