【问题标题】:R in parallel using doMC library: How to reuse cores for subsequent parallel processes?R并行使用doMC库:如何为后续并行进程重用内核?
【发布时间】:2014-02-13 22:18:43
【问题描述】:

在 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的线程,让整个脚本始终使用3个核心运行。

【问题讨论】:

  • 您应该将您的编辑转换为答案,以便于查找和理解。
  • 完成(见下面的答案)

标签: r parallel-processing domc


【解决方案1】:

感谢 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)

【讨论】:

  • 这是有道理的。您还可以提到foreach 使用模块化系统,您可以在其中插入不同的后端,具有不同的语法和副作用(如核心的持久性)。
猜你喜欢
  • 2012-11-09
  • 2018-06-14
  • 2017-01-28
  • 2016-02-10
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
相关资源
最近更新 更多