8
votes

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?

2
For anyone looking at this post, I've long since abandoned parallel in favor of doParallel, which seems to Just Work.Patrick McCarthy

2 Answers

23
votes

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
votes

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.