- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How do can I write nested DO loops more concisely? If I have (as an example)....
Do i1=1,N1
Do i2=1,N2
Do i3=1,N3
Statement(i)=i2*i3+i1
Do i2=1,N2
Do i3=1,N3
Statement(i)=i2*i3+i1
i=i+1
END
END
END
END
END
END
But sayI actually do not know how many DO loops I want to nest, it might be 3, as above, it might be 10 (though with a different example statement)? How can I code this? (I have heard there are ways in C++, any in Fortran?)
Cheers, T
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you can stay within the rules of FORALL(), you could write this more concisely. If you have something in mind for C++, you could specify it and ask if there is a Fortran equivalent.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Idid solve this problem by using a recursive routine. See the following code example:
Code:
recursive subroutine loop(i1, i2, inc, n) use loop_info ! module with info about array's k1, k2 and kinc (the do-loop information) and nr_loops implicit none integer, intent(in) :: i1, i2, inc integer, intent(in) :: n integer i, m do i = i1, i2, inc if ( n == nr_loops ) then ! here the code to be executed in the inner loop else m = n + 1 call loop(k1(m), k2(m), kinc(m), m) endif enddo end
The first call is :
call loop(k1(1), k2(1), kinc(1), 1)
in which the k1, k2 and kinc contain the do loop start, end and increment starting with the outer loop.
Guus
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here's non-recursive version by Skip Knoble (it will probably come out as "unwound" version ofGuus's recursive one):
Code:
C====================================================================== C C ROUTINE: LOOPNEST FORTRAN C C PURPOSE: To illustrate how to program an indefinite number of loops C C COPYRIGHT (c) H. D. Knoble03/16/88 C The Pennsylvania State University C Center for Academic Computing C hdk@psu.edu C C====================================================================== C INTEGER NumberOfLoops,t,n,z,LoopIndex(10),TripCount(10),demo * Forever,TotalTrips LOGICAL PREVIOUS DATA Forever/999999999/ C DO 100 demo=1,forever WRITE(6,*) 'Please enter the number of loops to be generated:' READ(5,*,END=99) NumberOfLoops WRITE(6,*) 'Please enter #trips for each loop (',NumberOfLoops, * ' positive integers):' READ(5,*,END=99) (TripCount(t),t=1,NumberOfLoops) C C---Algorithm Begin C C Input: NumberOfLoops, and the trip count set: C (TripCount(1), TripCount(2), ... TripCount(NumberOfLoops). C Output: Each index set, one at a time, in lexicographic order. C C--- Initialize. TotalTrips=1 DO 2 t=1,NumberOfLoops TotalTrips=TotalTrips*TripCount(t) LoopIndex(t)=TripCount(t) 2 CONTINUE C--- The two nested coded loops following the comments are equivalent C to an indefinite number "NumberOfLoops" of loops illustrated in C the comments: C do LoopIndex(1)=1 to TripCount(1) C do LoopIndex(2)=1 to TripCount(2) C ... C do LoopIndex(NumberOfLoops) = 1 to TripCount(NumberOfLoops) C WRITE(6,*) (LoopIndex(t),t=1,NumberOfLoops) C enddo; enddo; , ... , enddo C DO 4 z=1,TotalTrips previous=.TRUE. DO 3 t=NumberOfLoops,1,-1 C-------- Generate the index set "LoopIndex" in lexicographic order, if(previous) LoopIndex(t)=1+MOD(LoopIndex(t),TripCount(t)) previous=previous .AND. (LoopIndex(t).EQ.1) 3 CONTINUE C------ and use them (e.g. output) one set at a time. WRITE(6,*) (LoopIndex(t),t=1,NumberOfLoops) 4 CONTINUE C---Algorithm End 100 CONTINUE 99 STOP END
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Cheers for the help and the code!
Helped alot, recursive routines are a bit of a brain tease, took me a while to get the hang of them.
Cheer,T

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page