For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2005 > array 2D to 1D









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author array 2D to 1D
Fabian Braennstroem

2005-05-23, 3:57 am

Hi,

I wonder if the code runs faster when converting a 2D array
into an 1D array and looping over it? If so, are there any
downsides using just 1D arrays, besides the bad readability?

Greetings, Fabian
glen herrmannsfeldt

2005-05-23, 8:57 am

Fabian Braennstroem wrote:

> I wonder if the code runs faster when converting a 2D array
> into an 1D array and looping over it? If so, are there any
> downsides using just 1D arrays, besides the bad readability?


If the loops are in the right order, such that the leftmost
subscript varies fastest, the time difference should be small.

There might be an advantage if the 1D array uses larger variables,
for example with EQUIVALENCE, such that more bits are moved per
assignment, but you didn't ask that. (Assuming the processor
supports the larger assignment.)

-- glen


Jan Vorbrüggen

2005-05-23, 3:58 pm

> I wonder if the code runs faster when converting a 2D array
> into an 1D array and looping over it? If so, are there any
> downsides using just 1D arrays, besides the bad readability?


Even Steve's VAX Fortran compiler some twenty, twenty-five years
ago did a lot of common subexpression elimination of array index
expressions or converting them to marching pointers. A modern
compiler should be even better. If you addressing the array elements
in some random order - e.g., the dreaded A(B(I,j), C(I,J) thingy -
then the speed of address computation is your least worry. If you
_are_ using expression of this kind but you are still addressing
the array elements sequentially, then your code needs improving
in that it should make it clear to the compiler that you are
accessing elements sequentially.

Jan
Fabian Braennstroem

2005-05-24, 8:57 am

Hi Glen,

On 2005-05-23, glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
> Fabian Braennstroem wrote:
>
>
> If the loops are in the right order, such that the leftmost
> subscript varies fastest, the time difference should be small.
>
> There might be an advantage if the 1D array uses larger variables,
> for example with EQUIVALENCE, such that more bits are moved per
> assignment, but you didn't ask that. (Assuming the processor
> supports the larger assignment.)


Thanks!

Greetings, Fabian
Fabian Braennstroem

2005-05-24, 8:57 am

Hi Jan,

have some problems understanding you ...

On 2005-05-23, Jan Vorbrüggen <jvorbrueggen-not@mediasec.de> wrote:
>
> Even Steve's VAX Fortran compiler some twenty, twenty-five years
> ago did a lot of common subexpression elimination of array index
> expressions or converting them to marching pointers. A modern
> compiler should be even better. If you addressing the array elements
> in some random order - e.g., the dreaded A(B(I,j), C(I,J) thingy -


what do you mean by that?

> then the speed of address computation is your least worry. If you
> _are_ using expression of this kind but you are still addressing
> the array elements sequentially, then your code needs improving
> in that it should make it clear to the compiler that you are
> accessing elements sequentially.


I meant that I have a 2D array A(10,10) and was curious if
the code runs faster when I transform it to a Aa(100)
array!? I am little bit about your answer.

Greetings, Fabian
Tim Prince

2005-05-24, 3:59 pm

Fabian Braennstroem wrote:


> I meant that I have a 2D array A(10,10) and was curious if
> the code runs faster when I transform it to a Aa(100)
> array!? I am little bit about your answer.
>

In this case, if the compiler can see the fixed dimensions, it should unroll
automatically to a sufficient extent to produce good performance. You may
want to check that this is happening for your platform.
If you select limited optimizations, no doubt you could find situations
where your proposed transformation gives a big gain.

--
Tim Prince
Fabian Braennstroem

2005-05-25, 3:59 am

On 2005-05-24, Tim Prince <timothyprince@sbcglobal.net> wrote:
> Fabian Braennstroem wrote:
>
>
> In this case, if the compiler can see the fixed dimensions, it should unroll
> automatically to a sufficient extent to produce good performance. You may
> want to check that this is happening for your platform.
> If you select limited optimizations, no doubt you could find situations
> where your proposed transformation gives a big gain.


Thanks! I will try it out.

Greetings, Fabian
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com