R - 并行化错误,checkCluster(cl) - 不是有效的集群

时间:2017-09-30 14:54:37

标签: r parallel-processing

此代码给我带来错误:Error in checkCluster(cl): not a valid cluster

library(parallel)
numWorkers <-8

cl <-makeCluster(numWorkers, type="PSOCK")

   res.mat <- parLapply(1:10, function(x) my.fun(x))

stopCluster(cl)

如果没有并行化尝试,这完全没问题:

res.mat <- lapply(1:10, function(x) my.fun(x))

这个例子也很有效:

workerFunc <- function(n){return(n^2)}

library(parallel)

numWorkers <-8

cl <-makeCluster(numWorkers, type ="PSOCK")

res <- parLapply(cl, 1:100, workerFunc)

stopCluster(cl)

print(unlist(res))

我如何解决我的问题?

我找到了例如

class(cl)
[1] "SOCKcluster" "cluster"  

一个cl是:

cl
socket cluster with 8 nodes on host ‘localhost’

2 个答案:

答案 0 :(得分:1)

library(parallel)
numWorkers <- 8

cl <-makeCluster(numWorkers, type="PSOCK")

res.mat <- parLapply(cl,1:10, function(x) my.fun(x))

stopCluster(cl)

答案 1 :(得分:0)

过于具体,问题在于 res.mat <- parLapply(1:10, function(x) my.fun(x)) 不一定是参数的顺序,但未指定参数 cl

cl <-makeCluster(numWorkers, type ="PSOCK")
res.mat <- parLapply(x = 1:10,
                     fun = function(x) my.fun(x),
                     cl = cl
                     )

应该可以工作,因为指定了所有必需的参数。或者,如果未指定 ?parLapplyparLapply 表示 cl 使用默认集群。可以使用 parallel::setDefaultCluster() 设置默认集群,然后当 parLapply 未包含在用户输入中时,它允许 cl 恢复为默认行为。

cl <-makeCluster(numWorkers, type ="PSOCK")
parallel::setDefaultCluster(cl)
res.mat <- parallel::parLapply(x = 1:10,#by default cl = NULL if not specified
                               fun = function(x) my.fun(x),
                               )