4

There are many responses on other posts related to the issue of stack space, OpenMP, and how to deal with it. However, I could not find information to truly understand why OpenMP adjusts the compiler options:

What is the reasoning behind why -fopenmp in gfortran implies -frecursive?

The documentation says:

Allow indirect recursion by forcing all local arrays to be allocated on the stack

However, I don't have the context to understand this. Why would parallelization require indirect recursion?

Why would parallelization want all local arrays to be on the stack?

I wish to understand so I know the consequences of overriding these options, say, with -fmax-stack-var-size=n, to avoid issues with stack overflows.

francescalus
  • 30,576
  • 16
  • 61
  • 96
midnightGreen
  • 130
  • 1
  • 8
  • take a look at [this question](https://stackoverflow.com/questions/6605677) – Rodrigo Rodrigues Nov 27 '18 at 05:35
  • 1
    "_Why would parallelization want all local arrays to be on the stack?_" By using stack-arrays, it automatically makes them thread safe, in threaded regions where each thread has its own stack, also usually apart enough to avoid locks from false sharing. – Rodrigo Rodrigues Nov 27 '18 at 07:01

1 Answers1

4

Without -frecursive, the compiler will put local variables exceeding the limit -fmax-stack-var-size= in static memory instead of the stack. That is, they will behave as if they have the SAVE attribute, and they are shared among all the threads. These semantics are non-sensical for a multi-threaded program, hence -fopenmp implies -frecursive.

Due to the increasing prevalence of multi-threaded programs, and because F2018 specifies that procedures are recursive by default, this behavior will change in a future release of GFortran, most likely by switching to heap allocation when exceeding the size limit for stack variables instead of using static memory. But for now, this is not an option.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • OK so the key is that each thread has its own stack space, where it keeps its own copy of local variables. Whereas in static memory, all threads share the same variable so it is not safe. Can you also help me understand why they even mention 'indirect recursion' in addition to the information about variable allocation? – midnightGreen Nov 27 '18 at 15:36
  • @midnightGreen Hmm, not sure, but I guess it's due to many codes using indirect recursion to get around the restriction that recursion is verboten (since it's often impossible for the compiler to diagnose indirect recursion, but relatively straightforward to notice direct recursion and generate an error message). – janneb Nov 27 '18 at 18:17