Skip to content

Incorrect amaxv results #380

Closed
Closed

Description

@matcross

(This supersedes #379.)

I'm using

$ ./configure -d CC=gcc auto
configure: detected Linux kernel version 5.5.8-200.fc31.x86_64.
configure: python interpeter search list is: python python3 python2.
configure: using 'python' python interpreter.
configure: found python version 3.7.6 (maj: 3, min: 7, rev: 6).
configure: python 3.7.6 appears to be supported.
configure: C compiler search list is: gcc clang cc.
configure: using 'gcc' C compiler.
configure: C++ compiler search list is: g++ clang++ c++.
configure: using 'g++' C++ compiler (for sandbox only).
configure: found gcc version 9.2.1 (maj: 9, min: 2, rev: 1).
configure: checking for blacklisted configurations due to gcc 9.2.1.
configure: checking gcc 9.2.1 against known consequential version ranges.
configure: found assembler ('as') version 2.32 (maj: 2, min: 32, rev: ).
configure: checking for blacklisted configurations due to as 2.32.
configure: reading configuration registry...done.
configure: determining default version string.
configure: found '.git' directory; assuming git clone.
configure: executing: git describe --tags.
configure: got back 0.6.1-5-gc01d249d.
configure: truncating to 0.6.1-5.
configure: starting configuration of BLIS 0.6.1-5.
configure: configuring with official version string.
configure: found shared library .so version '3.0.0'.
configure:   .so major version: 3
configure:   .so minor.build version: 0.0
configure: automatic configuration requested.
configure: hardware detection driver returned 'haswell'.
configure: checking configuration against contents of 'config_registry'.
configure: configuration 'haswell' is registered.
configure: 'haswell' is defined as having the following sub-configurations:
configure:    haswell
configure: which collectively require the following kernels:
configure:    haswell zen
configure: checking sub-configurations:
configure:   'haswell' is registered...and exists.
configure: checking sub-configurations' requisite kernels:
configure:   'haswell' kernels...exist.
configure:   'zen' kernels...exist.
configure: no install prefix option given; defaulting to '/usr/local'.
configure: no install exec_prefix option given; defaulting to PREFIX.
configure: no install libdir option given; defaulting to EXECPREFIX/lib.
configure: no install includedir option given; defaulting to PREFIX/include.
configure: no install sharedir option given; defaulting to PREFIX/share.
configure: final installation directories:
configure:   prefix:      /usr/local
configure:   exec_prefix: ${prefix}
configure:   libdir:      ${exec_prefix}/lib
configure:   includedir:  ${prefix}/include
configure:   sharedir:    ${prefix}/share
configure: NOTE: the variables above can be overridden when running make.
configure: no preset CFLAGS detected.
configure: no preset LDFLAGS detected.
configure: enabling debug symbols; optimizations disabled.
configure: disabling verbose make output. (enable with 'make V=1'.)
configure: disabling ARG_MAX hack.
configure: building BLIS as both static and shared libraries.
configure: exporting only public symbols within shared library.
configure: threading is disabled.
configure: requesting slab threading in jr and ir loops.
configure: internal memory pools for packing blocks are enabled.
configure: internal memory pools for small blocks are enabled.
configure: memory tracing output is disabled.
configure: libmemkind not found; disabling.
configure: compiler appears to support #pragma omp simd.
configure: the BLAS compatibility layer is enabled.
configure: the CBLAS compatibility layer is disabled.
configure: mixed datatype support is enabled.
configure: mixed datatype optimizations requiring extra memory are enabled.
configure: small matrix handling is enabled.
configure: the BLIS API integer size is automatically determined.
configure: the BLAS/CBLAS API integer size is 32-bit.
configure: configuring for conventional gemm implementation.
configure: creating ./config.mk from ./build/config.mk.in
configure: creating ./bli_config.h from ./build/bli_config.h.in
configure: creating ./obj/haswell
configure: creating ./obj/haswell/config/haswell
configure: creating ./obj/haswell/kernels/haswell
configure: creating ./obj/haswell/kernels/zen
configure: creating ./obj/haswell/ref_kernels/haswell
configure: creating ./obj/haswell/frame
configure: creating ./obj/haswell/blastest
configure: creating ./obj/haswell/testsuite
configure: creating ./lib/haswell
configure: creating ./include/haswell
configure: mirroring ./config/haswell to ./obj/haswell/config/haswell
configure: mirroring ./kernels/haswell to ./obj/haswell/kernels/haswell
configure: mirroring ./kernels/zen to ./obj/haswell/kernels/zen
configure: mirroring ./ref_kernels to ./obj/haswell/ref_kernels
configure: mirroring ./ref_kernels to ./obj/haswell/ref_kernels/haswell
configure: mirroring ./frame to ./obj/haswell/frame
configure: creating makefile fragments in ./obj/haswell/config/haswell
configure: creating makefile fragments in ./obj/haswell/kernels/haswell
configure: creating makefile fragments in ./obj/haswell/kernels/zen
configure: creating makefile fragments in ./obj/haswell/ref_kernels
configure: creating makefile fragments in ./obj/haswell/frame
configure: configured to build within top-level directory of source distribution.

on Core i9.

The following Fortran program shows incorrect results for i*amax:

Program p
  Real, Allocatable :: f(:)
  Double Precision, Allocatable :: x(:)
  Character (:), Allocatable :: c
  x = [ (42.D0,i=1,4) ]
  c = ' double, constant'
  Print *, merge('PASS', 'FAIL', idamax(size(x),x,1)==1)//c
  f = [ (42.E0,i=1,4) ]
  c = ' float, constant'
  Print *, merge('PASS', 'FAIL', isamax(size(f),f,1)==1)//c
  x = [ Double Precision :: 8, -7, 9, 5, 9, 8, 7, 7 ]
  c = ' double'
  Print *, merge('PASS', 'FAIL', idamax(size(x),x,1)==3)//c
  f = [ Real :: 8, -7, 9, 5, 9, 8, 7, 7 ]
  c = ' float'
  Print *, merge('PASS', 'FAIL', isamax(size(f),f,1)==3)//c
End Program

in which the smallest of the max indices are not being returned:

 FAIL double, constant
 PASS float, constant
 FAIL double
 FAIL float

Activity

  1. fgvanzee commented on Mar 20, 2020

    @fgvanzee
    Member

    @matcross Thanks for this bug report. I'm going to try to reproduce this. In the meantime, could you modify and rerun your test driver to instead output the return value of i?amax()?

  2. matcross commented on Mar 20, 2020

    @matcross
    Author

    Sure, here's

    Program p
      Real, Allocatable :: f(:)
      Double Precision, Allocatable :: x(:)
      Character (:), Allocatable :: c
      Integer i, i_exp
    
      i_exp = 1
    
      c = ' double, constant'
      x = [ (42.D0,i=1,4) ]
      i = idamax(size(x), x, 1)
      Call check_result
    
      c = ' float, constant'
      f = [ (42.E0,i=1,4) ]
      i = isamax(size(f), f, 1)
      Call check_result
    
      i_exp = 3
    
      c = ' double'
      x = [ Double Precision :: 8, -7, 9, 5, 9, 8, 7, 7 ]
      i = idamax(size(x), x, 1)
      Call check_result
    
      c = ' float'
      f = [ Real :: 8, -7, 9, 5, 9, 8, 7, 7 ]
      i = isamax(size(f), f, 1)
      Call check_result
    Contains
      Subroutine check_result
        Print *, merge('PASS', 'FAIL', i==i_exp) // c, ' expected ', i_exp, &
          ' got ', i
      End Subroutine
    End Program
    

    with output (gfortran -g)

     FAIL double, constant expected            1  got            2
     PASS float, constant expected            1  got            1
     FAIL double expected            3  got            5
     FAIL float expected            3  got            5
    
  3. fgvanzee commented on Mar 20, 2020

    @fgvanzee
    Member

    @matcross Thanks for those additional printouts. They were quite helpful.

    All three failures appear to be a result of how the amaxv kernel is implemented (via vector instructions), and fixing them will take a bit of time since I did not write those kernels (they were contributed by AMD).

    Furthermore, I suspect if you made your Real (float) vector length 8 instead of 4, you would also observe a failure for that case.

    As for the failure itself... I am not a heavy user of amaxv/i?amax(), so please forgive the question, but can you explain to me why the above printouts are "wrong"? In all cases, the index leads to a value whose absolute value is the maximum across all elements, but it seems to be that you have a preference for which of the eligible values is returned (or that there is some convention whereby it must be the first value). Can you elaborate on this?

  4. matcross commented on Mar 21, 2020

    @matcross
    Author

    Thanks for the comments. Good point about the constant float case, you are right that using 8 elements does also wrongly return 2.
    The spec of i*amax is that it returns the smallest integer for which the corresponding array element is of max abs value.
    Our testing at NAG shows that a number of LAPACK failures occur if this is not observed.

  5. fgvanzee commented on Mar 21, 2020

    @fgvanzee
    Member

    The spec of i*amax is that it returns the smallest integer for which the corresponding array element is of max abs value.

    @matcross Thanks for pointing this out. I actually pulled up idamax.f yesterday and looked at the subroutine definition and still somehow missed this detail. I will use the old "distracted by a global pandemic" excuse. :)

    My PI has pointed out to me that this operation should probably not be vectorized in the first place since it is never amounts to any significant portion of computation, and therefore it is hard to imagine it ever being a bottleneck. I will disable the vectorized code in the haswell and zen subconfigurations so that this is no longer an issue for you (and others).

  6. added a commit that references this issue on Mar 21, 2020
  7. devinamatthews commented on Mar 21, 2020

    @devinamatthews
    Member

    FYI I just peeked at the code and it wouldn't be too hard to fix it to return the expected result. In fact, I think the whole problem is here and here.

  8. fgvanzee commented on Mar 21, 2020

    @fgvanzee
    Member

    @devinamatthews Thanks for pointing this out. If you (or someone else) wants to develop a quick patch and confirm it works, I'll merge it.

  9. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    There's another wrinkle: if the vector contains NaNs then i?amax is supposed to return the index of the first NaN. I think I can handle this too but it will certainly need some testing.

  10. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    @matcross I made a PR (#382) with a possible fix. This should also fix issues with NaNs. I recommend testing on arrays of size 23 with value like you have now, and also on arrays of the form (0 [repeats n times], NaN [repeats 23-n times]).

  11. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    OK, I wrote a new test program to test a couple extra facets of the code:

    Program p
      use, intrinsic :: ieee_arithmetic
      Integer i, n, i_exp, nmax
      real :: fnan
      double precision :: dnan
    
      fnan = ieee_value(fnan, ieee_quiet_nan)
      dnan = ieee_value(dnan, ieee_quiet_nan)
    
      do nmax=3,23,4
    
      print *, 'nmax =',nmax
    
      call test('constr', 0, nmax-1, -1, 0.D0, 42.D0)
      call test('constl', 1, nmax, 1, 42.D0, 0.D0)
      call test('nanr', 0, nmax-1, -1, 0.D0, dnan)
      call test('nanl', 1, nmax, 1, dnan, 0.D0)
    
      enddo
    Contains
      subroutine test(c, a, b, i_exp0, val1, val2)
        integer :: a, b, i_exp0, i_exp
        double precision :: val1, val2
        Real, Allocatable :: f(:)
        Double Precision, Allocatable :: x(:)
        Character (*) :: c
        Character (:), Allocatable :: c2
        
        do n=a,b
    
        i_exp = i_exp0
        if (i_exp .eq. -1) i_exp = n+1
    
        c2 = ' double, '//c
        x = [ (val1,i=1,n), (val2,i=n+1,nmax) ]
        !print '(23F6.1)', x
        i = idamax(size(x), x, 1)
        Call check_result(c2, i, i_exp)
    
        enddo
        do n=a,b
    
        i_exp = i_exp0
        if (i_exp .eq. -1) i_exp = n+1
    
        c2 = '  float, '//c
        f = [ (val1,i=1,n), (val2,i=n+1,nmax) ]
        !print '(23F6.1)', f
        i = isamax(size(f), f, 1)
        Call check_result(c2, i, i_exp)
    
        enddo
      end subroutine
      Subroutine check_result(c, i, i_exp)
        integer :: i, i_exp
        character (:), allocatable :: c
        Print *, merge('PASS', 'FAIL', i==i_exp) // c, ' expected ', i_exp, &
          ' got ', i
      End Subroutine
    End Program

    #382 works for the constl and constr tests but NaN is still a problem. Will fix.

  12. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    @matcross, I finally managed to test #382 completely and it works as expected now. Can you comment on why the LAPACK test suite fails for the previous behavior? Perhaps it is explicitly checking this behavior and otherwise it would be fine?

  13. matcross commented on Mar 22, 2020

    @matcross
    Author

    Thanks a lot guys (particularly @devinamatthews )

    I'll start a test run with our library tomorrow when I'm back in the 'office' and feed back here.

    We were seeing incorrect pivots ipiv returned by dgetrf for singular input A, a number of failures in the example programs that we distribute for LAPACK eigensolvers, and another in our example program for a Jacobi solver of sparse linear systems.

  14. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    Hmm, so I guess the result with the "wrong" pivots would be numerically equivalent but not the particular solution expected. Well, that's the interface so we'll live with it.

  15. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    @fgvanzee I should note that there is another lurking problem: the indices are stored as floats, so they could overflow/lose precision for very large arrays. Technically we should use proper integer vectors; I suppose we could use uint32_t's and split very large arrays into chunks?

  16. matcross commented on Mar 22, 2020

    @matcross
    Author

    Hmm, so I guess the result with the "wrong" pivots would be numerically equivalent but not the particular solution expected. Well, that's the interface so we'll live with it.

    In this particular instance we were seeing that the dgetrf output didn't remotely satisfy A = PLU.

  17. devinamatthews commented on Mar 22, 2020

    @devinamatthews
    Member

    Weird. But I am not a numerical analysis person so I couldn't say why that is.

  18. matcross commented on Mar 23, 2020

    @matcross
    Author

    I'll start a test run with our library tomorrow when I'm back in the 'office' and feed back here.

    Yes, this is looking good. Thanks again.

  19. fgvanzee commented on Mar 23, 2020

    @fgvanzee
    Member

    @fgvanzee I should note that there is another lurking problem: the indices are stored as floats, so they could overflow/lose precision for very large arrays. Technically we should use proper integer vectors; I suppose we could use uint32_t's and split very large arrays into chunks?

    Could you clarify what code you're talking about? The reference kernels store the index as integers. Are you referring to the vector intrinsic code in kernels/zen/1/bli_amaxv_zen_int.c?

  20. fgvanzee commented on Mar 23, 2020

    @fgvanzee
    Member

    Could you clarify what code you're talking about? The reference kernels store the index as integers. Are you referring to the vector intrinsic code in kernels/zen/1/bli_amaxv_zen_int.c?

    Nevermind, I just saw #386.

  21. added a commit that references this issue on Mar 24, 2020
  22. fgvanzee commented on Mar 24, 2020

    @fgvanzee
    Member

    I just merged #382, so this should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions