-
Notifications
You must be signed in to change notification settings - Fork 9
/
parallel.lyx
2978 lines (2412 loc) · 59.7 KB
/
parallel.lyx
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
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\begin_preamble
\usepackage{/accounts/gen/vis/paciorek/latex/paciorek-asa,times,graphics}
\input{/accounts/gen/vis/paciorek/latex/paciorekMacros}
%\renewcommand{\baselinestretch}{1.5}
\usepackage[unicode=true]{hyperref}
\hypersetup{unicode=true, pdfusetitle,
bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=true,}
\end_preamble
\use_default_options false
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize 12
\spacing onehalf
\use_hyperref false
\papersize letterpaper
\use_geometry true
\use_amsmath 1
\use_esint 0
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 1in
\topmargin 1in
\rightmargin 1in
\bottommargin 1in
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Title
An Introduction to Parallel Processing in R,
\begin_inset Newline newline
\end_inset
Including Use on Clusters and in the Cloud
\end_layout
\begin_layout Author
Chris Paciorek
\begin_inset Newline newline
\end_inset
Department of Statistics
\begin_inset Newline newline
\end_inset
University of California, Berkeley
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
<<setup, include=FALSE, cache=FALSE>>=
\end_layout
\begin_layout Plain Layout
options(replace.assign=TRUE, width=55)
\end_layout
\begin_layout Plain Layout
@
\end_layout
\end_inset
\end_layout
\begin_layout Chunk
<<read-chunk, echo=FALSE>>=
\end_layout
\begin_layout Chunk
read_chunk('parallel.R')
\end_layout
\begin_layout Chunk
read_chunk('doMPIexample.R')
\end_layout
\begin_layout Chunk
read_chunk('RmpiExample.R')
\end_layout
\begin_layout Chunk
read_chunk('parallel.sh')
\end_layout
\begin_layout Chunk
read_chunk('starcluster.sh')
\end_layout
\begin_layout Chunk
read_chunk('pbd-mpi.R')
\end_layout
\begin_layout Chunk
read_chunk('pbd-apply.R')
\end_layout
\begin_layout Chunk
read_chunk('pbd-linalg.R')
\end_layout
\begin_layout Chunk
@
\end_layout
\begin_layout Section
How to follow and try out this material
\end_layout
\begin_layout Standard
Note that my examples here will be silly toy examples for the purpose of
keeping things simple and focused on the parallelization approaches.
The demo code embedded in this document is available in the various .R and
.sh files in the Github repository,
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/berkeley-scf/parallelR-biostat-2015"
\end_inset
.
This document was created using Sweave.
\end_layout
\begin_layout Standard
I will do demos on an Ubuntu Linux virtual machine (VM), on the biostat
cluster, and on Amazon's AWS.
\end_layout
\begin_layout Standard
We'll use the
\begin_inset CommandInset href
LatexCommand href
name "BCE Virtual Machine"
target "bce.berkeley.edu"
\end_inset
.
You can run this on your laptop (see the
\begin_inset CommandInset href
LatexCommand href
name "BCE installation instructions"
target "bce.berkeley.edu/install.html"
\end_inset
), and I encourage you to do so to follow along.
Note: to allow for parallelization, before starting the BCE VM, go to
\family typewriter
Machine > Settings
\family default
, select
\family typewriter
System
\family default
and increase the number of processors.
You may also want to increase the amount of memory.
\end_layout
\begin_layout Standard
We'll also use BCE as the basis for the virtual machines we start on Amazon's
AWS.
\end_layout
\begin_layout Section
Overview of parallel processing computers
\end_layout
\begin_layout Standard
There are two basic flavors of parallel processing (leaving aside GPUs):
distributed memory and shared memory.
With shared memory, multiple processors (which I'll call cores) share the
same memory.
With distributed memory, you have multiple nodes, each with their own memory.
You can think of each node as a separate computer connected by a fast network.
\end_layout
\begin_layout Subsection
Some useful terminology:
\end_layout
\begin_layout Itemize
\emph on
cores
\emph default
: We'll use this term to mean the different processing units available on
a single node.
\end_layout
\begin_layout Itemize
\emph on
nodes
\emph default
: We'll use this term to mean the different computers, each with their own
distinct memory, that make up a cluster or supercomputer.
\end_layout
\begin_layout Itemize
\emph on
processes
\emph default
: computational tasks executing on a machine.
A given program may start up multiple processes at once.
Ideally we have no more processes than cores on a node.
\end_layout
\begin_layout Itemize
\emph on
thread
\emph default
s: multiple paths of execution within a single process; the OS sees the
threads as a single process, but one can think of them as 'lightweight'
processes.
Ideally when considering the processes and their threads, we would have
no more processes and threads combined than cores on a node.
\end_layout
\begin_layout Itemize
\emph on
forking
\emph default
: child processes are spawned that are identical to the parent, but with
different process id's and their own memory.
\end_layout
\begin_layout Itemize
\emph on
sockets
\emph default
: some of R's parallel functionality involves creating new R processes (e.g.,
starting processes via
\emph on
Rscript
\emph default
) and communicating with them via a communication technology called sockets.
\end_layout
\begin_layout Standard
\begin_inset Note Note
status open
\begin_layout Plain Layout
[see George's pdf for graphical representation, p.
23]
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
Shared memory
\end_layout
\begin_layout Standard
For shared memory parallelism, each core is accessing the same memory so
there is no need to pass information (in the form of messages) between
different machines.
But in some programming contexts one needs to be careful that activity
on different cores doesn't mistakenly overwrite places in memory that are
used by other cores.
\end_layout
\begin_layout Standard
The shared memory parallelism approaches that we'll cover are:
\end_layout
\begin_layout Enumerate
threaded linear algebra and
\end_layout
\begin_layout Enumerate
simple parallelization of embarrassingly parallel computations.
\end_layout
\begin_layout Paragraph*
Threading
\end_layout
\begin_layout Standard
Threads are multiple paths of execution within a single process.
Using
\emph on
top
\emph default
to monitor a job that is executing threaded code, you'll see the process
using more than 100% of CPU.
When this occurs, the process is using multiple cores, although it appears
as a single process rather than as multiple processes.
In general, threaded code will detect the number of cores available on
a machine and make use of them.
However, you can also explicitly control the number of threads available
to a process.
\end_layout
\begin_layout Subsection
Distributed memory
\end_layout
\begin_layout Standard
Parallel programming for distributed memory parallelism requires passing
messages between the different nodes.
The standard protocol for doing this is MPI, of which there are various
versions, including
\emph on
openMPI
\emph default
.
The Python package
\emph on
mpi4py
\emph default
implements MPI in Python and the R package
\emph on
Rmpi
\emph default
implements MPI in R.
\end_layout
\begin_layout Standard
Some of the distributed memory approaches that we'll cover are:
\end_layout
\begin_layout Enumerate
simple parallelization of embarrassingly parallel computations,
\end_layout
\begin_layout Enumerate
using MPI for explicit distributed memory processing, and
\end_layout
\begin_layout Enumerate
distributed linear algebra.
\end_layout
\begin_layout Subsection
Other type of parallel processing
\end_layout
\begin_layout Standard
We won't cover either of the following in this material.
\end_layout
\begin_layout Subsubsection
GPUs
\end_layout
\begin_layout Standard
GPUs (Graphics Processing Units) are processing units originally designed
for rendering graphics on a computer quickly.
This is done by having a large number of simple processing units for massively
parallel calculation.
The idea of general purpose GPU (GPGPU) computing is to exploit this capability
for general computation.
\end_layout
\begin_layout Standard
In spring 2014, I gave a
\begin_inset CommandInset href
LatexCommand href
name "workshop on using GPUs"
target "http://statistics.berkeley.edu/computing/gpu"
\end_inset
.
One easy way to use a GPU is on an Amazon EC2 virtual machine.
\end_layout
\begin_layout Subsubsection
Spark and Hadoop
\end_layout
\begin_layout Standard
Spark and Hadoop are systems for implementing computations in a distributed
memory environment, using the MapReduce approach.
In fall 2014 I gave a
\begin_inset CommandInset href
LatexCommand href
name "workshop on Spark"
target "http://statistics.berkeley.edu/computing/spark"
\end_inset
.
One easy way to use Spark is on a cluster of Amazon EC2 virtual machines.
\end_layout
\begin_layout Section
Basic suggestions for parallelizing your code
\end_layout
\begin_layout Standard
The easiest situation is when your code is embarrassingly parallel, which
means that the different tasks can be done independently and the results
collected.
When the tasks need to interact, things get much harder.
Much of the material here is focused on embarrassingly parallel computation.
\end_layout
\begin_layout Standard
The following are some basic principles/suggestions for how to parallelize
your computation.
\end_layout
\begin_layout Itemize
If you can do your computation on the cores of a single node using shared
memory, that will be faster than using the same number of cores (or even
somewhat more cores) across multiple nodes.
Similarly, jobs with a lot of data/high memory requirements that one might
think of as requiring Hadoop may in some cases be much faster if you can
find a single machine with a lot of memory.
\end_layout
\begin_deeper
\begin_layout Itemize
That said, if you would run out of memory on a single node, then you'll
need to use distributed memory.
\end_layout
\end_deeper
\begin_layout Itemize
If you have nested loops, you generally only want to parallelize at one
level of the code.
That said, there may be cases in which it is helpful to do both.
Keep in mind whether your linear algebra is being threaded.
Often you will want to parallelize over a loop and not use threaded linear
algebra.
(That said, if you have multiple nodes, you might have one task per node
and use threaded linear algebra to exploit the cores on each node.)
\end_layout
\begin_layout Itemize
Often it makes sense to parallelize the outer loop when you have nested
loops.
\end_layout
\begin_layout Itemize
You generally want to parallelize in such a way that your code is load-balanced
and does not involve too much communication.
\end_layout
\begin_deeper
\begin_layout Itemize
If you have very few tasks, particularly if the tasks take different amounts
of time, often some processors will be idle and your code poorly load-balanced.
\end_layout
\begin_layout Itemize
If you have very many tasks and each one takes little time, the communication
overhead of starting and stopping the tasks will reduce efficiency.
\end_layout
\end_deeper
\begin_layout Standard
I'm happy to help discuss specific circumstances, so just email [email protected]
eley.edu.
The new Berkeley Research Computing (BRC) initiative is also providing
consulting on efficient parallelization strategies.
If my expertise is not sufficient, I can help you get assistance from BRC.
\end_layout
\begin_layout Paragraph
Static vs.
dynamic assignment of tasks
\end_layout
\begin_layout Standard
Some of R's parallel functions allow you to say whether the tasks can be
divided up and allocated to the workers at the beginning or whether tasks
should be assigned individually as previous tasks complete.
E.g., the
\emph on
mc.preschedule
\emph default
argument in
\emph on
mclapply()
\emph default
and the
\emph on
.scheduling
\emph default
argument in
\emph on
parLapply()
\emph default
.
\end_layout
\begin_layout Standard
Basically if you have many tasks that each take similar time, you want to
preschedule to reduce communication.
If you have few tasks or tasks with highly variable completion times, you
don't want to preschedule, to improve load-balancing.
\end_layout
\begin_layout Section
Threaded linear algebra and the BLAS
\end_layout
\begin_layout Standard
The BLAS is the library of basic linear algebra operations (written in Fortran
or C).
A fast BLAS can greatly speed up linear algebra relative to the default
BLAS on a machine.
Some fast BLAS libraries are Intel's
\emph on
MKL
\emph default
, AMD's
\emph on
ACML
\emph default
, and the open source (and free)
\emph on
openBLAS
\emph default
(formerly
\emph on
GotoBLAS
\emph default
).
For the Mac, there is
\emph on
vecLib
\emph default
BLAS.
All of these BLAS libraries are now threaded - if your computer has multiple
cores and there are free resources, your linear algebra will use multiple
cores, provided your program is linked against the specific BLAS and provided
OMP_NUM_THREADS is not set to one.
(Macs make use of VECLIB_MAXIMUM_THREADS rather than OMP_NUM_THREADS.)
\end_layout
\begin_layout Subsection
Fixing the number of threads (cores used)
\end_layout
\begin_layout Standard
In general, if you want to limit the number of threads used, you can set
the OMP_NUM_THREADS environment variable on UNIX machine.
This can be used in the context of R or C code that uses BLAS or your own
threaded C code, but this does not work with Matlab.
In the UNIX shell, you'd do this as follows (e.g.
to limit to 3 cores):
\end_layout
\begin_layout Standard
\family typewriter
export OMP_NUM_THREADS=3 # bash
\end_layout
\begin_layout Standard
\family typewriter
setenv OMP_NUM_THREADS 3 # tcsh
\end_layout
\begin_layout Standard
If you are running R, you'd need to do this in your shell session before
invoking R.
\end_layout
\begin_layout Standard
\begin_inset Note Note
status open
\begin_layout Plain Layout
warning off MATLAB:maxNumCompThreads:Deprecated
\end_layout
\begin_layout Plain Layout
nslots = getenv('NSLOTS')
\end_layout
\begin_layout Plain Layout
maxNumCompThreads(nslots);
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
Using threading in R
\end_layout
\begin_layout Standard
Threading in R is limited to linear algebra, for which R calls external
BLAS and LAPACK libraries.
\end_layout
\begin_layout Standard
Here's some code that when run in an R executable linked to a threaded BLAS
illustrates the speed of using a threaded BLAS:
\end_layout
\begin_layout Chunk
<<RlinAlg, cache=TRUE, eval=TRUE>>=
\end_layout
\begin_layout Chunk
@
\end_layout
\begin_layout Subsubsection
Setting up R with a fast BLAS
\end_layout
\begin_layout Standard
R on the Biostat cluster is linked to openBLAS.
So you should be able to use OMP_NUM_THREADS to control the number of threads
used for linear algebra on the Biostat cluster.
\end_layout
\begin_layout Standard
In general, the
\begin_inset CommandInset href
LatexCommand href
name "R installation manual"
target "http://cran.r-project.org/manuals.html"
\end_inset
gives information on how to link R to a fast BLAS.
On Ubuntu Linux, if you install openBLAS as follows, the
\emph on
/etc/alternatives
\emph default
system will set
\emph on
/usr/lib/libblas.so
\emph default
to point to openBLAS.
This is what is done in the BCE virtual machine.
By default on Ubuntu, R will use the system BLAS, so it will as a result
use openBLAS.
\end_layout
\begin_layout Chunk
<<etc-alternatives, eval=FALSE, engine='bash'>>=
\end_layout
\begin_layout Chunk
@
\end_layout
\begin_layout Standard
To use a fast, threaded BLAS enabled on your own Mac, do the following as
the administrative user:
\end_layout
\begin_layout Chunk
<<MacBLAS, eval=FALSE, engine='bash'>>=
\end_layout
\begin_layout Chunk
@
\end_layout
\begin_layout Standard
\begin_inset Note Note
status open
\begin_layout Plain Layout
this tests fine on Arwen and as SGE job on cluster
\end_layout
\end_inset
\end_layout
\begin_layout Subsubsection
Important warnings about use of threaded BLAS
\end_layout
\begin_layout Paragraph
Conflict between openBLAS and some parallel functionality in R
\end_layout
\begin_layout Standard
There are conflicts between forking in R and threaded BLAS that in some
cases affect
\emph on
foreach
\emph default
(when using the
\emph on
multicore
\emph default
and
\emph on
parallel
\emph default
backends),
\emph on
mclapply()
\emph default
, and (only if
\emph on
cluster()
\emph default
is set up with forking (not the default))
\emph on
par{L,S,}apply()
\emph default
.
The result is that if linear algebra is used within your parallel code,
R hangs.
This affects (under somewhat different circumstances) both ACML and openBLAS.
\end_layout
\begin_layout Standard
To address this, before running an R job that does linear algebra, you can
set OMP_NUM_THREADS to 1 to prevent the BLAS from doing threaded calculations.
Alternatively, you can use MPI as the parallel backend (via
\emph on
doMPI
\emph default
in place of
\emph on
doMC
\emph default
or
\emph on
doParallel
\emph default
-- see Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Distributed-memory"
\end_inset
).
You may also be able to convert your code to use
\emph on
par{L,S,}apply()
\emph default
[with the default PSOCK type] and avoid
\emph on
foreach
\emph default
entirely.
\end_layout
\begin_layout Paragraph
Conflict between threaded BLAS and R profiling
\end_layout
\begin_layout Standard
There is also a conflict between threaded BLAS and R profiling, so if you
are using
\emph on
Rprof()
\emph default
, you may need to set OMP_NUM_THREADS to one.
This has definitely occurred with openBLAS; I'm not sure about other threaded
BLAS libraries.
\end_layout
\begin_layout Paragraph
Speed and threaded BLAS
\end_layout
\begin_layout Standard
In many cases, using multiple threads for linear algebra operations will
outperform using a single thread, but there is no guarantee that this will
be the case, in particular for operations with small matrices and vectors.
Testing with openBLAS suggests that sometimes a job may take more time
when using multiple threads; this seems to be less likely with ACML.
This presumably occurs because openBLAS is not doing a good job in detecting
when the overhead of threading outweights the gains from distributing the
computations.
You can compare speeds by setting OMP_NUM_THREADS to different values.
In cases where threaded linear algebra is slower than unthreaded, you would
want to set OMP_NUM_THREADS to 1.
\end_layout
\begin_layout Standard
Therefore I recommend that you test any large jobs to compare performance
with a single thread vs.
multiple threads.
Only if you see a substantive improvement with multiple threads does it
make sense to have OMP_NUM_THREADS be greater than one.
\end_layout
\begin_layout Section
Basic shared memory parallel programming in R
\end_layout
\begin_layout Subsection
foreach
\end_layout
\begin_layout Standard
A simple way to exploit parallelism in R when you have an embarrassingly
parallel problem (one where you can split the problem up into independent
chunks) is to use the
\emph on
foreach
\emph default
package to do a for loop in parallel.
For example, bootstrapping, random forests, simulation studies, cross-validatio
n and many other statistical methods can be handled in this way.
You would not want to use
\emph on
foreach
\emph default
if the iterations were not independent of each other.
\end_layout
\begin_layout Standard
The
\emph on
foreach
\emph default
package provides a
\emph on
foreach
\emph default
command that allows you to do this easily.
\emph on
foreach
\emph default
can use a variety of parallel
\begin_inset Quotes eld
\end_inset
back-ends
\begin_inset Quotes erd
\end_inset
.
It can use
\emph on
Rmpi
\emph default
to access cores in a distributed memory setting as discussed in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Distributed-memory"
\end_inset
or the
\emph on
parallel
\emph default
or
\emph on
multicore
\emph default
packages to use shared memory cores.
When using
\emph on
parallel
\emph default
or
\emph on
multicore
\emph default
as the back-end (these are equivalent to each other from the user perspective),
you should see multiple processes (as many as you registered; ideally each
at 100%) when you look at
\emph on
top
\emph default
.
The multiple processes are created by forking or using sockets; this is
discussed a bit more later in this document.
\end_layout
\begin_layout Chunk
<<foreach, eval=FALSE, tidy=FALSE>>=
\end_layout
\begin_layout Chunk
@
\end_layout
\begin_layout Standard
The result of
\emph on
foreach
\emph default
will generally be a list, unless
\emph on
foreach
\emph default
is able to put it into a simpler R object.
Note that
\emph on
foreach
\emph default
also provides some additional functionality for collecting and managing
the results that mean that you don't have to do some of the bookkeeping
you would need to do if writing your own for loop.
\end_layout
\begin_layout Standard
You can debug by running serially using
\emph on
%do%
\emph default
rather than
\emph on
%dopar%
\emph default
.
Note that you may need to load packages within the
\emph on
foreach
\emph default
construct to ensure a package is available to all of the calculations.
\end_layout