R: any faster R function than "tcrossprod" for symmetric dense matrix multiplication?

Janak

Let

x = matrix(rnorm(1000000), nrow = 5000)

I would like to compute matrix multiplication with its transpose: x %*% t(x).

After googling I found a possible faster way of doing the above is

tcrossprod(x)

And time taken is

 user  system elapsed 
2.975   0.000   2.960

Is there is any other R-function which can do the task faster than the above function?

李哲源

No. At R level this is already the fastest. But internally it calls level-3 BLAS routine dsyrk. So if you can have a high performance BLAS library this will be a lot faster. Try linking OpenBLAS to your R.

Linking a BLAS library does not require rebuilding R. You may have a read on my question linking R to BLAS library for an overview, which contains several links showing you how to set up alias then switch between different BLAS libraries on the machine.

Alternatively, you can read my extremely long question and answer Without root access, run R with tuned BLAS when it is linked with reference BLAS which gives various ways to use an external BLAS library even if R is linked to reference BLAS library.


As a side note, for a matrix with dimension m * n, dsyrk has FLOP counts n * m ^ 2. (Note, this is the computational costs for tcrossprod. For crossprod it is m * n ^ 2.)

You have m = 5000 and n = 200, and computation takes 2.96s. Thus, computation has speed: (200 * 5000 ^ 2 / 2.96) * 1e-9 = 1.68 GFLOPs. Well, this is an ordinary level of performance so at the moment you are definitely using reference BLAS. With OpenBLAS, performance can reach 10 GFLOPs or more, depending on your CPU. Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is this naive matrix multiplication faster than base R's?

Is matrix multiplication speed faster for sparse matrixes than dense matrixes?

Faster function than an ifelse() in r

Creating a symmetric matrix in R

Constructing symmetric matrix on R

R tcrossprod with unique combinations

Any other process faster than sqldf in r

r: how to make matrix multiplication faster (special case)

Why is matrix multiplication faster with Repa than with hmatrix?

numpy: is matrix multiplication faster than sum of a vector?

Why is sparse-dense multiplication faster than dense-sparse multiplication?

Is there a R function to remove rows and columns to make a symmetric square matrix?

R: creating a symmetric matrix with xtabs

Is there any faster alternative to stats:uniroot function in R?

Rowwise matrix multiplication in R

multiplication in matrix R

Matrix multiplication and addition in R

Multiplication of matrix in R

R : convert a matrix to symmetric matrix with diagonal 0

How to convert sparse matrix to dense matrix in R

Why can GPU do matrix multiplication faster than CPU?

Why is matrix multiplication faster with numpy than with ctypes in Python?

Why is matrix multiplication faster with numpy than with ctypes in Python?

OpenCL CPU is faster than OpenCL GPU for naive matrix multiplication

MATLAB matrix multiplication performance is 5x faster than NumPy

Explicit matrix multiplication much faster than numpy.matmul?

How is Numba faster than NumPy for matrix multiplication with integers?

Faster Matrix Multiplication in CUDA

Is sum or matrix multiplication faster?