Parallel and Multicore Processing in R [closed]

↘锁芯ラ 提交于 2019-12-03 00:21:29

The multicore package is deprecated: not parallel. Take a look at the documentation for the mclapply function: it's the easiest way to execute functions in parallel in the parallel package. It's very similar to lapply but with a few new, optional arguments:

library(parallel)
myfun <- function(i) { Sys.sleep(1); i }
mclapply(1:8, myfun, mc.cores=4)

Note that mclapply uses processes, not threads, and doesn't support parallel execution on Windows. For Windows, you should take a look at parLapply, which is also in parallel. It is also similar to lapply, but requires a cluster object as the first argument. Here's the same example, but this works on essentially any platform:

library(parallel)
cl <- makePSOCKcluster(4)
myfun <- function(i) { Sys.sleep(1); i }
parLapply(cl, 1:8, myfun)
stopCluster(cl)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!