Skip to content

Problems parallelizing glmnet

6 messages · Patrik Waldmann, Zachary Mayer, Stephen Weston +3 more

#
y<-rnorm(1000)
x<-matrix(rnorm(1000*10000),ncol=10000)
dimx<-dim(x)

library(doParallel)
library(foreach)
cl <- makeCluster(8, methods=FALSE)
registerDoParallel(cl)
print(system.time(
pval <- foreach (i =1:dimx[2], .combine=c) %dopar% {
mod <- lm(y ~ x[,i])
summary(mod)$coefficients[2,4]
}
))

  user  system elapsed 
 12.28    2.75  231.93 

stopCluster(cl)

library(parallel)
cl <- makeCluster(8, methods=FALSE)
print(system.time(
pval <- unlist(mclapply(1:dimx[2], function(i) summary(lm(y ~ x[,i]))$coefficients[2,4]))
))

  user  system elapsed 
 21.80    1.33   25.78 

stopCluster(cl)
On 09/06/2012 12:15 PM, Patrik Waldmann wrote:
A search of the list archives doesn't provide any backup that you have 
'showed' anything.

The only other post by you that I can see on the list shows the total 
elapsed time decreasing with the use of foreach, even though it was, 
indeed, a trivial function evaluation.
#
In this example, you're comparing doParallel using a
PSOCK cluster with mclapply.  A better comparison would
use doParallel with mclapply by registering doParallel
with:

registerDoParallel(cores=8)

That will give performance much closer to mclapply,
although still not as good due to the extremely short
tasks.  With very short tasks, the overhead is
magnified.

For a single (non-Windows) machine, there's no need to
create a cluster object at all, either for doParallel or for
mclapply.  You should only create a cluster object
if you're running on multiple nodes or a Windows
machine, but in those cases, you can't use mclapply.

- Steve


On Thu, Sep 6, 2012 at 4:16 PM, Patrik Waldmann
<patrik.waldmann at boku.ac.at> wrote:
#
On Thu, Sep 6, 2012 at 1:58 PM, Zachary Mayer <zach.mayer at gmail.com> wrote:
FWIW, the foreach construct itself (without any parallel backend) is
quite slow and I would not use to loop over a large number of quick
calculations.

Peter
#
I can tell you from my testing with caret that there is considerable
speedup using foreach. See Figure 3 of

  http://cran.r-project.org/web/packages/caret/vignettes/caretTrain.pdf

Of course it is model dependent but I have yet to see it slow down
computations (but I' sure it is possible).

Max

On Thu, Sep 6, 2012 at 5:09 PM, Peter Langfelder
<peter.langfelder at gmail.com> wrote:

  
    
#
On 09/06/2012 04:09 PM, Peter Langfelder wrote:
Peter, this is right.  It is not a general replacement for a for 
loop.... there's too much infrastructure involved.  However, we 
typically write all of our code that will be parallelized for deployment 
using %dopar% and then use registerDoSEQ() and run single threaded on a 
smaller test set while debugging.

Regards,

    - Brian