, . doParallel , , ? , makeCluster "outfile"? , .
getDoParRegistered, , , .
:
library(doParallel)
parfun <- function(n=10, parallel=FALSE,
cores=getOption('mc.cores', 2L)) {
if (parallel) {
if (! getDoParRegistered()) {
cl <- makePSOCKcluster(cores)
registerDoParallel(cl)
message('Registered doParallel with ',
cores, ' workers')
} else {
message('Using ', getDoParName(), ' with ',
getDoParWorkers(), ' workers')
}
`%d%` <- `%dopar%`
} else {
message('Executing parfun sequentially')
`%d%` <- `%do%`
}
foreach(i=seq_len(n), .combine='c') %d% {
Sys.sleep(1)
i
}
}
, , parallel=TRUE, :
> parfun()
Executing parfun sequentially
[1] 1 2 3 4 5 6 7 8 9 10
parallel=TRUE , :
> parfun(parallel=TRUE, cores=3)
Registered doParallel with 3 workers
[1] 1 2 3 4 5 6 7 8 9 10
parfun parallel=TRUE , :
> parfun(parallel=TRUE)
Using doParallelSNOW with 3 workers
[1] 1 2 3 4 5 6 7 8 9 10
-: . , , , , .
, , CRAN. detectCores(). , mclapply, , , .
stopCluster
, , stopCluster. , , foreach, . , , cl.
:
stopCluster parfun makePSOCKcluster;- , (
stopImplicitCluster doParallel); - .
, . .