Lili
Lili

Reputation: 371

Parallel with Foreach and doMC packages in Linux- Error with mclapply

I want to run parallel computing in Linux. After i managed to do so in Windows i need to run the function below in Linux. I changed the package doSnow to doMC that suppose to work in Linux but i get an error related to mclapply.

Code:

foreachFunc = function(Data) {

  RowFunction<-function(d)
  {
    (chisq.test(d)$p.value)}

  P<-as.matrix(apply(Data,1,RowFunction))

  return(P)}

library(doMC)
library(foreach)

number_of_cpus=4
cl<-makeCluster(number_of_cpus) 
registerDoMC(cl)


Chunks<-c(1:NROW(Data_new))%%4

P<-foreach(i=0:3, .combine=rbind, mc.cores=4) %dopar%  {
  foreachFunc(Data_new[Chunks==i, ])}
stopCluster(cl)

Error:

Error in mclapply(argsList, FUN, mc.preschedule = preschedule, mc.set.seed = set.seed,  : 
  (list) object cannot be coerced to type 'integer'

Upvotes: 1

Views: 706

Answers (1)

Roland
Roland

Reputation: 132959

Read vignette("gettingstartedMC"). I can reproduce your error like this:

number_of_cpus=4
cl<-makeCluster(number_of_cpus) 
registerDoMC(cl)
P<-foreach(i=0:3) %dopar%  i
#Error in mclapply(argsList, FUN, mc.preschedule = preschedule, mc.set.seed = set.seed,  : 
#  (list) object cannot be coerced to type 'integer'
stopCluster(cl)

This works as expected:

registerDoMC(cores=4)
P<-foreach(i=0:3) %dopar%  i

Explanation: registerDoMC expects an integer value for its first argument. You gave it a list, i.e. the return object of makeCluster.

Upvotes: 2

Related Questions