Skip to content

Try: safe scaling xNRM2 - #514

Merged
weslleyspereira merged 3 commits into
Reference-LAPACK:masterfrom
weslleyspereira:try-xNRM2-from-Ed-Anderson
Apr 6, 2021
Merged

weslleyspereira merged 3 commits into
Reference-LAPACK:masterfrom
weslleyspereira:try-xNRM2-from-Ed-Anderson

Conversation

@weslleyspereira

Copy link
Copy Markdown
Collaborator

Edward Anderson stated in his paper https://doi.org/10.1145/3061665:

The square root of a sum of squares is well known to be prone to overflow and underflow. Ad hoc scaling
of intermediate results, as has been done in numerical software such as the BLAS and LAPACK, mostly
avoids the problem, but it can still occur at extreme values in the range of representable numbers. More
careful scaling, as has been implemented in recent versions of the standard algorithms, may come at the
expense of performance or clarity. This work reimplements the vector 2-norm and the generation of Givens
rotations from the Level 1 BLAS to improve their performance and design. In addition, support for negative
increments is extended to the Level 1 BLAS operations on a single vector, and a comprehensive test suite
for all the Level 1 BLAS is included.

  • This PR replaces the original xNRM2 routines with Edward's safe scaling codes plus some minor modifications.

  • Since the codes were written in the Fortran90 standard, I removed

# Tell CMake that our Fortran sources are written in fixed format.
set(CMAKE_Fortran_FORMAT FIXED)

from the CMakeLists.txt. The compiler now determines the Fortran layout by the file extension.

@codecov

codecov Bot commented Mar 15, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #514 (0791c22) into master (5a929d8) will decrease coverage by 0.00%.
The diff coverage is n/a.

❗ Current head 0791c22 differs from pull request most recent head 92c19c2. Consider uploading reports for the commit 92c19c2 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #514      +/-   ##
==========================================
- Coverage   83.36%   83.35%   -0.01%     
==========================================
  Files        1820     1820              
  Lines      185545   185545              
==========================================
- Hits       154673   154660      -13     
- Misses      30872    30885      +13     
Impacted Files Coverage Δ
SRC/clarrv.f 61.30% <0.00%> (-3.58%) ⬇️
SRC/slarrb.f 86.81% <0.00%> (-3.30%) ⬇️
SRC/slaein.f 78.44% <0.00%> (-2.30%) ⬇️
SRC/dtrexc.f 89.07% <0.00%> (-1.69%) ⬇️
SRC/dtrsna.f 97.27% <0.00%> (-1.37%) ⬇️
SRC/sbbcsd.f 92.34% <0.00%> (-0.88%) ⬇️
SRC/slaed6.f 84.32% <0.00%> (-0.75%) ⬇️
SRC/claqr0.f 84.35% <0.00%> (-0.69%) ⬇️
SRC/chgeqz.f 92.68% <0.00%> (-0.61%) ⬇️
SRC/dhgeqz.f 95.70% <0.00%> (-0.50%) ⬇️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6728b6d...92c19c2. Read the comment docs.

@weslleyspereira

Copy link
Copy Markdown
Collaborator Author

These new routines propagate Infs and NaNs. Check with the following code:

program testCornerCasesIn_xNRM2
    implicit none
    integer, parameter :: wp = kind(1.e0)
    real(wp), external :: snrm2

    real(wp), parameter :: zero = 0
    real(wp), parameter :: pinf = 2*huge(zero)
    real(wp), parameter :: ninf = -pinf-1
    real(wp) :: aNaN

    integer, parameter :: n = 3
    real(wp) x(n)
    real(wp) r
    x = (/ 1,2,3 /)

    aNaN = zero
    aNaN = zero/aNaN

    ! Tests for NaN
    print *, "aNaN = ", aNaN
    print *, "(aNaN > 0) = ", (aNaN > 0)
    print *, "(aNaN > +inf) = ", (aNaN > pinf)
    print *, "(aNaN < +inf) = ", (aNaN < pinf)
    print *, "(aNaN > -inf) = ", (aNaN > ninf)
    print *, "(aNaN < -inf) = ", (aNaN < ninf)
    print *, "(aNaN /= aNaN) = ", (aNaN /= aNaN)
    print *, " "

    ! Test without NaN
    r = snrm2(n, x, 1)
    print *, "x = ", x
    print *, "nrm2(x) = ", r
    print *, " "

    ! Test with Infs
    x(3) = pinf
    print *, "x = ", x
    r = snrm2(n, x, 1)
    print *, "nrm2(x) = ", r
    print *, " "
    x(2) = ninf
    print *, "x = ", x
    r = snrm2(n, x, 1)
    print *, "nrm2(x) = ", r
    print *, " "
    x(1) = pinf
    print *, "x = ", x
    r = snrm2(n, x, 1)
    print *, "nrm2(x) = ", r
    print *, " "

    ! Test with NaN
    x(3) = aNaN
    print *, "x = ", x
    r = snrm2(n, x, 1)
    print *, "nrm2(x) = ", r
    print *, " "
    x(3) = pinf
    x(2) = aNaN
    print *, "x = ", x
    r = snrm2(n, x, 1)
    print *, "nrm2(x) = ", r

end program testCornerCasesIn_xNRM2

Results:

 aNaN =               NaN
 (aNaN > 0) =  F
 (aNaN > +inf) =  F
 (aNaN < +inf) =  F
 (aNaN > -inf) =  F
 (aNaN < -inf) =  F
 (aNaN /= aNaN) =  T
  
 x =    1.00000000       2.00000000       3.00000000    
 nrm2(x) =    3.74165750    
  
 x =    1.00000000       2.00000000             Infinity
 nrm2(x) =          Infinity
  
 x =    1.00000000            -Infinity         Infinity
 nrm2(x) =          Infinity
  
 x =          Infinity        -Infinity         Infinity
 nrm2(x) =          Infinity
  
 x =          Infinity        -Infinity              NaN
 nrm2(x) =               NaN
  
 x =          Infinity              NaN         Infinity
 nrm2(x) =               NaN

@weslleyspereira weslleyspereira added this to the Next Release milestone Mar 18, 2021

@langou langou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. We will wait for 3.10.0 to release this. Part of Ed Anderson's contributions. I like the fact that you hard-coded la_constants.f90 in this file for now. I am not sure we want to introduce la_constants.f90 in BLAS. (Whereas it is great in LAPACK.) BLAS does not seem to need these constants much. There are in NRM2(), and probably ROT(). Note: Weslley only pushed Ed's contributions to BLAS/NRM2() and LAPACK/ROT() but not BLAS/ROT().

@weslleyspereira

weslleyspereira commented Mar 20, 2021 •

Copy link
Copy Markdown
Collaborator Author

Some comments about the implementation:

  • BLAS does not have ISINFINTY(). I used aNumber > huge(0.0_wp) to verify the number is +infinity.
  • BLAS does not check NaNs. I used aNumber /= aNumber to verify if the number is NaN. By definition, NAN is not equal to anything, even itself.

@weslleyspereira
weslleyspereira force-pushed the try-xNRM2-from-Ed-Anderson branch from 510ac6f to 0791c22 Compare March 31, 2021 21:55
@weslleyspereira
weslleyspereira merged commit 4e40cbe into Reference-LAPACK:master Apr 6, 2021
christoph-conrads pushed a commit to christoph-conrads/lapack that referenced this pull request May 23, 2021
…2-from-Ed-Anderson

Try: safe scaling xNRM2
@martin-frbg martin-frbg mentioned this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants