JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.73k stars 5.48k forks source link

ldiv! with transpose uses less allocations than ldiv! #31377

Open wsshin opened 5 years ago

wsshin commented 5 years ago

ldiv! with transposed LU factors uses less allocations and ldiv! with the original LU factors. For example,

julia> VERSION
v"1.2.0-DEV.487"

julia> using LinearAlgebra, BenchmarkTools

julia> A = rand(3,3); b = rand(3); x = rand(3); F = lu(A);

julia> @btime ldiv!($x, $F, $b);
  169.591 ns (3 allocations: 160 bytes)

julia> @btime ldiv!($x, transpose($F), $b);
  103.688 ns (0 allocations: 0 bytes)

Is this expected behavior?

dkarrasch commented 2 years ago

The allocations are (still) due to (internal) views. For the transpose/adjoint case, we fall back to some 3-arg matrix ldiv! instead of to the 3-arg ldiv!(y, F::Factorization, x). We should perhaps widen the signature of the factorization-based method to handle non-square cases correctly.