Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Self-nesting subroutines

OP1
New Contributor III
558 Views

Hi,

I am solving a set S1 of nonlinear equations, where some of these nonlinear equations involve the solutions of another set S2 of nonlinear equations.

So, assuming that my main solver subroutine is SOLVE, my program will call SOLVE(S1), which in turn (at some point)will require a call SOLVE(S2).

This looks like a recursive call - except that there are several levels of nested subroutines between the S1 and S2 problems.

Obviously there is a conflict when SOLVE(S2) is called - as its local variables will erase the one needed for the SOLVE(S1) call.

Is there a convenient way to get around this? The obvious choice is to duplicate (with different names) the subroutines involved in my solver; but that certainly does not look very elegant...

Thanks,

Olivier

0 Kudos
2 Replies
reinhold-bader
New Contributor II
558 Views

Hello,

If you put the RECURSIVE prefix on your SOLVE subroutine:

RECURSIVE subroutine solve(...)

...

end subroutine

you might be fine, even if multiple levels of other subprogram calls are interleaved. Without the prefix, your code is non-conforming (although you still might get correct results if the implementation uses a separate local context for each call).

Note however that if you have side effects (e.g. writing to module globals, variables in COMMON or local variables with the SAVE attribute) inside your SOLVE subprogram you still could be in trouble, since writes from different call sites could interfere with one another.

Regards

Reinhold

0 Kudos
OP1
New Contributor III
558 Views

This solved my problem(s): I had to declare explicitly ALL the subroutines of my solver as recursive, and I had to take care of a couple module variables as well.

Thanks Reinhold!

Olivier

0 Kudos
Reply