-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.html
2312 lines (1973 loc) · 128 KB
/
parallel.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
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Package 'parallel' reference manual</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<!-- styling for math. katex.min.css expects fonts in same dir so use CDN -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css">
<link rel="stylesheet" type="text/css" href="https://r-universe.dev/static/R.css">
<link rel="stylesheet" type="text/css" href="https://r-universe.dev/static/prism.css">
<!-- for (future) customizations -->
<script src="https://r-universe.dev/static/manual.js"></script>
<link rel="stylesheet" type="text/css" href="https://r-universe.dev/static/manual.css?nocache=1">
</head>
<body class="postdoc macintosh">
<h1 class="manual-title">Package 'parallel'</h1>
<table class="description-table">
<tr>
<th>Title:</th>
<td class="description-title">Support for Parallel Computation in R</td>
</tr>
<tr>
<th>Description:</th>
<td class="description-description">Support for parallel computation, including by forking
(taken from package multicore), by sockets (taken from package snow)
and random-number generation.</td>
</tr>
<tr>
<th>Authors:</th>
<td class="description-author"><span>R Core Team</span></td>
</tr>
<tr>
<th>Maintainer:</th>
<td class="description-maintainer">R Core Team <[email protected]></td>
</tr>
<tr>
<th>License:</th>
<td class="description-license">Part of R 4.4.1</td>
</tr>
<tr>
<th>Version:</th>
<td class="description-version">4.4.1</td>
</tr>
<tr>
<th>Built:</th>
<td class="description-date">2024-06-15 17:27:47 UTC</td>
</tr>
<tr>
<th>Source:</th>
<td class="description-source">base</td>
</tr>
</table>
<a href="#help-index" style="color:black;"><h2 id="help-index">Help Index</h2></a>
<ul id="help-index-list">
<li class="help-index-item"><a href="#parallel-package">
Support for Parallel Computation
</a></li>
<li class="help-index-item"><a href="#clusterApply">Apply Operations using Clusters</a></li>
<li class="help-index-item"><a href="#detectCores">Detect the Number of CPU Cores</a></li>
<li class="help-index-item"><a href="#makeCluster">
Create a Parallel Socket Cluster
</a></li>
<li class="help-index-item"><a href="#mcaffinity">Get or Set CPU Affinity Mask of the Current Process</a></li>
<li class="help-index-item"><a href="#children">Low-level Functions for Management of Forked Processes</a></li>
<li class="help-index-item"><a href="#mcfork">Fork a Copy of the Current R Process</a></li>
<li class="help-index-item"><a href="#mclapply">Parallel Versions of lapply and mapply using Forking</a></li>
<li class="help-index-item"><a href="#mcparallel">Evaluate an R Expression Asynchronously in a Separate Process</a></li>
<li class="help-index-item"><a href="#pvec">Parallelize a Vector Map Function using Forking</a></li>
<li class="help-index-item"><a href="#RngStream">Implementation of Pierre L'Ecuyer's RngStreams</a></li>
<li class="help-index-item"><a href="#splitIndices">Divide Tasks for Distribution in a Cluster</a></li>
</ul>
<hr>
<div class="manual-pages-content">
<div class="container manual-page" id="parallel-package"><div class="page-main">
<a href="#parallel-package" class="help-page-title"><h2>
Support for Parallel Computation
</h2></a>
<h3>Description</h3>
<p>Support for parallel computation, including random-number generation.
</p>
<h3>Details</h3>
<p>This package was first included with <span class="rlang"><b>R</b></span> 2.14.0 in 2011.
</p>
<p>There is support for multiple RNG streams with the
‘<span class="samp">"L'Ecuyer-CMRG"</span>’ <a href="https://r-universe.dev/manuals/base.html#Random">RNG</a>: see <code><a href="#RngStream">nextRNGStream</a></code>.
</p>
<p>It contains functionality derived from and pretty much equivalent to
that contained in packages <span class="pkg">multicore</span> (formerly on
<abbr>CRAN</abbr>, with some low-level functions renamed and not
exported) and <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a> (for socket clusters only, but MPI
clusters generated by <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a> are also supported). There
have been many enhancements and bug fixes since 2011.
</p>
<p>This package also provides <code><a href="#makeCluster">makeForkCluster</a></code> to create
socket clusters by forking (not Windows).
</p>
<p>For a complete list of exported functions, use
<code>library(help = "parallel")</code>.
</p>
<h3>Author(s)</h3>
<p>Brian Ripley, Luke Tierney and Simon Urbanek
</p>
<p>Maintainer: R Core Team <a href="mailto:[email protected]">[email protected]</a>
</p>
<h3>See Also</h3>
<p>Parallel computation involves launching worker processes: functions
<code><a href="https://r-universe.dev/manuals/tools.html#psnice">psnice</a></code> and <code><a href="https://r-universe.dev/manuals/tools.html#pskill">pskill</a></code> in package <span class="pkg">tools</span>
provide means to manage such processes.
</p>
<hr>
</div></div>
<div class="container manual-page" id="clusterApply"><div class="page-main">
<a href="#clusterApply" class="help-page-title"><h2>Apply Operations using Clusters</h2></a>
<h3>Description</h3>
<p>These functions provide several ways to parallelize computations using
a cluster.
</p>
<h3>Usage</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">clusterCall(cl = NULL, fun, ...)
clusterApply(cl = NULL, x, fun, ...)
clusterApplyLB(cl = NULL, x, fun, ...)
clusterEvalQ(cl = NULL, expr)
clusterExport(cl = NULL, varlist, envir = .GlobalEnv)
clusterMap(cl = NULL, fun, ..., MoreArgs = NULL, RECYCLE = TRUE,
SIMPLIFY = FALSE, USE.NAMES = TRUE,
.scheduling = c("static", "dynamic"))
clusterSplit(cl = NULL, seq)
parLapply(cl = NULL, X, fun, ..., chunk.size = NULL)
parSapply(cl = NULL, X, FUN, ..., simplify = TRUE,
USE.NAMES = TRUE, chunk.size = NULL)
parApply(cl = NULL, X, MARGIN, FUN, ..., chunk.size = NULL)
parRapply(cl = NULL, x, FUN, ..., chunk.size = NULL)
parCapply(cl = NULL, x, FUN, ..., chunk.size = NULL)
parLapplyLB(cl = NULL, X, fun, ..., chunk.size = NULL)
parSapplyLB(cl = NULL, X, FUN, ..., simplify = TRUE,
USE.NAMES = TRUE, chunk.size = NULL)
</code><code class="language-r">clusterCall<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
clusterApply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
clusterApplyLB<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
clusterEvalQ<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> expr<span class="token punctuation">)</span>
clusterExport<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> varlist<span class="token punctuation">,</span> envir <span class="token operator">=</span> .GlobalEnv<span class="token punctuation">)</span>
clusterMap<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> MoreArgs <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> RECYCLE <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span>
SIMPLIFY <span class="token operator">=</span> <span class="token boolean">FALSE</span><span class="token punctuation">,</span> USE.NAMES <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span>
.scheduling <span class="token operator">=</span> c<span class="token punctuation">(</span><span class="token string">"static"</span><span class="token punctuation">,</span> <span class="token string">"dynamic"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
clusterSplit<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> seq<span class="token punctuation">)</span>
parLapply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> X<span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parSapply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> X<span class="token punctuation">,</span> FUN<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> simplify <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span>
USE.NAMES <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parApply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> X<span class="token punctuation">,</span> MARGIN<span class="token punctuation">,</span> FUN<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parRapply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> FUN<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parCapply<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> x<span class="token punctuation">,</span> FUN<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parLapplyLB<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> X<span class="token punctuation">,</span> fun<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
parSapplyLB<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> X<span class="token punctuation">,</span> FUN<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">,</span> simplify <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span>
USE.NAMES <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">,</span> chunk.size <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span></code></pre>
<h3 class="r-arguments-title">Arguments</h3>
<table>
<tr>
<td><code id="cl">cl</code></td>
<td>
<p>a cluster object, created by this package or by package
<a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a>. If <code>NULL</code>, use the registered default cluster.</p>
</td>
</tr>
<tr>
<td>
<code id="fun">fun</code>, <code id="FUN">FUN</code>
</td>
<td>
<p>function or character string naming a function.</p>
</td>
</tr>
<tr>
<td><code id="expr">expr</code></td>
<td>
<p>expression to evaluate.</p>
</td>
</tr>
<tr>
<td><code id="seq">seq</code></td>
<td>
<p>vector to split.</p>
</td>
</tr>
<tr>
<td><code id="varlist">varlist</code></td>
<td>
<p>character vector of names of objects to export.</p>
</td>
</tr>
<tr>
<td><code id="envir">envir</code></td>
<td>
<p>environment from which to export variables</p>
</td>
</tr>
<tr>
<td><code id="x">x</code></td>
<td>
<p>a vector for <code>clusterApply</code> and <code>clusterApplyLB</code>, a
matrix for <code>parRapply</code> and <code>parCapply</code>.</p>
</td>
</tr>
<tr>
<td><code id="...">...</code></td>
<td>
<p>additional arguments to pass to <code>fun</code> or <code>FUN</code>:
beware of partial matching to earlier arguments.</p>
</td>
</tr>
<tr>
<td><code id="MoreArgs">MoreArgs</code></td>
<td>
<p>additional arguments for <code>fun</code>.</p>
</td>
</tr>
<tr>
<td><code id="RECYCLE">RECYCLE</code></td>
<td>
<p>logical; if true shorter arguments are recycled.</p>
</td>
</tr>
<tr>
<td><code id="X">X</code></td>
<td>
<p>A vector (atomic or list) for <code>parLapply</code> and
<code>parSapply</code>, an array for <code>parApply</code>.</p>
</td>
</tr>
<tr>
<td><code id="chunk.size">chunk.size</code></td>
<td>
<p>scalar number; number of invocations of <code>fun</code> or
<code>FUN</code> in one chunk; a chunk is a unit for scheduling.</p>
</td>
</tr>
<tr>
<td><code id="MARGIN">MARGIN</code></td>
<td>
<p>vector specifying the dimensions to use.</p>
</td>
</tr>
<tr>
<td>
<code id="simplify">simplify</code>, <code id="USE.NAMES">USE.NAMES</code>
</td>
<td>
<p>logical; see <code><a href="https://r-universe.dev/manuals/base.html#lapply">sapply</a></code>.</p>
</td>
</tr>
<tr>
<td><code id="SIMPLIFY">SIMPLIFY</code></td>
<td>
<p>logical; see <code><a href="https://r-universe.dev/manuals/base.html#mapply">mapply</a></code>.</p>
</td>
</tr>
<tr>
<td><code id=".scheduling">.scheduling</code></td>
<td>
<p>should tasks be statically allocated to nodes or
dynamic load-balancing used?</p>
</td>
</tr>
</table>
<h3>Details</h3>
<p><code>clusterCall</code> calls a function <code>fun</code> with identical
arguments <code>...</code> on each node.
</p>
<p><code>clusterEvalQ</code> evaluates a literal expression on each cluster
node. It is a parallel version of <code><a href="https://r-universe.dev/manuals/base.html#eval">evalq</a></code>, and is a
convenience function invoking <code>clusterCall</code>.
</p>
<p><code>clusterApply</code> calls <code>fun</code> on the first node with
arguments <code>x[[1]]</code> and <code>...</code>, on the second node with
<code>x[[2]]</code> and <code>...</code>, and so on, recycling nodes as needed.
</p>
<p><code>clusterApplyLB</code> is a load balancing version of
<code>clusterApply</code>. If the length <code>n</code> of <code>x</code> is not
greater than the number of nodes <code>p</code>, then a job is sent to
<code>n</code> nodes. Otherwise the first <code>p</code> jobs are placed in order
on the <code>p</code> nodes. When the first job completes, the next job is
placed on the node that has become free; this continues until all jobs
are complete. Using <code>clusterApplyLB</code> can result in better
cluster utilization than using <code>clusterApply</code>, but increased
communication can reduce performance. Furthermore, the node that
executes a particular job is non-deterministic. This means that
simulations that assign RNG streams to nodes will not be reproducible.
</p>
<p><code>clusterMap</code> is a multi-argument version of <code>clusterApply</code>,
analogous to <code><a href="https://r-universe.dev/manuals/base.html#mapply">mapply</a></code> and <code><a href="https://r-universe.dev/manuals/base.html#funprog">Map</a></code>. If
<code>RECYCLE</code> is true shorter arguments are recycled (and either none
or all must be of length zero); otherwise, the result length is the
length of the shortest argument. Nodes are recycled if the length of
the result is greater than the number of nodes. (<code>mapply</code> always
uses <code>RECYCLE = TRUE</code>, and has argument <code>SIMPLIFY = TRUE</code>.
<code>Map</code> always uses <code>RECYCLE = TRUE</code>.)
</p>
<p><code>clusterExport</code> assigns the values on the master <span class="rlang"><b>R</b></span> process of
the variables named in <code>varlist</code> to variables of the same names
in the global environment (aka ‘workspace’) of each node. The
environment on the master from which variables are exported defaults
to the global environment.
</p>
<p><code>clusterSplit</code> splits <code>seq</code> into a consecutive piece for
each cluster and returns the result as a list with length equal to the
number of nodes. Currently the pieces are chosen to be close
to equal in length: the computation is done on the master.
</p>
<p><code>parLapply</code>, <code>parSapply</code>, and <code>parApply</code> are parallel
versions of <code>lapply</code>, <code>sapply</code> and <code>apply</code>. Chunks of
computation are statically allocated to nodes using <code>clusterApply</code>.
By default, the number of chunks is the same as the number of nodes.
<code>parLapplyLB</code>, <code>parSapplyLB</code> are load-balancing versions,
intended for use when applying <code>FUN</code> to different elements of
<code>X</code> takes quite variable amounts of time, and either the function is
deterministic or reproducible results are not required. Chunks of
computation are allocated dynamically to nodes using
<code>clusterApplyLB</code>. From <span class="rlang"><b>R</b></span> 3.5.0, the default number of chunks is
twice the number of nodes. Before <span class="rlang"><b>R</b></span> 3.5.0, the (fixed) number of chunks
was the same as the number of nodes. As for <code>clusterApplyLB</code>,
with load balancing the node that executes a particular job is
non-deterministic and simulations that assign RNG streams to nodes
will not be reproducible.
</p>
<p><code>parRapply</code> and <code>parCapply</code> are parallel row and column
<code>apply</code> functions for a matrix <code>x</code>; they may be slightly
more efficient than <code>parApply</code> but do less post-processing of the
result.
</p>
<p>A chunk size of <code>0</code> with static scheduling uses the default (one
chunk per node). With dynamic scheduling, chunk size of <code>0</code> has the
same effect as <code>1</code> (one invocation of <code>FUN</code>/<code>fun</code> per
chunk).
</p>
<h3>Value</h3>
<p>For <code>clusterCall</code>, <code>clusterEvalQ</code> and <code>clusterSplit</code>, a
list with one element per node.
</p>
<p>For <code>clusterApply</code> and <code>clusterApplyLB</code>, a list the same
length as <code>x</code>.
</p>
<p><code>clusterMap</code> follows <code><a href="https://r-universe.dev/manuals/base.html#mapply">mapply</a></code>.
</p>
<p><code>clusterExport</code> returns nothing.
</p>
<p><code>parLapply</code> returns a list the length of <code>X</code>.
</p>
<p><code>parSapply</code> and <code>parApply</code> follow <code><a href="https://r-universe.dev/manuals/base.html#lapply">sapply</a></code> and
<code><a href="https://r-universe.dev/manuals/base.html#apply">apply</a></code> respectively.
</p>
<p><code>parRapply</code> and <code>parCapply</code> always return a vector. If
<code>FUN</code> always returns a scalar result this will be of length the
number of rows or columns: otherwise it will be the concatenation of
the returned values.
</p>
<p>An error is signalled on the master if any of the workers produces an
error.
</p>
<h3>Note</h3>
<p>These functions are almost identical to those in package <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a>.
</p>
<p>Two exceptions: <code>parLapply</code> has argument <code>X</code>
not <code>x</code> for consistency with <code><a href="https://r-universe.dev/manuals/base.html#lapply">lapply</a></code>, and
<code>parSapply</code> has been updated to match <code><a href="https://r-universe.dev/manuals/base.html#lapply">sapply</a></code>.
</p>
<h3>Author(s)</h3>
<p>Luke Tierney and R Core.
</p>
<p>Derived from the <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a> package.
</p>
<h3>Examples</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">
## Use option cl.cores to choose an appropriate cluster size.
cl <- makeCluster(getOption("cl.cores", 2))
clusterApply(cl, 1:2, get("+"), 3)
xx <- 1
clusterExport(cl, "xx")
clusterCall(cl, function(y) xx + y, 2)
## Use clusterMap like an mapply example
clusterMap(cl, function(x, y) seq_len(x) + y,
c(a = 1, b = 2, c = 3), c(A = 10, B = 0, C = -10))
parSapply(cl, 1:20, get("+"), 3)
## A bootstrapping example, which can be done in many ways:
clusterEvalQ(cl, {
## set up each worker. Could also use clusterExport()
library(boot)
cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v)
cd4.mle <- list(m = colMeans(cd4), v = var(cd4))
NULL
})
res <- clusterEvalQ(cl, boot(cd4, corr, R = 100,
sim = "parametric", ran.gen = cd4.rg, mle = cd4.mle))
library(boot)
cd4.boot <- do.call(c, res)
boot.ci(cd4.boot, type = c("norm", "basic", "perc"),
conf = 0.9, h = atanh, hinv = tanh)
stopCluster(cl)
## or
library(boot)
run1 <- function(...) {
library(boot)
cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v)
cd4.mle <- list(m = colMeans(cd4), v = var(cd4))
boot(cd4, corr, R = 500, sim = "parametric",
ran.gen = cd4.rg, mle = cd4.mle)
}
cl <- makeCluster(mc <- getOption("cl.cores", 2))
## to make this reproducible
clusterSetRNGStream(cl, 123)
cd4.boot <- do.call(c, parLapply(cl, seq_len(mc), run1))
boot.ci(cd4.boot, type = c("norm", "basic", "perc"),
conf = 0.9, h = atanh, hinv = tanh)
stopCluster(cl)
</code><code class="language-r"><span class="token comment">## Use option cl.cores to choose an appropriate cluster size.</span>
cl <span class="token operator"><-</span> makeCluster<span class="token punctuation">(</span>getOption<span class="token punctuation">(</span><span class="token string">"cl.cores"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
clusterApply<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">:</span><span class="token number">2</span><span class="token punctuation">,</span> get<span class="token punctuation">(</span><span class="token string">"+"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
xx <span class="token operator"><-</span> <span class="token number">1</span>
clusterExport<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token string">"xx"</span><span class="token punctuation">)</span>
clusterCall<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span> xx <span class="token operator">+</span> y<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment">## Use clusterMap like an mapply example</span>
clusterMap<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> seq_len<span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token operator">+</span> y<span class="token punctuation">,</span>
c<span class="token punctuation">(</span>a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> b <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span> c <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> c<span class="token punctuation">(</span>A <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> B <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> C <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
parSapply<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span> get<span class="token punctuation">(</span><span class="token string">"+"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token comment">## A bootstrapping example, which can be done in many ways:</span>
clusterEvalQ<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token punctuation">{</span>
<span class="token comment">## set up each worker. Could also use clusterExport()</span>
library<span class="token punctuation">(</span>boot<span class="token punctuation">)</span>
cd4.rg <span class="token operator"><-</span> <span class="token keyword">function</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> mle<span class="token punctuation">)</span> MASS<span class="token operator">::</span>mvrnorm<span class="token punctuation">(</span>nrow<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span> mle<span class="token operator">$</span>m<span class="token punctuation">,</span> mle<span class="token operator">$</span>v<span class="token punctuation">)</span>
cd4.mle <span class="token operator"><-</span> list<span class="token punctuation">(</span>m <span class="token operator">=</span> colMeans<span class="token punctuation">(</span>cd4<span class="token punctuation">)</span><span class="token punctuation">,</span> v <span class="token operator">=</span> var<span class="token punctuation">(</span>cd4<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">NULL</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
res <span class="token operator"><-</span> clusterEvalQ<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> boot<span class="token punctuation">(</span>cd4<span class="token punctuation">,</span> corr<span class="token punctuation">,</span> R <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">,</span>
sim <span class="token operator">=</span> <span class="token string">"parametric"</span><span class="token punctuation">,</span> ran.gen <span class="token operator">=</span> cd4.rg<span class="token punctuation">,</span> mle <span class="token operator">=</span> cd4.mle<span class="token punctuation">)</span><span class="token punctuation">)</span>
library<span class="token punctuation">(</span>boot<span class="token punctuation">)</span>
cd4.boot <span class="token operator"><-</span> do.call<span class="token punctuation">(</span>c<span class="token punctuation">,</span> res<span class="token punctuation">)</span>
boot.ci<span class="token punctuation">(</span>cd4.boot<span class="token punctuation">,</span> type <span class="token operator">=</span> c<span class="token punctuation">(</span><span class="token string">"norm"</span><span class="token punctuation">,</span> <span class="token string">"basic"</span><span class="token punctuation">,</span> <span class="token string">"perc"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
conf <span class="token operator">=</span> <span class="token number">0.9</span><span class="token punctuation">,</span> h <span class="token operator">=</span> atanh<span class="token punctuation">,</span> hinv <span class="token operator">=</span> tanh<span class="token punctuation">)</span>
stopCluster<span class="token punctuation">(</span>cl<span class="token punctuation">)</span>
<span class="token comment">## or</span>
library<span class="token punctuation">(</span>boot<span class="token punctuation">)</span>
run1 <span class="token operator"><-</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token ellipsis">...</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
library<span class="token punctuation">(</span>boot<span class="token punctuation">)</span>
cd4.rg <span class="token operator"><-</span> <span class="token keyword">function</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> mle<span class="token punctuation">)</span> MASS<span class="token operator">::</span>mvrnorm<span class="token punctuation">(</span>nrow<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span> mle<span class="token operator">$</span>m<span class="token punctuation">,</span> mle<span class="token operator">$</span>v<span class="token punctuation">)</span>
cd4.mle <span class="token operator"><-</span> list<span class="token punctuation">(</span>m <span class="token operator">=</span> colMeans<span class="token punctuation">(</span>cd4<span class="token punctuation">)</span><span class="token punctuation">,</span> v <span class="token operator">=</span> var<span class="token punctuation">(</span>cd4<span class="token punctuation">)</span><span class="token punctuation">)</span>
boot<span class="token punctuation">(</span>cd4<span class="token punctuation">,</span> corr<span class="token punctuation">,</span> R <span class="token operator">=</span> <span class="token number">500</span><span class="token punctuation">,</span> sim <span class="token operator">=</span> <span class="token string">"parametric"</span><span class="token punctuation">,</span>
ran.gen <span class="token operator">=</span> cd4.rg<span class="token punctuation">,</span> mle <span class="token operator">=</span> cd4.mle<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
cl <span class="token operator"><-</span> makeCluster<span class="token punctuation">(</span>mc <span class="token operator"><-</span> getOption<span class="token punctuation">(</span><span class="token string">"cl.cores"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">## to make this reproducible</span>
clusterSetRNGStream<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> <span class="token number">123</span><span class="token punctuation">)</span>
cd4.boot <span class="token operator"><-</span> do.call<span class="token punctuation">(</span>c<span class="token punctuation">,</span> parLapply<span class="token punctuation">(</span>cl<span class="token punctuation">,</span> seq_len<span class="token punctuation">(</span>mc<span class="token punctuation">)</span><span class="token punctuation">,</span> run1<span class="token punctuation">)</span><span class="token punctuation">)</span>
boot.ci<span class="token punctuation">(</span>cd4.boot<span class="token punctuation">,</span> type <span class="token operator">=</span> c<span class="token punctuation">(</span><span class="token string">"norm"</span><span class="token punctuation">,</span> <span class="token string">"basic"</span><span class="token punctuation">,</span> <span class="token string">"perc"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
conf <span class="token operator">=</span> <span class="token number">0.9</span><span class="token punctuation">,</span> h <span class="token operator">=</span> atanh<span class="token punctuation">,</span> hinv <span class="token operator">=</span> tanh<span class="token punctuation">)</span>
stopCluster<span class="token punctuation">(</span>cl<span class="token punctuation">)</span></code></pre>
<hr>
</div></div>
<div class="container manual-page" id="detectCores"><div class="page-main">
<a href="#detectCores" class="help-page-title"><h2>Detect the Number of CPU Cores</h2></a>
<h3>Description</h3>
<p>Attempt to detect the number of CPU cores on the current host.
</p>
<h3>Usage</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">detectCores(all.tests = FALSE, logical = TRUE)
</code><code class="language-r">detectCores<span class="token punctuation">(</span>all.tests <span class="token operator">=</span> <span class="token boolean">FALSE</span><span class="token punctuation">,</span> logical <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">)</span></code></pre>
<h3 class="r-arguments-title">Arguments</h3>
<table>
<tr>
<td><code id="all.tests">all.tests</code></td>
<td>
<p>Logical: if true apply all known tests.</p>
</td>
</tr>
<tr>
<td><code id="logical">logical</code></td>
<td>
<p>Logical: if possible, use the number of physical CPUs/cores
(if <code>FALSE</code>) or logical CPUs (if <code>TRUE</code>). Currently this
is honoured only on macOS, Solaris and Windows.</p>
</td>
</tr>
</table>
<h3>Details</h3>
<p>This attempts to detect the number of available CPU cores.
</p>
<p>It has methods to do so for Linux, macOS, FreeBSD, OpenBSD, Solaris
and Windows. <code>detectCores(TRUE)</code> could be tried on other
Unix-alike systems.
</p>
<h3>Value</h3>
<p>An integer, <code>NA</code> if the answer is unknown.
</p>
<p>Exactly what this represents is OS-dependent: where possible by
default it counts logical (e.g., hyperthreaded) CPUs and not physical
cores or packages.
</p>
<p>Under macOS there is a further distinction between ‘available in
the current power management mode’ and ‘could be available
this boot’, and this function returns the first.
</p>
<p>On Sparc Solaris <code>logical = FALSE</code> returns the number of physical
cores and <code>logical = TRUE</code> returns the number of available
hardware threads. (Some Sparc CPUs have multiple cores per CPU, others
have multiple threads per core and some have both.) For example, the
UltraSparc T2 CPU in the former CRAN check server was a single
physical CPU with 8 cores, and each core supports 8 hardware threads.
So <code>detectCores(logical = FALSE)</code> returns 8, and
<code>detectCores(logical = TRUE)</code> returns 64.
</p>
<p>Where virtual machines are in use, one would hope that the result
for <code>logical = TRUE</code> represents the number of CPUs available (or
potentially available) to that particular <abbr>VM</abbr>.
</p>
<h3>Note</h3>
<p>This is not suitable for use directly for the <code>mc.cores</code> argument
of <code>mclapply</code> nor specifying the number of cores in
<code>makeCluster</code>. First because it may return <code>NA</code>, second
because it does not give the number of <em>allowed</em> cores, and third
because on Sparc Solaris and some Windows boxes it is not reasonable
to try to use all the logical CPUs at once.
</p>
<h3>Author(s)</h3>
<p>Simon Urbanek and Brian Ripley
</p>
<h3>Examples</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">detectCores()
detectCores(logical = FALSE)
</code><code class="language-r">detectCores<span class="token punctuation">(</span><span class="token punctuation">)</span>
detectCores<span class="token punctuation">(</span>logical <span class="token operator">=</span> <span class="token boolean">FALSE</span><span class="token punctuation">)</span></code></pre>
<hr>
</div></div>
<div class="container manual-page" id="makeCluster"><div class="page-main">
<a href="#makeCluster" class="help-page-title"><h2>
Create a Parallel Socket Cluster
</h2></a>
<h3>Description</h3>
<p>Creates a set of copies of <span class="rlang"><b>R</b></span> running in parallel and communicating
over sockets.
</p>
<h3>Usage</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">makeCluster(spec, type, ...)
makePSOCKcluster(names, ...)
makeForkCluster(nnodes = getOption("mc.cores", 2L), ...)
stopCluster(cl = NULL)
setDefaultCluster(cl = NULL)
getDefaultCluster()
</code><code class="language-r">makeCluster<span class="token punctuation">(</span>spec<span class="token punctuation">,</span> type<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
makePSOCKcluster<span class="token punctuation">(</span>names<span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
makeForkCluster<span class="token punctuation">(</span>nnodes <span class="token operator">=</span> getOption<span class="token punctuation">(</span><span class="token string">"mc.cores"</span><span class="token punctuation">,</span> <span class="token number">2L</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token ellipsis">...</span><span class="token punctuation">)</span>
stopCluster<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
setDefaultCluster<span class="token punctuation">(</span>cl <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span>
getDefaultCluster<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre>
<h3 class="r-arguments-title">Arguments</h3>
<table>
<tr>
<td><code id="spec">spec</code></td>
<td>
<p>A specification appropriate to the type of cluster.</p>
</td>
</tr>
<tr>
<td><code id="names">names</code></td>
<td>
<p>Either a character vector of host names on which to run
the worker copies of <span class="rlang"><b>R</b></span>, or a positive integer (in which case
that number of copies is run on ‘<span class="samp">localhost</span>’).</p>
</td>
</tr>
<tr>
<td><code id="nnodes">nnodes</code></td>
<td>
<p>The number of nodes to be forked.</p>
</td>
</tr>
<tr>
<td><code id="type">type</code></td>
<td>
<p>One of the supported types: see ‘Details’.</p>
</td>
</tr>
<tr>
<td><code id="...">...</code></td>
<td>
<p>Options to be passed to the function spawning the workers.
See ‘Details’.</p>
</td>
</tr>
<tr>
<td><code id="cl">cl</code></td>
<td>
<p>an object of class <code>"cluster"</code>.</p>
</td>
</tr>
</table>
<h3>Details</h3>
<p><code>makeCluster</code> creates a cluster of one of the supported types.
The default type, <code>"PSOCK"</code>, calls <code>makePSOCKcluster</code>. Type
<code>"FORK"</code> calls <code>makeForkCluster</code>. Other types are passed to
package <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a>.
</p>
<p><code>makePSOCKcluster</code> is an enhanced version of
<code>makeSOCKcluster</code> in package <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a>. It runs
<code>Rscript</code> on the specified host(s) to set up a worker process
which listens on a socket for expressions to evaluate, and returns the
results (as serialized objects).
</p>
<p><code>makeForkCluster</code> is merely a stub on Windows. On Unix-alike
platforms it creates the worker process by forking.
</p>
<p>The workers are most often running on the same host as the master,
when no options need be set.
</p>
<p>Several options are supported (mainly for <code>makePSOCKcluster</code>):
</p>
<dl>
<dt><code>master</code></dt>
<dd>
<p>The host name of the master, as known to the
workers. This may not be the same as it is known to the master,
and on private subnets it may be necessary to specify this as a
numeric IP address. For example, macOS is likely to detect a
machine as ‘<span class="samp">somename.local</span>’, a name known only to itself.</p>
</dd>
<dt><code>port</code></dt>
<dd>
<p>The port number for the socket connection,
default taken from the environment variable <span class="env">R_PARALLEL_PORT</span>,
then a randomly chosen port in the range <code>11000:11999</code>.</p>
</dd>
<dt><code>timeout</code></dt>
<dd>
<p>The timeout in seconds for that port. This is
the maximum time of zero communication between master and worker
before failing. Default is 30 days (and the POSIX standard only
requires values up to 31 days to be supported).</p>
</dd>
<dt><code>setup_timeout</code></dt>
<dd>
<p>The maximum number of seconds a worker
attempts to connect to master before failing. Default is 2
minutes. The waiting time before the next attempt starts at
0.1 seconds and is incremented 50% after each retry.</p>
</dd>
<dt><code>outfile</code></dt>
<dd>
<p>Where to direct the <code><a href="https://r-universe.dev/manuals/base.html#showConnections">stdout</a></code> and
<code><a href="https://r-universe.dev/manuals/base.html#showConnections">stderr</a></code> connection output from the workers.
<code>""</code> indicates no redirection (which may only be useful for
workers on the local machine).
Defaults to ‘<span class="file">/dev/null</span>’ (‘<span class="file">nul:</span>’ on Windows). The other
possibility is a file path on the worker's host.
Files will be opened in append mode, as all workers log to the
same file.</p>
</dd>
<dt><code>homogeneous</code></dt>
<dd>
<p>Logical, default true. See ‘Note’.</p>
</dd>
<dt><code>rscript</code></dt>
<dd>
<p>See ‘Note’.</p>
</dd>
<dt><code>rscript_args</code></dt>
<dd>
<p>Character vector of additional
arguments for <code>Rscript</code> such as <span class="option">--no-environ</span>.</p>
</dd>
<dt><code>renice</code></dt>
<dd>
<p>A numerical ‘niceness’ to set for the
worker processes, e.g. <code>15</code> for a low priority.
OS-dependent: see <code><a href="https://r-universe.dev/manuals/tools.html#psnice">psnice</a></code> for details.</p>
</dd>
<dt><code>rshcmd</code></dt>
<dd>
<p>The command to be run on the master to launch a
process on another host. Defaults to <code>ssh</code>.</p>
</dd>
<dt><code>user</code></dt>
<dd>
<p>The user name to be used when communicating with
another host.</p>
</dd>
<dt><code>manual</code></dt>
<dd>
<p>Logical. If true the workers will need to be
run manually.</p>
</dd>
<dt><code>methods</code></dt>
<dd>
<p>Logical. If true (default) the workers will
load the <span class="pkg">methods</span> package: not loading it saves ca 30% of the
startup CPU time of the cluster.</p>
</dd>
<dt><code>useXDR</code></dt>
<dd>
<p>Logical. If true (default) serialization will
use XDR: where large amounts of data are to be transferred and
all the nodes are little-endian, communication may be
substantially faster if this is set to false.</p>
</dd>
<dt><code>setup_strategy</code></dt>
<dd>
<p>Character. If <code>"parallel"</code> (default)
workers will be started in parallel during cluster setup when this is
possible, which is now for homogeneous <code>"PSOCK"</code> clusters with
all workers started automatically (<code>manual = FALSE</code>) on the local
machine. Workers will be started sequentially on other clusters, on
all clusters with <code>setup_strategy = "sequential"</code> and on <span class="rlang"><b>R</b></span> 3.6.0
and older. This option is for expert use only (e.g. debugging) and
may be removed in future versions of R.</p>
</dd>
</dl>
<p>Function <code>makeForkCluster</code> creates a socket cluster by forking
(and hence is not available on Windows). It supports options
<code>port</code>, <code>timeout</code> and <code>outfile</code>, and always uses
<code>useXDR = FALSE</code>. It is <em>strongly discouraged</em> to use the
<code>"FORK"</code> cluster with GUI front-ends or multi-threaded libraries.
See <code><a href="#mcfork">mcfork</a></code> for details.
</p>
<p>It is good practice to shut down the workers by calling
<code><a href="#makeCluster">stopCluster</a></code>: however the workers will terminate
themselves once the socket on which they are listening for commands
becomes unavailable, which it should if the master <span class="rlang"><b>R</b></span> session is
completed (or its process dies).
</p>
<p>Function <code>setDefaultCluster</code> registers a cluster as the default one
for the current session. Using <code>setDefaultCluster(NULL)</code> removes
the registered cluster, as does stopping that cluster.
</p>
<h3>Value</h3>
<p>For the cluster creators, an object of class
<code>c("SOCKcluster", "cluster")</code>.
</p>
<p>For the default cluster setter and getter, the registered default
cluster or <code>NULL</code> if there is no such cluster.
</p>
<h3>Note</h3>
<p>Option <code>homogeneous = TRUE</code> was for years documented as
‘Are all the hosts running identical setups?’, but this was
apparently more restrictive than its author intended and not required
by the code.
</p>
<p>The current interpretation of <code>homogeneous = TRUE</code> is that
<code>Rscript</code> can be launched using the same path on each worker.
That path is given by the option <code>rscript</code> and defaults to the
full path to <code>Rscript</code> on the master. (The workers are not
required to be running the same version of <span class="rlang"><b>R</b></span> as the master, nor even
as each other.)
</p>
<p>For <code>homogeneous = FALSE</code>, <code>Rscript</code> on the workers is
found on their default shell's path.
</p>
<p>For the very common usage of running both master and worker on a
single multi-core host, the default settings are the appropriate ones.
</p>
<p>A socket <a href="https://r-universe.dev/manuals/base.html#connections">connection</a> is used to communicate from the master to
each worker so the maximum number of connections (default 128 but some
will be in use) may need to be increased when the master process is
started.
</p>
<h3>Author(s)</h3>
<p>Luke Tierney and R Core.
</p>
<p>Derived from the <a href="https://CRAN.R-project.org/package=snow" target="_blank"><span class="pkg">snow</span></a> package.
</p>
<hr>
</div></div>
<div class="container manual-page" id="mcaffinity"><div class="page-main">
<a href="#mcaffinity" class="help-page-title"><h2>Get or Set CPU Affinity Mask of the Current Process</h2></a>
<h3>Description</h3>
<p><code>mcaffinity</code> retrieves or sets the CPU affinity mask of the
current process, i.e., the set of CPUs the process is allowed to be
run on. (CPU here means logical CPU which can be CPU, core or
hyperthread unit.)
</p>
<h3>Usage</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">mcaffinity(affinity = NULL)
</code><code class="language-r">mcaffinity<span class="token punctuation">(</span>affinity <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">)</span></code></pre>
<h3 class="r-arguments-title">Arguments</h3>
<table><tr>
<td><code id="affinity">affinity</code></td>
<td>
<p>specification of the CPUs to lock this process to
(numeric vector) or <code>NULL</code> if no change is requested</p>
</td>
</tr></table>
<h3>Details</h3>
<p><code>mcaffinity</code> can be used to obtain (<code>affinity = NULL</code>)
or set the CPU affinity mask of the current process. The affinity mask
is a list of integer CPU identifiers (starting from 1) that this
process is allowed to run on. Not all systems provide user access to
the process CPU affinity, in cases where no support is present at all
<code>mcaffinity()</code> will return <code>NULL</code>. Some systems may take
into account only the number of CPUs present in the mask.
</p>
<p>Typically, it is legal to specify larger set than the number of
logical CPUs (but at most as many as the OS can handle) and the system
will return back the actually present set.
</p>
<h3>Value</h3>
<p><code>NULL</code> if CPU affinity is not supported by the system or an
integer vector with the set of CPUs in the active affinity mask for
this process (this may be different than <code>affinity</code>).
</p>
<h3>Author(s)</h3>
<p>Simon Urbanek.
</p>
<h3>See Also</h3>
<p><code><a href="#mcparallel">mcparallel</a></code>
</p>
<hr>
</div></div>
<div class="container manual-page" id="children"><div class="page-main">
<a href="#children" class="help-page-title"><h2>Low-level Functions for Management of Forked Processes</h2></a>
<h3>Description</h3>
<p>These are low-level support functions for the forking approach.
</p>
<p>They are not available on Windows, and not exported from the namespace.
</p>
<h3>Usage</h3>
<pre class="language-r"><code class="language-r-input" hidden="hidden">children(select)
readChild(child)
readChildren(timeout = 0)
selectChildren(children = NULL, timeout = 0)
sendChildStdin(child, what)
sendMaster(what, raw.asis = TRUE)
mckill(process, signal = 2L)
</code><code class="language-r">children<span class="token punctuation">(</span>select<span class="token punctuation">)</span>
readChild<span class="token punctuation">(</span>child<span class="token punctuation">)</span>
readChildren<span class="token punctuation">(</span>timeout <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span>
selectChildren<span class="token punctuation">(</span>children <span class="token operator">=</span> <span class="token keyword">NULL</span><span class="token punctuation">,</span> timeout <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span>
sendChildStdin<span class="token punctuation">(</span>child<span class="token punctuation">,</span> what<span class="token punctuation">)</span>
sendMaster<span class="token punctuation">(</span>what<span class="token punctuation">,</span> raw.asis <span class="token operator">=</span> <span class="token boolean">TRUE</span><span class="token punctuation">)</span>
mckill<span class="token punctuation">(</span>process<span class="token punctuation">,</span> signal <span class="token operator">=</span> <span class="token number">2L</span><span class="token punctuation">)</span></code></pre>
<h3 class="r-arguments-title">Arguments</h3>
<table>
<tr>
<td><code id="select">select</code></td>
<td>
<p>if omitted, all active children are returned, otherwise
<code>select</code> should be a list of processes and only those from the
list that are active will be returned.</p>
</td>
</tr>
<tr>
<td><code id="child">child</code></td>
<td>
<p>child process (object of the class <code>"childProcess"</code>) or a
process ID (<abbr>pid</abbr>). See also ‘Details’.</p>
</td>
</tr>
<tr>
<td><code id="timeout">timeout</code></td>
<td>
<p>timeout (in seconds, fractions supported) to wait
for a response before giving up.</p>
</td>
</tr>
<tr>
<td><code id="children">children</code></td>
<td>
<p>list of child processes or a single child process
object or a vector of process IDs or <code>NULL</code>. If <code>NULL</code>
behaves as if all currently known children were supplied.</p>
</td>
</tr>
<tr>
<td><code id="what">what</code></td>
<td>