Re: Automatically transform or expand do loop in a subroutine
- From: Arjen Markus <arjen.markus@xxxxxxxxxx>
- Date: Thu, 31 Jan 2008 00:02:40 -0800 (PST)
On 31 jan, 05:15, yaqi <yaqiw...@xxxxxxxxx> wrote:
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
An alternative might be to store the results as you go along
and return a previous result if that is available. The process
is called memoisation (or memoization, depending on your
flavour of spelling). In some languages this can be automated,
but not in Fortran.
However, the process is fairly mechanical:
integer function f(c,d)
integer :: c, d
integer, save, dimension(100,100) :: saved_results
logical, save :: init = .false.
if ( .not. init ) then
init = .true.
saved_results = -999
endif
if ( saved_results(c,d) /= -999 ) then
f = saved_results(c,d)
else
!
! Compute the value
!
saved_results(c,d) = f
endif
end function
Some remarks:
- The array saved_results should be large enough for the
typical values of c and d (or be allocatable to accommodate
whatever values you need)
- The array must be initialised to some value that will not
be a valid outcome of the function (in the above: -999)
or you need to keep track of previously stored values
by for instance a logical array.
- I chose a simple array to store the results, but other
storage schemes are possible too. Arrays will give the
best performance (or at least I can not think of
better ones) but they may be memory-hungry.
- This method works for any function of discrete variables,
if you have real arguments, you will have more trouble
(but it would be less likely also that you have previous
results)
Regards,
Arjen
.
- References:
- Prev by Date: Re: Automatically transform or expand do loop in a subroutine
- Next by Date: Re: Automatically transform or expand do loop in a subroutine
- Previous by thread: Re: Automatically transform or expand do loop in a subroutine
- Next by thread: Re: Automatically transform or expand do loop in a subroutine
- Index(es):
Relevant Pages
|
|