Re: automatic arrays versus saved, allocated arrays



In article <1160639410.683498.154900@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
robert.corbett@xxxxxxx wrote:

Bil Kleb wrote:

Can someone explain what is going on behind the curtain
that would explain this difference, e.g., stack, heap,
CPU cache misses, TLB misses? Is this a general result,
or will it vary with work array size and work flow memory
utilization?

There could be many reasons for the performance advantage
of automatic arrays over pointer arrays, but usually the biggest
advantage is that the automatic array is known to be contiguous.

Consider the translation of a simple array assignment such as

A = 0.0

where A is a rank 2 array. Given the pointer array, the compiler
is likely to generate code similar to a doubly nested DO-loop.
For an automatic array, the compiler can interate over the entire
array with a single loop. Furthermore, the address calculations,
even after optimization, are simpler and on some machines are
hardware assisted.

I'm not sure this is causing the problem. I would guess that both
the automatic array and the allocated pointer array are contiguous.

However, I think this is probably close to the problem, which
involves aliasing. The compiler knows that an automatic array
cannot be aliased to any other variable in scope, so it is free to
perform various optimizations (e.g. storing the value of some
elements in registers). However, the pointer array can also be the
target of a pointer assignment, so the compiler may be conservative
about what kind of optimizations it attempts. That is, using
pointer arrays in fortran causes the compiler to behave more like a
C compiler as far as optimizations, and this may be particularly
true for fortran compilers (such as g95 and gfortran) that share
some of the same code as a C compiler.

Changing the pointer array to an allocatable array would eliminate
the aliasing possibilities too. That is, it is probably not the
difference between using allocate() and the automatic array, it is
that pointer arrays can be targets of pointer assignments, even
though they do not have the target attribute in their declaration,
whereas other types of arrays cannot.

$.02 -Ron Shepard
.



Relevant Pages

  • Re: Library Design, f0dders nightmare.
    ... demo mistake but I suggest to you that a sequence of blunders on this ... Feed it through your parser to get the offset of each ... offset to its appropriate place in the array of pointers and when you ... dividing the byte count by two to determine the maximum pointer array ...
    (alt.lang.asm)
  • Is the syntax for multi-dimensional arrays counter-intuitive?
    ... means that there is an array of pointers to int with a size of 5, ... of whose elements is an array of int with a size of 3. ... Allocate memory for 5 pointers to int (i.e. pointer array) ...
    (comp.lang.c)
  • Re: automatic arrays versus saved, allocated arrays
    ... of automatic arrays over pointer arrays, ... advantage is that the automatic array is known to be contiguous. ... For an automatic array, the compiler can interate over the entire ...
    (comp.lang.fortran)
  • Re: question on reference to array slice
    ... array by looking it up in this pointer array. ... AREF => $aref, ... OFFSET => $offset, ...
    (comp.lang.perl.misc)
  • Re: [PATCH 1/2] irq: sparseirq enabling v2
    ... use pointer array instead of hash ... losing the fundamental abstraction that it's a 0..NR_IRQS array (just ...
    (Linux-Kernel)