9

What is the gfortran flag equivalent for intel ifort's

-heap-arrays [size]
JohnE
  • 29,156
  • 8
  • 79
  • 109
user189035
  • 5,589
  • 13
  • 52
  • 112

2 Answers2

8

I've found this:

-fmax-stack-var-size=n This option specifies the size in bytes of the largest array that will be put on the stack; if the size is exceeded static memory is used (except in procedures marked as RECURSIVE). Use the option -frecursive to allow for recursive procedures which do not have a RECURSIVE attribute or for parallel programs. Use -fno-automatic to never use the stack. This option currently only affects local arrays declared with constant bounds, and may not apply to all character variables. Future versions of GNU Fortran may improve this behavior.

The default value for n is 32768.

from gfortran's website. I think it'll do the trick.

user189035
  • 5,589
  • 13
  • 52
  • 112
7

This is an old question, but the accepted answer is not fully correct and I would like to add context for future users like me who come across the post looking for answers.

I believe both intel's ifort and gcc's gfortran have some byte limit where arrays above said limit are not allocated on the stack, but instead are in static memory.

Intel's: -heap-arrays [size], will put any array bigger than [size] kilobytes on the heap, instead of in static memory or on the stack depending on the size.

Gcc does not have this option and instead only has -fmax-stack-var-size=n, where any variable above n bytes is not placed on the stack. The documentation (https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html) says:

if the size is exceeded static memory is used (except in procedures marked as RECURSIVE).

The key difference here is that these large variables are NOT guaranteed to be placed on the heap.

Therefore the two options from intel and gcc are not identical, and more care needs to be taken to ensure large arrays in gfortran are not shared in static memory.

midnightGreen
  • 130
  • 1
  • 8
  • 1
    Thanks a lot @midnightGreen. I have not used fortran in many years so I cannot vouch/add to your answer, but I accept it in order to attract the scrutiny of future readers to it, so they can upvote/evaluate it. – user189035 Nov 28 '18 at 20:46
  • Note that all procedures will be recursive by default in Fortran 2018 and new versions of GCC will reflect this. Also, using `-fopenmp` makes all procedures recursive. – Vladimir F Героям слава Nov 28 '18 at 21:01
  • @VladimirF Do you know if making all procedures recursive by default will mean more variables on the heap? I have been having issues with `-fopenmp` and blowing through stack memory. My solution has been to add back in `-fmax-stack-var-size=32768` and use `threadprivate` and `private` variables to ensure they are not shared – midnightGreen Nov 28 '18 at 21:09
  • Yes, there should be more usage of heap instead of the static memory with recursive procedures. One of the gfortran developers answered your question with this content yesterday https://stackoverflow.com/questions/53493043/fortran-open-mp-indirect-recursion-and-limited-stack-memory/53495067#53495067 (I did not realize that before my first comment) I am not exactly sure what happens currently, janneb will know better. – Vladimir F Героям слава Nov 28 '18 at 21:15
  • @VladimirF haha, thanks yes now I see again he explained that: `most likely by switching to heap allocation when exceeding the size limit for stack variables instead of using static memory`. I missed that before. – midnightGreen Nov 28 '18 at 21:18
  • The ifort option to set a threshold for switching from stack to heap is disrecommended by Steve Lionel, the foremost expert on the question. I believe it takes effect only where the size of the allocation is known at compile time, with the effect that heap-arrays is disabled in the usual case. With respect to the discussion of RECURSIVE, that is expected to become the default, but is recommended to be used always in preference to equivalent compiler options for both ifort and gfortran if there is any chance of it being required. – tim18 Nov 29 '18 at 11:25