Error using% dopar% instead of% do% in R (doParallel package)

I came up with a strange mistake. Suppose I have 10 xxt objects in a list called data. Now I am looking for three combinations using

data_names <- names(data) combs <- combn(data_names, 3) 

My main goal is to make a PCA on these 1080 triples. To speed things up, I wanted to use the doParallel package. So, the fragment is reduced to the moment the error occurs:

 list <- foreach(i=1:ncol(combs)) %dopar% { tmp_triple <- combs[,i] p1<-data[tmp_triple[[1]]][[1]] p2<-data[tmp_triple[[2]]][[1]] p3<-data[tmp_triple[[3]]][[1]] data.merge <- merge(p1,p2,p3,all=FALSE) } 

Here the merge function seems to be a problem. Error

task 1 failed - "cannot force class" c ("xts", "zoo") "in data.frame"

However, when changing% dopar% to normal serial% do do%, everything works as accepted.

So far, I could not find a solution to this problem, and I'm not even sure what to look for.

+4
source share
3 answers

Probably the problem is that you did not call library(xts) for each of the workers. You do not say which backend you are using, so I cannot be 100% sure.

If this is a problem, then this code will fix it:

 list <- foreach(i=1:ncol(combs)) %dopar% { library(xts) tmp_triple <- combs[,i] p1<-data[tmp_triple[[1]]][[1]] p2<-data[tmp_triple[[2]]][[1]] p3<-data[tmp_triple[[3]]][[1]] data.merge <- merge(p1,p2,p3,all=FALSE) } 
+4
source

A better solution, rather than explicitly loading the libraries inside the function, would be to use the .packages argument of the foreach () function:

 list <- foreach(i=1:ncol(combs),.packages=c("xts","zoo")) %dopar% { tmp_triple <- combs[,i] p1<-data[tmp_triple[[1]]][[1]] p2<-data[tmp_triple[[2]]][[1]] p3<-data[tmp_triple[[3]]][[1]] data.merge <- merge(p1,p2,p3,all=FALSE) } 
+10
source

A quick fix to the problem with foreach% dopar% is to reinstall these packages:

 install.packages("doSNOW") install.packages("doParallel") install.packages("doMPI") 

They are responsible for parallelism in R. Bug, which existed in older versions of these packages, is now removed. It worked in my case.

0
source

Source: https://habr.com/ru/post/1486713/


All Articles