gpt4 book ai didi

r - 每次我想在 R 中使用 foreach() 时,都必须注册 DoParallel() 和 stopCluster() 吗?

转载 作者:行者123 更新时间:2023-12-05 02:14:52 24 4
gpt4 key购买 nike

我读到你必须在运行并行函数后使用 stopCluster():R 中的 foreach()。但是,我可以使用 registerDoParallel() ,然后在不使用 stopCluster() 的情况下多次运行 foreach()。那么我是否需要 stopCluster() 呢?

不使用 stopCluster() 是否意味着您的内核正在处理当前任务?因此,如果我使用并行编程,中间只有一点点单核顺序任务,那么我不需要stopCluster()?我知道设置并行也会消耗大量的开销时间。

最佳答案

parallel::makeCluster()doParallel::registerDoParallel() 创建一组并行运行的 R 副本。这些副本称为 worker

parallel::stopCluster()doParallel::stopImplicitCluster() 是关闭 worker 的安全方法。从帮助页面?stopCluster:

It is good practice to shut down the workers by calling ‘stopCluster’: however the workers will terminate themselves once the socket on which they are listening for commands becomes unavailable, which it should if the master R session is completed (or its process dies).

事实上,未使用的 worker 的 CPU 使用率通常可以忽略不计。然而,如果工作人员加载大型 R 对象,例如大型数据集,他们可能会使用大部分内存,因此会减慢计算速度。在这种情况下,关闭未使用的 worker 效率更高。

关于r - 每次我想在 R 中使用 foreach() 时,都必须注册 DoParallel() 和 stopCluster() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52980793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com