-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Specialize copyto!
for triangular matrices
#52730
Conversation
From the test failure, it looks like we may need something like #52528 before this to avoid accessing uninitialized indices in |
9a9fcb8
to
a6bcc53
Compare
What's up with this PR? Seems to be in good shape, doesn't it? |
I don't see a test failure here? Was that meant on a different PR? The |
I think we need to fix One workaround for this PR might be to use |
a6bcc53
to
8765286
Compare
I think this broke JuMP: julia> using JuMP, LinearAlgebra
julia> model = Model();
julia> @variable(model, x[1:2, 1:2], Symmetric)
2×2 Symmetric{VariableRef, Matrix{VariableRef}}:
x[1,1] x[1,2]
x[1,2] x[2,2]
julia> Matrix(UpperTriangular(x))
ERROR: UndefRefError: access to undefined reference
Stacktrace:
[1] getindex
@ ./essentials.jl:895 [inlined]
[2] getindex
@ ./array.jl:910 [inlined]
[3] _zero
@ ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LinearAlgebra/src/dense.jl:113 [inlined]
[4] triu!(M::Matrix{AffExpr}, k::Int64)
@ LinearAlgebra ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LinearAlgebra/src/dense.jl:145
[5] triu!
@ ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LinearAlgebra/src/generic.jl:509 [inlined]
[6] _copyto!
@ ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LinearAlgebra/src/triangular.jl:528 [inlined]
[7] copyto!
@ ~/.julia/juliaup/julia-nightly/share/julia/stdlib/v1.12/LinearAlgebra/src/triangular.jl:522 [inlined]
[8] copyto_axcheck!
@ ./abstractarray.jl:1175 [inlined]
[9] Matrix{AffExpr}(x::UpperTriangular{VariableRef, Symmetric{VariableRef, Matrix{VariableRef}}})
@ Base ./array.jl:606
[10] (Matrix)(x::UpperTriangular{VariableRef, Symmetric{VariableRef, Matrix{VariableRef}}})
@ MutableArithmetics ~/.julia/packages/MutableArithmetics/iovKe/src/dispatch.jl:563
[11] top-level scope
@ REPL[42]:1 |
The issue seems to be with julia> M = Matrix{AffExpr}(undef, 2, 2)
2×2 Matrix{AffExpr}:
#undef #undef
#undef #undef
julia> triu!(M);
ERROR: UndefRefError: access to undefined reference
Stacktrace:
[1] getindex
@ ./essentials.jl:14 [inlined]
[2] triu!(M::Matrix{AffExpr}, k::Int64)
@ LinearAlgebra ~/.julia/juliaup/julia-1.10.3+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/dense.jl:139
[3] triu!(M::Matrix{AffExpr})
@ LinearAlgebra ~/.julia/juliaup/julia-1.10.3+0.x64.linux.gnu/share/julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:435
[4] top-level scope
@ REPL[12]:1 We had actually made julia> LinearAlgebra.haszero(AffExpr)
false
julia> zero(AffExpr)
0 in which case the julia> LinearAlgebra.haszero(::Type{AffExpr}) = true
julia> Matrix(UpperTriangular(x))
2×2 Matrix{AffExpr}:
x[1,1] x[1,2]
0 x[2,2] I wonder if we need to be more conservative with |
We should make the behavior consistent with v1.10, and allow types to opt-in to a more efficient implementation via the trait. We should try to avoid breaking changes in minor Julia releases (although I get that LinearAlgebra is particularly prone to these issues, and we've worked around things before). |
This provides a performance boost in copying a triangular matrix to a `StridedMatrix`, which is a common operation (e.g. in broadcasting or in `Matrix(::UpperTriangular)`). The main improvement is improved cache locality for strided triangular matrices by fusing the loops. On master ```julia julia> U = UpperTriangular(rand(4000,4000)); julia> @Btime Matrix($U); 64.649 ms (3 allocations: 122.07 MiB) ``` This PR ```julia julia> @Btime Matrix($U); 48.332 ms (3 allocations: 122.07 MiB) ```
This provides a performance boost in copying a triangular matrix to a
StridedMatrix
, which is a common operation (e.g. in broadcasting or inMatrix(::UpperTriangular)
). The main improvement is improved cache locality for strided triangular matrices by fusing the loops.On master
This PR