Comments (9)
I've got some functions for views into sparse matrices (currently here https://github.com/EcoJulia/SpatialEcology.jl/blob/master/src/Sparse_matrixfunctions.jl ), not mostly coded by me, though, but based on answers from @perrutquist and @getzdan .
Would it be interesting for Base to have more functionality for (views into) sparse matrices? alternatively:
Would it be better to make a SparseUtils.jl package with utility functions for sparse matrices (possibly moving Base functions out)?
from sparsearrays.jl.
I think we should have better support for this is base. At least AbstractUnitRange
slices of columns of sparse matrices. I've a few ad hoc implementations of these in various projects and often most of the existing code can be reused just by using a generic function instead of a direct field access.
from sparsearrays.jl.
Not naive at all. I could even get behind doing that for all SubArray
s on the assumption that other .parent
s may specialize on nonscalar indexing.
from sparsearrays.jl.
sum(x,1)
can be thought of as a special case of vector-matrix multiplication, which seems to be slow in general for views into spares matrices.
d = sprand(Float64,10000,10000, 0.01)
i = rand(1:10000,5000)
j = rand(1:10000,9000)
e = view(d, i, j)
v = rand(Float64,1,5000)
f1(v,e) = v*e
f2(v,d,i,j) = (sparse(ones(Int,5000), i, v[:], 1, 10000)*d)[j]
# f2 computes the exact same thing as f1, only faster.
@assert isapprox(f1(v,e)[:], f2(v,d,i,j))
using BenchmarkTools
@benchmark f1($v,$e)
BenchmarkTools.Trial:
memory estimate: 585.39 KiB
allocs estimate: 57
--------------
minimum time: 1.822 s (0.00% GC)
median time: 1.823 s (0.00% GC)
mean time: 1.823 s (0.00% GC)
maximum time: 1.824 s (0.00% GC)
--------------
samples: 3
evals/sample: 1
@benchmark f2($v,$d,$i,$j)
BenchmarkTools.Trial:
memory estimate: 751.31 KiB
allocs estimate: 37
--------------
minimum time: 6.853 ms (0.00% GC)
median time: 6.984 ms (0.00% GC)
mean time: 7.106 ms (0.92% GC)
maximum time: 10.845 ms (27.13% GC)
--------------
samples: 701
evals/sample: 1
from sparsearrays.jl.
There's a framework underway for this kind of thing in https://github.com/timholy/ArrayIteration.jl, but that can't be very efficient until we have JuliaLang/julia#12205 (it creates about a zillion array wrappers, sometimes of fairly small regions, and it would be expensive to heap-allocate them all).
from sparsearrays.jl.
In terms of the copy issue described above, defining simply
mycopy(A::SubArray{R,S,T}) where R where S where T <: SparseMatrixCSC = d.parent[d.indices...]
makes it possible to do
using SparseArrays
c = sprand(Bool,6000,18000, 0.01);
d = view(c, 1:6000, rand(1:18000, 1800));
c1 = copy(d);
@time copy(d)
2.456731 seconds (12 allocations: 18.556 MiB)
c2 = mycopy(d);
@time mycopy(d)
0.000564 seconds (12 allocations: 962.156 KiB)
c2 == c1
true
Which is 3000 times faster (at least with @time
). So it turns out it can indeed be optimised quite easily. Is this something to follow in terms of optimisation, or too naive? @andreasnoack
from sparsearrays.jl.
That sounds amazing, thanks a lot for the response.
from sparsearrays.jl.
As an addition to this, copy
is extremely slow on views into sparse matrices - here's an example:
using SparseArrays
c = sprand(Bool,6000,18000, 0.01)
d = view(c, 1:6000, rand(1:18000, 1800))
copy(d);
@time copy(d)
2.626470 seconds (12 allocations: 18.557 MiB, 0.42% gc time)
2.6 seconds to generate a 6000x1800 sized matrix, a very realistically sized array seems really excessive. I saw recent PRs (JuliaLang/julia#30531) made progress on copying Sparse Vectors, but this does not seem to help this case.
Unfortunately, practically all of my workflow relies on this functionality :-s - so I'm really motivated to help doing something about it, though its mostly out of my pay grade.
from sparsearrays.jl.
Are there any news about views into sparse matrices?
from sparsearrays.jl.
Related Issues (20)
- Regression for `mul!` from 1.9 to 1.10 HOT 11
- Using `@view` leads to 100x performance loss HOT 7
- Loads of warnings about method redefinitions HOT 2
- Wrapper generator needs updating to pick SuiteSparse headers from new location HOT 1
- Single precision support in CHOLMOD HOT 3
- Cholesky F.PtL \ Av where Av is a view does not work HOT 1
- Base.stack is underperforming for SparseArrays HOT 3
- Regression in invalidations caused by SparseArrays HOT 3
- Elementwise multiplication by a view of a dense matrix gives a dense matrix
- `findmin(A; dims=1)` is much slower than manually looping over. HOT 1
- Sparse array of string types HOT 17
- Memory Mapped SparseArrays HOT 3
- Extra allocations when using generalized `mul!` operation
- Attempting to run sparse `qr` produces StackOverflow when run on a sparse matrix of `ForwardDiff.Dual`. HOT 6
- Inconsistent addition between sparse and dense HOT 1
- `ldiv` of `LUFactorization` can throw `SingluarException` HOT 1
- Thread-safe dropstored! HOT 1
- Merge SparseMatricesCSR.jl in HOT 2
- Support zero-based indices HOT 3
- Windows threading tests fail in GitHub Actions CI but pass in Buildkite CI
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sparsearrays.jl.