首页 > 解决方案 > 在 R 中使用多个内核

问题描述

我最近发现 R 默认运行在单个处理器上,即使我的系统具有多核。如果我想减少我的处理时间,我必须利用所有 8 个内核,但我不知道如何做到这一点。我确实尝试使用此链接中的功能,但没有帮助。有谁知道如何给 R 一个全局命令以默认使用所有内核?或者针对同一问题的任何其他解决方案?

标签: 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)

推荐阅读