r - 在 R 中使用多个内核
问题描述
我最近发现 R 默认运行在单个处理器上,即使我的系统具有多核。如果我想减少我的处理时间,我必须利用所有 8 个内核,但我不知道如何做到这一点。我确实尝试使用此链接中的功能,但没有帮助。有谁知道如何给 R 一个全局命令以默认使用所有内核?或者针对同一问题的任何其他解决方案?
解决方案
你可以运行它,但如果你有一个顺序算法,它对你没有多大帮助。
library(parallel)
detectCores() # returns 1,2,4,8 or whatever
# Create cluster via makeCluster
cl <- makeCluster(8)
# you can do a parallel matrix operation, apply() like this.
# here, calculate column medians
parApply(cl, mymatrix, 2, median)
# run 100 iterations in parallel
# with a parallel sapply() like this
res <- parSapply(cl, 1:100, function(x) myfunction(x))
stopCluster(cl)
推荐阅读
- matlab - parfor 循环与 for 循环相比没有明显的加速
- java - 你能在txt文件java中做一个列吗
- spock - 数据驱动的 Spock 测试
- mysql - SQL 左连接表只有一些 id 行
- asp.net-core - 如何在 .NET Core 中抑制 JIT 优化
- css - React 地图功能和 CSS 网格
- html - 如何将焦点环添加到html中的菜单项
- python - 如何使用for循环将字符串附加到数组中的特定位置
- python - 使用 dbm.ndbm / Berkeley DB 在似乎只安装 dbm.dumb 的机器上打开序列化 Python 搁置
- javascript - 如何在另一个组件内设置背景颜色?反应原生