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

provide non-aliasing constructors for sparse arrays #34647

Merged
merged 4 commits into from
Feb 5, 2020
Merged

provide non-aliasing constructors for sparse arrays #34647

merged 4 commits into from
Feb 5, 2020

Conversation

KlausC
Copy link
Contributor

@KlausC KlausC commented Feb 3, 2020

This fixes #34630, which does not touch makes copy sparse constructors.

@KlausC KlausC requested a review from dkarrasch February 4, 2020 07:52
@andreasnoack
Copy link
Member

I'm not completely up-to-date on the conversations on the sparse matrix constructors but around 0.6/0.7, we discussed the memory behavior of constructors and convert methods when the input is of the same type and I believe the conclusion was the convert should avoid copying when possible whereas constructors should avoid aliasing.

@KlausC
Copy link
Contributor Author

KlausC commented Feb 4, 2020

convert should avoid copying when possible whereas constructors should avoid aliasing

this implementation of convert avoids copying, if possible (that means the argument has already the target type). If that is not the case, a type conversion is necessary, which implies at least one of the 3 vector fields of SparseMatrixCSC or the 2 vector fields of SparseVector has to be copied, too.
The demand, that the usage of published methods does not surprisingly destroy the integrity of some of the read-only arguments of those methods leads to this solution.

I agree with you, that it would be better to make copies in the constructor SparseMatrixCSC{Tv,Ti}(::AbstractSparseMatrx), but got backfire on this suggestion (see #34630).

@KristofferC
Copy link
Sponsor Member

KristofferC commented Feb 4, 2020

I agree with you, that it would be better to make copies in the constructor SparseMatrixCSC{Tv,Ti}(::AbstractSparseMatrx), but got backfire on this suggestion (see #34630).

But the "constructor" for sparse matrices is sparse?

@KristofferC
Copy link
Sponsor Member

I do agree that it is pretty weird that the SparseMatrixCSC constructor is undocumented. Documenting it and making it the "first class" constructor would make sense to me.

Right now we have SparseMatrixCSC(M::Matrix) = sparse(M) but it should really be the opposite imo.

@KristofferC
Copy link
Sponsor Member

Deciding between doing this for convert and SparseMatrixCSC, I would prefer to copy for SparseMatrixCSC for the reasons outlined in #34630 (comment) (consistency with Array).

@KlausC
Copy link
Contributor Author

KlausC commented Feb 4, 2020

Deciding between doing this for convert and SparseMatrixCSC, I would prefer to copy for SparseMatrixCSC

Ok, that is fine. I will change this PR accordingly.

@KlausC KlausC changed the title provide non-aliasing convert methods for sparse arrays provide non-aliasing constructors for sparse arrays Feb 4, 2020
@KristofferC KristofferC added domain:arrays:sparse Sparse arrays kind:bugfix This change fixes an existing bug labels Feb 4, 2020
@KristofferC KristofferC merged commit 48bdac0 into JuliaLang:master Feb 5, 2020
@KlausC KlausC deleted the krc/sparsealias branch February 5, 2020 13:59
KristofferC pushed a commit that referenced this pull request Apr 11, 2020
* provide non-aliasing convert methods for sparse arrays

* provide non-aliasing constructors for sparse arrays

* make all sparse constructors copy

* remove unnecessary type assetion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays:sparse Sparse arrays kind:bugfix This change fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Aliasing problem with SparseMatrixCSC and SparseVector
4 participants