Search code examples
rsparse-matrixrcpp

Parallelized sparse matrix rowSums in R (or Rcpp)


For the simple task of summing up all rows in a sparse matrix in R, Matrix::rowSums() does a great job:

library(Matrix)    
m <- rsparsematrix(10000, 5000, 0.1)
rsums <- Matrix::rowSums(m)

Obviously, this function can be parallelized by blocking the summations by rows. What options are out there? I can't seem to find any.

Setting the stage for some benchmarking...

library(rbenchmark)
benchmark(
        "Matrix::rowSums" = {
            Matrix::rowSums(m)
        },
        replications = 10,
        columns = c("test", "replications", "elapsed", "relative", "user.self", "sys.self"))


#              test replications elapsed relative user.self sys.self
# 1 Matrix::rowSums           10    1.14        1      0.95     0.19

I'm up for the fastest solution, R or Rcpp.

See this outstanding study by Will Townes on looping through sparse matrices: https://rpubs.com/will_townes/sparse-apply. The conclusion seems to be that Matrix::rowSums and family are solid performers, and parallelization may be achieved with slam and/or data.table.


Solution

  • I don't see how you could parallelize row summations in a column-oriented sparse matrix format or a coordinate sparse matrix format, which are the two formats Matrix stores sparse matrices in. Rows are not memory-contiguous. If you chunk a set of rows and give it to a thread, that thread will have to read through the entire matrix data structure to give you the rowsum for that chunk. If you chunked it and ran 10 threads, you might have 10 times as much processing power, but you have to do 10 times the work.