-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_processing.R
More file actions
46 lines (34 loc) · 1.31 KB
/
parallel_processing.R
File metadata and controls
46 lines (34 loc) · 1.31 KB
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
#' This is how to use more than one core in R at a time. Not all processes
#' will be worth parellising as the CPU usage to communicate is greater than
#' the cost of just running things sequentially
library(parallel)
library(benchmarkme)
detectCores()
get_cpu()
# create data
m <- matrix(rnorm(10000), ncol = 10)
# use apply to calculate median for each row
system.time(res <- apply(m, 1, median))
# use n-1 cores for R so that you can still do other things too
copies_of_r <- detectCores() - 1
cl <- makeCluster(copies_of_r)
## parApply
system.time(res <- parApply(cl, m, 1, median)) # parallel apply function - needs additional argument for cluster
stopCluster(cl) # free up resources
## parSapply
pokemon <- read.csv("Dropbox/PhD2/Research Skills/Statistics/R/datacamp/pokemon.csv")
## normal single thread
bootstrap <- function(data_set){
# sample with replacement
s <- sample(1:nrow(data_set), replace = TRUE)
new_data <- data_set[s,]
# calculate the correlation
cor(new_data$Attack, new_data$Defense)
}
system.time(sapply(1:100, function(i) bootstrap(pokemon)))
## multi thread
copies_of_r <- detectCores() - 1
cl <- makeCluster(copies_of_r)
clusterExport(cl, c("bootstrap", "pokemon")) # have to explicitly export functions as well
system.time(parSapply(cl, 1:100, function(i) bootstrap(pokemon)))
stopCluster(cl)