Skip to content

Parallel smmp - #201

Merged
vbarrielle merged 13 commits into
masterfrom
parallel_smmp
Jul 3, 2020
Merged

vbarrielle merged 13 commits into
masterfrom
parallel_smmp

Conversation

@vbarrielle

Copy link
Copy Markdown
Collaborator

This pull request improves the performance of the sparse matrix product by taking advantage of multiple cores, thanks to rayon. Here are benchmark results demonstrating the improvements (benchmarked on a AMD Ryzen 7 3700X 8-Core Processor this time):

sparse_mult_perf_by_shape_no_old

Here's a description of the entries:

  • the red line corresponds to the SMMP algorithm as implemented in Better performance for sparse matrix product #187.
  • the pink line (boolvec 1T) is a micro-optimization of the SMMP algorithm, using a Vec<bool> in the symbolic part of the algorithm to check if a column has been seen, which is more cache friendly than the index based "linked list" in the original SMMP algorithm
  • boolvec 2T and 4T are the same as boolvec 1T, but with the workloads for the symbolic and numeric parts divided on respectively 2 and 4 threads.
  • boolvec auto dynamically chooses the number of threads, to avoid the threading overhead for small nonzero counts, and to be able to use all CPU cores if needed. This is the most efficient strategy and thus the default, though it can be configured. There may be some tuning to do in the thresholds to pick the number of threads.

As before, with no multithreading, it takes a very large matrix to beat scipy's implementation (mostly because we need to sort the indices whereas scipy does not guarantee sorted indices). However, as far as I know scipy's matrix product is not parallelized, so taking advantage of multiple cores gives sprs an advantage.

On smaller shapes it takes a lot of threads to beat scipy (here the shape is 15000 x 25000):

sparse_mult_perf_150000_25000

I'm not entirely satisfied with the current multithreading implementation, dividing the work into independent chunks has been a bit clunky, but I'm sure it could be done in a cleaner way. That can wait though, the current state is correct and is a nice milestone.

It's quite possible some performance can be gained in the future, as currently the heuristic to divide the workload for the symbolic part has not been extensively tested, so some threads could be starving. A simple improvement could be to delay the sorting of indices, to know the actual number of nonzeros of the result and be able to divide the sorting workload evenly.

vbarrielle added 13 commits July 2, 2020 21:46
Using this bool vector enables to directly store the column indices in
the result indices vector, and we should get less cache misses thanks to
the usage of a boolean vector to check for duplicate column indices.

I also introduce an API to dynamically choose the strategy, to be able
to benchmark them. It's possible I keep this API in the future, if it
turns out there are some good strategies for specialized workloads.

Future work: try a bitvector instead of a bool vector. Try and sort the
indices on insertion.
Benchmarks have shown it consistently outperforms the linked list
strategy, probably thanks to less cache misses.
This is mostly to establish the structure of parallel computation before
it's done in a smarter way.
The work splitting is a bit naive right now, as it splits according to
the number of nonzeros of the left-hand side of the product. A future
improvement should be to delay sorting to be able to have a more even
workload.
It's been shown in the past that they are too slow, we need to keep
acceptable bench times.
Benchmarks have shown this is beneficial on a few machines, and there is
a configuration to only use physical cores.
This is useful when running profilers, and should not perturb
performance too much.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant