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
An alternate method provided by Martin Morgan would work here as well.
This method supplies the objects to each node in the cluster directly in parLapply
call with no need to 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
)