Fortran: Passing loop names to subroutines

  • Context: Fortran 
  • Thread starter Thread starter dervish
  • Start date Start date
  • Tags Tags
    Fortran Loop
Click For Summary

Discussion Overview

The discussion revolves around the challenges of managing nested, recursive subroutines in Fortran, specifically regarding the conditional exit from multiple layers of subroutines and the passing of loop information from the main program to these subroutines.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests adding a switch to the main subroutine call to indicate when a data point is done being manipulated, potentially skipping nested subroutine calls.
  • Another proposes creating a data object that includes both the data and index information, allowing for a return to the main subroutine with the next data point's command.
  • A participant expresses uncertainty about the feasibility of using switches to exit multiple layers of nested subroutines, noting a lack of information on how to achieve this in Fortran.
  • One contributor argues against trying to exit all subroutines, suggesting instead to pass data within an object that tracks processing status and location.
  • Another participant acknowledges the need for additional checks for a 'finished' switch when calling the original subroutine again, indicating a potential increase in code complexity.
  • One suggestion is to run the program until data is exhausted, which may not be best practice but could simplify the process.

Areas of Agreement / Disagreement

Participants express various approaches to the problem, with no consensus on a single solution. Disagreement exists regarding the best method to manage exits from nested subroutines and the handling of data passing.

Contextual Notes

Participants note limitations in existing methods for managing nested subroutine exits and the complexity introduced by recursion depth, which may affect the implementation of proposed solutions.

dervish
Messages
4
Reaction score
0
Hello,

A Fortran program I'm working on has a few nested, recursive subroutines, with many entry points between each one (there are lots of places in the first subroutine where calls to the 2nd depth subroutine are made, etc.).

I'm looking for a way to conditionally exit all subroutines and return to the main program (and then cycle some named loop in this program). An ideal way to do this would be to pass the outer loop information from the main program into the subroutines.

Is this possible?

The only other way I can think of doing this would be to conditionally exit each nested subroutine one at a time, however this would add hundreds of lines of code (checking after each entry point to a deeper subroutine whether the exit condition had been reached).

Any ideas would be really appreaciated.
Thanks!
 
Technology news on Phys.org
One your main subroutine call from the main program, could you add a switch or something that tells it for example, that the current data point is done being manipulated. At that point, it skips over the nested subroutine calls.

Or perhaps you add another subroutine which when called sends the information back to the original subroutine with information regarding the next data point. i.e. Let's say you have an array, ni x nj x nk big that your looping over. You grab a points data and proceed through your nested recursive subroutines, where your doing something.

Now, instead of just sending that points' data, put it in a data object along with the index information, i,j,k. Then, you can call back to the main subroutine which the resultant information, along with the command to start the next point.

Or...maybe I'm misunderstanding you completely.
 
Thanks for the quick reply!

I'm not sure if switches can work like that in Fortran. So far I haven't been able to find any way to exit out of more than one layer of nested subroutines at a time - if this could be done it would be ideal (a conditional exit from all nested subroutines would be sufficient for what I'm trying to do).
 
I'm pretty well verse in Fortran. What I'm saying is that not necessarily try to exit out of all the subroutines. That seems too hard; there's no information on where you're at, or how deep you are.

Instead, pass the data from subroutine to subroutine not by itself, but inside an object. The object then will contain all the information regarding not only the information, but "where" you're at. Then, when you're done processing the data, call the original program, or subroutine or whatever. After the data has been processed change a "finished" switch in the object to TRUE. Then, when you call the original subroutine again, it knows to go to either the next point, or continue with the rest of the program.
 
Thanks minger, that's probably what i'll have to do. The only problem is that calling the original subroutine will increment the recursion depth again, eventually i'll need to exit from each at least once (involving quite a few checks for the 'finished' switch).

Still, that's not really a problem, it just means a few more lines of code.
 
Or you just run until you run out of data, hit and end of file and "bomb" out the program. Not best practice, but it might be the easiest.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 16 ·
Replies
16
Views
6K
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K