CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   which is a better way to loop in fortran? (http://www.cfd-online.com/Forums/main/11631-better-way-loop-fortran.html)

zonexo June 13, 2006 22:29

which is a better way to loop in fortran?
 
hi,

i've a question regarding looping. I have thought of 2 types of looping but I'm not very sure which is better. Hope some programming expert can enlighten me... I'm using fortran, but I think it should apply to most languages.

For e.g.

case 1:

if x=1 if y=1

... else if y/=1

... end if else if y=1

... else if y/=1

... end if end if

case 2:

if x=1 and y=1 ... else if x=1 and y/=1 ... else if x/=1 and y/=1 ... else if x/=1 and y=1 ... end if

I have similar problem but with more conditions. So is one definitely better than the other? which one then? Thanks!


Tom June 14, 2006 03:35

Re: which is a better way to loop in fortran?
 
Since you appear to be using f90 (i.e. your using /= for .ne.) why not see if you can use the case statement:

select case (expr)

case (1)

.....

case (2)

.....

.....

end select

The other option may be the where/elsewhere structure.

In practice you want to avoid if tests within do loops if you can.

zonexo June 15, 2006 00:27

Re: which is a better way to loop in fortran?
 
tks for your suggestion. does it mean that for my case 2 e.g., it has to go thru 1 by 1 to test for the condition (min 1 time, max 4 times).

however, if i use the case statement, after evaluating the case(expr), it 'll jump immediately to the correct area. is that so or will it also do condition testing?

tks again.


Tom June 15, 2006 03:49

Re: which is a better way to loop in fortran?
 
If set up correctly it should jump to directly to correct piece of code (although in most cases it will need to check against each condition separately - the point of case statements is to make the code easier to read compared to the equilavent nested if tests).

you really need to look at why you're doing this inside of a loop though - it tends to make the code rather inefficient.

buch June 15, 2006 08:38

Re: which is a better way to loop in fortran?
 
Hi,

Using conditions inside loops is not efficient at all. On some architectures it will inhibate many compiling optimizations. Use it only when you have no other choice. It means you might rewrite your code maybe more crudely to explicitely treat some special areas of your computational domain (e.g. boundary conditions), or sometimes use step functions (in fortran, with min and max functions) to avoid IF conditions. Depends on your compiler, architecture, and code requirements ...

Regards


Renato. June 15, 2006 10:15

Re: which is a better way to loop in fortran?
 
I would say that efficiency or inefficiency is a bit relative... how many steps does your loop have? Are you using this loop within an iterative process, i.e., are you calling this loop many times or you only need to use it once?

Of course that branching statements or subroutine callings within do loops are not a good idea but if you only need to use such statements few times... no problem...

Regards

Renato.

zonexo June 23, 2006 04:26

Re: which is a better way to loop in fortran?
 
i've done 2 types of looping:

1. do j=1,10 do i=1,10 if (i>5) then a(i,j)=2*i*j else a(i,j)=10*i*j end if end do end do

2. t(1:5)=1 t(6:10)=0 do j=1,10 do i=1,10 a(i,j)=2*i*j*t(i)+10*i*j*(1-t(i)) end if end do end do

both should give same results, with the 2nd one has no "if" inside the loop. I tried to use the profiler for a large sample size but the results are quite inconisistant, probably due to the caching. But is the second method going to be faster? tks


Tom June 23, 2006 04:47

Re: which is a better way to loop in fortran?
 
Not necessarily. As Renato said it all depends on how many times this bit of code is used (the small value of 10 means that it'll run quickly anyway).

Two better ways of writing your example code are

do j=1,10

do i=1,5

a(i,j)=2*i*j

enddo

do i=6,10

a(i,j)=10*i*j

enddo

enddo

or

do j=1,10

do i=1,5

a(i,j)=2*i*j

a(i+5,j)=10*(i+5)*j

enddo

enddo


All times are GMT -4. The time now is 21:59.