Skip to content

Pointers to memory aren't passed when unsafe = true #160

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

Open
luke-kiernan opened this issue Apr 4, 2025 · 1 comment
Open

Pointers to memory aren't passed when unsafe = true #160

luke-kiernan opened this issue Apr 4, 2025 · 1 comment

Comments

@luke-kiernan
Copy link
Contributor

Quoting from the docs: "When unsafe=true is passed, if the type of A is already consistent with the type of mumps, then pointers to A's memory are passed directly to MUMPS, so modifying A will modify the matrix in mumps." MUMPS requires the matrix to be in coordinate form: the code calls findnz, then sets the mumps object's pointers to the result. So changes in A's data aren't reflected in the mumps object.

Short demonstration of this:

using MUMPS, MPI, SparseArrays
MPI.Init()
A = sparse([1, 1, 2, 2], [1, 2, 1, 2], [1.0, 0.0, 0.0, 1.0])
# A is [1.0 0.0; 0.0 1.0], but with all 4 elements structurally nonzero.
rhs = [1.0, 0.0]
mumps = Mumps{Float64}(mumps_unsymmetric, default_icntl, default_cntl64)
associate_matrix!(mumps, A; unsafe = true)
associate_rhs!(mumps, rhs)
A[1,1] = 0.0; A[1, 2] = 1.0; A[2,1] = 1.0; A[2,2] = 0.0 # now A is [0.0 1.0; 1.0 0.0]
factorize!(mumps); solve!(mumps)
x = MUMPS.get_sol(mumps)
@assert A*x == rhs # nope! x is [1.0, 0.0], the solution before we changed A.

The easy fix to this would be to simply change the docs. However, I'm hoping to use this functionality. Let's see...maybe add SparseArraysCOO.jl as a dependency and implement associate_matrix!(A::SparseMatrixCOO)?

If adding that dependency is ok, I'd be willing to write the implementation.

@amontoison
Copy link
Member

amontoison commented Apr 4, 2025

@luke-kiernan It is fine to add an extension for SparseMatricesCOO.jl.
We added one for QRMumps.jl:
https://github.com/JuliaSmoothOptimizers/QRMumps.jl/tree/main/ext

I am wondering if we should not create a function to update the pointer only for the nonzeros of A.
It is the only vector that we expect to have new values inside.
Compared to the COO format, only colptr needs to be transformed from a sparse CSC matrix. findnz is maybe not the most efficient routine if we don't want to allocate storage.

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

No branches or pull requests

2 participants