SparseArrays.jl icon indicating copy to clipboard operation
SparseArrays.jl copied to clipboard

Base.stack is underperforming for SparseArrays

Open rcalxrc08 opened this issue 1 year ago • 4 comments

Describe the bug Stacking arrays of SparseArrays is slow. Similar to JuliaGPU/CUDA.jl#2248.

To reproduce

The Minimal Working Example (MWE) for this bug:

using BenchmarkTools, SparseArrays;
N=100;
M=1000;
x=spzeros(N);
x[1]=10.0;
x_dense=collect(x);
@btime stack(fill($x,$M));
@btime stack(fill($x_dense,$M));
@btime sparse(stack(fill(collect($x),$M)));

As timing I am getting:

2.211 ms (15 allocations: 75.20 KiB)
74.900 μs (3 allocations: 789.23 KiB)
194.600 μs (7 allocations: 813.98 KiB)

Version info

Details on Julia: 1.10

Julia Version 1.10.0
Commit 3120989f39 (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
  Threads: 1 on 8 virtual cores

rcalxrc08 avatar Jan 23 '24 12:01 rcalxrc08

Stacking sparse arrays should be slower compared to dense arrays. Is this issue that it is too slow (compared to some other system, or a regression)?

ViralBShah avatar Apr 02 '24 16:04 ViralBShah

Stacking sparse arrays should be slower compared to dense arrays. Is this issue that it is too slow (compared to some other system, or a regression)?

Should it be slower than moving to dense, stacking, and moving back to Sparse? I don't think so

rcalxrc08 avatar Apr 02 '24 18:04 rcalxrc08

Yes, making a sparse matrix dense and then back sparse is a very expensive operation.

ViralBShah avatar Apr 02 '24 19:04 ViralBShah