public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code.
@ 2018-02-18  4:01 Bojan Bozovic
  2018-02-19 15:47 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Bojan Bozovic @ 2018-02-18  4:01 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2513 bytes --]

This is the output of the program attached with these arguments as
provided on Windows 10 x86_64, and Cygwin x86_64. GNAT/2017 compiler
also doesn't optimize with -O3 -mavx2, neither with -O3 alone (same
output).

I have Skylake avx2 intel core i3 PC, but actually SSE2/SSE3 switches
should optimize this.

Output:


Bojan@DESKTOP-UT5MH6N ~
$ cd /cygdrive/c/Users/Bojan/Documents

Bojan@DESKTOP-UT5MH6N /cygdrive/c/Users/Bojan/Documents
$ gnatmake -O3 -march=skylake matrix_mul.adb -largs -s
gcc -c -O3 -march=skylake matrix_mul.adb
gnatbind -x matrix_mul.ali
gnatlink matrix_mul.ali -O3 -march=skylake -s

Bojan@DESKTOP-UT5MH6N /cygdrive/c/Users/Bojan/Documents
$ ./matrix_mul.exe
 4.00000E+01 3.60000E+01 3.20000E+01 2.80000E+01
 8.00000E+01 7.20000E+01 6.40000E+01 5.60000E+01
 1.20000E+02 1.08000E+02 9.60000E+01 8.40000E+01
 1.60000E+02 1.44000E+02 1.28000E+02 1.12000E+02

Elapsed Time is      1.725664889
 4.00000E+01 3.60000E+01 3.20000E+01 2.80000E+01
 8.00000E+01 7.20000E+01 6.40000E+01 5.60000E+01
 1.20000E+02 1.08000E+02 9.60000E+01 8.40000E+01
 1.60000E+02 1.44000E+02 1.28000E+02 1.12000E+02

Elapsed time is      0.290578222

Bojan@DESKTOP-UT5MH6N /cygdrive/c/Users/Bojan/Documents
$ gcc --version
gcc (GCC) 6.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\Users\Bojan\Documents>gnatmake -O3 -march=skylake matrix_mul.adb -largs -s
gcc -c -O3 -march=skylake matrix_mul.adb
gnatbind -x matrix_mul.ali
gnatlink matrix_mul.ali -O3 -march=skylake -s

C:\Users\Bojan\Documents>matrix_mul
 4.00000E+01 3.60000E+01 3.20000E+01 2.80000E+01
 8.00000E+01 7.20000E+01 6.40000E+01 5.60000E+01
 1.20000E+02 1.08000E+02 9.60000E+01 8.40000E+01
 1.60000E+02 1.44000E+02 1.28000E+02 1.12000E+02

Elapsed Time is      1.307566667
 4.00000E+01 3.60000E+01 3.20000E+01 2.80000E+01
 8.00000E+01 7.20000E+01 6.40000E+01 5.60000E+01
 1.20000E+02 1.08000E+02 9.60000E+01 8.40000E+01
 1.60000E+02 1.44000E+02 1.28000E+02 1.12000E+02

Elapsed time is      0.299596000

C:\Users\Bojan\Documents>gcc --version
gcc (Rev1, Built by MSYS2 project) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This is unexpected behavior as these times should be similar. Problem
with vectorizing/optimizing the code?

