简体   繁体   English

R Parallel Package:clusterExport 到每个单独的核心,而不是一个大对象到所有核心

[英]R Parallel Package: clusterExport to each individual core rather than one large object to all cores

I'm using the Parallel Package to save me some time in R. Usually I export whatever object I am trying to do calculations on with clusterExport and run the relevant function.我正在使用 Parallel Package 来节省我在 R 中的一些时间。通常我会导出我尝试使用 clusterExport 进行计算的任何对象并运行相关函数。 However, this time I'm working with a very large object (10+GB) and even creating a small cluster uses a ton of RAM.但是,这次我正在处理一个非常大的对象(10+GB),甚至创建一个小集群也会使用大量 RAM。 The calculation I'm doing only requires a part of the 10 GB object for each copy of R. I'm wondering if there is a way to use clusterExport to only export the relevant half gigabyte or so to each copy of R. From what I can tell clusterExport always sends the entire object to each copy so if there is a work around to this please let me know.我正在做的计算只需要 R 的每个副本的 10 GB 对象的一部分。我想知道是否有办法使用 clusterExport 仅将相关的半 GB 左右导出到 R 的每个副本。从什么我可以告诉 clusterExport 总是将整个对象发送到每个副本,所以如果有解决方法请告诉我。

You can see in this example below I store the data.frame x in each copy of R. My goal would be in this case to have 10 different datasets x1-x10 and load x1 to the first copy, x2 to the second, and so on.您可以在下面的示例中看到,我将 data.frame x 存储在 R 的每个副本中。在这种情况下,我的目标是拥有 10 个不同的数据集 x1-x10 并将 x1 加载到第一个副本,将 x2 加载到第二个副本,依此类推上。 If there is another way to do this efficiently please let me know as well.如果有另一种方法可以有效地做到这一点,也请告诉我。

library('doParallel')
x <- data.frame(a=1:10,b=1:10)
test <- function(y) {
  return(x$a[y]+x$b[y])
}
cl <- makeCluster(10)
registerDoParallel(cl)
clusterExport(cl,list('test','x'))
test1 <- parLapply(cl,1:10,fun=test)
stopCluster(cl)

I came up with a solution to this after a few days.几天后我想出了一个解决方案。 The solution I came up with was to save each portion of the data.frame in seperate csv files.我想出的解决方案是将 data.frame 的每个部分保存在单独的 csv 文件中。 Then within each virtual R session I read in the relevant csv file so each R session only contained the necessary data for their computations.然后在每个虚拟 R 会话中,我读入相关的 csv 文件,因此每个 R 会话只包含计算所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM