-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRfuture2.Rpres
More file actions
321 lines (242 loc) · 7.43 KB
/
Rfuture2.Rpres
File metadata and controls
321 lines (242 loc) · 7.43 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
R future
========================================================
css: bootstrap.min.css
width: 1440
height: 900
## Non blocking, parallel assignment in R
[EdinbR](http://edinbr.org/), 21/09/2016
Guillaume Devailly, [@G_Devailly](https://twitter.com/G_Devailly)

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, prompt = TRUE)
library(future)
plan(multiprocess)
options(digits = 3)
```
R future
========================================================
Future is an R package by [Henrik Bengtsson](https://twitter.com/henrikbengtsson), available on [CRAN](https://cran.r-project.org/package=future).
```{r install package, eval = FALSE}
install.packages("future")
library(future)
plan(multiprocess) # more on that later
```
It introduces yet another assignment operator: `%<-%`
`%<-%` is yet another assignment operator:
========================================================
```{r YAAO}
a %<-% 42
a
b %<-% c(rep(a, 4), 43)
b
```
`%<-%` is non blocking (1/2):
========================================================
First, let's create slow functions:
```{r slow function}
slow <- function(myFunc, by_seconds = 3) {
return(
function(...) {
Sys.sleep(by_seconds)
myFunc(...)
}
)
}
slow_rnorm <- slow(rnorm)
t0 <- Sys.time()
slow_rnorm(4)
Sys.time() - t0
```
`%<-%` is non blocking (2/2):
========================================================
Without `%<-%`:
```{r blocking}
t0 <- Sys.time()
a <- slow_rnorm(2)
Sys.time() - t0
a
```
***
With `%<-%`:
```{r non blocking}
t0 <- Sys.time()
b %<-% slow_rnorm(2)
Sys.time() - t0
b
```
`%<-%` is **not** magic:
========================================================
It create a *future*, a variable that will be available in the future.
The task is **not** magically optimised, it runs in the background.
When the variable is needed, the R (main) process will wait until the *future* is resolved.
```{r non magical}
t0 <- Sys.time()
b %<-% slow_rnorm(2)
Sys.time() - t0
b
Sys.time() - t0
```
*futures* can be run in parallel:
========================================================
Standard assignment:
```{r parallel 1}
t0 <- Sys.time()
x1 <- slow_rnorm(2)
x2 <- slow_rnorm(2)
Sys.time() - t0
list(x1, x2)
Sys.time() - t0
```
***
Assignment with future:
```{r parallel 2}
t0 <- Sys.time()
x3 %<-% slow_rnorm(2)
x4 %<-% slow_rnorm(2)
Sys.time() - t0
list(x3, x4)
Sys.time() - t0
```
*future* allows easy parallelisation of heterogeneous tasks (1/3)
========================================================
```{r complex task 0}
myMat <- matrix(1:6, ncol = 2)
slow_apply <- slow(apply)
```
Standard assignment:
```{r complex task 1}
t0 <- Sys.time()
myMean <- slow_apply(myMat, 1, mean)
mySd <- slow_apply(myMat, 1, sd)
myRnorm <- slow_rnorm(3)
myRunif <- slow(runif)(3)
data.frame(myMean, mySd, myRnorm, myRunif)
Sys.time() - t0
```
*future* allows easy parallelisation of heterogenous tasks (2/3)
========================================================
Parallelization with *future*:
```{r complex task 2}
t0 <- Sys.time()
myMean %<-% slow_apply(myMat, 1, mean)
mySd %<-% slow_apply(myMat, 1, sd)
myRnorm %<-% slow_rnorm(3)
myRunif %<-% slow(runif)(3)
data.frame(myMean, mySd, myRnorm, myRunif)
Sys.time() - t0
```
*future* allows easy parallelisation of heterogenous tasks (3/3)
========================================================
Parallelization with parallel:
```{r complex task 3}
library(parallel)
t0 <- Sys.time()
myCommands <- c(
myMean = "slow_apply(myMat, 1, mean)",
mySd = "slow_apply(myMat, 1, sd)",
myRnorm = "slow_rnorm(3)",
myRunif = "slow(runif)(3)"
)
as.data.frame(mclapply( # not parallel on windows, but should work elsewhere
myCommands,
function(x) eval(parse(text = x))
# mc.cores = 4L <- add this on non windows OS
))
Sys.time() - t0
```
A *future_mclapply* draft function (1/3)
========================================================
```{r future_mclapply}
future_mclapply <- function(myList, myFunction) {
if(is.vector(myList)) myList <- as.list(myList) # the function will work on vectors to
for(i in seq_along(myList)) {
command <- paste0(
"x",
i,
" %<-% do.call(",
deparse(substitute(myFunction)),
",",
deparse(substitute(myList[i])),
")"
)
eval(parse(text = command))
}
outputVars <- paste(
paste0("x", seq_along(myList)),
collapse = ", "
)
output <- eval(parse(text = paste0("list(", outputVars, ")")))
names(output) <- names(myList)
return(output)
}
```
A *future_mclapply* draft function (2/3)
========================================================
```{r future_mclapply2}
t0 <- Sys.time()
future_mclapply(
list(1:5, 6:10, pi),
slow(mean)
)
Sys.time() - t0
```
A *future_mclapply* function (3/3)
========================================================
Or the hidden function (may change / disappear in future version of the package):
```{r future_mclapply3}
t0 <- Sys.time()
future:::flapply(
list(1:5, 6:10, pi),
slow(mean)
)
Sys.time() - t0
```
One package, many plans
========================================================
* `plan(multiprocess)`: parallel, non blocking
* `plan(eager)`: non parallel, blocking (default)
* `plan(lazy)`: non parallel, non blocking
* `plan(cluster, workers = c("n1", "n2", "n3"))`: run in a cluster, non blocking. Nice gestion of the global variables.
* see also [future.BatchJobs](https://cran.r-project.org/web/packages/future.BatchJobs/index.html) for more cluster plans.
Write package using *future*, users will choose how to run it by changing the `plan`.
Limiting the number of cores in `plan(multiprocess)` (1/2)
========================================================
```{r limiting number of cores}
availableCores()
plan(multiprocess(workers = 1 + 3)) # 1 main + 3 background
t0 <- Sys.time()
x1 %<-% slow_rnorm(1)
x2 %<-% slow_rnorm(1)
x3 %<-% slow_rnorm(1)
Sys.time() - t0
c(x1, x2, x3)
Sys.time() - t0
```
Limiting the number of cores in `plan(multiprocess)` (2/2)
========================================================
```{r limiting number of cores 2}
plan(multiprocess(workers = 1 + 2)) # 1 main + 2 background
t0 <- Sys.time()
x1 %<-% slow_rnorm(1)
x2 %<-% slow_rnorm(1)
x3 %<-% slow_rnorm(1) # no more background process available, blocks the main process
Sys.time() - t0
c(x1, x2, x3)
Sys.time() - t0
```
Miscellaneous
========================================================
* Nested features are supported, see this [vignette](https://cran.r-project.org/web/packages/future/vignettes/future-3-topologies.html)
* Check if a future is resolved:
```{r pid}
x1 %<-% slow_rnorm(3)
f <- futureOf(x1)
resolved(f)
```
* No easy way to stop an unresolved future at the moment (killing the process?)
Links
========================================================
* The package: [(https://cran.r-project.org/web/packages/future/index.html)](https://cran.r-project.org/web/packages/future/index.html)
* The comprehensive vignette:[https://cran.r-project.org/web/packages/future/vignettes/future-1-overview.html](https://cran.r-project.org/web/packages/future/vignettes/future-1-overview.html)
* [slides from useR2016](http://www.jottr.org/2016/07/a-future-for-r-slides-from-user-2016.html) by Henrik Bengtsson.
* [`doFuture` package](https://cran.r-project.org/package=doFuture): alternative to `doMC`, `doParallel`, `doMPI`, and `doSNOW`.