![](/img/trans.png)
[英]Multicore / Parallel simulation in R using doRedis and doMC on AWS EC2
[英]R in parallel using doMC library: How to reuse cores for subsequent parallel processes?
在R中运行以下脚本时:
library(doMC)
registerDoMC(cores=3)
# First foreach
# This runs in 3 threads
foreach(i=1:3) %dopar% sqrt(i)
# Second foreach
# This add 3 threads to the previous ones (now inactive but still consuming memory), totalling 6 threads
foreach(i=1:3) %dopar% sqrt(i)
我想知道在运行第二个foreach
时如何重用第一个foreach
的线程,以便整个脚本始终使用3个内核运行。
感谢doMC的一位开发人员的建议,我可以找到一种解决方法。 使用其他库,以下代码可以实现我想要的功能:
library(doParallel)
cores=makeForkCluster(3)
registerDoParallel(cores)
# First foreach
# This runs in 3 threads
foreach(i=1:3) %dopar% sqrt(i)
# Second foreach
# This reuses the previous 3 threads (total of 3 active threads)
foreach(i=1:3) %dopar% sqrt(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.