Eric Johnson
Eric Johnson

Reputation: 714

Factorize a Matrix without Any Memory Allocations in Julia

Julia supports in place factorization of matrices (for some factorizations).
I wonder if one could also eliminate any allocation of memory inside the function.

For instance, is there a way to apply a Cholesky factorization on a matrix with no hidden memory allocation?

Upvotes: 3

Views: 484

Answers (1)

Martin Möddel
Martin Möddel

Reputation: 106

Non allocating LAPACK functions have bindings in Julia. They are documented in Julia Documentation - Linear Algebra - LAPACK Functions.


The Cholesky factorization cholesky!(A) overwrites A and does allocate a fixed small amount of memory, whereas cholesky(A) does allocate a larger amount. Here, allocations (bytes) do grow quadratically with the size of A.

let n = 1000; M = rand(n,n); B = transpose(M)*M
    cholesky(B)
    @time cholesky(B)
    # 0.023478 seconds (5 allocations: 7.630 MiB)
end

vs

let n = 1000; M = rand(n,n); B = transpose(M)*M
    cholesky!(copy(B))
    @time cholesky!(B)
    # 0.021360 seconds (3 allocations: 80 bytes)
end

Performance differences are small as pointed out by Oscar Smith.

Upvotes: 3

Related Questions