Eliminate zeros in sparse spectral operators #569
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #565, I implemented dedicated functionality for block-diagonal matrices and said I didn't know why this uses less memory than constructing block-diagonal matrices from all possible blocks in scipy. I have since figured out why.
The memory size of a sparse matrix is proportional to the number of non-zero entries in the matrix. However, setting individual values in a matrix to zero does not remove them from storage, but keeps them around as non-zero entries with value zero. You have to eliminate zeros explicitly. See the following example:
After adding
eliminate_zeros
after construction of linear operators, there is no more memory to be saved by using special block-diagonal functions. Therefore, I essentially reverted #565 and memory is now even more reduced.Note that cupy does support this function, but I kept getting memory errors after using it. I suspect this is a bug in cupy. The solution I found for the moment was copying the matrices back to CPU, eliminating zeros there and then copying to GPU again. This is not great, but given that this only happens before a run is started, it seems fine for now.