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

[WIP] Support Eigen::Map<Eigen::SparseMatrix> #782

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

Conversation

alecjacobson
Copy link

If completed would fix #781.

Phew, I learned a lot poking around, but I'm mostly just cribbing the original Eigen::SparseMatrix support. I have barely tested any of this. I will use this PR to add tests etc.

@wjakob
Copy link
Owner

wjakob commented Nov 14, 2024

Hi Alec, thank you very much for working on this, zero-copy sparse matrix passing will be useful. There were still some internal comments and questions inside the PR, so I am assuming that it is still in progress. Please let me know if/when you'd like me to do a review.

Comment on lines +178 to +181
object matrix_type = module_::import_("scipy.sparse").attr(RowMajor ? "csr_matrix" : "csc_matrix");
if (!obj.type().is(matrix_type))
obj = matrix_type(obj);
} catch (const python_error &) {
Copy link

@bilmes bilmes Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment on lines 178-180. I might not be understanding this precisely, but doesn't this bit of code attempt to convert (line 180) obj to a sparse matrix if it is not already one? If so, this might cause issues where an overloaded routine that could take either a dense or sparse matrix, if the sparse matrix routine is considered first, anything that can be converted to a sparse matrix via csr_matrix (such as an int, float, or dense matrix) would then be converted to a sparse matrix and that's probaby not desirable. A simple fix would be to just fail (return false) if obj.type().is(matrix_type) is not true.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, another minor comment on the above. If it is the case that you do end up returning false if !obj.type().is(matrix_type) then the catch block of the exception I'd guess should probably re-throw the exception since it probably means something is wrong other than that the object is not a sparse matrix (e.g., an import or some other error). I.e., the return of true/false should probably (unless I don't understand things) only happen if there are no errors. Similarly, in the below, when you are returning false, this means that it passes the above type check but there is still a problem getting, say, indices, and I'd guess that would mean something is quite wrong (not well indicated by a false return) with the object and/or setup.

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

Successfully merging this pull request may close these issues.

[BUG]: scipy.sparse._csc.csc_matrix → Eigen::SparseMatrix appears to incur a copy
3 participants