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

inv or logdet for symmetric matrices #977

Open
theogf opened this issue Jun 14, 2021 · 2 comments
Open

inv or logdet for symmetric matrices #977

theogf opened this issue Jun 14, 2021 · 2 comments
Labels
cuda array Stuff about CuArray. enhancement New feature or request good first issue Good for newcomers

Comments

@theogf
Copy link

theogf commented Jun 14, 2021

If you are looking like me to compute logdet on Symmetric matrices you need to go call some "low-level" functions in CUDA.CUSOLVER
This is connected to #116

Here is an unsafe gist for logdet :

function LinearAlgebra.logdet(A::CUDA.CuMatrix)
    d_A = copy(A) # Create a copy
    _, info = CUDA.CUSOLVER.potrfBatched!('L', [d_A]) # Update d_A in-place to be passed to a Cholesky constructor
    L = LinearAlgebra.Cholesky(d_A, 'L', first(info)).L # Create the corresponding lower-triangular matrix
    return 2 * sum(log, diag(L)) # Compute logdet by summing the elements of the diagonal
end

I suppose this could be improved and be more faithful to the implementation in LinearAlgebra but that's further work
For inv one can do a similar thing :

function LinearAlgebra.inv(A::CUDA.CuMatrix)
    d_A = copy(A)
    _, info = CUDA.CUSOLVER.potrfBatched!('L', [d_A])
    L = LinearAlgebra.Cholesky(d_A, 'L', first(info)).L
    return (I / L) / L'
end

If you'd rather use lu instead replace the second and third line of each algorithm by

    d_A, p_inv, info = CUDA.CUSOLVER.getrf!(d_A) # Update d_A in-place to be passed to be passed to a LU constructor
    fact = LinearAlgebra.LU(d_A, Int64.(collect(p_inv), zero()) # Create a LU object

and change the last line accordingly

@maleadt maleadt added the cuda array Stuff about CuArray. label Jun 18, 2021
@maleadt maleadt changed the title Useful gists for inv or logdet for symmetric matrices (before a PR) inv or logdet for symmetric matrices Jun 18, 2021
@theogf
Copy link
Author

theogf commented Jun 21, 2021

I just noted that (I / L) / L' is definitely not good since it is calling the generic LinearAlgebra functions and are therefore using scalar indexing

@kshyatt
Copy link
Contributor

kshyatt commented Feb 13, 2025

inv at least works currently, though logdet does not. Would be a nice first issue!

@kshyatt kshyatt added enhancement New feature or request good first issue Good for newcomers labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuda array Stuff about CuArray. enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants