我在这里问了一个相关的问题并且响应运行良好: 使用parallel的parLapply:无法访问并行代码中的变量
问题是,当我尝试使用函数内部的答案时,它将无法工作,因为我认为它具有默认环境clusterExport.我已经阅读了小插图并查看了帮助文件,但我的知识库非常有限.我使用的方式我parLapply期望它的行为类似lapply但似乎没有.
这是我的尝试:
par.test <- function(text.var, gc.rate=10){
ntv <- length(text.var)
require(parallel)
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
cl <- makeCluster(mc <- getOption("cl.cores", 4))
clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"))
parLapply(cl, seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
}
par.test(rep("I like cake and ice cream so much!", 20))
#gives this error message
> par.test(rep("I like cake and ice cream so much!", 20))
Error in …Run Code Online (Sandbox Code Playgroud) 我有一个函数调用 (to jags.parallel),当给定数字参数时,该函数调用有效,n.iter = 100但当参数使用变量值 时,该函数调用失败n.iter = n.iter。这看起来可能是一个错误jags.parallel
错误的最小可重现示例:
library(R2jags)
model.file <- system.file(package="R2jags", "model", "schools.txt")
J <- 8.0
y <- c(28.4,7.9,-2.8,6.8,-0.6,0.6,18.0,12.2)
sd <- c(14.9,10.2,16.3,11.0,9.4,11.4,10.4,17.6)
jags.data <- list("y","sd","J")
jags.params <- c("mu","sigma","theta")
jags.inits <- function(){
list("mu"=rnorm(1),"sigma"=runif(1),"theta"=rnorm(J))
}
Run Code Online (Sandbox Code Playgroud)
然后这有效:
jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
n.iter=5000, model.file=model.file)
Run Code Online (Sandbox Code Playgroud)
但这并不:
n.iter=5000
jagsfit.p <- jags.parallel(data=jags.data, inits=jags.inits, jags.params,
n.iter=n.iter, model.file=model.file)
Run Code Online (Sandbox Code Playgroud)
给出错误:
Error in checkForRemoteErrors(lapply(cl, recvResult)) :
3 nodes produced errors; first error: object 'n.iter' not found
Run Code Online (Sandbox Code Playgroud)
我认为这与不将变量导出到集群有关n.iter,但尚不清楚 jags.parallel 使用的是什么并行引擎。n.iter …