Why doesn't the plyr package use my parallel backend?

£可爱£侵袭症+ 提交于 2019-12-05 20:54:08

问题


I'm trying to use the parallel package in R for parallel operations rather than doSNOW since it's built-in and ostensibly the way the R Project wants things to go. I'm doing something wrong that I can't pin down though. Take for example this:

a <- rnorm(50)
b <- rnorm(50)

arr <- matrix(cbind(a,b),nrow=50)

aaply(arr,.margin=1,function(x){x[1]+x[2]},.parallel=F)

This works just fine, producing the sums of my two columns. But if I try to bring in the parallel package:

library(parallel)
nodes <- detectCores()
cl <- makeCluster(nodes)
setDefaultCluster(cl)

aaply(arr,.margin=1,function(x){x[1]+x[2]},.parallel=T)

It throws the error

2: In setup_parallel() : No parallel backend registered
3: executing %dopar% sequentially: no parallel backend registered 

Am I initializing the backend wrong?


回答1:


Try this setup:

library(doParallel)
library(plyr)

nodes <- detectCores()
cl <- makeCluster(nodes)
registerDoParallel(cl)

aaply(ozone, 1, mean,.parallel=TRUE)

stopCluster(cl)

Since I have never used plyr for parallel computing I have no idea why this issues warnings. The result is correct anyway.




回答2:


The documentation for aaply states

.parallel: if ‘TRUE’, apply function in parallel, using parallel backend provided by foreach

so presumably you need to use the foreach package rather than the parallel package.



来源:https://stackoverflow.com/questions/15642722/why-doesnt-the-plyr-package-use-my-parallel-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!