Skip to content
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

remove copy-allocation on accessing cholesky factors (.L, .U) #1186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

st--
Copy link
Contributor

@st-- st-- commented Jan 29, 2025

(Port of JuliaLang/julia#42920)

In master, accessing fields L or U of Cholesky or CholeskyPivoted calls copy() on the transpose. Here I add tests thayt check that the accessors don't allocate more memory than is needed for e.g. the Adjoint() construction, and remove the unnecessary copy(). Generally julia seems to be trying hard to avoid unnecessary allocations, so I would consider this to be a bugfix, not a breaking change.

There is a related discourse thread: https://discourse.julialang.org/t/implementation-for-accessing-cholesky-factors/56219

Side note 1: there is also a copy() in the definition of _chol!(A::AbstractMatrix, ::Type{UpperTriangular}) - it's a bit less clear to me in what cases this will get run and whether it's okay to remove the copy(), so let me know if you think it'd be worth spending the time to go deeper into that also.

Side note 2: some of the code tests uplo using Cuplo === char_uplo(d), some of it using sym_uplo(Cuplo) == d, is there any particular reason for this, or would it be worth unifying it?

@stevengj
Copy link
Member

Right now this leads to slightly slower triangular solves:

julia> using BenchmarkTools, LinearAlgebra

julia> A = rand(1000,1000); b = rand(1000); x = copy(b);

julia> U1 = LowerTriangular(A)';

julia> U2 = copy(U1);

julia> @btime ldiv!($x, $U1, $b);
  81.000 μs (0 allocations: 0 bytes)

julia> @btime ldiv!($x, $U2, $b);
  69.875 μs (0 allocations: 0 bytes)

although it still might be worth it to save allocations. (Presumably the ldiv! performance can be fixed by an optimized method if needed.)

Not sure what other code might be affected by this … in general, it seems okay since (a) the documentation does not specify whether a copy is made and (b) it is still returning a subtype of UpperTriangular or LowerTriangular.

@stevengj stevengj added the performance Must go faster label Jan 29, 2025
@ViralBShah
Copy link
Member

Test failures need to be fixed.

Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.89%. Comparing base (443aa0f) to head (cb2e5c7).
Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1186   +/-   ##
=======================================
  Coverage   91.89%   91.89%           
=======================================
  Files          34       34           
  Lines       15360    15360           
=======================================
  Hits        14115    14115           
  Misses       1245     1245           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dkarrasch
Copy link
Member

Right now this leads to slightly slower triangular solves:

This is just a matter of memory access, I think. The solves all run LAPACK's trtrs!.

@dkarrasch
Copy link
Member

... or BLAS: #1194.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants