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

僤鯓⒐⒋嵵緔 提交于 2019-12-08 13:20:35

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/39532187/r-any-faster-r-function-than-tcrossprod-for-symmetric-dense-matrix-multiplica

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!