From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,56525db28240414a X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.224.174.74 with SMTP id s10mr2734152qaz.1.1342578626270; Tue, 17 Jul 2012 19:30:26 -0700 (PDT) Received: by 10.66.78.233 with SMTP id e9mr80985pax.35.1342578626031; Tue, 17 Jul 2012 19:30:26 -0700 (PDT) Path: a15ni8315952qag.0!nntp.google.com!x2no8434404qaj.0!news-out.google.com!p10ni160544444pbh.1!nntp.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!border6.newsrouter.astraweb.com!border6.a.newsrouter.astraweb.com!news.astraweb.com!border3.a.newsrouter.astraweb.com!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder5.xlned.com!feed.xsnews.nl!border-1.ams.xsnews.nl!plix.pl!newsfeed2.plix.pl!news.mi.ras.ru!goblin2!goblin.stu.neva.ru!feeder3.cambriumusenet.nl!feed.tweaknews.nl!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Keean Schupke Newsgroups: comp.lang.ada Subject: Re: Efficient Sequential Access to Arrays Date: Sun, 15 Jul 2012 13:56:46 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <01983f1c-f842-4b1f-a180-bcef531dad4c@googlegroups.com> <44f2bfd0-75ed-4b9c-982e-61d5308f9c01@googlegroups.com> NNTP-Posting-Host: 82.44.19.199 Mime-Version: 1.0 X-Trace: posting.google.com 1342385806 10017 127.0.0.1 (15 Jul 2012 20:56:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 15 Jul 2012 20:56:46 +0000 (UTC) Cc: mailbox@dmitry-kazakov.de In-Reply-To: <44f2bfd0-75ed-4b9c-982e-61d5308f9c01@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=82.44.19.199; posting-account=T5Z2vAoAAAB8ExE3yV3f56dVATtEMNcM User-Agent: G2/1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2012-07-15T13:56:46-07:00 List-Id: 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: > > On Sun, 15 Jul 2012 11:40:08 -0700 (PDT), Keean Schupke wrote: > >=20 > > &gt; However in order to achieve this performance I needed to re= work the arrays > > &gt; as the use of Indexes was too slow. > >=20 > > You have to measure it in order to know. Write two variants and comp= are > > their performance. >=20 > Have done, the difference is about 10k simulations per second. >=20 > >=20 > > &gt; An example of this is lets say we are > > &gt; accessing element 5 from array A &quot;A(5)&quot; t= his requires a multiplication to > > &gt; access (base_address + index * record_size). > >=20 > > It does not, because 5 is constant. >=20 > Obviously the index is not actually a constant but the result of selectin= g a random element in the list. The multiplication is necessary for finding= this first element, but the operations on relative elements (+/-1, +/-2 et= c) can use addition with a pointer, not so with an index. >=20 > >=20 > > &gt; To access the neighbour A(6) > > &gt; also requires a multiplication. Accessing the array sequent= ially requires > > &gt; a multiplication per step.=20 > >=20 > > That depends on loop optimizations. I would expect GCC to optimize a= ccess > > to array elements per the loop&#39;s index. >=20 > It does not seem to, I'll see if I can post some assembly. >=20 > >=20 > > &gt; So assuming we need this level of performance, what would b= e the best (and > > &gt; most idiomatic Ada) > >=20 > > Indexing is unlikely to have a significant (&gt;5%) influence on= overall > > performance. Usually it is other things. >=20 > It seems to have an significant influence in the benchmarks I have run an= d has a particular influence when sequentially accessing elements. >=20 > >=20 > > &gt; way to package this type of usage pattern as an > > &gt; abstract datatype? > >=20 > > Array of aliased elements, to ensure elements independently addressa= ble. >=20 > Yes, the type itself will need to have aliased elements, but I was assumi= ng this would be hidden in a package as an ADT, and would expose an indexed= -iterator that has '+' and '-' operations on the iterator (= not just a forward or bidirectional iterator). >=20 >=20 > Cheers, > Keean. So, looking at some test code, with simple loop bounds (say the range of th= e array or other constants) the whole loop gets optimised and unrolled. How= ever this is not the case with more complex bounds (like random numbers nee= ded in the monte-carlo simulation). So with the following Ada code: for J in S .. E loop Y :=3D 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) co= ntaining 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.