-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.Rmd
45 lines (36 loc) · 954 Bytes
/
parallel.Rmd
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
---
title: "Testing parallel"
output:
html_document:
number_sections: yes
---
```{r loadLibs}
library(parallel)
```
See https://www.r-bloggers.com/how-to-go-parallel-in-r-basics-tips/
Set up cluster:
```{r parSetUp}
# Calculate the number of cores
n_cores <- detectCores() - 1
# Initiate cluster
cl <- makeCluster(n_cores, type="FORK") # so all vars & envs go to and it also saves memomry space
```
Using `r n_cores` of the available `r detectCores()` cores.
Do something:
```{r runPar}
myList <- list(1:20)
parLapply(cl, myList,
tryCatch({
function(exponent)
2^exponent
}, error = function(e) return(paste0("The variable '",
exponent, "'",
" caused the error: '",
e, "'"))
)
)
```
Close the cluster:
```{r closeClus}
stopCluster(cl)
```