Skip to content

LinSolverDirectKLU quietly assumes each matrix is a CSC matrix #251

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
pelesh opened this issue Apr 5, 2025 · 3 comments
Open

LinSolverDirectKLU quietly assumes each matrix is a CSC matrix #251

pelesh opened this issue Apr 5, 2025 · 3 comments
Assignees
Labels
bug Something isn't working direct solver

Comments

@pelesh
Copy link
Collaborator

pelesh commented Apr 5, 2025

Summary

Direct solver LinSolverDirectKLU will accept as an input any matrix::Sparse object, however, it will quietly assume the input matrix is in CSC format. This will likely lead to incorrect result if a nonsymmetric CSR matrix is passed as the input. It would be good to enable LinSolverDirectKLU to operate correctly on general CSR matrices in addition to CSC.

Rationale

Typically sparse matrices are represented in CSR format and most of the Re::Solve use cases of interest involve CSR matrices.

Description

As the first step, LinSolverDirectKLU should check the matrix type ID, and exit immediately if the matrix is neither CSC nor CSR.

If the matrix is CSC, the computation currently implemented should be executed.

If the matrix is CSR, we could use the fact that $\mathrm{csc}(A) = \mathrm{csr}(A^T)$. Passing CSR matrix to KLU factorization should then give factors like

$$A^T = (LU)^T = U^T L^T$$

Based on this, we can implement getLFactor and getUFactor to return proper output when $A$ is a CSR matrix.

KLU provides function klu_tsolve which will do backward-forward substitution for $A^T$, which is de facto matrix stored in CSR format.

Additional information

SUNDIALS wrapper for KLU could provide ideas how to implement similar in LinSolverDirectKLU.

@pelesh pelesh added bug Something isn't working direct solver labels Apr 5, 2025
@pelesh pelesh added this to the Release 0.99.2 milestone Apr 5, 2025
@shakedregev
Copy link
Collaborator

Can you provide more details on the location of this "quiet assumption". Is it the analyze function?

@shakedregev
Copy link
Collaborator

shakedregev commented Apr 7, 2025

@pelesh - attempting to fix this here. Not sure why such a simple fix makes the test fail. I haven't even created a test with CSR yet, this is still running the old test.

@shakedregev
Copy link
Collaborator

shakedregev commented May 13, 2025

See here.
I commented out these lines just to verify that this is indeed what is making the tests fail. If I convert the matrix to csc, all KLU tests fail the residual norm test.

27/42 Testing: klu_klu_test
27/42 Test: klu_klu_test
Command: "/home/szb/ReSolve_dir/build/tests/functionality/klu_klu_test.exe" "/home/szb/ReSolve_dir/ReSolve/tests/functionality/" "-d" "/home/szb/ReSolve_dir/ReSolve/tests/functionality/"
Directory: /home/szb/ReSolve_dir/build/tests/functionality
"klu_klu_test" start time: May 13 20:25 UTC
Output:
----------------------------------------------------------
[^[[1;31mERROR^[[0m] Symbolic_ factorization failed with Common_.status = -3
KLU factorize status: 1

Results (first matrix):

         Residual norm           ||b-A*x||               : 1.9417829952960370e+11
         Relative residual norm  ||b-A*x||/||b||         : 9.8560450386021912e+04
         Error norm              ||x-x_true||            : 1.9701241923392559e+06
         Relative error norm     ||x-x_true||/||x_true|| : 2.8909960511985591e+04
         Exact solution residual ||b-A*x_true||          : 0.0000000000000000e+00
Result inaccurate!
KLU refactorization status: 1

Results (second matrix):

         Residual norm           ||b-A*x||               : 1.9906908563755438e+11
         Relative residual norm  ||b-A*x||/||b||         : 9.9766685823954322e+04
         Error norm              ||x-x_true||            : 1.9953262733661148e+06
         Relative error norm     ||x-x_true||/||x_true|| : 2.9279780430008756e+04
         Exact solution residual ||b-A*x_true||          : 0.0000000000000000e+00
Result inaccurate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working direct solver
Projects
None yet
Development

No branches or pull requests

2 participants