Skip to content

Instantly share code, notes, and snippets.

@pedro
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedro/f931e8cd20b5770b6780 to your computer and use it in GitHub Desktop.
Save pedro/f931e8cd20b5770b6780 to your computer and use it in GitHub Desktop.
Running R code in parallel on Heroku
# used by the R buildpack to install dependencies:
# https://github.com/virtualstaticvoid/heroku-buildpack-r
install.packages("foreach", dependencies = TRUE)
install.packages("doParallel", dependencies = TRUE)
$ heroku run bash
Running `bash` attached to terminal... up, run.5154
~ $ Rscript testSequential.r
[1] "Hello"
Loading required package: iterators
Loading required package: parallel
elapsed
57.408
~ $ Rscript testParallel.r
[1] "Hello"
Loading required package: iterators
Loading required package: parallel
elapsed
33.467
# example from: http://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf
library(foreach)
library(doParallel)
registerDoParallel(cores=2)
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
print(ptime)
# example from: http://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf
library(foreach)
library(doParallel)
registerDoParallel(cores=2)
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %do% { # note the only difference here is using %do% instead of %dopar%
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
print(ptime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment