New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parLapply with makeForkCluster is faster than mclapply #31

Closed
psolymos opened this Issue Jan 23, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@psolymos
Owner

psolymos commented Jan 23, 2018

Forking builds up overhead, but #30 gave a hint that using Fork cluster can be considerably faster than mclapply:

> set.seed(1234)
> n <- 200
> x <- rnorm(n)
> y <- rnorm(n, crossprod(t(model.matrix(~ x)), c(0, 1)), sd = 0.5)
> d <- data.frame(y, x)
> ## model fitting and bootstrap
> mod <- lm(y ~ x, d)
> ndat <- model.frame(mod)
> B <- 100
> bid <- sapply(1:B, function(i) sample(nrow(ndat), nrow(ndat), TRUE))
> fun <- function(z) {
+     if (missing(z))
+         z <- sample(nrow(ndat), nrow(ndat), TRUE)
+     coef(lm(mod$call$formula, data=ndat[z,]))
+ } 

## forking with mclapply
> system.time(res1 <- pblapply(1:B, function(i) fun(bid[,i]), cl = 2L))
   |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 01s
   user  system elapsed 
  0.587   0.919   0.845 

## forking with parLapply
> cl <- makeForkCluster(2L)
> system.time(res1 <- pblapply(1:B, function(i) fun(bid[,i]), cl = cl))
   |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 00s
   user  system elapsed 
  0.058   0.009   0.215 
> stopCluster(cl)

## Socker cluster (need to pass objects to workers)
> cl <- makeCluster(2L)
> clusterExport(cl, c("fun", "mod", "ndat", "bid"))
> system.time(res1 <- pblapply(1:B, function(i) fun(bid[,i]), cl = cl))
   |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 00s
   user  system elapsed 
  0.053   0.008   0.169 
> stopCluster(cl)

I was wondering if this should be exploited for instances when cl argument is integer (indicating forking). We would then create a Fork cluster (cl <- makeForkCluster(cl)), run parLapply(cl, ...), and destroy the cluster with on.exit(stopCluster(cl), add = TRUE).

@psolymos

This comment has been minimized.

Owner

psolymos commented Jan 23, 2018

It turns our that pbapply now cathed up to pbmcapply:

> ncl <- 2
> B <- 1000
> fun2 <- function(x) {
+     Sys.sleep(0.01)
+     x^2
+ }
> library(pbmcapply)
> (t1 <- system.time(pbmclapply(1:B, fun2, mc.cores = ncl)))
  |========================================================| 100%, ETA 00:00
   user  system elapsed 
  0.242   0.114   5.461 

> library(pbapply) # 1.3-4 CRAN version
> (t2 <- system.time(pblapply(1:B, fun2, cl = ncl)))
   |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 07s
   user  system elapsed 
  0.667   1.390   6.547 

> library(pbapply) # 1.3-5 fork-cluster-speedup branch
> (t2 <- system.time(pblapply(1:B, fun2, cl = ncl)))
   |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed = 06s
   user  system elapsed 
  0.225   0.100   5.710 
@psolymos

This comment has been minimized.

Owner

psolymos commented Jan 23, 2018

I typed up my findings here.

I am a bit reluctant of merging the new branch for the following reasons:

  • makeForkCluster was already an option before by explicitly stating the cluster to be a Fork;
  • by hiding the process of creating and destroying the cluster, user options are restricted (i.e. no control over RNGs, which can be a major drawback for simulations);
  • mclapply wasn't so bad to begin with, because the number of updates were capped by the nout option.

I would recommend the following workflow that is based purely on the stable CRAN version:

cl <- makeForkCluster(2L)
output <- pblapply(..., cl = cl)
stopCluster(cl)

@psolymos psolymos closed this Jan 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment