How good is it to parallelize twice using foreach in R?

0

I am programming a code that calculates some parameters in a series of time of which I must generate 200 functions of each, on which I calculate the parameters with a function that I built. My code is as follows

library(foreach)
library(doParallel)

freq<-list()  #lista donde cada elemento es una matriz cuyas filas son las series de tiempo
HC<-function(ts, D) #HC es una funcion donde ts es una serie de tiempo y D un parametro de control

ptm2<-proc.time()
#crea cluster y paraleliza calculos
cl<-makeCluster(8) 
registerDoParallel(cl)
#calcula parametros
hc<-foreach(x=freq, .combine = rbind) %dopar% 
    t(apply(as.matrix(3:7), 1, function(i) t(HC(x,i))))
stopCluster(cl)

tiempo2<-proc.time()-ptm2

As HC is a function that takes time and I must calculate it over 40000 series (which I can divide into lists of 200 elements) I thought about doing something like this

library(foreach)
library(doParallel)

data<-list()  #lista de 200 matrices donde cada fila es una serie de tiempo
HC<-function(ts, D) 

mat2list<-function(x){y<-lapply(1:nrow(x), function(i) x[i,])}  #convierte una matriz en una lista

ptm2<-proc.time()
#crea cluster y paraleliza
cl<-makeCluster(8) 
registerDoParallel(cl)
#calculo de parametros
hc<-foreach(freq=data, .combine = rbind) %dopar% 
    function (freq) { foreach(x=mat2list(freq)) %dopar% 
    t(apply(as.matrix(3:7), 1, function(i) t(HC(x,i))))}
stopCluster(cl)

tiempo2<-proc.time()-ptm2

My question is, is it good to parallelize twice or does the cluster cl only focus on the first foreach ? Thank you very much.

    
asked by Jorge Mendoza Ruiz 19.08.2018 в 22:13
source

0 answers