Why sequential code outperfom parallel code in R -


i converted serial code parallel, don't know why parallel code takes longer sequential code. in sequential takes few seconds while in parallel takes 8 9 minutes. how can improve code?

i able create similar problem original one. here code. data structure same in original problem , can't change (i believe).

parallel code:

rm(list=ls()) library(foreach); library(doparallel)  cores<-16 c1<-makecluster(cores) registerdoparallel(c1)  cases<-64 prev<-list() (i in 1:cases) prev[[i]]<-c(110, 1340, 4560, 230, 10)  #mimicking original problem #=========================================== out<-list() (i in 1:cases) out[[i]]<-list() out1<-matrix(0,nrow=131041,ncol=6) (i in 1:cases){    out1[,]<-runif(6*131041,1,100)   out[[i]]<-rbind(out[[i]],out1) } #===========================================  y1<-y2<-y3<-y4<-y5<-c(0) tol <- 1e-3  time1<-sys.time() status<-foreach (j = 1:cases, .combine='rbind', .inorder=false) %dopar% { #status<-list(); (i in 1:cases) status[[i]]<-list() #for (j in 1:cases)  {   y1[j] <- as.numeric(out[[j]][length(out[[j]][,1]),2])   y2[j] <- as.numeric(out[[j]][length(out[[j]][,1]),3])   y3[j] <- as.numeric(out[[j]][length(out[[j]][,1]),4])   y4[j] <- as.numeric(out[[j]][length(out[[j]][,1]),5])   y5[j] <- as.numeric(out[[j]][length(out[[j]][,1]),6])    v1<-abs(y1[j]-prev[[j]][1])   v2<-abs(y2[j]-prev[[j]][2])   v3<-abs(y3[j]-prev[[j]][3])   v4<-abs(y4[j]-prev[[j]][4])   v5<-abs(y5[j]-prev[[j]][5])     if (v1<tol & v2<tol & v3<tol & v4<tol & v5<tol)     st<-1   else     st<-0    #status[[j]]<-c(y1[j],y2[j],y3[j],y4[j],y5[j],st)   return (c(j,sys.getpid(),y1[j],y2[j],y3[j],y4[j],y5[j],st)) }# end of j-loop  time2<-sys.time()  stopcluster(c1) print(difftime(time2,time1)) 

to convert parallel code serial:

uncomment following 3 three lines

#status<-list(); (i in 1:cases) status[[i]]<-list() #for (j in 1:cases)  { .............. .............. #status[[j]]<-c(y1[j],y2[j],y3[j],y4[j],y5[j],st) 

and comment out following 2 lines:

status<-foreach (j = 1:cases, .combine='rbind', .inorder=false) %dopar% { .................. .................. return (c(j,sys.getpid(),y1[j],y2[j],y3[j],y4[j],y5[j],st)) 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -