forked from RBigData/R4HPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR4HPC_Part1.html
990 lines (796 loc) · 29.9 KB
/
R4HPC_Part1.html
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Using R on HPC Clusters Part 1</title>
<meta charset="utf-8" />
<meta name="author" content="George Ostrouchov" />
<script src="libs/header-attrs/header-attrs.js"></script>
<link href="libs/remark-css/default.css" rel="stylesheet" />
<link href="libs/remark-css/default-fonts.css" rel="stylesheet" />
<link href="libs/tile-view/tile-view.css" rel="stylesheet" />
<script src="libs/tile-view/tile-view.js"></script>
<link href="libs/animate.css/animate.xaringan.css" rel="stylesheet" />
<link href="libs/tachyons/tachyons.min.css" rel="stylesheet" />
<link rel="stylesheet" href="my-theme.css" type="text/css" />
<link rel="stylesheet" href="widths.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: right, inverse, title-slide
.title[
# Using R on HPC Clusters Part 1
]
.author[
### George Ostrouchov
]
.institute[
### Oak Ridge National Laboratory
]
.date[
### <br><br><br><br><br><br> August 17, 2022 <br><br><span style="font-size: 50%;"> Background Image: FRONTIER, First Top500 exascale system, announced June 2022</span>
]
---
# Get this presentation:
`git clone https://github.com/RBigData/R4HPC.git`
* Open <br><br>`R4HPC_Part1.html` <br><br> in your web browser (help ? toggle)
<br>
Slack workspace link for this workshop was emailed to you.
<br><br><br><br>
*Many thanks to my colleagues and former colleagues who contributed to the software and ideas presented here. See the RBigData Organization on Github: https://github.com/RBigData. Also, many thanks to all R developers of packages used in this presentation.*
*Slides are made with the xaringan R package. It is an R Markdown extension based on the JavaScript library remark.js.*
???
The first hands-on session will do this also
<br><br><br><br><br><br><br><br>
Of course, any mistakes are mine alone!!
---
# Using R on HPC Clusters Webinar
* A basic workflow for how to use R on an HPC cluster
* Speed up R scripts with parallel computing concepts
* Many packages in R offer parallel computing abstractions, yet they use a much smaller set of underlying approaches:
* multithreading in compiled code, the unix fork, and MPI
* We take a narrow path to focus on the direct approaches
* Targeted for current users of OLCF, CADES, ALCF and NERSC
* Others are welcome to the lecture portions but will not be able to participate in all of the hands-on activities
#### Objectives
* Learn a workflow to edit R code on your laptop and run it on an HPC cluster
* Learn how to use multicore and distributed parallel concepts in R on an HPC cluster system
???
* The workflow is half the battle to manage frustration of new users
* HPC cluster has several parallel resources used simulatneously
* On a laptop, often one choice among approaches is made
* Many abstractions are a layer above the basics
* foreach, dopar, futures
* Understanding basics helps understanding the abstractions
* Add another layer to a complex situation - harder debug
* Basics are closer to HPC community (largely C and C++) terminology
* Helps understanding HPC language
* Some of the exercises can be done on a laptop but miss the important workflow socialization
---
background-image: url(pics/01-intro/WorkflowCluster.jpg)
background-position: top right
background-size: 20%
## The Clusters
ORNL OLCF Andes
* 704 nodes, each with two 16-core 3.0 GHz AMD EPYC processors
ORNL CADES SHPC Condos
* ~650 nodes, a mix of x86_64 processors with 32 to 128 cores
* New LMOD software stack (see https://docs.cades.ornl.gov/#condos/software/bash-env/#new-software-stack)
LBL NERSC Perlmutter
* 3,072 CPU-only nodes, AMD EPYC Milan, each with 64 cores
* 1,536 GPU-accelerated nodes, AMD EPYC Milan + 4 NVIDIA A100 GPU
ANL ACLF Polaris
* 560 nodes, each with AMD EPYC Milan (32 cores) + 4 NVIDIA A100 GPU
---
# Access to HPC Clusters
* DOE OLCF https://docs.olcf.ornl.gov/accounts/accounts_and_projects.html
* DOE ORNL CADES https://cades.ornl.gov/
* DOE ALCF https://www.alcf.anl.gov/support-center/account-and-project-management/allocations
* DOE NERSC https://www.nersc.gov/users/accounts/allocations/
* NSF XSEDE to ACCESS https://www.xsede.org/
* A cluster at your institution
* International:
* EU PRACE https://prace-ri.eu/hpc-access/ (for example IT4I.cz https://www.it4i.cz/en/for-users/computing-resources-allocation)
* UK, Switzerland, Japan, and many others have similar programs
---
## Section I: **Environment and Workflow**
## Section II: Parallel Hardware and Software Overview
## Section III: Shared Memory Tools
## Section IV: Distributed Memory Tools
---
## Working with a remote cluster using R
<img src="pics/01-intro/Workflow.jpg" height="500" />
???
* Please excuse my art
* Conveys what's needed but could be a LOT better
* If someone has better skills, I'd be very grateful for a better pic
* A workflow that I've settled-on last few years
* manages frustration with interactive - batch transition
* I see it mostly from a macOS perspective but works in Windows too
---
background-image: url(pics/01-intro//Workflow.jpg)
background-position: top right
background-size: 20%
### Laptop RStudio (Posit in October, 2022)
* Familiar custom editing environment (Windows, Mac, Unix)
* Interactive Syntax checking
### GitHub/GitLab
* Portability to remote computing
* Version control
* Collaboration
### Cluster unix
* Same environment for all
* Batch job submission
#### Advanced: interactive multinode development and debugging
* Available now (packages: launchr, pbdCS, pbdRPC, remoter)
* Needs further development (particularly launchr) and standardization
???
* Portability - in your basement or on another continent - remorely in EU
* RStudio on cluster:
* Installs difficult: legacy OS on HPC
* Bandwidth
* Minor RStudio differences due to OS
---
background-image: url(pics/01-intro//WorkflowRunning.jpg)
background-position: top right
background-size: 20%
## Running Distributed on a Cluster
<img src="pics/01-intro/BatchRonCluster.jpg" height="500" />
???
Ultimate goal of workshop
* Pic of cluster use with R: 32 R sessions collaborating on 8 nodes
* Laptop - login node - resource script - multi R session collaboration
* BIG data on parallel file system - not on laptop!
* Can monitor a longer run with logins to compute nodes
* Batch
---
background-image: url(pics/01-intro//WorkflowLaptop.jpg)
background-position: top right
background-size: 20%
### Software Needed on Laptop
* Mac
* R, RStudio
* terminal, git (in Xcode)
* Windows
* R, RStudio
* putty
* git
* WinSCP
---
background-image: url(pics/01-intro/WorkflowCluster.jpg)
background-position: top right
background-size: 20%
## Software on Cluster
* OpenBLAS
* FlexiBLAS
* OpenMPI
* HDF5 (for parallel I/O)
* R (>= 4.0)
**Packages:** <br>
Day 1: `flexiblas`, `remotes`, `RBigData/pbdMPI`, `randomForest`, `mlbench` <br>
Day 2: `RBigData/kazaam`, `RBigData/pbdDMAT`
#### R vs conda-R Deployment
* Direct R is preferred
* CRAN and Anaconda differ in package management philosophy
* Can end up with conflicts if mixing
* Conda adds a layer of complexity
* If already used to Conda, you may find it useful
???
* Complexity without obvious benefit if using current CRAN packages
---
background-image: url(pics/01-intro/WorkflowGit.jpg)
background-position: top left
background-size: 20%
# <font size="+2">2</font> `\(\qquad\)` GitHub and git (laptop to cluster)
.w80.pull-left[
<img src="pics/01-intro/Git_operations.svg" height="400" />
<font size="-6">*By Daniel Kinzler - Own work, CC BY 3.0, https://commons.wikimedia.org/w/index.php?curid=25223536</font>
]
.w20.pull-right[
<br>
<img src="pics/01-intro/WorkflowCluster.jpg" height="80" />
3
<br><br><br><br><br><br><br><br><br><br>
<img src="pics/01-intro/WorkflowLaptop.jpg" height="80" />
1
]
---
background-image: url(pics/01-intro/WorkflowPushPull.jpg)
background-position: top right
background-size: 20%
# Making **git** easy: set ssh keys
.w80.pull-left[
<img src="pics/01-intro/ssh-key-based-authentication.png" width="450" height="400" />
<font size="-4">Graphic from </font>
<img src="pics/01-intro/ssh-credit.png" width="280" height="10" />
]
.w20.pull-right[
A message encrypted by public key is decrypted by private key
<br><br><br>
Works like a single-use password generator and authenticator
<br><br><br>
Your private keys are protected in your account (laptop and cluster)
<br><br><br><br>
Put your public key on GitHub to enable easy access
]
---
background-image: url(pics/01-intro//WorkflowCluster.jpg)
background-position: top right
background-size: 20%
# Clusters are Linux systems
* Linux is one of many descendants of original Unix. MacOS is another.
* Like all file systems, Linux files are organized as a tree.
* When in a terminal, you are talking to a *shell* program (*bash* is most common)
* Each command can have a list of *options* and a list of *arguments*
* *Standard input* and *standard output* of a command is the terminal but can be redirected
* **<**, **<<**, **>**, **>>** redirect standard input and output
* *command1* **|** *command2* pipes standard output1 to standard input2
* Commands are looked up in directories listed in your PATH variable (try "echo $PATH")
* $ means substitute variable value
* *export* lists (or sets) shell variables and their values
* There are many resources on the web to learn Linux basics
???
* A few unix things to explain scripts you will see
---
background-image: url(pics/01-intro/WorkflowCluster.jpg)
background-position: top right
background-size: 20%
## Job Submission on Cluster
* Command line submission
* Shell script submission (preferred)
.pull-left[
#### Slurm (Andes, CADES, Perlmutter)
<mark>sbatch *your-shell-script.sh* </mark>
<mark>squeue -u *uid*</mark>
<mark>scancel *jobnumber*</mark>
]
.pull-right[
#### Cobalt - PBS (Polaris)
<mark>qsub *your-shell-script.sh*
<mark>qstat -u *uid*
<mark>qdel *jobname*</mark>
]
<br>
* **module** to set software environment (PATH)
* <mark>*module list*</mark> - list what is loaded
* <mark>*module avail*</mark> - list what is available
* <mark>*module load r*</mark>
???
* More explanation for scripts you will see
---
## Hands-on Session 1 - Fork and clone your R4HPC
* Fork R4HPC to your GitHub account
* Login to GitHub (or GitLab)
* Navigate to RBigData/R4HPC repository
* GitHub: Fork button near top-right and copy forked repo url
* GitLab: See https://docs.gitlab.com/ee/api/import.html
* Clone to New Project in RStudio
* Open Terminal window (ssh or putty)
* Login to cluster
* clone your R4HPC (git clone ...)
* You are ready for the development loop:
* edit -> commit -> push -> pull -> run -> examine output
.pull-left[
<img src="pics/01-intro/Workflow.jpg" height="250" />
]
.pull-right[
<img src="pics/01-intro/BatchRonCluster.jpg" height="250" />
]
---
## Hands-on Session 1 - On Login Node
* Go to `R4HPC/code_1` directory
* `cat hello_MACHINE_slurm.sh` to see what modules to load and do so
* Start R and install needed packages:
* `install.pacages("remotes")`
* `install.packages("flexiblas")`
* `remotes::install_github("RBigData/pbdMPI")`
* Submit the hello_MACHINE_slurm.sh
* Examine output in `hello.e` and `hello.o` and notice that:
* 4 nodes are involved
* 4 R sessions were running on each node
* Each R session ran `mclapply` on several cores
* All `mclapply` process id's are reported
* The code figured out how many cores in total
* Only one R session wrote the output
???
#SBATCH -A your-group-account
`group` will show what groups you are in
---
# Section II: <br> <br> Parallel Hardware
---
background-image: url(pics/Mangalore/ParallelHardware/Slide7.png)
background-position: bottom
background-size: 90%
# Three Basic Concepts in Hardware
???
# GPU - NVIDIA
# MIC - Intel KNL - ARM
* Manycore chip with memory on the chip instead of separate memory boards: https://www.youtube.com/watch?v=eXhlDt2SD8o
* A manycore that can act as a GPU
* Multocores are MIMD and can run anything
* GPU are SIMD and more limited
---
background-image: url(pics/Mangalore/ParallelHardware/Slide1.png)
background-position: bottom
background-size: 90%
# Three Basic Concepts in Hardware
???
# GPU - NVIDIA
# MIC - Intel KNL - ARM
* Manycore chip with memory on the chip instead of separate memory boards: https://www.youtube.com/watch?v=eXhlDt2SD8o
* A manycore that can act as a GPU
* Multocores are MIMD and can run anything
* GPU are SIMD and more limited
---
background-image: url(pics/Mangalore/ParallelHardware/Slide2.png)
background-position: bottom
background-size: 90%
???
Graphics
---
background-image: url(pics/Mangalore/ParallelHardware/Slide3.png)
background-position: bottom
background-size: 90%
???
GPU - NVIDIA
MIC - Intel KNL
---
background-image: url(pics/Mangalore/ParallelHardware/Slide4.png)
background-position: bottom
background-size: 90%
---
background-image: url(pics/Mangalore/ParallelHardware/Slide5.png)
background-position: bottom
background-size: 90%
---
background-image: url(pics/Mangalore/ParallelHardware/Slide6.png)
background-position: bottom
background-size: 90%
---
# Section III: <br> <br> Parallel Software
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide2.png)
background-position: bottom
background-size: 90%
# Native Programming Mindset
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide3.png)
background-position: bottom
background-size: 90%
# Native Programming Models and Tools
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide4.png)
background-position: bottom
background-size: 90%
# 35+ Years of Practical Parallel Computing
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide5.png)
background-position: bottom
background-size: 90%
# Last 15+ years of Advances
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide6.png)
background-position: bottom
background-size: 90%
## Distributed Programming Works in Shared Memory
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7.png)
background-position: bottom
background-size: 90%
# R Interfaces to Low-Level Native Tools
---
# Section IV: <br> <br> Shared Memory Tools
## Working with a single node
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7shared.jpg)
background-position: bottom
background-size: 90%
# Working with a single node
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: bottom
background-size: 90%
# fork via mclapply
???
* we begin with `paralel`'s multicore parts
* continue with Foreign language via libraries (OpenBLAS, nvBLAS)
* go to SPMD MPI with collectives
* reverse of history - because we are used to a laptop
* Distributed - some things are recomputed rather than communicated
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
# Unix `fork`
* A memory efficient parallelism on shared memory devices
* Copy-on-write: copy page if forked process tries to write
* R: **parallel** package `mclapply` and friends
* Use for numerical sections only
* Avoid GUI, I/O, and graphics sections
* Convenient for data (not modified)
* Convenient for functional languages like R
* Careful with nested parallelism
* OpenBLAS takes all cores by default
* data.table switches to single threaded mode upon fork
.footnote[A deeper discussion of `fork` memory (if you have interest) on [YouTube](https://www.youtube.com/watch?v=8hVLcyBkSXY) by Chris Kanich (UIC)]
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
# Copy-on-write
<img src="pics/MC/Fork/Slide1.png" height="500" style="display: block; margin: auto;" />
???
* All done with pointers
* Memory is in pages
* Processes not aware of each other or other's memory use
* OS is aware of memory use
* 16 forks write = 16 copies of memory
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
# Mapping Threads to Cores
### Theory and Reality
* Operating system manages core affinity
* OS tasks can compete and core switching occurs frequently
.pull-left[
<img src="pics/WSC/iDVTstR_theory.jpg" title="Photo used in many HPC presentations and seems to originate on Reddit" alt="Photo used in many HPC presentations and seems to originate on Reddit" height="300" style="display: block; margin: auto;" />
]
.pull-right[
<img src="pics/WSC/iDVTstR_reality.jpg" title="Photo used in many HPC presentations and seems to originate on Reddit" alt="Photo used in many HPC presentations and seems to originate on Reddit" height="300" style="display: block; margin: auto;" />
]
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
### `R`: Parallel Drop-in replacements (almost) <br> for `lapply`, `mapply`, and `Map`
<mark>
`mclapply(X, FUN, ...,`
` mc.preschedule = TRUE, mc.set.seed = TRUE,`
` mc.silent = FALSE, mc.cores = getOption("mc.cores", 2L),`
` mc.cleanup = TRUE, mc.allow.recursive = TRUE, affinity.list = NULL)`
</mark>
`mcmapply(FUN, ...,`
` MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE,`
` mc.preschedule = TRUE, mc.set.seed = TRUE,`
` mc.silent = FALSE, mc.cores = getOption("mc.cores", 2L),`
` mc.cleanup = TRUE, affinity.list = NULL)`
`mcMap(f, ...)`
---
## Hands-on Session 2 - Multicore Random Forest
* Go to `R4HPC/code_2` directory
* Look at the `rf_serial.R` and `rf_mc.R` codes
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
## Hands-on Session 2 - Example Random forest Code
#### Letter recognition data ( `\(20\,000 \times 17\)` )
<img src="pics/MC/ML_FreySlate1991.png" height="350" style="display: block; margin: auto;" />
.footnote[*Parallel Statistical Computing with R: An Illustration on Two Architectures [ arXiv:1709.01195](https://arxiv.org/abs/1709.01195)]
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7fork.jpg)
background-position: top right
background-size: 20%
## Hands-on Session 2 - Random Forest Classification
### Build many decision trees
### Each tree built from
* random subset of variables: subset of columns
* resampled (with replacement) data: same number of rows
### Use their majority votes to classify
---
### Hands-on Session 2 - `R4HPC/code_2/rf_serial.R`
```r
suppressMessages(library(randomForest))
data(LetterRecognition, package = "mlbench")
set.seed(seed = 123)
n = nrow(LetterRecognition)
n_test = floor(0.2 * n)
i_test = sample.int(n, n_test)
train = LetterRecognition[-i_test, ]
test = LetterRecognition[i_test, ]
rf.all = randomForest(lettr ~ ., train, ntree = 500, norm.votes = FALSE)
pred = predict(rf.all, test)
correct = sum(pred == test$lettr)
cat("Proportion Correct:", correct/(n_test), "\n")
```
---
### Hands-on Session 2 - `R4HPC/code_2/rf_mc.R`
```r
*library(parallel)
library(randomForest)
data(LetterRecognition, package = "mlbench")
*set.seed(seed = 123, "L'Ecuyer-CMRG")
n = nrow(LetterRecognition)
n_test = floor(0.2 * n)
i_test = sample.int(n, n_test)
train = LetterRecognition[-i_test, ]
test = LetterRecognition[i_test, ]
*nc = as.numeric(commandArgs(TRUE)[2])
cat("cores:", nc, "\n")
*ntree = lapply(splitIndices(500, nc), length)
*rf = function(x, train) randomForest(lettr ~ ., train, ntree=x,
* norm.votes = FALSE)
*rf.out = mclapply(ntree, rf, train = train, mc.cores = nc)
*rf.all = do.call(combine, rf.out)
*crows = splitIndices(nrow(test), nc)
*rfp = function(x) as.vector(predict(rf.all, test[x, ]))
*cpred = mclapply(crows, rfp, mc.cores = nc)
*pred = do.call(c, cpred)
correct <- sum(pred == test$lettr)
cat("Proportion Correct:", correct/(n_test), "\n")
```
---
## Hands-on Session 2 - Assignment
Time the random forest code `rf_mc.R` for 1 through 32 cores by modifying the `rf_MACHINE_slurm.sh` script.
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7libs.jpg)
background-position: bottom
background-size: 90%
# Libraries via compiled language interfaces
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7libs.jpg)
background-position: top right
background-size: 20%
# R-LAPACK-BLAS
* BLAS: Basic Linear Algebra Subroutines - A matrix multiplication library
* `%*%`, `crossprod()`, `sweep()`, `scale()`, and many more
* LAPACK: dense and banded matrix decomposition and more
* `svd()`, `La.svd()`, `prcomp()`, `princomp()`, `qr()`, `solve()`, `chol()`, `norm()`, and many more
* But not `lm()`, careful with `qr(x, LAPACK = TRUE)`: column pivoting
* Implementations: OpenBLAS, Intel MKL, Nvidia nvBLAS, Apple vecLib, AMD BLIS, Arm Performance Libraries
* **FlexiBLAS**: A BLAS and LAPACK wrapper library with runtime exchangeable backends
* Great for benchmarking implementations
* Great for dynamic core assignment
???
* Optimizes algorithm to chip microarchitecture details
* memory hierarchies (L1 cache, L2 cache, etc.) and
* register vector length
* FlexiBLAS Standardization of API for BLAS core control
* C, C++, R, Python/numpy, Julia
---
## Faster BLAS For Faster R on your Laptop (macOS)
```r
## Default BLAS from Netlib
> x = matrix(rnorm(1e7), nrow = 1e4)
> system.time(crossprod(x))
user system elapsed
6.752 0.023 6.801
```
```r
## vecLib (4 cores)
> system.time(crossprod(x))
user system elapsed
0.420 0.003 0.078
```
```r
## OpenBLAS (4 cores)
> system.time(crossprod(x))
user system elapsed
0.457 0.028 0.075
```
---
## Install FlexiBLAS For BLAS Control on macOS Laptop
* Install Xcode and command line tools
* Install Homebrew: https://brew.sh/
* In a terminal window:
* `brew install cmake`
* `brew install openblas`
* cmake needs to be told about OpenBLAS:
* `export CMAKE_PREFIX_PATH=/usr/local/opt/openblas:$CMAKE_PREFIX_PATH`
* Install FlexiBLAS: https://www.mpi-magdeburg.mpg.de/projects/flexiblas
* See Install section in its README.md
* After installation, link to R (terminal window):
* `ln -sf /usr/local/lib/libflexiblas.dylib /Library/Frameworks/R.framework/Resources/lib/libRblas.dylib`
* In R, `install.packages("flexiblas")` and test if it works:
* `flexiblas_avail()`
* `flexiblas_list()`
R can now swap OpenBLAS and APPLE vecLib dynamically. <br>
Dynamically control number of cores used in OpenBLAS. <br>
Control vecLib threads with VECLIB_MAXIMUM_THREADS before starting R.
???
Long Install
Eases transition to cluster if practiced on laptop
Brings more portability to parallel codes
---
## For faster R on your Windows laptop
Assessing R performance with optimized BLAS across three operating systems [link](https://thomasmcrow.com/blog/2021-08-optimized-blas-in-r/)
<br>
Building R 4+ for Windows with OpenBLAS [link](https://www.r-bloggers.com/2020/05/building-r-4-for-windows-with-openblas/)
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7libs.jpg)
background-position: top right
background-size: 20%
## Hands-on Session 3 - FlexiBLAS
`code_3/flexiblas_setup.R`
```r
library(flexiblas)
flexiblas_avail()
flexiblas_version()
flexiblas_current_backend()
flexiblas_list()
flexiblas_list_loaded()
getthreads = function() {
flexiblas_get_num_threads()
}
setthreads = function(thr, label = "") {
cat(label, "Setting", thr, "threads\n")
flexiblas_set_num_threads(thr)
}
setback = function(backend, label = "") {
cat(label, "Setting", backend, "backend\n")
flexiblas_switch(flexiblas_load_backend(backend))
}
```
.footnote[
[https://github.com/Enchufa2/r-flexiblas](https://github.com/Enchufa2/r-flexiblas)
[https://cran.r-project.org/package=flexiblas](https://cran.r-project.org/package=flexiblas)
]
---
background-image: url(pics/Mangalore/ParallelSoftware/Slide7libs.jpg)
background-position: top right
background-size: 20%
# Hands-on Session 3 - FlexiBLAS
* Go to `code_3` directory
* Submit `flexiblas_MACHINE_slurm.sh`
* Examine output to notice:
* Maximum cores can be slow
* Optimal cores can depend on matrix size and shape
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();
// add `data-at-shortcutkeys` attribute to <body> to resolve conflicts with JAWS
// screen reader (see PR #262)
(function(d) {
let res = {};
d.querySelectorAll('.remark-help-content table tr').forEach(tr => {
const t = tr.querySelector('td:nth-child(2)').innerText;
tr.querySelectorAll('td:first-child .key').forEach(key => {
const k = key.innerText;
if (/^[a-z]$/.test(k)) res[k] = t; // must be a single letter (key)
});
});
d.body.setAttribute('data-at-shortcutkeys', JSON.stringify(res));
})(document);
(function() {
"use strict"
// Replace <script> tags in slides area to make them executable
var scripts = document.querySelectorAll(
'.remark-slides-area .remark-slide-container script'
);
if (!scripts.length) return;
for (var i = 0; i < scripts.length; i++) {
var s = document.createElement('script');
var code = document.createTextNode(scripts[i].textContent);
s.appendChild(code);
var scriptAttrs = scripts[i].attributes;
for (var j = 0; j < scriptAttrs.length; j++) {
s.setAttribute(scriptAttrs[j].name, scriptAttrs[j].value);
}
scripts[i].parentElement.replaceChild(s, scripts[i]);
}
})();
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
// adds .remark-code-has-line-highlighted class to <pre> parent elements
// of code chunks containing highlighted lines with class .remark-code-line-highlighted
(function(d) {
const hlines = d.querySelectorAll('.remark-code-line-highlighted');
const preParents = [];
const findPreParent = function(line, p = 0) {
if (p > 1) return null; // traverse up no further than grandparent
const el = line.parentElement;
return el.tagName === "PRE" ? el : findPreParent(el, ++p);
};
for (let line of hlines) {
let pre = findPreParent(line);
if (pre && !preParents.includes(pre)) preParents.push(pre);
}
preParents.forEach(p => p.classList.add("remark-code-has-line-highlighted"));
})(document);</script>
<style>
.logo {
background-image: url(pics/ORNL_Two-line_color.png);
background-size: contain;
background-repeat: no-repeat;
position: absolute;
bottom: -1em;
left: 1em;
width: 110px;
height: 64px;
z-index: 0;
}
</style>
<script>
document
.querySelectorAll(
'.remark-slide-content' +
':not(.title-slide)' +
// add additional classes to exclude here, e.g.
// ':not(.inverse)' +
':not(.hide-logo)'
)
.forEach(el => {
el.innerHTML += '<div class="logo"></div>';
});
</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>