comp.lang.ada
 help / color / mirror / Atom feed
From: Keean Schupke <keean.schupke@googlemail.com>
Cc: mailbox@dmitry-kazakov.de
Subject: Re: Efficient Sequential Access to Arrays
Date: Sun, 15 Jul 2012 13:56:46 -0700 (PDT)
Date: 2012-07-15T13:56:46-07:00	[thread overview]
Message-ID: <b23dec87-cfa4-4d32-9ac9-da6bfe6d0c85@googlegroups.com> (raw)
In-Reply-To: <44f2bfd0-75ed-4b9c-982e-61d5308f9c01@googlegroups.com>

On Sunday, 15 July 2012 21:03:12 UTC+1, Keean Schupke  wrote:
> On Sunday, 15 July 2012 20:48:35 UTC+1, Dmitry A. Kazakov  wrote:
> &gt; On Sun, 15 Jul 2012 11:40:08 -0700 (PDT), Keean Schupke wrote:
> &gt; 
> &gt; &amp;gt; However in order to achieve this performance I needed to rework the arrays
> &gt; &amp;gt; as the use of Indexes was too slow.
> &gt; 
> &gt; You have to measure it in order to know. Write two variants and compare
> &gt; their performance.
> 
> Have done, the difference is about 10k simulations per second.
> 
> &gt; 
> &gt; &amp;gt; An example of this is lets say we are
> &gt; &amp;gt; accessing element 5 from array A &amp;quot;A(5)&amp;quot; this requires a multiplication to
> &gt; &amp;gt; access (base_address + index * record_size).
> &gt; 
> &gt; It does not, because 5 is constant.
> 
> Obviously the index is not actually a constant but the result of selecting a random element in the list. The multiplication is necessary for finding this first element, but the operations on relative elements (+/-1, +/-2 etc) can use addition with a pointer, not so with an index.
> 
> &gt; 
> &gt; &amp;gt; To access the neighbour A(6)
> &gt; &amp;gt; also requires a multiplication. Accessing the array sequentially requires
> &gt; &amp;gt; a multiplication per step. 
> &gt; 
> &gt; That depends on loop optimizations. I would expect GCC to optimize access
> &gt; to array elements per the loop&amp;#39;s index.
> 
> It does not seem to, I&#39;ll see if I can post some assembly.
> 
> &gt; 
> &gt; &amp;gt; So assuming we need this level of performance, what would be the best (and
> &gt; &amp;gt; most idiomatic Ada)
> &gt; 
> &gt; Indexing is unlikely to have a significant (&amp;gt;5%) influence on overall
> &gt; performance. Usually it is other things.
> 
> It seems to have an significant influence in the benchmarks I have run and has a particular influence when sequentially accessing elements.
> 
> &gt; 
> &gt; &amp;gt; way to package this type of usage pattern as an
> &gt; &amp;gt; abstract datatype?
> &gt; 
> &gt; Array of aliased elements, to ensure elements independently addressable.
> 
> Yes, the type itself will need to have aliased elements, but I was assuming this would be hidden in a package as an ADT, and would expose an indexed-iterator that has &#39;+&#39; and &#39;-&#39; operations on the iterator (not just a forward or bidirectional iterator).
> 
> 
> Cheers,
> Keean.


So, looking at some test code, with simple loop bounds (say the range of the array or other constants) the whole loop gets optimised and unrolled. However this is not the case with more complex bounds (like random numbers needed in the monte-carlo simulation).  So with the following Ada code:


for J in S .. E loop
    Y := Y + X(J).Z;
end loop;


Where S and E are random numbers in the index range of the array and where S < E, and the array is an array of records (in this case 28 bytes long) containing the integer Z.


imul   $0x1c,%r9,%r10
add    0x48(%rsp,%r10,1),%edi


So you can see the multiplication is executed for every iteration.


Cheers,
Keean.



  reply	other threads:[~2012-07-18  2:30 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 [this message]
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
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