共享内存与R并行foreach

时间:2015-07-22 23:13:14

标签: r foreach parallel-processing r-bigmemory

问题描述:

我有一个大矩阵c,加载到RAM内存中。我的目标是通过并行处理对其进行只读访问。但是,当我使用doSNOWdoMPIbig.matrix等创建连接时,使用ram的数量会急剧增加。

有没有办法正确创建可以读取所有进程的共享内存,而无需创建所有数据的本地副本?

示例:

libs<-function(libraries){# Installs missing libraries and then load them
  for (lib in libraries){
    if( !is.element(lib, .packages(all.available = TRUE)) ) {
      install.packages(lib)
    }
    library(lib,character.only = TRUE)
  }
}

libra<-list("foreach","parallel","doSNOW","bigmemory")
libs(libra)

#create a matrix of size 1GB aproximatelly
c<-matrix(runif(10000^2),10000,10000)
#convert it to bigmatrix
x<-as.big.matrix(c)
# get a description of the matrix
mdesc <- describe(x)
# Create the required connections    
cl <- makeCluster(detectCores ())
registerDoSNOW(cl)
out<-foreach(linID = 1:10, .combine=c) %dopar% {
  #load bigmemory
  require(bigmemory)
  # attach the matrix via shared memory??
  m <- attach.big.matrix(mdesc)
  #dummy expression to test data aquisition
  c<-m[1,1]
}
closeAllConnections()

RAM: Ram usage during <code>foreach</code> 在上图中,您可能会发现内存会增加很多,直到foreach结束并且它被释放。

2 个答案:

答案 0 :(得分:13)

我认为问题的解决方案可以从foreach软件包here的作者Steve Weston的帖子中看到。在那里他说:

  

doParallel包会自动将变量导出到foreach循环中引用的worker。

所以我认为问题在于,在您的代码中,您的大矩阵c在作业c<-m[1,1]中被引用。只需尝试xyz <- m[1,1],看看会发生什么。

以下是文件支持的big.matrix

的示例
#create a matrix of size 1GB aproximatelly
n <- 10000
m <- 10000
c <- matrix(runif(n*m),n,m)
#convert it to bigmatrix
x <- as.big.matrix(x = c, type = "double", 
                 separated = FALSE, 
                 backingfile = "example.bin", 
                 descriptorfile = "example.desc")
# get a description of the matrix
mdesc <- describe(x)
# Create the required connections    
cl <- makeCluster(detectCores ())
registerDoSNOW(cl)
## 1) No referencing
out <- foreach(linID = 1:4, .combine=c) %dopar% {
  t <- attach.big.matrix("example.desc")
  for (i in seq_len(30L)) {
    for (j in seq_len(m)) {
      y <- t[i,j]
    }
  }
  return(0L)
}

enter image description here

## 2) Referencing
out <- foreach(linID = 1:4, .combine=c) %dopar% {
  invisible(c) ## c is referenced and thus exported to workers
  t <- attach.big.matrix("example.desc")
  for (i in seq_len(30L)) {
    for (j in seq_len(m)) {
      y <- t[i,j]
    }
  }
  return(0L)
}
closeAllConnections()

enter image description here

答案 1 :(得分:4)

或者,如果您使用的是Linux / Mac并且需要CoW共享内存,请使用分叉。首先将所有数据加载到主线程中,然后从mcparallel包中启动具有通用函数parallel的工作线程(forks)。

您可以使用mccollect或使用Rdsm库使用真正的共享内存来收集结果,如下所示:

library(parallel)
library(bigmemory) #for shared variables
shared<-bigmemory::big.matrix(nrow = size, ncol = 1, type = 'double')
shared[1]<-1 #Init shared memory with some number

job<-mcparallel({shared[1]<-23}) #...change it in another forked thread
shared[1,1] #...and confirm that it gets changed
# [1] 23

如果你延迟写:

,你可以确认,值确实在backgruound中更新了
fn<-function()
{
  Sys.sleep(1) #One second delay
  shared[1]<-11
}

job<-mcparallel(fn())
shared[1] #Execute immediately after last command
# [1] 23
aaa[1,1] #Execute after one second
# [1] 11
mccollect() #To destroy all forked processes (and possibly collect their output)

要控制确定性并避免竞争条件,请使用锁定:

library(synchronicity) #for locks
m<-boost.mutex() #Lets create a mutex "m"

bad.incr<-function() #This function doesn't protect the shared resource with locks:
{
  a<-shared[1]
  Sys.sleep(1)
  shared[1]<-a+1
}

good.incr<-function()
{
  lock(m)
  a<-shared[1]
  Sys.sleep(1)
  shared[1]<-a+1
  unlock(m)
}

shared[1]<-1
for (i in 1:5) job<-mcparallel(bad.incr())
shared[1] #You can verify, that the value didn't get increased 5 times due to race conditions

mccollect() #To clear all threads, not to get the values
shared[1]<-1
for (i in 1:5) job<-mcparallel(good.incr())
shared[1] #As expected, eventualy after 5 seconds of waiting you get the 6
#[1] 6 

mccollect()

编辑:

我通过将Rdsm::mgrmakevar交换为bigmemory::big.matrix来简化了依赖关系。无论如何,mgrmakevar内部会拨打big.matrix,我们也不再需要任何其他内容。