-1

I am trying to run a loop in R for string detect for on multiple cores using foreach but I see it runs only on one core and I was not able to bring it to run on several cores.

    library("parallel")
library("foreach")
library("doParallel")

#How to detect how many cores are on the server
detectCores()

cl <- makeCluster(3)
registerDoParallel(cl)

x<-c("Hello My Name is", "Happy Birthday", "Hi How are you today? My Name is walley", "Nice to meet you", "Best friends")
y<-c("Hello", "Birthday", "Hi", "Nice", "Best friends")

foreach(i = 1:length(y)) %dopar%{

print(i)
if (sum(str_detect(x,paste("\\b",as.character(y),"\\b", sep=""))) > 0){

    str_replace(x[i], as.character(y[which(str_detect(x[i],paste("\\b",as.character(y),"\\b", sep="")) == TRUE)]), "")
    }
}

write.csv(mycatalog, "Matching.csv")
getDoParWorkers()

stopCluster(cl)
Micmic
  • 1
  • 4
  • 1
    This is not reproducible. Please boil down your example to a small, _reproducible_ example. – Roman Luštrik Oct 13 '15 at 09:58
  • [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zx8754 Oct 13 '15 at 10:04
  • Ok this is the new modified and easier code. I created two vectors x and y and my idea is to do matching of each element of y if it is encountered in x and replace this element. – Micmic Oct 13 '15 at 10:13
  • From where come functions `str_detect` and `str_replace`? –  Oct 13 '15 at 10:20
  • from R library(stringr) library(Hmisc) – Micmic Oct 13 '15 at 10:23

1 Answers1

0

I guess that the loop is not executed in parallel because you are trying to modify a single array (x) from all the threads.

Try the following and see if it solves your problem

modifiedX = foreach(i = 1:length(y), .combine = c) %dopar%{

  stringCopy = x[i]

  if (sum(str_detect(x,paste("\\b",as.character(y),"\\b", sep=""))) > 0) {    
    str_replace(stringCopy, as.character (y[which(str_detect(x[i],paste("\\b",as.character(y),"\\b", sep="")) == TRUE)]), "")   
  }

  return(stringCopy)

}

print(modifiedX)