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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: GNAT can't vectorize Real_Matrix multiplication from Ada.Numerics.Real_Arrays. What a surprise! Date: Sun, 18 Feb 2018 11:35:50 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 18 Feb 2018 10:35:50 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="afc0ab5a5d30e5b8a23c5faffa82ab20"; logging-data="28915"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Z88/eWHtrjdoQndn2MUvD60ZF8pCxa6U=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 In-Reply-To: Content-Language: en-US Cancel-Lock: sha1:xuxA/C572Cow/6qtYh2EJppeA7I= Xref: reader02.eternal-september.org comp.lang.ada:50499 Date: 2018-02-18T11:35:50+01:00 List-Id: On 02/18/2018 02:51 AM, Bojan Bozovic wrote: > > Apart from response that they don't offer support on GPL versions of compiler, I got no response. I hope this will be useful to someone. Nor should you expect anything to happen, since you have not found an error. An error is when the compiler accepts illegal code, rejects legal code, or produces object code that gives incorrect results. At best you have found an opportunity for better optimization. Even that seems unlikely. Remember that Ada.Numerics.Real_Arrays."*" is a general-purpose library function. As such, it has to do things that your specific implementation of matrix multiplication doesn't: It has to check that Left'Length (2) = Right'Length (1) and raise an exception if they're not equal. Your code doesn't, and any such check should be optimized away because its condition will be statically False. The general code has to handle the case where Left'First (2) /= Right'First (1). Even when the offset is zero, that's still an extra addition in the inner loop. Also, Ada.Numerics.Real_Arrays is provided precompiled, and is surely compiled with different optimization options than your code. (IIRC, -O3 is considered experimental, and AdaCore is not going to compile its library code with experimental optimization.) You can find GNAT's implementation of matrix multiplication as System.Generic_Array_Operations.Matrix_Matrix_Product. This is extremely general, allowing for non-numeric matrix components (complex numbers, for example), and different component types for the left, right, and result matrices. This may introduce additional barriers to optimization. Since Ada.Numerics.Real_Arrays is an instantiation of Ada.Numerics.Generic_Real_Arrays for Float, and GNAT does macro expansion of generics, if you use an explicit instantiation of Ada.Numerics.Generic_Real_Arrays in your code, it should be compiled with your compiler options and thus remove that variable from your comparison. Doing that in your code, and building with gnatmake -O3 -mavx2 matrix_mul -largs -s cuts the reported time for "*" by a factor of about 4. Not surprisingly, still slower. YMMV -- Jeff Carter Just as Khan was hindered by two-dimensional thinking in a three-dimensional situation, so many developers are hindered by sequential thinking in concurrent situations. 118