并行大矩阵乘法

时间:2018-09-11 10:38:43

标签: r r-bigmemory

我需要按如下方式将两个大矩阵AB相乘:

library(bigmemory)
library(bigalgebra)
library(biganalytics)

A <- big.matrix( replicate(100,  rnorm(10^5)) )
B <- big.matrix( replicate(10^5, rnorm(100))  )
AB <- A %*% B

如何并行计算此乘法?

到目前为止,我遇到的唯一教程是this one

> library("doRedis")
> registerDoRedis(queue="example")
> L = foreach(j=1:2,.packages="VAM",.combine=c) %dopar%
+ {
+ key = paste("X",j,sep="")
+ ridx = ((j-1)*5 + 1):min((j*5),nrow(A))
+ X = A[ridx,] %*% B[,]
+ Y = as.big.matrix(X,backingfile=key)
+ vnew(Y, key)
+ key
+ }
> X = vam(matrix(L,nrow=2))
> sum(X[,] - A[,] %*% B[,])
[1] 0

但是我不确定如何将其付诸实践。也许还有一种更简单/更有效的方法来达到相同的结果?

1 个答案:

答案 0 :(得分:1)

安装Microsoft R Open,我从3秒缩短到0.1秒!

library(bigmemory)
library(bigalgebra)

N <- 200
M <- 1e5

A <- big.matrix(N, M, init = rnorm(N * M))
B <- big.matrix(M, N, init = rnorm(N * M))
system.time(AB <- A %*% B)

Change the version of R you use in RStudio settings (need to quit RStudio afterwards)