# [R] From nested loop to mclapply

Alaios alaios at yahoo.com
Tue Apr 19 12:31:53 CEST 2011

```Dear Allan,
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)?

Best Regards
Alex

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

> From: Allan Engelhardt <allane at cybaea.com>
> Subject: Re: [R] From nested loop to mclapply
> To: "Alaios" <alaios at yahoo.com>
> Cc: R-help at r-project.org
> Date: Monday, April 18, 2011, 5:36 PM
> Try help("expand.grid",
> package="base") for one way to create the
> combinations of (i,j) outside the loop, or perhaps
> vignette("nested",
> package="foreach") which does it "automatically" (rather:
> naturally).
>
> Allan
>
> On 18/04/11 16:53, Alaios wrote:
> > Dear all,
> > I am trying to find a decent way to speed up my code.
> >
> > So far I have used mclapply with really good results
> (parallel version of lapply). I have a nested loop that I
> would like to help me convert it to lapply
> >
> > for (i in
> seq(from=-1,to=1-2/ncol(sr),length=ncol(sr))){
> >       for (j in
> seq(from=-1,to=1-2/nrow(sr),length=nrow(sr))){
> >
> estimatedsr[findCoord(c(i,j),sr),findCoord(c(i,j),sr)
> ]<-fxy(c(i,j))
> >       }
> >
> > So far I have converted some one-depth for loops to
> lapply but I am not sure If I can use lapply to convert a
> nested loop to something simpler.
> >
> > Best Regards
> > Alex
> >
> > ______________________________________________
> > R-help at r-project.org
> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help