|
Prev: f2003 or F2003 ?
Next: cant' print the contents of a character*(*) string in gdb that is passed in a fortran subroutine
From: Michael Metcalf on 8 Apr 2008 07:05 "glen herrmannsfeldt" <gah(a)ugcs.caltech.edu> wrote in message news:aZWdndO1lJb9jWbanZ2dnUVZ_remnZ2d(a)comcast.com... > > Hmmm. I like 4*J/9 for integers when needed, but probably would > avoid the unnecessary run time divide with (4./9.)*PI, in the > case that PI was a variable. Yes, that conforms to the rule that expressions in loops are best written in the form constant-invaraiant-variant to make optimization easier. Whether, in this example, PI is a variant or an invariant is your call! Regards, Mike Metcalf
From: James Giles on 8 Apr 2008 07:34 glen herrmannsfeldt wrote: > James Giles wrote: .... >> 4/9 would evaluate to zero, but not PI*4/9. I think writing it as >> 4*PI/9 is better. Again, if you change the declaration of PI to >> double precision, you don't have to change the literals at all. > > Hmmm. I like 4*J/9 for integers when needed, but probably would > avoid the unnecessary run time divide with (4./9.)*PI, in the > case that PI was a variable. For a PARAMETER, it should be a > compile time constant in all cases. But, if PI is not default precision, (4./9.)*PI is not the same as 4*PI/9 is. The advantage of the latter is that it's always the correct precision. Mathematically, 4*PI/9 is the same as (4.0_same_kind_as_PI / 9.0_same_kind_as_PI) * PI anyway - and the compiler is allowed to notice this and optimize accordingly. I don't know how many do. But, it's a very easy optimization. Do we have to spend our entire lives worrying about deficiencies compilers had in the 1960's? Constant folding was indeed something invented in the 1960's (or maybe in the late 1950's). -- J. Giles "I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." -- C. A. R. Hoare
From: glen herrmannsfeldt on 8 Apr 2008 13:25 James Giles wrote: (snip) > But, if PI is not default precision, (4./9.)*PI is not the same > as 4*PI/9 is. The advantage of the latter is that it's always > the correct precision. Mathematically, 4*PI/9 is the same > as (4.0_same_kind_as_PI / 9.0_same_kind_as_PI) * PI > anyway - and the compiler is allowed to notice this and > optimize accordingly. I don't know how many do. It might be that they do if higher levels of optimization are turned on, otherwise maybe not. I was surprised how much difference there was in the generated code for the population count routine (with ishft, iand, and addition) with g95, where it stored and later reloaded all the intermediate values. With -O2 it kept all intermediates in registers. > But, > it's a very easy optimization. Do we have to spend our > entire lives worrying about deficiencies compilers had > in the 1960's? Constant folding was indeed something > invented in the 1960's (or maybe in the late 1950's). Constant folding yes, but commutativity and associativity are different. (Especially on Cray machines where A*B wasn't always equal to B*A, as stories go.) I suppose I agree that compilers should do it, I don't know how many actually do with default optimization levels. -- glen
From: James Giles on 8 Apr 2008 14:38 glen herrmannsfeldt wrote: > James Giles wrote: .... >> But, >> it's a very easy optimization. Do we have to spend our >> entire lives worrying about deficiencies compilers had >> in the 1960's? Constant folding was indeed something >> invented in the 1960's (or maybe in the late 1950's). > > Constant folding yes, but commutativity and associativity > are different. (Especially on Cray machines where > A*B wasn't always equal to B*A, as stories go.) > > I suppose I agree that compilers should do it, I don't know > how many actually do with default optimization levels. OK. So your advice is that we *do* have to worry about things that were resolved in the 1960's. Note that commutativity and associativity have always been *mathematically* correct, so Fortran compilers have always been allowed to use them for optimization. And I know I saw people doing it in the late 1960's. I would recommend that people only worry about it under the following circumstances: 1) you have directly verified that your compiler *isn't* doing constant folding and 2) you can prove the sequence of code in question is a bottleneck of your program. Other than that, legibility and correctness are more important. -- J. Giles "I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies." -- C. A. R. Hoare
From: glen herrmannsfeldt on 8 Apr 2008 15:30 James Giles wrote: (snip) > I would recommend that people only worry about it under the > following circumstances: 1) you have directly verified that > your compiler *isn't* doing constant folding and 2) you can > prove the sequence of code in question is a bottleneck of > your program. Other than that, legibility and correctness are > more important. I think I agree. Those who need the speed should use the compiler optimization options, and the compilers should then do it. I doubt many check the generated code even when it is known to have bottlenecks. -- glen
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: f2003 or F2003 ? Next: cant' print the contents of a character*(*) string in gdb that is passed in a fortran subroutine |