2

When I using the doParallel library, I encountered this weird error, the system throws this

" Error in { : task 1 failed -could not find function "%dopar%"

To be specific, this is what I did

library(doParallel)
cl <- makeCluster(4)
registerDoParallel(cl)
# Read the data
coin95 <-read.csv('~/Documents/coin95.csv')
coin95 <- coin95[,!(names(coin95) %in% c("X"))]
coin95[c("Person")] <- sapply(coin95[c("Person")],as.character)
# create the name list
coin95_name <- as.character(coin95$Person)  
coin95_name <- unique(coin95_name)
n <- as.numeric(length(coin95_name))

# the average counting process

ntw <- function(now){
  foreach (Ii = coin95_name,.combine = "+",.export = c("coin95","n")) %dopar% {
    time <-subset(coin95, subset = coin95$Person == Ii)$duration
    stepfun(time,seq(0,length(time)))(now)/n
  }
}

# the average cumulative hazard
lambda <- function(now,params){
  b <- params[1]
  sigma <- params[2]
  mu <- params[3]
  xi <- params[4]
  beta1 <- params[5]
  beta2 <- params[6]

  k <- function(spread){
    L0 <- (1+(spread -mu)*xi/sigma)^(-1/xi)
    return(L0)
  }

  foreach(Ii = coin95_name,.combine = "+",.export = c("coin95","n")) %dopar% {
    time <- subset(coin95, subset = coin95$Person == Ii)$duration
    noncov <- subset(coin95, subset = coin95$Person == Ii)$noncovered
    reim <- subset(coin95, subset = coin95$Person == Ii)$reimbursement
    (b*now+sum( exp(-k(now-time[(time < now)])+beta1*noncov[(time < now)]+beta2*reim[(time <now)]) ))/n

  }

}

So far, everything is GOOD, I have created two functions ntw and lambda using the foreach. They worked perfectly.

Then I create the third function also using the foreach:

# the distance
Time <- coin95$duration
Time <- sort(as.double(Time))

jl <- function(params){
       res<-foreach(Ii = Time,.combine = "rbind",.export = c("ntw","lambda")) %dopar% {
         (ntw(Ii)-ntw(Ii-1e-7)) * (ntw(Ii)- lambda(Ii,params))^2
         }
       return(sqrt(sum(res))) 
     }

guess<-c(0.0,1.3333,0.0,0.1,-1.2,3e-3)

Type jl(guess):

> jl(guess)
 Show Traceback

 Rerun with Debug
 Error in { : task 1 failed -could not find function "%dopar%" 

Any Idea what's going wrong ?

epo3
  • 2,991
  • 2
  • 33
  • 60
skyindeer
  • 165
  • 2
  • 11
  • 1
    You shouldn't (can't?) parallelize inside parallelized code. Use the nesting operator `%:%` instead. (And you should seriously consider if you can't turn some of your loops into vectorized operations. That way your performance improves by orders of magnitude compared to the relatively small gain from parallelization.) – Roland Apr 12 '16 at 11:54
  • Thanks @Roland , I rewrite the program, giving up the idea that the first two functions use `foreach` and only the last function use parallel, and it works. As for vectorize, I am not sure how to do it in my case. – skyindeer Apr 12 '16 at 12:14

1 Answers1

0

Quick fix for problem with foreach %dopar% is to reinstall these packages:

install.packages("doSNOW")

install.packages("doParallel") 

install.packages("doMPI")

Above packages are responsible for parallelism in R. Bug which existed in old versions of these packages is now removed. I should mention that it will most likely help even though you are not using these packages in your code.

M_D
  • 287
  • 3
  • 13