GithubHelp home page GithubHelp logo

sandy4321 / matrixcsr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dselivanov/matrixcsr

0.0 1.0 0.0 6 KB

Additional functionality for dgRMatrix matrices (aka CSR format) in Matrix package

R 22.12% C++ 77.88%

matrixcsr's Introduction

NOW PART OF THE RECO PACKAGE

Faster sparse-dense matrix products

Default format for sparse matrices in R is CSC - compressed column sparse. However CSR is more mainstream in many algorithms and modern C/C++ libraries.

MatrixCSR package brings faster CSR * dense matrix products to R. Noticeably CSR * dense matrix products are multithreaded (thanks to Eigen and RcppEigen libraries).

In example below on my laptop with 4-core/8-thread Intel Core i7 CPU CSR * dense martix product is more than 6 times faster then default CSC * dense product.

library(Matrix)
library(MatrixCSR)
library(microbenchmark)

set.seed(1)
nnz_share = 0.0001
n_row = 1e6
n_col = 1e5
n_row_dense = n_col
n_col_dense = 1e2

nnz = n_row * n_col * nnz_share
sp_m_coo = sparseMatrix(i = sample(n_row, nnz, TRUE), j = sample(n_col, nnz, TRUE), 
                        x = runif(nnz, TRUE), dims = c(n_row, n_col), giveCsparse = FALSE)

object.size(sp_m_coo)/1e6
# 160 Mb

sp_m_csc = as(sp_m_coo, "CsparseMatrix")
sp_m_csr = as(sp_m_coo, "RsparseMatrix")

dense_m = matrix(runif(n_row_dense * n_col_dense), nrow = n_row_dense, ncol = n_col_dense)
object.size(dense_m)/1e6
# 80 Mb


microbenchmark(
  csc_prod = sp_m_csc %*% dense_m, 
  csr_prod = sp_m_csr %*% dense_m, 
  times = 10
)
#Unit: seconds
#     expr       min        lq     mean    median        uq       max neval
# csc_prod 10.550470 10.617285 10.85617 10.713557 11.224757 11.362016    10
# csr_prod  1.627799  1.717712  1.75765  1.729939  1.774248  1.905975    10

Faster truncated SVD

irlba package provides two of state-of the art implementations of truncated SVD solvers:

  1. Implicitly-restarted Lanczos method
  2. Randomized SVD

With MatrixCSR package second approach can be significantly accelerated for free (without any additional manual adjustments). For example on matrix from example aboce it is more than 2 times faster than default CSC.

library(irlba)
microbenchmark(
  # randomized SVD on CSC matrix
  SVD_CSC = svdr(x = sp_m_csc, k = 10),
  # randomized SVD on CSR matrix
  SVD_CSR = svdr(x = sp_m_csr, k = 10),
  times = 1
)
#Unit: seconds
#    expr      min       lq     mean   median       uq      max neval
# SVD_CSC 25.70179 25.70179 25.70179 25.70179 25.70179 25.70179     1
# SVD_CSR 11.61360 11.61360 11.61360 11.61360 11.61360 11.61360     1

matrixcsr's People

Contributors

dselivanov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.