[-- Attachment #2: matrix_mul.adb --]
[-- Type: application/octet-stream, Size: 1957 bytes --]

with Ada.Calendar;
with Ada.Text_IO;
with Ada.Numerics.Real_Arrays;
use Ada.Calendar;
use Ada.Text_IO;
use Ada.Numerics.Real_Arrays;

procedure Matrix_Mul is

   package F_IO is new Ada.Text_IO.Fixed_IO (Day_Duration);
   use F_IO;

   Start_Time, End_Time : Time;
   
   procedure Put (X : Real_Matrix) is
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Float'Image (Float (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;


   Matrix_A, Matrix_B, Result : Real_Matrix (1 .. 4, 1 .. 4);
   Elapsed_Time               : Duration;
   Sum                        : Float;
   pragma Volatile (Matrix_A);
   pragma Volatile (Matrix_B);
   pragma Volatile (Result);
   Iterations : constant := 10_000_000;
begin
   Matrix_A :=
     ((1.0, 1.0, 1.0, 1.0),
      (2.0, 2.0, 2.0, 2.0),
      (3.0, 3.0, 3.0, 3.0),
      (4.0, 4.0, 4.0, 4.0));
   Matrix_B :=
     ((16.0, 15.0, 14.0, 13.0),
      (12.0, 11.0, 10.0, 9.0),
      (8.0, 7.0, 6.0, 5.0),
      (4.0, 3.0, 2.0, 1.0));

   Start_Time := Clock;
   for Iteration in 1..Iterations loop
      Result := Matrix_A * Matrix_B;
	end loop;
   End_Time     := Clock;
   Elapsed_Time := End_Time - Start_Time;
   Put (Result);
   New_Line;
   Put ("Elapsed Time is ");
   Put (Elapsed_Time);
   New_Line;
   Start_Time := Clock;
   for Iteration in 1 .. Iterations loop
   
      for I in Matrix_A'Range (1) loop
         for J in Matrix_A'Range (2) loop
            Sum := 0.0;
            for K in Matrix_A'Range (2) loop
               Sum := Sum + Matrix_A (I, K) * Matrix_B (K, J);
            end loop;
            Result (I, J) := Sum;
         end loop;
      end loop;
end loop;
   End_Time     := Clock;
   Elapsed_Time := End_Time - Start_Time;
   Put (Result);
   New_Line;
   Put ("Elapsed time is ");
   Put (Elapsed_Time);
   New_Line;
end Matrix_Mul;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code.
  2018-02-18  4:01 I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code Bojan Bozovic
@ 2018-02-19 15:47 ` Jonathan Wakely
  2018-02-19 17:18   ` Bojan Bozovic
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2018-02-19 15:47 UTC (permalink / raw)
  To: Bojan Bozovic; +Cc: gcc-help

If you want a bugzilla account then you should send an email to the
address that Bugzilla tells you to contact.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code.
  2018-02-19 15:47 ` Jonathan Wakely
@ 2018-02-19 17:18   ` Bojan Bozovic
  2018-02-19 18:06     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Bojan Bozovic @ 2018-02-19 17:18 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

My email got rejected when I tried to get bugzilla account manually.
Anyway this is confirmed not a bug just instantiating with : package
R_A is new Ada.Numerics.Generic_Real_Arrays (Float); use R_A; makes
code run comparable to coding multiplication by hand (and GCC C code).
It's just run time library isn't compiled with aggressive
optimizations (-O3 -march=skylake for x86_64 or -O3 -mavx2 for i686).

On Mon, Feb 19, 2018 at 4:47 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> If you want a bugzilla account then you should send an email to the
> address that Bugzilla tells you to contact.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code.
  2018-02-19 17:18   ` Bojan Bozovic
@ 2018-02-19 18:06     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2018-02-19 18:06 UTC (permalink / raw)
  To: Bojan Bozovic; +Cc: gcc-help

On 19 February 2018 at 17:18, Bojan Bozovic wrote:
> My email got rejected when I tried to get bugzilla account manually.

Yes, and when that happens it tells you the address to contact.

> Anyway this is confirmed not a bug just instantiating with : package
> R_A is new Ada.Numerics.Generic_Real_Arrays (Float); use R_A; makes
> code run comparable to coding multiplication by hand (and GCC C code).

OK, thanks for following up with that information.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-02-19 18:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-18  4:01 I can't make Bugzilla account and there is possible GCC Ada (GNAT) bug in optimizing the code - ada.numerics.real_arrays Real_Matrix multiplication slower than doing things 'by hand'. I attached code Bojan Bozovic
2018-02-19 15:47 ` Jonathan Wakely
2018-02-19 17:18   ` Bojan Bozovic
2018-02-19 18:06     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).