Skip to content

1d set/getindex for sparse#7047

Merged
ViralBShah merged 2 commits into
JuliaLang:masterfrom
tanmaykm:tanmaykm
Jun 6, 2014
Merged

1d set/getindex for sparse#7047
ViralBShah merged 2 commits into
JuliaLang:masterfrom
tanmaykm:tanmaykm

Conversation

@tanmaykm

Copy link
Copy Markdown
Member

ref #7023

This implements setindex! and getindex with 1d and boolean indices for AbstractSparseMatrix. An optimized implementation for SparseMatrixCSC can be done separately.

@ViralBShah

Copy link
Copy Markdown
Member

I suspect these are going to be very slow, especially the setindex cases.

@tanmaykm

Copy link
Copy Markdown
Member Author

Yes. This PR is not marked to fix the issue.
I'll either update this or follow this up with another one with an implementation on SparseMatrixCSC.

@ViralBShah

Copy link
Copy Markdown
Member

How about doing it as part of this PR?

@IainNZ

IainNZ commented May 30, 2014

Copy link
Copy Markdown
Member

I don't understand when this could would be used?

@ViralBShah

Copy link
Copy Markdown
Member

Just like you index a dense matrix with a vector, so can you with a sparse matrix. See the linked issue. It is also needed for completeness. Currently the following does not work:

julia> s = sprand(5,5,0.5);
julia> x = [5,6,7];
julia> s[x]
ERROR: no method similar(SparseMatrixCSC{Float64,Int64}, Type{Float64}, (Int64,))
 in getindex at abstractarray.jl:364

@IainNZ

IainNZ commented May 31, 2014

Copy link
Copy Markdown
Member

I more meant the "abstractness" of it - surely this would be a lot slower than a specialized method? This would like it would for any AbstractMatrix.

@tanmaykm

Copy link
Copy Markdown
Member Author

The abstract methods would make it quicker to add new concrete types and optimize them incrementally.

@ViralBShah

Copy link
Copy Markdown
Member

I agree. The abstract implementations are not useful except as an implementation guide. We can merge when the implementation of the remaining methods is done.

@tanmaykm

tanmaykm commented Jun 4, 2014

Copy link
Copy Markdown
Member Author

Added implementation for SparseMatrixCSC and rebased.

@ViralBShah

Copy link
Copy Markdown
Member

Tests in the gcc build are failing. I can't quite tell if it is related to this PR or not.

@tanmaykm

tanmaykm commented Jun 4, 2014

Copy link
Copy Markdown
Member Author

It passes now. I had to rename a local variable to something other than sum2, but I wonder why.

@tanmaykm

tanmaykm commented Jun 5, 2014

Copy link
Copy Markdown
Member Author

@ViralBShah Rebased and passing the tests.

@ViralBShah

Copy link
Copy Markdown
Member

Could you remove the AbstractSparseMatrix methods? These are only likely to be slow for any sparse matrix data structure. Otherwise, please feel free to merge whenever you are ready.

@ViralBShah

Copy link
Copy Markdown
Member

Also Cc:@mauro3

Comment thread base/sparse/sparsematrix.jl Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it possible to reuse the binarysearch method in sparsematrix.jl?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think it is possible to reuse the binarysearch method here. This expects slightly different result than what binarysearch would give. Here ridx points to the index uptill row, and has a valid value irrespective of whether row was found in rowval[r1:r2]. And r1 is advanced till ridx so that subsequent searches in the same column are narrower.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sounds like the binary search which is implemented in sort.jl under the name of searchsortedfirst (which was not what I needed, thus I wrote my own). Also, note that you should probably use >>>, see http://googleresearch.blogspot.co.uk/2006/06/extra-extra-read-all-about-it-nearly.html

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, searchsortedfirst indeed can be used. I shall update the PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am curious if this has any performance impact. Worth checking for sure.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Will check that. There are a few other places in sparsematrix.jl where searchsorted can be used. Shall replace them as well if there's no performance impact.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is a difference in performance. Here's a code snippet and some results for comparison: https://gist.github.com/tanmaykm/6871d2d014df4a92f40f

But it does not seem significant.

@tanmaykm

tanmaykm commented Jun 5, 2014

Copy link
Copy Markdown
Member Author

Removed the abstract methods.

@tanmaykm

tanmaykm commented Jun 6, 2014

Copy link
Copy Markdown
Member Author

Added changes to use existing search methods. No noticeable performance impact.

Before:

setindex!: elapsed time: 0.013420411599999998 seconds (363840.0 bytes allocated)
diag: elapsed time: 0.0003199585 seconds (8088.0 bytes allocated)

With this change:

setindex!: elapsed time: 0.012916476900000001 seconds (363840.0 bytes allocated)
diag: elapsed time: 0.0003488753 seconds (8088.0 bytes allocated)

Test code: https://gist.github.com/tanmaykm/e878ff0fe14f6b01b6dd

@ViralBShah

Copy link
Copy Markdown
Member

Thanks for working through the various things.

ViralBShah added a commit that referenced this pull request Jun 6, 2014
1d set/getindex for abstract sparse
@ViralBShah
ViralBShah merged commit 5705a10 into JuliaLang:master Jun 6, 2014
@jiahao

jiahao commented Jun 6, 2014

Copy link
Copy Markdown
Member

Any idea what's going on here?

$ make
...
linalg.jl
Warning: New definition 
    Ac_ldiv_B! could not show value of type Tuple at linalg/umfpack.jl:356
is ambiguous with: 
    Ac_ldiv_B! could not show value of type Tuple at linalg/umfpack.jl:351.
To fix, define 
    Ac_ldiv_B! could not show value of type Tuple
before the new definition.

@ViralBShah

Copy link
Copy Markdown
Member

I don't get this. I saw this yesterday though, but it seemed to go away after a make clean.

@jiahao

jiahao commented Jun 6, 2014

Copy link
Copy Markdown
Member

Ok. The other odd linalg-related thing is this test failure; it's new to me.

@mbauman

mbauman commented Jun 6, 2014

Copy link
Copy Markdown
Member

I think it's an inference problem. The test works just fine at top-level.

@ViralBShah

Copy link
Copy Markdown
Member

@jiahao The test failure seems to be unrelated to this PR. Should we have a new issue? I don't get the failure on my mac.

@mbauman

mbauman commented Jun 6, 2014

Copy link
Copy Markdown
Member

As far as I'm aware, it's only happening in my dict-print PR: #7147. I cannot reproduce on master on my local machine.

@mauro3

mauro3 commented Jun 6, 2014

Copy link
Copy Markdown
Contributor

I've seen the Ac_ldiv_B! warning popping up at random as well.

tanmaykm added a commit to tanmaykm/julia that referenced this pull request Jun 7, 2014
tanmaykm added a commit to tanmaykm/julia that referenced this pull request Jun 9, 2014
tanmaykm added a commit to tanmaykm/julia that referenced this pull request Jun 9, 2014
also replaced binarysearch with methods from sort.jl
ref JuliaLang#7131, JuliaLang#7047
@ViralBShah ViralBShah changed the title 1d set/getindex for abstract sparse 1d set/getindex for sparse Jun 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sparse Sparse arrays

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants