Skip to content
Prev 257357 / 398506 Next

From nested loop to mclapply

Dear Allan,
thank you very much for your answer.
If I got it right your idea is 
a: create first all the i,j combinations and then 
b. use mclapply (parallel version of lapply).

so I wrote some draft and run three experiments:

# code
require('multicore')

sr<-matrix(data=NA,ncol=256,nrow=256)

sum=0

i <- seq(from=-1,to=1-2/ncol(sr),length=ncol(sr))
j <- seq(from=-1,to=1-2/nrow(sr),length=nrow(sr))
iandj<-expand.grid(i=i,j=j)


system.time(for (i in seq(from=-1,to=1-2/ncol(sr),length=ncol(sr))){#Calculate the estimated values, ncol*2 just to make sure all cels are #there
	  for (j in seq(from=-1,to=1-2/nrow(sr),length=nrow(sr))){ 
	    sum=i+j+sum
	  }
})


system.time(sum(unlist(lapply(1:nrow(iandj),function(rowId) { return (iandj$i[rowId]+iandj$j[rowId]) }))))
system.time(sum(unlist(mclapply(1:nrow(iandj),function(rowId) { return (iandj$i[rowId]+iandj$j[rowId]) }))))


# # # # Code end

Please feel free to copy and paste it. This will returns three times the results of system.time for
a) normal for ...loop case
b) lapply
c) mclapply

In my normal four-core system I get the following results

a) user  system elapsed 
  9.143   1.301   5.148 
b)   user  system elapsed 
  3.482   0.534   1.993 
c)   user  system elapsed 
  0.456   0.242   1.031 


so far so good.
Then comes a parallel system with 32 cores that I have in work
This returns some strange results and I would like to ask from everyone to comment
 user  system elapsed 
a)  0.124   0.000   0.124 
  user  system elapsed 
b)  0.876   0.000   0.877 
   user  system elapsed 
c)  0.176   0.080   0.261 

Why do you believe that a) performed much better (normal nested for loop) than the 32 cores in parallel c)?

I would like to thank you in advance for your help

Best Regards
Alex
--- On Mon, 4/18/11, Allan Engelhardt <allane at cybaea.com> wrote: