comp.lang.ada
 help / color / mirror / Atom feed
From: Keean Schupke <keean.schupke@googlemail.com>
Subject: Re: Efficient Sequential Access to Arrays
Date: Wed, 25 Jul 2012 02:03:15 -0700 (PDT)
Date: 2012-07-25T02:03:15-07:00	[thread overview]
Message-ID: <f2f61da1-3139-4dfc-b851-8398f27d558d@googlegroups.com> (raw)
In-Reply-To: <a79kcjFufvU1@mid.individual.net>

On Wednesday, 25 July 2012 08:09:09 UTC+1, Niklas Holsti  wrote:
> On 12-07-24 19:00 , robin vowels wrote:
> &gt; On Monday, 23 July 2012 03:34:26 UTC+10, Niklas Holsti  wrote:
> &gt;&gt; On 12-07-22 19:21 , Florian Weimer wrote:
> &gt;&gt; &amp;gt; * Keean Schupke:
> &gt;&gt; &amp;gt;
> &gt;&gt; &amp;gt;&amp;gt; So assuming we need this level of performance, what would be the
> &gt;&gt; &amp;gt;&amp;gt; best (and most idiomatic Ada) way to package this type of usage
> &gt;&gt; &amp;gt;&amp;gt; pattern as an abstract datatype?
> &gt;&gt; &amp;gt;
> &gt;&gt; &amp;gt; You should look at compiler dumps to figure out why the strength
> &gt;&gt; &amp;gt; reduction optimization does not kick in.
> &gt;&gt; &amp;gt;
> &gt;&gt;
> &gt;&gt; As I understand it, the program is accessing A(I,J), where A is a 2D
> &gt;&gt; array, and also accessing A(I+1,J), A(I,J+1), and the other neighbours
> &gt;&gt; of A(I,J), eight in all.
> &gt;&gt;
> &gt;&gt; I,J are indices that are *not* traversed in sequential order, but in
> &gt;&gt; some scattered order.
> &gt;&gt;
> &gt;&gt; Computing the address of A(I,J) involves multiplications of J and I with
> &gt;&gt; the size of an array element and the size of an array row, respectively.
> &gt;&gt; The problem seems to be that the results of these multiplications are
> &gt;&gt; not reused in computing the address of A(I+1,J) etc.
> &gt;&gt;
> &gt;&gt; If the index constraints (or at least the row length) of A are static
> &gt;&gt; (compile-time) constants, and the address of A(I,J) has been computed,
> &gt;&gt; and c1 and c2 are some other compile-time constants (-1, 0, +1 in this
> &gt;&gt; program), then the address of A(I+c1,J+c2) can be computed by adding a
> &gt;&gt; static offset to the address of A(I,J). This is what happens when A(I,J)
> &gt;&gt; and the neighbours are accessed through a C pointer, or when Keean uses
> &gt;&gt; address arithmetic explicitly in the Ada program. The question is why
> &gt;&gt; the Ada compiler is not doing this optimization.
> &gt;&gt;
> &gt;&gt; As I understand it, &amp;quot;strength reduction&amp;quot; is a loop optimization where an
> &gt;&gt; expression of the form C*i, with &amp;quot;i&amp;quot; the loop index, is replaced by a
> &gt;&gt; new variable that is incremented by C on each loop iteration. But in
> &gt;&gt; Keean&amp;#39;s program the indices I,J are not loop indices, nor linearly
> &gt;&gt; related to loop indices.
> &gt;&gt;
> &gt;&gt; The optimization that would be required here is to replace (X+C)*F by
> &gt;&gt; X*F+C*F, when C and F are static constants and X*F has already been
> &gt;&gt; computed and is available. This would replace the multiplication by an
> &gt;&gt; addition of a static constant. I&#39;ve never seen this special kind of
> &gt;&gt; optimization proposed or described.
> &gt;
> &gt; I think that it&#39;s a standard optimisation.
> 
> I&#39;m willing to be convinced of that. But it seems that GNAT/GCC is not 
> performing this optimization in Keean&#39;s program.
> 
> &gt; For the PL/I optimising compiler for the CDC Cyber (c. 1980),
> &gt; multiplication was eliminated for matrix elements
> &gt; by using a table of offsets into each row.
> &gt; Thus, double subscripting reduced to simple addition.
> 
> That sounds like a specific choice of code generation for arrays, rather 
> than a general optimization.
> 
> Keean could simulate this code by defining the array as a 
> one-dimensional array of one-dimensional arrays (rows), which would 
> replace the row-length multiplication by an additional indexing.
> 
> &gt;&gt; (We are of course (I believe) assuming that index range checks are
> &gt;&gt; turned off.)
> &gt;
> &gt; Index range checks should be left on.
> 
> Sure, in general. But here the OP (Keean) is trying to beat the 
> performance of C++ that uses pointers without index checks. Enabling 
> index checks in the Ada code would probably (in this case, where the 
> algorithm has scatterered indexing of the array) slow it by an amount 
> that swamps the effect of the multiplication optimization.
> 
> -- 
> Niklas Holsti
> Tidorum Ltd
> niklas holsti tidorum fi
>        .      @       .

On the specific point of array index checking, if you can statically prove the algorithm using the array will never use an out of range index, there is no need to run-time index check.

If we generate a random number between one and N, and then lookup the Nth element, we do not need to range check as the number is by definition in range. If the type system can carry such proofs (for example using range types in Ada) so you have:


subtype Idx is Integer range 1 .. 10;
type Arr is array (Idx) of Integer;  
function Random returns Idx;
A : Arr;
A(Random); -- as the random number is already in the range there needs to be no range checks on the array lookup.


I don't know how good the compiler is at doing this?

There are times when the programmer can guarantee that the output of a function will be within range due to extensive unit testing and validation.


I wonder, is there a way to get the compiler (GNAT) to issue a warning when it has to insert a range check?


Cheers,
Keean.



  reply	other threads:[~2012-07-25  9:03 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-15 18:40 Efficient Sequential Access to Arrays Keean Schupke
2012-07-15 19:48 ` Dmitry A. Kazakov
2012-07-15 20:03   ` Keean Schupke
2012-07-15 20:56     ` Keean Schupke
2012-07-16 11:34       ` Jacob Sparre Andersen
2012-07-15 21:05     ` tmoran
2012-07-15 21:08       ` Keean Schupke
2012-07-16  1:19         ` tmoran
2012-07-15 21:44     ` Georg Bauhaus
2012-07-16  7:47     ` Dmitry A. Kazakov
2012-07-16 10:16       ` Keean Schupke
2012-07-16 14:57         ` Dmitry A. Kazakov
2012-07-16 16:39           ` Keean Schupke
2012-07-16 17:02             ` Georg Bauhaus
2012-07-16 18:48             ` Dmitry A. Kazakov
2012-07-16 19:12               ` Keean Schupke
2012-07-17  6:31                 ` Dmitry A. Kazakov
2012-07-17  6:50                   ` Georg Bauhaus
2012-07-17  8:42                     ` Dmitry A. Kazakov
2012-07-17  7:24                   ` Keean Schupke
2012-07-16 19:43             ` John B. Matthews
2012-07-16 20:44               ` Keean Schupke
2012-07-16 22:23             ` tmoran
2012-07-17  6:40               ` Keean Schupke
2012-07-17  9:01                 ` Dmitry A. Kazakov
2012-07-18  8:10                   ` Keean Schupke
2012-07-18 18:11                     ` tmoran
2012-07-19 10:41                       ` Keean Schupke
2012-07-19 11:18                         ` Georg Bauhaus
2012-07-19 19:58                           ` Keean Schupke
2012-07-21  8:24                             ` Keean Schupke
2012-07-21 11:52                               ` Georg Bauhaus
2012-07-21 16:14                                 ` Keean Schupke
2012-07-21 17:09                                   ` Keean Schupke
     [not found]                                   ` <i78m081tccmp078spmsei1l5vnj3k0kbkj@invalid.netcom.com>
2012-07-22 15:50                                     ` Keean Schupke
2012-07-22  5:13                               ` Shark8
2012-07-15 21:35   ` J-P. Rosen
2012-07-15 19:52 ` Shark8
2012-07-15 20:16   ` Keean Schupke
2012-07-15 21:33     ` Georg Bauhaus
2012-07-17 18:16 ` Florian Weimer
2012-07-22 10:01 ` robin.vowels
2012-07-30  6:31   ` Jacob Sparre Andersen
2012-07-30  7:16     ` Keean Schupke
2012-07-30  9:20     ` Georg Bauhaus
2012-07-30 14:04       ` Keean Schupke
2012-07-22 10:09 ` robin.vowels
2012-07-22 16:02   ` Keean Schupke
2012-07-22 16:21 ` Florian Weimer
2012-07-22 16:46   ` Keean Schupke
2012-07-22 18:41     ` Florian Weimer
2012-07-24  8:21       ` Keean Schupke
2012-07-22 17:34   ` Niklas Holsti
2012-07-22 18:21     ` Keean Schupke
2012-07-30 14:18       ` Jacob Sparre Andersen
2012-07-24 16:00     ` robin.vowels
2012-07-25  7:09       ` Niklas Holsti
2012-07-25  9:03         ` Keean Schupke [this message]
2012-07-26  0:15           ` Randy Brukardt
2012-07-26  8:38             ` Keean Schupke
2012-07-26 10:10               ` Brian Drummond
2012-07-26 12:32                 ` Keean Schupke
2012-07-26 13:46                   ` Dmitry A. Kazakov
2012-07-26 17:40                     ` Shark8
2012-07-26 18:56                       ` Dmitry A. Kazakov
2012-07-27  5:25                         ` Shark8
2012-07-27  6:28                           ` Dmitry A. Kazakov
2012-07-27  7:01                             ` Vasiliy Molostov
2012-07-27  8:48                               ` Dmitry A. Kazakov
2012-07-27 12:01                                 ` Georg Bauhaus
2012-07-27 16:49                                 ` Vasiliy Molostov
2012-07-27 19:38                                   ` Dmitry A. Kazakov
2012-07-28  5:32                             ` Shark8
2012-07-28  7:37                               ` Dmitry A. Kazakov
2012-07-28 18:32                                 ` Shark8
     [not found]                     ` <6ov218tnkbqu3vpkuo4t77rd7de0a3aesf@invalid.netcom.com>
2012-07-26 18:49                       ` Dmitry A. Kazakov
     [not found]                         ` <86d31898ne39maimbl24knds7rf38qg7vc@invalid.netcom.com>
2012-07-27  6:45                           ` Dmitry A. Kazakov
2012-07-27  8:21                   ` Keean Schupke
2012-07-27  8:50                     ` Dmitry A. Kazakov
2012-07-27  9:17                       ` Keean Schupke
2013-02-25 21:18                     ` Eryndlia Mavourneen
2012-07-26 13:08               ` Georg Bauhaus
2012-07-26 19:37                 ` stefan-lucks
2012-07-26 19:21               ` stefan-lucks
2012-07-31  3:12               ` Randy Brukardt
2012-07-26  7:11           ` Markus Schöpflin
2012-07-26 14:47         ` Robin Vowels
2012-07-26 15:18           ` Martin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox