Last active
August 29, 2015 14:22
-
-
Save pedro/f931e8cd20b5770b6780 to your computer and use it in GitHub Desktop.
Running R code in parallel on Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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