2

我正在编写一个处理几个非常大的 data.tables 的函数,我想在 Windows 机器上并行化这个函数。

我可以使用snow包来执行此操作clusterExport,以便为集群中的每个节点创建每个 data.tables 的副本。但是,这不起作用,因为它使用了太多内存。

我想通过将 data.tables 的不同子集导出到每个节点来解决此问题,但是,我看不到如何在雪包中执行此操作。

这是一个有效但内存效率低的代码的玩具示例:

library(snow)
dd <- data.frame(a = rep(1:5, each = 2), b = 11:20)
cl <- makeCluster(2, type = "SOCK")
clusterExport(cl = cl, "dd")
clusterApply(cl, x = c(2,7),  function(thresh) colMeans(dd[dd$a < thresh,]))
stopCluster(cl)

这是一个不起作用的代码示例,但解释了我们希望如何将子集分发dd到节点:

library(snow)
dd <- data.frame(a = rep(1:5, each = 2), b = 11:20)
cl <- makeCluster(2, type = "SOCK")

dd_exports <- lapply(c(2,7), function(thresh) dd[dd$a < thresh])
#Now we export the ith element of dd_exports to the ith node:
clusterExport(cl = cl, dd_exports) 
clusterApply(cl, x = c(2,7),  function(x) colMeans(dd))
stopCluster(cl)
4

0 回答 0