Fortran Fortran: Passing loop names to subroutines

  • Thread starter Thread starter dervish
  • Start date Start date
  • Tags Tags
    Fortran Loop
AI Thread Summary
The discussion revolves around managing nested, recursive subroutines in a Fortran program, specifically the challenge of conditionally exiting all subroutines to return control to the main program. The original poster seeks an efficient method to handle this without adding excessive code for each entry point. Suggestions include using a switch or flag to indicate when to skip nested subroutine calls, and encapsulating data along with its context in an object to facilitate communication between subroutines. The idea is to update a "finished" status within this object to signal when processing is complete, allowing the main subroutine to proceed to the next data point. However, concerns are raised about the complexity of managing recursion depth and ensuring proper exits from each subroutine. Ultimately, while the proposed solutions may increase code length, they provide a structured way to manage control flow without overwhelming complexity.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
4K
Replies
5
Views
8K
Replies
5
Views
3K
Replies
11
Views
2K
Replies
7
Views
2K
Replies
16
Views
6K
Replies
12
Views
3K
Back
Top