CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

which is a better way to loop in fortran?

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 13, 2006, 22:29
Default which is a better way to loop in fortran?
  #1
zonexo
Guest
 
Posts: n/a
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!

  Reply With Quote

Old   June 14, 2006, 03:35
Default Re: which is a better way to loop in fortran?
  #2
Tom
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   June 15, 2006, 00:27
Default Re: which is a better way to loop in fortran?
  #3
zonexo
Guest
 
Posts: n/a
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.

  Reply With Quote

Old   June 15, 2006, 03:49
Default Re: which is a better way to loop in fortran?
  #4
Tom
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   June 15, 2006, 08:38
Default Re: which is a better way to loop in fortran?
  #5
buch
Guest
 
Posts: n/a
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

  Reply With Quote

Old   June 15, 2006, 10:15
Default Re: which is a better way to loop in fortran?
  #6
Renato.
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   June 23, 2006, 04:26
Default Re: which is a better way to loop in fortran?
  #7
zonexo
Guest
 
Posts: n/a
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

  Reply With Quote

Old   June 23, 2006, 04:47
Default Re: which is a better way to loop in fortran?
  #8
Tom
Guest
 
Posts: n/a
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
  Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Gmsh] Problem with Gmsh nishant_hull OpenFOAM Meshing & Mesh Conversion 23 August 5, 2015 02:09
[CAD formats] my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 01:30
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 15:57
Re: which is a better way to loop in fortran? zonexo Main CFD Forum 3 June 25, 2006 15:19
Big loop problem of fortran Wen Long Main CFD Forum 9 April 1, 2004 12:38


All times are GMT -4. The time now is 23:09.