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

Should I use if-statement in a Do-loop?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 7, 1999, 19:25
Default Should I use if-statement in a Do-loop?
  #1
Jongtae Kim
Guest
 
Posts: n/a
Dear CFDers, I have a question related to CPU time or clocks. In a DO-loop I have two choices. The fist one is a multiplication of two real variables The other is an if-statement to compare two integer variables. What that means is if two integer variables are same then two real variables is mulitiplied and vise versa. but if if-statement is cheaper than real-multiplication on cache-based CPUs, real-multiplication can be conducted selectively. In my experience if-statement is more expensive than one time of real-multiplication. I want to know the correct comparison between them. Of course I would like to hear about your experiences related to the if-statements on cache or vector CPUs. Sincerely yours, Jongtae Kim

  Reply With Quote

Old   November 7, 1999, 19:46
Default Re: Should I use if-statement in a Do-loop?
  #2
Jin Wook LEE
Guest
 
Posts: n/a
Deat CFD friend in my country

I would like to comment only vector calculation(with CRAY2S). Simply speaking, 'if-statement in the DO-loop' obstructs vectorization of the program. I have had an experience to perform my calculation by only 20% CPU time(80% saving) after removing one 'if statement' in the DO-loop.

Sincerely, Jinwook

  Reply With Quote

Old   November 7, 1999, 20:48
Default Re: Should I use if-statement in a Do-loop?
  #3
Wang linxiang
Guest
 
Posts: n/a
Dear Jongtae Kim

I want to show you my opinion about the CPU time difference between If-statement and multiplication. as to my knowledge, there is a queue of instructions store in a register in CPU, and the instructions in the queue is executed one by one in its stored order for the sake of minimizing CPU time. But, if a If-statement occured in the queue, all following instructions will have to be cleared and re-loaded because the next instruction should be choosed according to the results of the if-statment. So, i think whenever the if-statements occured, the CPU will have to paused to load the resulted instruction according the if -statement. This limitation is caused by PC instruction, not advance computer languages, there are no way to avoid this obstacle, the only method you can avoid this pause is try your best to code you program without if-statements.

the quantitative compare about multiplication and if-statements is very difficult and i have no ability to contribute this problem.

Wang linxiang
  Reply With Quote

Old   November 8, 1999, 11:14
Default Re: Should I use if-statement in a Do-loop?
  #4
Neyval C. Reis Jr.
Guest
 
Posts: n/a
Hi,

Modern CPUs do have a lot of advances on the procedures used to execute a list of instructions, one of this advances is the branch predition. In this case, the CPU 'grabs' a chunk of intructions and executes it. For instance, 4 lines of instructions can be executed at the same time, because a super-scalar processor has 4 or more execution pipelines.

Obviously, these lines of instructions can be interdependant, and if this is the case, the CPU will have to try to 'guess' the result of the preceding instruction, i.e., if you do have an if-statement, the next line will be executed anyway, if the if-statement is true the result will be used, if the if-statement is false the result will be dumped. This seems to be stupid but, as Wang Linxiang said, the processing will have to stop, evaluate the if-statement and then proceed, which is a terrebly slow procedure. And of course, there are the parallelization issues.

In summary, I think the use of the if-statement is not necessary because the branch prediction routines of the processor will do the job.

I hope, this answers your question.

Cheers.

Neyval

  Reply With Quote

Old   November 8, 1999, 13:06
Default Re: Should I use if-statement in a Do-loop?
  #5
F. Olawsky
Guest
 
Posts: n/a
Hi!

Jinwook wrote: 'Simply speaking, 'if-statement in the DO-loop' obstructs vectorization of the program.'

That's not correct. Some compiler can 'vectorize' if-statements in a DO-loop. For example:

do i=1,1000 if (...) a(i)=b(i)+c(i) enddo

The computer will calculate d(i)=b(i)+c(i) for all i and then it will decide, which results d(i) are necessary. Of course, there are more floating points operations to do.

But I try to avoid such situations! It's better not to use if-statements in DO-loops.

Sincerely, F. Olawsky

  Reply With Quote

Old   November 8, 1999, 16:10
Default Re: Should I use if-statement in a Do-loop?
  #6
clifford bradford
Guest
 
Posts: n/a
Jong-Tae, i've always found it good to reduce the number of ifs in my codes and particularly in loops. the amount of slow down if they're there is variable depending on the compiler,compiler options and the computer you use but it would seem to me that reducing ifs would keep your cpu working fast and maximise the use of your cache. i've even found that writing longer repetitive code to avoid ifs (or cases) speeds up the simple codes i write.
  Reply With Quote

Reply


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 03:09
[CAD formats] my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30
for loop inside a cell_loop? MHDWill FLUENT 0 September 26, 2007 22:24
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 16:57
CFX TASCflow - while-endwhile loop ina macro Deepak Ganga CFX 0 September 6, 2003 23:21


All times are GMT -4. The time now is 08:30.