Why doesn't the plyr package use my parallel b

2019-04-06 19:39发布

问题:

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.