-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.qmd
681 lines (471 loc) · 12.3 KB
/
index.qmd
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
---
title: Moju Kapu モジュカプ
subtitle: How `mirai` and `crew` Are Powering the Next Generation of Parallel Computing in R
author: Charlie Gao and Will Landau
institute: Hibiki AI Limited, Eli Lilly and Company
date: July 9, 2024
format:
revealjs:
theme:
- custom.scss
incremental: true
footer: "slides available at https://shikokuchuo.net/user2024-conference"
embed-resources: true
slide-number: true
editor:
render-on-save: true
---
## {.center}
<style>
h1 {
font-size: 1.6em !important;
}
h2 {
font-size: 1.4em !important;
}
</style>
<img src="images/mojukapu.png" />
`moju-kapu` (モジュカプ) is shorthand for `modular encapsulation` (モジュラーカプセル化)
. . .
::: {.nonincremental}
- Effective stand-alone tools < > entire integrated systems
- Natural limits of a package
- Interfaces for developers as well as end-users
- Layered engineering approach
:::
# mirai
## {.center}
::: {.nonincremental}
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - the / moju kapu /
:::
::: {.nonincremental}
* **Motivation**: production-grade parallel computing for R
* **Modularity**: interfaces with and enhances base R / Shiny etc.
* **Encapsulation**: developer interface for 3rd party extensions such as `crew`
:::
## mirai {.center}
Parallel & distributed computing for R
. . .
:::: {.columns}
::: {.column width="20%"}
<img alt="mirai logo" src="images/mirai.png" width="120" />
:::
::: {.column width="80%"}
The 新幹線 Shinkansen (bullet train)
<img alt="Shinkansen" src="images/shinkansen_fuji.jpg" width="800" />
:::
::::
##
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - The Shinkansen of Parallel Computing
. . .
:::: {.columns}
::: {.column width="50%"}
::: {.nonincremental}
1. Fast
:::
:::
::: {.column width="50%"}
::: {.nonincremental}
- 100x faster
:::
:::
::::
. . .
:::: {.columns}
::: {.column width="50%"}
```r
future::value(future::future(1))
#> [1] 1
```
:::
::: {.column width="50%"}
```r
mirai::mirai(1)[]
#> [1] 1
```
:::
::::
. . .
:::: {.columns}
::: {.column width="50%"}
<img alt="Toy train" src="images/toy_train.jpg" height="300" />
:::
::: {.column width="50%"}
<img alt="Shinkansen" src="images/shinkansen.jpg" height="300" />
:::
::::
##
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - The Shinkansen of Parallel Computing
:::: {.columns}
::: {.column width="50%"}
::: {.nonincremental}
1. Fast
2. Reliable
:::
:::
::: {.column width="50%"}
::: {.nonincremental}
- 100x faster
- WYSIWYG concept
:::
:::
::::
<img alt="headline" src="images/shinkansen_headline.png" width="800" />
##
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - The Shinkansen of Parallel Computing
:::: {.columns}
::: {.column width="50%"}
::: {.nonincremental}
1. Fast
2. Reliable
:::
:::
::: {.column width="50%"}
::: {.nonincremental}
- 100x faster
- WYSIWYG concept
:::
:::
::::
```r
model$accuracy
#> [1] 0.95
```
. . .
```r
f <- future::future(model$accuracy)
```
. . .
<span style="color:#ff0000">
Error in getGlobalsAndPackages(expr, envir = envir, tweak = tweakExpression, :
The total size of the 1 globals exported for future expression (‘model$accuracy’) is 762.94 MiB.. This exceeds the maximum allowed size of 500.00 MiB (option 'future.globals.maxSize'). There is one global: ‘model’ (762.94 MiB of class ‘list’)
</span>
##
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - The Shinkansen of Parallel Computing
:::: {.columns}
::: {.column width="50%"}
::: {.nonincremental}
1. Fast
2. Reliable
:::
:::
::: {.column width="50%"}
::: {.nonincremental}
- 100x faster
- WYSIWYG concept
:::
:::
::::
```r
model$accuracy
#> [1] 0.95
```
. . .
```r
m <- mirai::mirai(x, x = model$accuracy)
```
. . .
```r
m[]
#> [1] 0.95
```
##
<img alt="mirai logo" src="images/mirai.png" width="120" />
mirai - The Shinkansen of Parallel Computing
:::: {.columns}
::: {.column width="50%"}
::: {.nonincremental}
1. Fast
2. Reliable
3. Scalable
:::
:::
::: {.column width="50%"}
::: {.nonincremental}
- 100x faster
- WYSIWYG concept
- one million promises
:::
:::
::::
. . .
:::: {.columns}
::: {.column width="50%"}
<img alt="tokyo metro" src="images/metro.png" width="400" />
:::
::: {.column width="50%"}
<img alt="shanghai maglev" src="images/maglev.webp" width="400" />
:::
::::
## Modularity: Interfaces with and Enhances
[<img alt="R parallel" src="https://www.r-project.org/logo/Rlogo.png" width="100" />](https://shikokuchuo.net/mirai/articles/parallel.html) An alternative communications backend for R
[<img alt="Shiny" src="https://github.com/rstudio/shiny/raw/main/man/figures/logo.png" width="100" />](https://shikokuchuo.net/mirai/articles/shiny.html)
[<img alt="Plumber" src="https://rstudio.github.io/cheatsheets/html/images/logo-plumber.png" width="100" />](https://shikokuchuo.net/mirai/articles/plumber.html) Async backend (supports Shiny ExtendedTask)
[<img alt="Arrow" src="https://arrow.apache.org/img/arrow-logo_hex_black-txt_white-bg.png" width="100" />](https://shikokuchuo.net/mirai/articles/databases.html) Host ADBC database connections
[<img alt="torch" src="https://torch.mlverse.org/css/images/hex/torch.png" width="100" />](https://shikokuchuo.net/mirai/articles/torch.html) Seamless handling of Torch tensors
# `crew`: an encapsulation of `mirai`
## Motivation for `crew`
<center>
<img src="./images/targets.png" width=200>
</center>
::: {.nonincremental style="font-size: 85%;"}
* `targets` is a pipeline tool for reproducible computation at scale in R
* Manages large workflows in statistics and data science:
* Bayesian data analysis
* Machine learning
* Simulation of clinical trials
* Genomic data analysis
:::
## Challenges
<center>
<img src="./images/targets.png" width=200>
</center>
::: {.nonincremental style="font-size: 85%;"}
1. Scale out parallel workers to meet demand
2. Scale in parallel workers to conserve resources
3. Tailor itself to arbitrary distributed computing environments
:::
## A `targets` pipeline
![](./images/autoscale1.png)
## A worker is an R process that runs tasks
![](./images/autoscale2.png)
## Add workers to meet demand
![](./images/autoscale3.png)
## Reuse workers for subsequent tasks
![](./images/autoscale4.png)
## Discard workers no longer needed
![](./images/autoscale5.png)
## Beginnings: `mirai` and `crew`
<center>
<img src="./images/crew-core.png" width=300>
</center>
::: {style="font-size:85%"}
* `crew` needed a backend to communicate with parallel workers over a local network connection
* Originally considered Redis, but `mirai` is ideal:
* Does not depend on Redis server
* Can send larger data objects over the network
* `crew` began using `mirai` in February 2023
:::
## How `mirai` supports `crew`
<center>
<img src="./images/crew-core.png" width=300>
</center>
::: {style="font-size:85%"}
* `crew` launches workers, `mirai` sends tasks to workers
* `mirai` supports modular building blocks for `crew`:
* **`mirai::daemon(url = "...")`: turn any R process into a worker on the network.**
* `mirai::saisei()`: rotates websocket connections
* Down-scaling workers: maximum idle time, maximum number of tasks
:::
## Moju Kapu design of `crew`
<center>
<img src="./images/crew-core.png" width=300>
</center>
* **Encapsulation**: centralized `R6` "controller" interface
* **Modularity**: plugins for different computing environments
## Encapsulation: `R6` classes to wrap `mirai`
:::: {.columns style="font-size: 90%;"}
::: {.column width="45%"}
<br>
<center>
<img src="./images/crew-design.png" width=400>
</center>
:::
::: {.column width="55%"}
<br>
```{r, eval = FALSE, echo = TRUE}
# Start a new controller.
x <- crew::crew_controller_local(
workers = 10,
seconds_idle = 30
)
# Submit many parallel tasks.
x$walk(
rnorm(1, x),
iterate = list(x = seq_len(1000))
)
# Optional: wait for all tasks.
x$wait(mode = "all")
# Collect results so far.
str(unlist(x$collect()$result))
#> num [1:1000] 3.2 4.1 2.31 ...
```
:::
::::
## Modularity: `crew` plugins
:::: {.columns style="font-size: 70%;"}
::: {.column width="50%"}
<center>
<img src="./images/crew.png" width=150>
</center>
```{r, echo = TRUE, eval = FALSE}
crew_controller_local()
```
<br>
<center>
<img src="./images/crew.cluster.png" width=150>
</center>
```{r, echo = TRUE, eval = FALSE}
crew_controller_slurm(
slurm_memory_gigabytes_per_cpu = 16,
script_lines = "module load R/4.4.0"
)
```
:::
::: {.column width="50%"}
<center>
<img src="./images/crew.aws.batch.png" width=150>
</center>
```{r, echo = TRUE, eval = FALSE}
crew_controller_aws_batch(
aws_batch_job_definition = "your_def",
aws_batch_job_queue = "your_queue"
)
```
<center>
<img src="./images/hex-custom.png" width=150>
</center>
```{r, echo = TRUE, eval = FALSE}
your_custom_controller(...)
```
:::
::::
## Users can write `crew` plugins
::: {.nonincremental}
```{r, echo = TRUE, eval = FALSE}
custom_launcher_class <- R6::R6Class(
classname = "custom_launcher_class",
inherit = crew::crew_class_launcher,
public = list(
launch_worker = function(call, name, launcher, worker, instance) {
# 1. Reserve compute for R to run, e.g. start a job on a cluster.
# 2. Make that job start an R process.
# 3. Make that R process run the code in `call`.
},
terminate_worker = function(handle) {
# Terminate a worker.
}
)
)
```
* How to write a `crew` plugin: <br />
<https://wlandau.github.io/crew/articles/plugins.html>
:::
## `targets` accepts any `crew` controller
::: {.nonincremental}
:::: {.columns}
::: {.column width="20%"}
<center>
<img src="./images/targets.png">
</center>
:::
::: {.column width="80%"}
<center>
<img src="./images/graph.png">
</center>
:::
::::
```{r, eval = FALSE, echo = TRUE}
tar_option_set(
controller = crew_controller_aws_batch(
workers = 3,
seconds_idle = 60,
aws_batch_job_definition = "your_def",
aws_batch_job_queue = "your_queue",
aws_batch_region = "us-east-2"
)
)
```
* <https://books.ropensci.org/targets/crew.html>
:::
# Thank you
## {.center}
:::: {.columns}
::: {.column width="50%"}
<img alt="mirai logo" src="images/mirai.png" width="120" />
The Shinkansen of parallel computing
<img alt="crew logo" src="images/crew.png" width="120" />
Bringing mirai to distributed data science workloads
:::
::: {.column width="50%"}
The obvious choice for long-distance travel...
<img src="images/shinkansen.png" />
get to your destination faster!
:::
::::
# Appendix: Supporting Slides
## mirai - 100x Faster
Benchmarking:
```{.r}
library(mirai)
daemons(6)
#> [1] 6
library(future)
plan("multisession", workers = 6)
mirai(1)[]
#> [1] 1
value(future(1))
#> [1] 1
bench::mark(mirai(1)[], value(future(1)), relative = TRUE)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 mirai(1)[] 1 1 148. 1 NaN
#> 2 value(future(1)) 186. 159. 1 152. Inf
```
<sup>Created on 2024-06-27 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>
## mirai - WYSIWYG Concept
Production usage requires 'correctness' over 'convenience'
Compare and contrast:
:::: {.columns}
::: {.column width="50%"}
``` r
library(mirai)
res <- list(model = double(1e8),
acc = 0.95)
m <- mirai(2 * x, x = res$acc)
m[]
#> [1] 1.9
```
:::
::: {.column width="50%"}
``` r
library(future)
res <- list(model = double(1e8),
acc = 0.95)
f <- future(2 * res$acc)
#> Error in getGlobalsAndPackages(
#> expr, envir = envir, tweak =
#> tweakExpression, : The total
#> size of the 1 globals exported
#> for future expression ('2 *
#> res$acc') is 762.94 MiB.. This
#> exceeds the maximum allowed
#> size of 500.00 MiB (option
#> 'future.globals.maxSize').
#> There is one global: 'res'
#> (762.94 MiB of class 'list')
```
:::
::::
## mirai - One Million Promises
```{.r}
library(mirai)
daemons(8, dispatcher = FALSE)
#> [1] 8
r <- 0
start <- Sys.time()
m <- mirai_map(1:1000000, \(x) x, .promise = \(x) r <<- r + x)
Sys.time() - start
#> Time difference of 6.14953 mins
later::run_now()
r
#> [1] 500000500000
```
<sup>Created on 2024-06-27 with [reprex v2.1.0](https://reprex.tidyverse.org) <br />
Running on an Intel i7 Gen 11 notebook with 16GB RAM.</sup>