Skip to content

lacpy routines are not optimal for copying the lower triangular part of a fat matrix #1183

Closed

Description

@putianyi889

Description
For the lower triangular part, the routines go through all columns and then below-diagonal entries in each column. However, for a matrix with a small height and a large width, unnecessary columns are looped, hurting performance.

Example for reference JuliaLang/LinearAlgebra.jl#1541

Checklist

  • I've included a minimal example to reproduce the issue
  • I'd be willing to make a PR to solve this issue

Activity

  1. langou commented on Jan 14, 2026

    @langou
    Contributor

    If I understand correctly, the idea is to change this line

    DO 40 J = 1, N

    with something like

             DO 40 J = 1, MIN(M,N)

    in DLACPY, SLACPY, ZLACPY and CLACPY.

    This sounds good to me.

    Three comments:

    ( 1 / 3 ) You would hope that the compiler automatically does the optimization when it sees:

             DO 40 J = 1, N
                DO 30 I = J, M
    
                   [ . . . ]
    
       30       CONTINUE
       40    CONTINUE

    I do not know whether compilers do this or not.

    ( 2 / 3 ) This proposed optimization could also be done in LANTR for example.

    ( 3 / 3 ) For LANTR, LACPY, etc., one idea might have been that the user would call

    DLACPY( 'L', 3, 3, A, LDA, B, LDB )

    and not

    DLACPY( 'L', 3, 100000, A, LDA, B, LDB )

    and, if the latter, well, performance hit would ensue.

  2. putianyi889 commented on Jan 14, 2026

    @putianyi889
    Author

    ( 3 / 3 ) For LANTR, LACPY, etc., one idea might have been that the user would call

    DLACPY( 'L', 3, 3, A, LDA, B, LDB )
    and not

    DLACPY( 'L', 3, 100000, A, LDA, B, LDB )
    and, if the latter, well, performance hit would ensue.

    Very helpful. Adding this information to documentation would also close this issue IMO.

    Since the 'U' part also has a min, I would prefer your first solution.

             DO 20 j = 1, n
                DO 10 i = 1, min( j, m )
                   b( i, j ) = a( i, j )
       10       CONTINUE
       20    CONTINUE
  3. martin-frbg commented on Apr 21, 2026

    @martin-frbg
    Collaborator

    Hmm, between the "let the compiler do this" and "let the user do this", I do not think I saw a clear argument for not implementing this trivial optimization in LAPACK ?

  4. putianyi889 commented on Apr 22, 2026

    @putianyi889
    Author

    My bad. I thought #14 fixed this.

  5. added a commit that references this issue on Jun 10, 2026
    29aba40
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions