Hi, I would like to compute: A %*% B %*% t(A) A is a mxn matrix and B is an nxn symmetric, positive-definite matrix, where m is large relative to n (e.g., m=50,000 and n=100). Here is a sample code. M <- 10000 N <- 100 A <- matrix(rnorm(M*N), M, N) B <- crossprod(matrix(rnorm(N*N), N, N)) # creating a symmetric positive-definite matrix # method 1 system.time(D <- A %*% B %*% t(A)) # I can obtain speedup by using a Cholesky decomposition of B # method 2 system.time({ C <- t(chol(B)) E <- tcrossprod(A%*%C) }) all.equal(D, E) I am wondering how to obtain more substantial speedup. Any suggestions would be greatly appreciated. Thanks, Ravi [[alternative HTML version deleted]]
Hi Ravi, You can achieve substantial speed up by using a faster BLAS (e.g., OpenBLAS or MKL), especially on systems with multiple CPUs. On my (6 year old, but 8 core) system your example takes 3.9 seconds with using the reference BLAS and only 0.9 seconds using OpenBLAS. Best, Ista On Fri, Aug 10, 2018 at 11:46 AM Ravi Varadhan <ravi.varadhan at jhu.edu> wrote:> > Hi, > > I would like to compute: A %*% B %*% t(A) > > > > A is a mxn matrix and B is an nxn symmetric, positive-definite matrix, where m is large relative to n (e.g., m=50,000 and n=100). > > > > Here is a sample code. > > > > M <- 10000 > > N <- 100 > > A <- matrix(rnorm(M*N), M, N) > > B <- crossprod(matrix(rnorm(N*N), N, N)) # creating a symmetric positive-definite matrix > > > > # method 1 > > system.time(D <- A %*% B %*% t(A)) > > > > # I can obtain speedup by using a Cholesky decomposition of B > > # method 2 > > system.time({ > > C <- t(chol(B)) > > E <- tcrossprod(A%*%C) > > }) > > > > all.equal(D, E) > > > > I am wondering how to obtain more substantial speedup. Any suggestions would be greatly appreciated. > > > > Thanks, > > Ravi > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Ista, Thank you for the response. I use Windows. Is there a pre-compiled version of openBLAS for windows that would make it easy for me to use it? Thanks, Ravi -----Original Message----- From: Ista Zahn <istazahn at gmail.com> Sent: Friday, August 10, 2018 12:20 PM To: Ravi Varadhan <ravi.varadhan at jhu.edu> Cc: r-help at r-project.org Subject: Re: [R] Fast matrix multiplication Hi Ravi, You can achieve substantial speed up by using a faster BLAS (e.g., OpenBLAS or MKL), especially on systems with multiple CPUs. On my (6 year old, but 8 core) system your example takes 3.9 seconds with using the reference BLAS and only 0.9 seconds using OpenBLAS. Best, Ista On Fri, Aug 10, 2018 at 11:46 AM Ravi Varadhan <ravi.varadhan at jhu.edu> wrote:> > Hi, > > I would like to compute: A %*% B %*% t(A) > > > > A is a mxn matrix and B is an nxn symmetric, positive-definite matrix, where m is large relative to n (e.g., m=50,000 and n=100). > > > > Here is a sample code. > > > > M <- 10000 > > N <- 100 > > A <- matrix(rnorm(M*N), M, N) > > B <- crossprod(matrix(rnorm(N*N), N, N)) # creating a symmetric > positive-definite matrix > > > > # method 1 > > system.time(D <- A %*% B %*% t(A)) > > > > # I can obtain speedup by using a Cholesky decomposition of B > > # method 2 > > system.time({ > > C <- t(chol(B)) > > E <- tcrossprod(A%*%C) > > }) > > > > all.equal(D, E) > > > > I am wondering how to obtain more substantial speedup. Any suggestions would be greatly appreciated. > > > > Thanks, > > Ravi > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.