如何让R使用计算机的所有核心?
我读到 R 仅使用单个 CPU。如何让 R 使用所有可用的内核来运行统计算法?
I have read that R uses only a single CPU. How can I let R use all the available cores to run statistical algorithms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,对于初学者,请参阅 CRAN 上的高性能计算任务视图。这列出了可用于支持单机并行计算的包的详细信息。
从 R 版本 2.14.0 开始,通过并行包内置了对并行计算的支持,其中包括对现有snow和多核稍作修改的版本> 包裹。 parallel 包中有一个小插图,您应该阅读。您可以使用以下方式查看它:
还有其他方法可以利用您的多核,例如通过使用多线程 BLAS 进行线性代数计算。
这些是否会加速您想要执行的“统计计算”将取决于这些“统计计算”是什么。产生多个线程或工作线程需要设置、管理它们和收集结果的开销成本。某些操作会看到使用多个核心/线程的好处(有些大,有些小),而其他操作则由于这种额外的开销而变慢。
简而言之,不要期望通过使用 n 个核心(而不是仅使用 1 个)来将计算时间减少 n 倍。
Yes, for starters, see the High Performance Computing Task View on CRAN. This lists details of packages that can be used in support of parallel computing on a single machine.
From R version 2.14.0, there is inbuilt support for parallel computing via the parallel package, which includes slightly modified versions of the existing snow and multicore packages. The parallel package has a vignette that you should read. You can view it using:
There are other ways to exploit your multiple cores, for example via use of a multi-threaded BLAS for linear algebra computations.
Whether any of this will speed up the "statistics calculations" you want to do will depend on what those "statistics calculations" are. Spawning off multiple threads or workers entails an overhead cost to set them up, manage them and collect the results. Some operations see a benefit (some large, some small) of using multiple cores/threads, others are slowed down because of this extra overhead.
In short, do not expect to get an n times decrease in your compute time by using n cores instead of just 1.
如果您碰巧对同一件事进行了几次*迭代(或者对几个*不同的参数使用了相同的代码),最简单的方法是运行多个 R 副本 - 操作系统将在不同的内核上分配工作。
相反的情况,就去学习如何使用真正的并行扩展。
就这个答案而言,很少意味着少于或等于核心数量。
If you happen to do few* iterations of the same thing (or same code for few* different parameters), the easiest way to go is to run several copies of R -- OS will allocate the work on different cores.
In the opposite case, go and learn how to use real parallel extensions.
For the sake of this answer, few means less or equal the number of cores.