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

1d set/getindex for sparse #7047

Merged
merged 2 commits into from
Jun 6, 2014
Merged

1d set/getindex for sparse #7047

merged 2 commits into from
Jun 6, 2014

Conversation

tanmaykm
Copy link
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
Member

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

@tanmaykm
Copy link
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
Member

How about doing it as part of this PR?

@IainNZ
Copy link
Member

IainNZ commented May 30, 2014

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

@ViralBShah
Copy link
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
Copy link
Member

IainNZ commented May 31, 2014

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
Member Author

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

@ViralBShah
Copy link
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
Copy link
Member Author

tanmaykm commented Jun 4, 2014

Added implementation for SparseMatrixCSC and rebased.

@ViralBShah
Copy link
Member

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

@tanmaykm
Copy link
Member Author

tanmaykm commented Jun 4, 2014

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

@tanmaykm
Copy link
Member Author

tanmaykm commented Jun 5, 2014

@ViralBShah Rebased and passing the tests.

@ViralBShah
Copy link
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
Member

Also Cc:@mauro3

ridx = mid + 1
end
end

Copy link
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
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
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
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
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
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
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
Copy link
Member Author

tanmaykm commented Jun 5, 2014

Removed the abstract methods.

@tanmaykm
Copy link
Member Author

tanmaykm commented Jun 6, 2014

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
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
Copy link
Member

jiahao commented Jun 6, 2014

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
Member

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

@jiahao
Copy link
Member

jiahao commented Jun 6, 2014

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

@mbauman
Copy link
Member

mbauman commented Jun 6, 2014

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

@ViralBShah
Copy link
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
Copy link
Member

mbauman commented Jun 6, 2014

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
Copy link
Contributor

mauro3 commented Jun 6, 2014

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