Created
September 10, 2015 15:20
-
-
Save agrueneberg/2a9649bcf858193b69db to your computer and use it in GitHub Desktop.
Parallel Computing in R
This file contains hidden or 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
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