From 8ea8f55205a212b1d3faa9f044de1436b9c9e8b4 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 14 Apr 2025 16:55:49 +0530 Subject: [PATCH 1/3] Unaliasing and short-circuiting in `copytrito!` --- src/generic.jl | 14 ++++++++++---- test/generic.jl | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/generic.jl b/src/generic.jl index de3b195c..44206d6a 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -2088,20 +2088,21 @@ julia> copytrito!(B, A, 'L') function copytrito!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar) require_one_based_indexing(A, B) BLAS.chkuplo(uplo) + B === A && return B m,n = size(A) A = Base.unalias(B, A) if uplo == 'U' LAPACK.lacpy_size_check(size(B), (n < m ? n : m, n)) + # extract the parents for UpperTriangular matrices + Bv, Av = uppertridata(B), uppertridata(A) for j in axes(A,2), i in axes(A,1)[begin : min(j,end)] - # extract the parents for UpperTriangular matrices - Bv, Av = uppertridata(B), uppertridata(A) @inbounds Bv[i,j] = Av[i,j] end else # uplo == 'L' LAPACK.lacpy_size_check(size(B), (m, m < n ? m : n)) + # extract the parents for LowerTriangular matrices + Bv, Av = lowertridata(B), lowertridata(A) for j in axes(A,2), i in axes(A,1)[j:end] - # extract the parents for LowerTriangular matrices - Bv, Av = lowertridata(B), lowertridata(A) @inbounds Bv[i,j] = Av[i,j] end end @@ -2109,5 +2110,10 @@ function copytrito!(B::AbstractMatrix, A::AbstractMatrix, uplo::AbstractChar) end # Forward LAPACK-compatible strided matrices to lacpy function copytrito!(B::StridedMatrixStride1{T}, A::StridedMatrixStride1{T}, uplo::AbstractChar) where {T<:BlasFloat} + require_one_based_indexing(A, B) + BLAS.chkuplo(uplo) + B === A && return B + A = Base.unalias(B, A) LAPACK.lacpy!(B, A, uplo) + return B end diff --git a/test/generic.jl b/test/generic.jl index 11194a60..fdb079ba 100644 --- a/test/generic.jl +++ b/test/generic.jl @@ -879,4 +879,13 @@ end @test mul!(copy!(similar(v), v), v, 2, 2, 0) == 4v end +@testset "aliasing in copytrito! for strided matrices" begin + M = rand(4, 1) + A = view(M, 1:3, 1:1) + A2 = copy(A) + B = view(M, 2:4, 1:1) + copytrito!(B, A, 'L') + @test B == A2 +end + end # module TestGeneric From 268050a8be1b9d97a822640bc2c99d77f5a0f755 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 14 Apr 2025 18:04:42 +0530 Subject: [PATCH 2/3] Move `chkuplo` to `lacpy!` --- src/generic.jl | 1 - src/lapack.jl | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic.jl b/src/generic.jl index 44206d6a..56a6b10f 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -2111,7 +2111,6 @@ end # Forward LAPACK-compatible strided matrices to lacpy function copytrito!(B::StridedMatrixStride1{T}, A::StridedMatrixStride1{T}, uplo::AbstractChar) where {T<:BlasFloat} require_one_based_indexing(A, B) - BLAS.chkuplo(uplo) B === A && return B A = Base.unalias(B, A) LAPACK.lacpy!(B, A, uplo) diff --git a/src/lapack.jl b/src/lapack.jl index 001cfc82..fcbc4db5 100644 --- a/src/lapack.jl +++ b/src/lapack.jl @@ -7176,6 +7176,7 @@ for (fn, elty) in ((:dlacpy_, :Float64), function lacpy!(B::AbstractMatrix{$elty}, A::AbstractMatrix{$elty}, uplo::AbstractChar) require_one_based_indexing(A, B) chkstride1(A, B) + chkuplo(uplo) m, n = size(A) m1, n1 = size(B) if uplo == 'U' From 6bf623af1d07000bc2199e04ce1b321b34440a8e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 14 Apr 2025 21:43:44 +0530 Subject: [PATCH 3/3] Revert "Move `chkuplo` to `lacpy!`" This reverts commit 4aa966fbd8a13ccf65737be1ea029d209d390c00. --- src/generic.jl | 1 + src/lapack.jl | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic.jl b/src/generic.jl index 56a6b10f..44206d6a 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -2111,6 +2111,7 @@ end # Forward LAPACK-compatible strided matrices to lacpy function copytrito!(B::StridedMatrixStride1{T}, A::StridedMatrixStride1{T}, uplo::AbstractChar) where {T<:BlasFloat} require_one_based_indexing(A, B) + BLAS.chkuplo(uplo) B === A && return B A = Base.unalias(B, A) LAPACK.lacpy!(B, A, uplo) diff --git a/src/lapack.jl b/src/lapack.jl index fcbc4db5..001cfc82 100644 --- a/src/lapack.jl +++ b/src/lapack.jl @@ -7176,7 +7176,6 @@ for (fn, elty) in ((:dlacpy_, :Float64), function lacpy!(B::AbstractMatrix{$elty}, A::AbstractMatrix{$elty}, uplo::AbstractChar) require_one_based_indexing(A, B) chkstride1(A, B) - chkuplo(uplo) m, n = size(A) m1, n1 = size(B) if uplo == 'U'