Jbi*_*ill 5 parallel-processing r
这段代码给我带来了一个错误: 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)
Run Code Online (Sandbox Code Playgroud)
如果没有并行化尝试,这完全可以正常工作:
res.mat <- lapply(1:10, function(x) my.fun(x))
Run Code Online (Sandbox Code Playgroud)
这个例子也很有效:
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))
Run Code Online (Sandbox Code Playgroud)
我该如何解决我的问题?
我发现例如
class(cl)
[1] "SOCKcluster" "cluster"
Run Code Online (Sandbox Code Playgroud)
cl 是:
cl
socket cluster with 8 nodes on host ‘localhost’
Run Code Online (Sandbox Code Playgroud)
小智 3
library(parallel)
numWorkers <- 8
cl <-makeCluster(numWorkers, type="PSOCK")
res.mat <- parLapply(cl,1:10, function(x) my.fun(x))
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)