Fortran: Passing loop names to subroutines


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.


Science Advisor
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).


Science Advisor
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.


Science Advisor
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.

The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving