通过mc.cores与makePSOCKcluster设置内核? [英] Setting cores via mc.cores vs. makePSOCKcluster?
问题描述
我想知道通过 makePSOCKcluster
和显式地在foreach循环中设置R使用的内核数之间有什么区别?我应该在两个实例中分别进行设置,还是在使 makePSOCKcluster
足够时进行设置?
I was wondering what is the difference between setting the number of cores for R to use via makePSOCKcluster
and explictly in the foreach loop? Should I be setting this seperately in both instances, or is doing so when making the makePSOCKcluster
enough?
cl <- makePSOCKcluster(max(1, detectCores() - 1))
registerDoParallel(cl)
和
mcoptions <- list( preschedule=FALSE, mc.cores = max(1,detectCores()-1) )
stream = foreach(i=1:NROW(stream_sponsored), .inorder=FALSE,
.combine=rbind,
.options.multicore=mcoptions) %dopar% {
#do something
}
推荐答案
'The "cores" options allows you to temporarily override the number of workers to use for a single foreach operation. This is more convenient than having to re-register doParallel. Although if no value of "cores" was specified when doParallel was registered, you can also change this value dynamically using the options function:
options(cores=2)
getDoParWorkers()
options(cores=3)
getDoParWorkers()
如果在注册doParallel时确实指定了内核数,则"cores"选项将被忽略:
If you did specify the number of cores when registering doParallel, the "cores" option is ignored:
registerDoParallel(4)
options(cores=2)
getDoParWorkers()
如您所见,可以使用许多选项来控制并行使用的工作程序的数量,但是默认行为通常可以满足您的要求.'
As you can see, there are a number of options for controlling the number of workers to use with parallel, but the default behaviour usually does what you want.'
因此,简而言之,如果您一次调用foreach,则无需在foreach中指定核心.但是,如果您要调用两个或多个具有不同内核的foreach,则无需在registerDoParallel中指定内核
这篇关于通过mc.cores与makePSOCKcluster设置内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!