Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在并行计算中更新相同的内存(矩阵)?_R_Concurrency_Parallel Processing_Multicore - Fatal编程技术网

R 在并行计算中更新相同的内存(矩阵)?

R 在并行计算中更新相同的内存(矩阵)?,r,concurrency,parallel-processing,multicore,R,Concurrency,Parallel Processing,Multicore,我有一个强大的用例,用于并行SGD算法。在这种情况下,我需要用delta梯度更新来更新矩阵P和Q,并对一批随机样本进行更新。每个进程将更新两个矩阵上的互斥索引 我打算做的简单说明如下: # create "big" matrix A <- matrix(rnorm(10000), 100, 100) system.time( # update each row vector independently using all my cores r <- mclapply(1:10

我有一个强大的用例,用于并行SGD算法。在这种情况下,我需要用delta梯度更新来更新矩阵P和Q,并对一批随机样本进行更新。每个进程将更新两个矩阵上的互斥索引

我打算做的简单说明如下:

# create "big" matrix
A <- matrix(rnorm(10000), 100, 100)
system.time(
  # update each row vector independently using all my cores
  r <- mclapply(1:100, mc.cores = 6, function(i) {
    # updating ... 
    A[i,] <- A[i,] - 0.01
    # return something, i.e. here I'd return the RMSE of this batch instead   
    sqrt(sum(A[i,]^2))
  }) 
)
#创建“大”矩阵

A使用与包{bigstatsr}共享的数据,按块重新实现代码:

N <- 10e3
A <- matrix(rnorm(N * N), N)

library(bigstatsr)
bigA <- as_FBM(A)

library(doParallel)
registerDoParallel(cl <- makeCluster(4))
system.time(
  r <- foreach(i = seq_len(N), .combine = 'c') %dopar% {
    # updating ... 
    A[i,] <- A[i,] - 0.01
    # return something, i.e. here I'd return the RMSE of this batch instead   
    sqrt(sum(A[i,]^2))
  }
) # 11 sec
stopCluster(cl)

registerDoParallel(cl <- makeCluster(4))
system.time(
  r2 <- big_apply(bigA, function(X, ind) {
    # updating ... 
    tmp <- bigA[ind, ] <- bigA[ind, ] - 0.01
    # return something, i.e. here I'd return the RMSE of this batch instead   
    sqrt(rowSums(tmp^2))
  }, a.combine = 'c')
) # 1 sec
stopCluster(cl)

all.equal(r, r2) # TRUE

N如果要并行更新,则必须使用共享内存,例如与包{bigmemory}或{bigstatsr}一起使用。另外,请记住矩阵是按列存储的,所以最好按列访问。@F.Privé
bigmemory
会很好,但我不能使用它们的
big。矩阵
我的代码将不再工作,因为缺少子集、colSums等。还有其他方法吗?这不是很难。您可以使用{bigstatsr}的
big_apply()
按块执行,例如:。实际上是这样,而且速度也较慢。当我说这是因为我已经尝试过了,我的R矩阵代码不再适用于将类型切换到
big.matrix