-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_cluster.R
48 lines (41 loc) · 1.34 KB
/
setup_cluster.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
## Setup Cluster
#Sys.setenv(PATH = paste0(Sys.getenv('PATH'), ':/usr/lib/rstudio-server/bin/postback'))
require("foreach")
require("doParallel") # install doParallel in all the hosts
primary <- '10.64.10.37' # remember to use IP or FQDN
machineAddresses <- list(
list(host=primary,user='harpo',
ncore=4),
list(host='10.64.10.36',user='harpo',
ncore=4)
)
spec <- lapply(machineAddresses,
function(machine) {
rep(list(list(host=machine$host,
user=machine$user)),
machine$ncore)
})
spec <- unlist(spec,recursive=FALSE)
parallelCluster <- parallel::makePSOCKcluster(
spec,
master=primary,
homogeneous=T,manual=F)
registerDoParallel(parallelCluster)
print(parallelCluster)
getDoParWorkers()
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
ptime <- system.time({
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(10000, 10000, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
})[3]
ptime
print(paste(ptime))
# Shutdown cluster neatly
if(!is.null(parallelCluster)) {
parallel::stopCluster(parallelCluster)
parallelCluster <- c()
}