Matrix multiplication problem when compiling with -march=native

Andrew Pinski pinskia@gmail.com
Sun Jul 16 17:17:00 GMT 2017


On Sun, Jul 16, 2017 at 8:18 AM, Mauro Fontana <fontana.mauro@gmail.com> wrote:
> Hello,
>
>
> I'm writing to this list for the first time in order to describe a
> curious error I'm encountering in gfortran 6.4.0 (up to date Debian
> testing). Not really sure if it's a bug or, more likely, a lack of
> knowledge from my part on the implications of the -march option (hence
> I opted for a mail instead of a bug report in the bugtracker).
>
>
> I've written a simple subroutine that performs matrix multiplication
> (using a very straightforward algorithm) given two squared matrices
> and its dimension as input. Then I replicate this same code changing
> the subroutine name (multO, multO1, multO2...) and I compile them (to
> objects) using the corresponding optimization flag (-O for the symbol
> multO, -O1 for the symbol multO1, and so). Then I have a main program
> where I generate the input matrices using pseudorandom numbers and
> call the aforementioned subroutines and the MATMUL intrinsic function.
> Finally I compare the arrays produced by the subroutines to the one
> obtained with MATMUL and output an error message if for some pair of
> indexes the absolute error is greater than some given tolerance (I
> used 1e-5 which seemed sensible for operations with REALs smaller than
> 1).
>
>
> To give some context, this program was written as an example for
> profiling different optimizations.
>
>
> So far so good. If I compile the subroutine objects with "gfortran -c
> -Ox fname.f90" it works as intended for matrices up to 1000x1000.
> However, if I add the -march=native switch, I get messages stating
> that the O2 and O3 versions differ from the MATMUL ones in more than
> 1e-5, at least for some element. I've tested this consistently (more
> than 15 runs of each variant) and it happens in every run (without
> march it's always ok, with march it fails every time). The march
> failure is size-dependent, in my systems starts to show up at 500x500
> matrices. Arrays smaller than that operate correctly.
>
>
> lscpu shows Model name: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz

-march=native enables fma (fused multiply add) instruction which was
included in your processor.  GCC/Gfortan takes advantage of the fma
instruction.  FMA instructions do multiply and addition in infinite
precision before doing a rounding operation.  Matrix multiplication is
all multiply adds.  Basically with bigger the size, the less rounding
errors than fma will provide over the non fused multiply add
instructions.

Thanks,
Andrew Pinski


>
>
> The source code for the program can be found in
> https://github.com/sabian2008/hotspot-fortrantest (as you may see, I'm
> not specially talented for writing Fortran code).
>
>
> I apologize in advance if this e-mails reports something silly.
>
>
> Cheers,
> Mauro



More information about the Fortran mailing list