Skip to content

Instantly share code, notes, and snippets.

@pedro pedro/init.r
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.