Skip to content

Instantly share code, notes, and snippets.

@agrueneberg
Created September 10, 2015 15:20
Show Gist options
  • Save agrueneberg/2a9649bcf858193b69db to your computer and use it in GitHub Desktop.
Save agrueneberg/2a9649bcf858193b69db to your computer and use it in GitHub Desktop.
Parallel Computing in R
library(parallel)
library(BGLR)
library(microbenchmark)
data(mice)
X <- mice.X
y <- mice.pheno$Obesity.BMI
GWAS <- function (i) {
summary(lm(y ~ X[, i]))$coef[2, ]
}
seq1 <- function () {
out <- matrix(nrow = ncol(X), ncol = 4, NA)
for (i in 1:ncol(X)) {
out[i, ] <- GWAS(i)
}
out
}
seq2 <- function () {
t(sapply(1:ncol(X), GWAS))
}
par1 <- function () {
cl <- makeCluster(detectCores())
clusterExport(cl, c('y', 'X'))
out <- t(parSapply(cl, 1:ncol(X), GWAS))
stopCluster(cl)
out
}
par2 <- function () {
t(simplify2array(mclapply(1:ncol(X), GWAS, mc.cores = detectCores())))
}
microbenchmark(
seq1(),
seq2(),
par1(),
par2(),
times = 5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment