在工作进程之间共享对象

gdkrr

我想f(x)在许多不同的工作进程上运行,这些进程运行一个(多于一个的奖励积分)远程机器,其中x有一个大对象。

我的交互式 R 会话继续运行node0并使用该parallel库,因此我执行以下操作:

library(parallel)

cl <- makeCluster(rep("node1", times = 64))
clusterExport(cl, "x")
clusterExport(cl, "f")

clusterEvalQ(cl, f(x))

问题是发送x需要相当长的时间,因为它与主进程通过网络连接运行的机器分开传输到每个工作进程。

问题:是否可以x只向每个节点发送一次并让工作进程在本地复制它?

亨利克

假设 master 和远程主机之间的连接是瓶颈,您可以将一个副本传输到第一个 worker,然后将其缓存到文件中,并让其他 worker 从该缓存文件中读取数据。就像是:

library("parallel")

## Large data object
x <- 1:1e6
f <- function(x) mean(x)

## All N=64 workers are on the same host
cl <- makeCluster(rep("node1", times = 64))

## Send function
clusterExport(cl, "f")

## Send data to first worker (over slow connection)
clusterExport(cl[1], "x")

## Save to cache file (on remote machine)
cachefile <- clusterEvalQ(cl[1], {
  saveRDS(x, file = (f <- tempfile())); f
})[[1]]

## Load cache file into remaining workers
clusterExport(cl[-1], "cachefile")
clusterEvalQ(cl[-1], { x <- readRDS(file = cachefile); TRUE })

# Resolve function on all workers
y <- clusterEvalQ(cl, f(x))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章