Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parallel foreach loops produce mclapply error

Those are my first steps with parallel computing in R. The code below results in the following error. I am clueless, since there is no mclapply function in what I wrote, at least I did not put it explicitly.

Error:

Error in mclapply(argsList, FUN, mc.preschedule = preschedule, mc.set.seed = set.seed,  : 
  (list) object cannot be coerced to type 'integer'
Calls: %dopar% -> <Anonymous> -> mclapply
Execution halted

Code:

dist<-array(0, dim=c(320,500,25))

mc<-8
cl<-makeCluster(mc)
registerDoMC(cl)
opts<-list(chunkSize=10)

for(a in 1:25) {
  dist[,,a]<-foreach(x=1:500, .combine='cbind', .options.mc=opts) %:% 
    foreach(y=1:320, .combine='c') %dopar% {
      gcd.slc(crdsx[y,x], crdsy[y,x], lot[a,5], lot[a,4])
    }
}
stopCluster(cl)

On a different machine, it works nicely with

registerDoParallel(cl)

instead of

registerDoMC(cl)
like image 519
user2530062 Avatar asked Jun 28 '13 00:06

user2530062


1 Answers

You're getting that error because registerDoMC expects an integer argument, not a cluster object, while registerDoParallel expects either an integer or a cluster object. Basically, you need to decide which package to use and not mix them.

If you use doMC, then you never create a cluster object. A minimal doMC example looks like:

library(doMC)
registerDoMC(3)
foreach(i=1:10) %dopar% sqrt(i)

The doParallel package is a mashup of the doMC and doSNOW packages, and so you don't need to use cluster objects. Converting the previous example to doParallel is very simple:

library(doParallel)
registerDoParallel(3)
foreach(i=1:10) %dopar% sqrt(i)

The confusing thing is that on Windows, doParallel will actually create and use a cluster object behind the scenes, while on Linux and Mac OS X, it doesn't use a cluster object because it uses mclapply just as in the doMC package. I think that is convenient, but it can be a source of confusion.

like image 187
Steve Weston Avatar answered Nov 15 '22 10:11

Steve Weston