Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: Passing loop names to subroutines

  1. Dec 15, 2009 #1
    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!
     
  2. jcsd
  3. Dec 16, 2009 #2

    minger

    User Avatar
    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.
     
  4. Dec 16, 2009 #3
    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).
     
  5. Dec 17, 2009 #4

    minger

    User Avatar
    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.
     
  6. Dec 17, 2009 #5
    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.
     
  7. Dec 18, 2009 #6

    minger

    User Avatar
    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook