Fortran, Open MP, indirect recursion, and limited

2019-07-07 07:34发布

问题:

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.

回答1:

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.