I recently got a computer with several cores and am learning to use parallel computing. I'm fairly proficient with lapply
and was told parLapply
works very similarly. I'm not operating it correctly though. It seems I have to explicitly put everything inside the parLapply
to make it work (that is functions to be use, variables etc.). With lapply
it reads from the parent environment and parLapply
does not seem to do this. So in my example below I could make everything work by placing all info inside parLapply
but if I use this inside a user defined function I can't explicitly put text.var
inside of parLapply
.
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=" | ")
}
lapply(seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
#doesn't work
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
#does work but have to specify all the stuff inside parLapply
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i) {
######stuff I have to put inside parLapply##########
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=" | ")
}
######stuff I have to put inside parLapply##########
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
How can I pass text.var
, ntv
, gc.rate
, and pos
to parLapply
without explicitly putting them inside? (I'm guessing you pass them as a list somehow)
PS windows 7 machine so I need to use parLapply
I think