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

improve performance for sorting columns in sparse matrix (thereby improving sparse matmul) #29682

Merged
merged 1 commit into from
Oct 19, 2018

Conversation

KristofferC
Copy link
Member

@KristofferC KristofferC commented Oct 17, 2018

This improves performance for sparse matrix multiply (by optimizing the sorting done after the multiply is done). The two main optimizations are:

  • use resize! instead of unsafe_wrap, saves two allocations per column
  • Hoist the creation of the Perm object, saves at least an allocation per column

Using the benchmarks in #29022

using SparseArrays
using BenchmarkTools

fun(S) = S*S';
for N = [10000 100000 1000000]
    S = spdiagm(0 => ones(N), 1 => -ones(N-1));
    @show N
    @btime fun($S);
end

Before:

N = 10000
  2.087 ms (30019 allocations: 3.13 MiB)
N = 100000
  23.200 ms (300019 allocations: 31.28 MiB)
N = 1000000
  354.681 ms (3000019 allocations: 312.81 MiB)

After

N = 10000
  536.658 μs (26 allocations: 1.45 MiB)
N = 100000
  6.598 ms (26 allocations: 14.50 MiB)
N = 1000000
  137.312 ms (26 allocations: 144.96 MiB)

And benchmarking the code in #29022 (comment)

using MatrixMarket
using SparseArrays
using BenchmarkTools

urls = [
"https://www.cise.ufl.edu/research/sparse/MM/Wissgott/parabolic_fem.tar.gz",
]

for url in urls
    filename = basename(url)
    if !isfile(filename)
        download(url, filename)
        run(`tar -xvf $filename`)
    end
end


fun(S) = S*S';

for url in urls
    path = split(basename(url), ".")[1]
    M = MatrixMarket.mmread(joinpath(path, path*".mtx"))
    println(path)
    @btime fun($M)
end

Before

  571.827 ms (1577502 allocations: 396.69 MiB)

After

  471.738 ms (28 allocations: 308.44 MiB)

cc @Alexander-Barth

@KristofferC KristofferC added performance Must go faster sparse Sparse arrays labels Oct 17, 2018
@KristofferC KristofferC changed the title improve performance for sorting columns in sparse matrix improve performance for sorting columns in sparse matrix (thereby improving sparse matmul) Oct 17, 2018
@KristofferC KristofferC merged commit 2885b62 into master Oct 19, 2018
@StefanKarpinski StefanKarpinski deleted the kc/perf_sparse_matmab branch October 19, 2018 20:53
KristofferC added a commit that referenced this pull request Oct 29, 2018
KristofferC added a commit that referenced this pull request Oct 31, 2018
KristofferC added a commit that referenced this pull request Nov 2, 2018
KristofferC added a commit that referenced this pull request Feb 11, 2019
KristofferC added a commit that referenced this pull request Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster sparse Sparse arrays
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant