Re: Automatically transform or expand do loop in a subroutine



On 2008-01-31 00:15:54 -0400, yaqi <yaqiwang@xxxxxxxxx> said:

Hi All,

I knew following piece of code will not be compiled, at least with
Compaq Visual Fortran. My intention of this code is to let the
compiler automatically transform or expand the loop inside the
function (I do not care the code size) if the caller has a SMALL
CONSTANT integer 'd' in its argument list when the compiler is doing
optimization. Because the function is really crucial for the
performance, I want to save computing time as much as I can. So do not
ask me why I want to do this. Actually I guess this idea is similar as
the template in C++.

Are there any ways to implement this idea easily with Fortran?

Thanks!

--------------------------------------------------------------------------
program main

integer :: e, c
e = ad(c, 10)

stop
end program main

integer function ad(c,d)
implicit none
integer :: c
integer, parameter :: d
integer :: i
do i=1,d
c = c + i
end do
ad = c
return
end function

Some compilers have options to unroll loops as part of their stronger
optimization strategies. Read the compiler documentation carefully.
This capability is more likely to be present in commercial compilers that
make a point of providing fast executables. As opposed to being free or
having good debugging. Some compilers will even inline small user functions
as part of their stronger optimization. If optimizing were truly important
for you I would expect that you would have current versions of several
compilers rather than one which has not been supported for several years.

Trying for improved performance by polishing code, whether manually or
by the compiler optimization, is usually a mugs game. The real improvements
come from better algorithms. Those are the 10 or 1000 times faster gains
if they can be done. Polishing is good for 10% gains and makes things so
complicated that you can no longer see the possibilities for better algorithms.
By all means set the optimization switches but put your real effort into
algorithms.

The fastest multiply you will ever do is the one you avoid doing!




.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... what experienced programmers do, ... optimization, ... Thugs" ad nauseum fits that a lot more closely than discussing compiler ... be modified outside a loop, and guessing ...
    (comp.programming)
  • long(!) Re: need help on CFLAGS in /etc/make.conf please
    ... For example, MPlayer sets this high on purpose, so GCC will actually ... and the K&R compiler would've known exactly the kind of optimization we wanted. ... >> A msg from Richard Coleman, taken together with the GCC 3.x Known Bugs ...
    (freebsd-questions)
  • Re: SETF and variable issues in Self Similar program.
    ... The way to get a compiler to optimize a tail-call ... call optimization turned on. ... It changes the semantics of the language in a big way. ... In Common Lisp it has to interact with condition handling, ...
    (comp.lang.lisp)
  • Re: Programming languages
    ... >> execution time, ... That's not a good candidate for optimization. ... if there is comething the compiler ... António> programmers and those can't cope with anything else than ...
    (sci.lang)
  • Re: Debugging and optimizations
    ... and compiler optimizations. ... Earlier in the week I was talking to a developer about optimization ... The problem is in the compiler not the source level debugging format. ... The compilers should be able to provide source level debug information ...
    (comp.programming)