Re: Speed penalty for using allocatable arrays?



"Leading dimension" means dimension 1, I assume. Is this an x86
problem or a general problem? It's rather disturbing, in any case, if
an innocent size choice along the first dimension can cause a
significant performance hit. What, rather than a 16-row array, should
a 17-row array be specified, and one row not used?

One would hope that this issue had been addressed by those writing
Fortran compilers, rather than having being left those writing Fortran
applications. It should not be necessary to master such arcana to be
able to write efficient Fortran code.

Kay Diederichs wrote:
jomarbueyes@xxxxxxxxxxx wrote:
Dear Group,

I am in the process of converting a collection of Fortran 77 programs
into
Fortran 90/95. One of the reasons for converting is the dynamic memory
allocation available in Fortran 90/95. However, I noticed that the
version
of the program that allocates the large arrays we use runs at a much
slower speed compared to the version of the same program that uses
static allocation. More precisely, the arrays are allocated once at the
begining of the program and then they are used in an iterative
algorithm.
With the Irix MIPSpro compiler version 7.4, each iteration slows down
by a factor of approx. one third when allocating the large arrays
(compared to statically allocating them). With the Absoft Fortran 90
compiler (Pro-Fortran Version 8) on G4 and G5 Macintosh computers
(Mac OS 10.3.9), each iteraion of the program that allocates the large
arrays runs at about half the speed of the static-memory version.

Is this a known "feature" of Fortran 90/95? Is there a solution to this
problem other than going back to static memory allocation?

Feedback to this newsgroup and/or e-mail to jomarBueyes@xxxxxxxxxxx
will be greatly appreciated.

Jomar
(jomarBueyes@xxxxxxxxxxx)


Jomar,

if you use two-dimensional (or higher) arrays then the leading dimension
should not be a power of 2 (e.g. 128, 256, ...), otherwise you run into
adverse cache effects in case you vary the second (or a higher) array
index fastest (this is inavoidable in 2D-FFTs, for example).

Probably your Fortran77 program had a non-power-of-2 leading dimension
for the array(s) it defined. Maybe your Fortran90 version allocates them
with the size violating the rule above.

Try the little test program below. On the CPUs that I tried, the penalty
was a factor of 5 and more.

HTH,

Kay

double complex,allocatable :: a(:,:)
integer i,j
real t1,t2,t3,t4
print*,'enter power of 2'
read*,n
allocate(a(n+4,n+4))
do m=n-4,n+4
a=1.
call cpu_time(t1)
do l=1,max(1,10000000/(n*n))
call test(a,m,n-4)
end do
call cpu_time(t2)
print '(i,f8.3)',m,t2-t1
end do
end
subroutine test(a,m,n)
! goes through a in "wrong" order
double complex a(m,m)
do i = 2, n-1
do j = 2, n-1
a(i,j) = (a(i+1,j-1) + a(i-1,j+1)) * 0.5
end do
enddo
end

.



Relevant Pages

  • Re: INTERFACE problem
    ... I have an integer at Fortran side, pass it by reference to C function, treat here as pointer, allocate memory and this C-pointer at Fortran side now is an integer which has represents address allocated at C side. ... I did something like this many years ago to allow dynamic allocation ... Then it was converted into a subroutine with the arrays and ... it called the sub by way of Pascal with those "pointers". ...
    (comp.lang.fortran)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Re: dgemm subroutine in BLAS - I think Ive cracked the difference, please confirm
    ... > matrix and m,n,k define the dimensions of the actual matrix to be worked ... Now that you know all about Fortran arrays and memory use, ... While newer Fortran versions actually supply dymamic allocation ...
    (comp.lang.fortran)
  • Re: allocating arrays, trouble
    ... as far as I can tell is fortran 77). ... so much: lists, map, lamba, etc. ... arrays, rather than lists. ... but the 3rd dimension is variable in length? ...
    (comp.lang.fortran)
  • Fortran and Python (was Re: Statement function host association)
    ... Are trying to sneak Python into Fortran? ... procedures taking allocatable arrays as arguments and returned ... automatic allocation of arrays in an expression ...
    (comp.lang.fortran)