Re: Automatically transform or expand do loop in a subroutine



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
.



Relevant Pages

  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
    (comp.lang.c)
  • Re: right sequence of included module files
    ... Figure out what module files are needed, ...   Most people separate the sources of different applications. ... Let compiler automatically find the necessary module ... Intel Fortran Support ...
    (comp.lang.fortran)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...   arrays and loops because it makes for less work. ...
    (rec.pets.dogs.behavior)
  • Re: Dynamic arrays of record/object
    ... For some reason I'm having problems setting the ... lenght of the array and assigning values to the elements of the array ...   public ...
    (comp.lang.pascal.delphi.misc)