这是一个使用
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
...