[ACCEPTED]-using parallel's parLapply: unable to access variables within parallel code-parallel-processing
Accepted answer
You need to export those variables to the 1 other R processes in the cluster:
cl <- makeCluster(mc <- getOption("cl.cores", 4))
clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"))
An alternate method provided by Martin Morgan would work here as well.
This method 3 supplies the objects to each node in the 2 cluster directly in parLapply
call with no need to 1 use cluster export:
library(parallel)
text.var <- rep("I like cake and ice cream so much!", 20)
ntv <- length(text.var)
gc.rate <- 10
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i, pos, text.var, ntv, gc.rate) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}, pos, text.var, ntv, gc.rate
)
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.