CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Should I use if-statement in a Do-loop? (https://www.cfd-online.com/Forums/main/1508-should-i-use-if-statement-do-loop.html)

Jongtae Kim November 7, 1999 18:25

Should I use if-statement in a Do-loop?
 
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


Jin Wook LEE November 7, 1999 18:46

Re: Should I use if-statement in a Do-loop?
 
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


Wang linxiang November 7, 1999 19:48

Re: Should I use if-statement in a Do-loop?
 
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

Neyval C. Reis Jr. November 8, 1999 10:14

Re: Should I use if-statement in a Do-loop?
 
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


F. Olawsky November 8, 1999 12:06

Re: Should I use if-statement in a Do-loop?
 
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


clifford bradford November 8, 1999 15:10

Re: Should I use if-statement in a Do-loop?
 
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.


All times are GMT -4. The time now is 10:03.