在工作进程之间共享对象

问题描述 投票:0回答:2

我想在许多不同的工作进程上运行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并让工作进程在本地复制它?

r parallel-processing
2个回答
2
投票

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

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))

0
投票

这是一个使用fifos的版本,我不确定这是多么便携,在Linux下运行,我不确定这与@HenrikB的anwser如何比较性能:

library(parallel)

# create a very large cluster on a single (remote) node:
cl <- makePSOCKcluster(3)

# create a very large object
o <- 1:10

# create a fifo on the node and retrieve the name
fifo_name <- clusterEvalQ(cl[1], {
                        fifo_name <- tempfile()
                        system2("mkfifo", fifo_name)
                        fifo_name
})[[1]]

# send the very large object to one process on the node and the name of the fifo to all nodes
clusterExport(cl[1], "o")
clusterExport(cl, "fifo_name")

# does the actual sharing through the fifo
# note that a fifo has to be opened for reading 
# before writing on it
for(i in 2:length(cl)) {
  clusterEvalQ(cl[i], { ff <- fifo(fifo_name, "rb")  })
  clusterEvalQ(cl[1], { ff <- fifo(fifo_name, "wb")
                        saveRDS(o, ff)
                        close(ff)                    })
  clusterEvalQ(cl[i], { o <- readRDS(ff)
                        close(ff)                    })
}

# cleanup
clusterEvalQ(cl[1], {   unlink(fifo_name)            })

# check if everything is there
clusterEvalQ(cl, exists("o"))

# now you can do the actual work
...
© www.soinside.com 2019 - 2024. All rights reserved.