-
Notifications
You must be signed in to change notification settings - Fork 0
/
doSnow.R
54 lines (41 loc) · 1.54 KB
/
doSnow.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
49
50
51
52
53
54
# ----------------------------------------------
# Deluxe doSNOW cluster setup in R
# Tobias Kind (2015)
# https://github.com/tobigithub/R-parallel/wiki/
# ----------------------------------------------
# Installation of the doSNOW parallel library with all dependencies
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("doSNOW")
if((doInstall) && (!is.element(toInstall, installed.packages()[,1])))
{
cat("Please install required package. Select server:"); chooseCRANmirror();
install.packages(toInstall, dependencies = c("Depends", "Imports"))
}
# load doSnow and (parallel for CPU info) library
library(doSNOW)
library(parallel)
# For doSNOW one can increase up to 128 nodes
# Each node requires 44 Mbyte RAM under WINDOWS.
# detect cores with parallel() package
nCores <- detectCores(logical = FALSE)
cat(nCores, " cores detected.")
# detect threads with parallel()
nThreads<- detectCores(logical = TRUE)
cat(nThreads, " threads detected.")
# Create doSNOW compute cluster (try 64)
# One can increase up to 128 nodes
# Each node requires 44 Mbyte RAM under WINDOWS.
cluster = makeCluster(nThreads, type = "SOCK")
class(cluster);
# register the cluster
registerDoSNOW(cluster)
#get info
getDoParWorkers(); getDoParName();
# insert parallel computation here
# stop cluster and remove clients
stopCluster(cluster); print("Cluster stopped.")
# insert serial backend, otherwise error in repetetive tasks
registerDoSEQ()
# clean up a bit.
invisible(gc); remove(nCores); remove(nThreads); remove(cluster);
# END