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

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

 November 7, 1999, 19:25 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

 November 7, 1999, 19:46 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

 November 7, 1999, 20:48 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

 November 8, 1999, 11:14 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

 November 8, 1999, 13:06 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

 November 8, 1999, 16:10 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.

 Thread Tools Display Modes Linear Mode

 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 OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 18 April 22, 2015 08:43 rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30 MHDWill FLUENT 0 September 26, 2007 21:24 Franny Main CFD Forum 13 July 7, 2007 15:57 Deepak Ganga CFX 0 September 6, 2003 22:21

All times are GMT -4. The time now is 17:41.