在R中使用多个内核

时间:2018-08-16 05:49:58

标签: r

我最近发现,即使我的系统具有多核,默认情况下R仍在单个处理器上运行。如果我想减少处理时间,则必须使用所有8个内核,但是我不确定该怎么做。我确实尝试过使用此link中的函数,但没有帮助。 有谁知道如何向R发出全局命令以默认使用所有内核?或其他解决同一问题的方法?

1 个答案:

答案 0 :(得分:1)

您可以运行它,但是如果您有顺序算法,它对您没有太大帮助。

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)