<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://info.stat.cmu.edu/index.php?action=history&amp;feed=atom&amp;title=Parallelization_in_r</id>
	<title>Parallelization in r - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://info.stat.cmu.edu/index.php?action=history&amp;feed=atom&amp;title=Parallelization_in_r"/>
	<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;action=history"/>
	<updated>2026-11-04T21:44:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=832&amp;oldid=prev</id>
		<title>Ajshen at 03:25, 29 June 2026</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=832&amp;oldid=prev"/>
		<updated>2026-06-29T03:25:22Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 03:25, 29 June 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Parallelization]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. They include:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. They include:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key infowiki:diff:1.41:old-221:rev-832:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Ajshen</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=221&amp;oldid=prev</id>
		<title>Ecucuzze at 14:19, 1 July 2025</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=221&amp;oldid=prev"/>
		<updated>2025-07-01T14:19:02Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 14:19, 1 July 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l124&quot;&gt;Line 124:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 124:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; [https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf manual]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; [https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf manual]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf vignette]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf vignette]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/del&gt;= The Fastest One as of February 2024:&amp;amp;nbsp;&amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;=&lt;/del&gt;=&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= The Fastest One as of February 2024:&amp;amp;nbsp;&amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt;=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;All of the above are good solutions, but from practical experience, we have seen that sometimes the previous methods can incur high overheads. A consistently faster solution comes from a package developed and maintained by the R bioinformatics community:&amp;amp;nbsp;&amp;amp;nbsp;&amp;lt;code&amp;gt;BioConductor&amp;lt;/code&amp;gt;. The package is named &amp;lt;code&amp;gt;BiocParallel&amp;amp;nbsp;&amp;lt;/code&amp;gt;and its interface is almost exactly the same as the one for the&amp;amp;nbsp;&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; package seen in the previous section. The only difference is that we need another package.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;All of the above are good solutions, but from practical experience, we have seen that sometimes the previous methods can incur high overheads. A consistently faster solution comes from a package developed and maintained by the R bioinformatics community:&amp;amp;nbsp;&amp;amp;nbsp;&amp;lt;code&amp;gt;BioConductor&amp;lt;/code&amp;gt;. The package is named &amp;lt;code&amp;gt;BiocParallel&amp;amp;nbsp;&amp;lt;/code&amp;gt;and its interface is almost exactly the same as the one for the&amp;amp;nbsp;&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; package seen in the previous section. The only difference is that we need another package.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key infowiki:diff:1.41:old-220:rev-221:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=220&amp;oldid=prev</id>
		<title>Ecucuzze: Updated and cleaned up the language on this page.</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=220&amp;oldid=prev"/>
		<updated>2025-07-01T14:18:32Z</updated>

		<summary type="html">&lt;p&gt;Updated and cleaned up the language on this page.&lt;/p&gt;
&lt;a href=&quot;https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;amp;diff=220&amp;amp;oldid=16&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=16&amp;oldid=prev</id>
		<title>Skipper: Created page with &quot; There are a few ways to parallelize within &lt;code&gt;R&lt;/code&gt;, these generally broken up into 3 different approaches the capture different “for-loop” frameworks in &lt;code&gt;R&lt;/c...&quot;</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=16&amp;oldid=prev"/>
		<updated>2024-03-29T23:46:46Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, these generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/c...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, these generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. They include:&lt;br /&gt;
&lt;br /&gt;
#parallelization in straight &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop-style with &amp;lt;code&amp;gt;foeach&amp;lt;/code&amp;gt; &lt;br /&gt;
#parallelization in &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;-style loops with &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; &lt;br /&gt;
#parallelization with &amp;lt;code&amp;gt;tidyverse&amp;lt;/code&amp;gt; nesting-style loops with &amp;lt;code&amp;gt;multidplyr&amp;lt;/code&amp;gt; (not discussed see: [https://blog.dominodatalab.com/multicore-data-science-r-python/ R and python parallelization] blog for discussion) &lt;br /&gt;
&lt;br /&gt;
The final option the above list (as of 8/2021) is still too experimental and limited to truly encourage.&lt;br /&gt;
&lt;br /&gt;
== General starting point ==&lt;br /&gt;
&lt;br /&gt;
To start parallelizing within R we need to have some understanding of how many cores we have. To do figures with out we use the following lines of code:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(parallel)&lt;br /&gt;
numCores &amp;lt;- detectCores()&lt;br /&gt;
numCores&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will tell you the total number of cores on your computer. We often recommend using at least we less than the total number of cores when working on your personal computer or on the department servers - to leave room for other jobs / use.&lt;br /&gt;
&lt;br /&gt;
== Extra resources: ==&lt;br /&gt;
&lt;br /&gt;
*[https://nceas.github.io/oss-lessons/parallel-computing-in-r/parallel-computing-in-r.html Matt Jones] &lt;br /&gt;
*[https://blog.dominodatalab.com/multicore-data-science-r-python/ R and python more options] &lt;br /&gt;
&lt;br /&gt;
= for-loop style: &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;foreach ()&amp;amp;nbsp;%dopar%&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The combination of &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; can convert for-loop style coding into parallel processing and provide a lot of additional parameters that can help reduce unnecessary memory usage, provide progress reports and more.&lt;br /&gt;
&lt;br /&gt;
In the following presentation, we’ll demonstrate the speed obtained with different approaches using a simulation function defined below&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;my_k &amp;lt;- rep(1:14, each = 20)&lt;br /&gt;
&lt;br /&gt;
my_simulation &amp;lt;- function(k) {&lt;br /&gt;
  x &amp;lt;- runif(30,-3, 3)&lt;br /&gt;
  y &amp;lt;- 2*x + 2* rnorm(n = 30)&lt;br /&gt;
  x0 &amp;lt;- runif(10000,-3, 3)&lt;br /&gt;
  y0 &amp;lt;- 2*x0 + 2*rnorm(n = 10000)&lt;br /&gt;
&lt;br /&gt;
  my_lm &amp;lt;- lm(y ~ poly(x,k))&lt;br /&gt;
  y0_hat &amp;lt;- predict(my_lm, data.frame(x = x0))&lt;br /&gt;
  return(mean((y0-y0_hat)^2))&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== single core analysis ==&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_f &amp;lt;- c()&lt;br /&gt;
  for (k in my_k) {&lt;br /&gt;
    storage_f &amp;lt;- c(storage_f, my_simulation(k))&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.811   0.633   2.449&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Additionally, in the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; package we can also use just a single core in the following way:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#single-core `foreach`&lt;br /&gt;
library(foreach)&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_fe &amp;lt;- foreach (k=my_k, .combine=c) %do% {&lt;br /&gt;
    my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.876   0.640   2.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This structure in the above code using &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;%do%&amp;lt;/code&amp;gt; are very similar to the actual code we use, so it’s useful to understand. It should be clear that the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function works very similar to the base &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop but we need to combined it with &amp;lt;code&amp;gt;%do%&amp;lt;/code&amp;gt;. Additionally, notice that the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; loops of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; for values in &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; and we express that differently than in a &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop (&amp;lt;code&amp;gt;k in my_k&amp;lt;/code&amp;gt; vs &amp;lt;code&amp;gt;k = my_k&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== multi-core analysis ==&lt;br /&gt;
&lt;br /&gt;
To multiple cores with &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; we just change the above code to&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doParallel)&lt;br /&gt;
registerDoParallel(numCores-2)  # use multicore, set to the number of our cores&lt;br /&gt;
&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_p &amp;lt;- foreach (k=my_k, .combine=c) %dopar% {&lt;br /&gt;
     my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&lt;br /&gt;
stopImplicitCluster()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   3.513   2.520   0.601&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last number under “elapsed” suggest that although we used more core + user time overall we completed the overall process faster than without parallelization.&lt;br /&gt;
&lt;br /&gt;
== additional parameters ==&lt;br /&gt;
&lt;br /&gt;
#&amp;lt;code&amp;gt;.combine&amp;lt;/code&amp;gt;: this parameter tells the system how you’d like to return of each step of the for-loop combined at the end. Above we used &amp;lt;code&amp;gt;.combine = c&amp;lt;/code&amp;gt; to get a vector back, but we could also use things like &amp;lt;code&amp;gt;rbind&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cbind&amp;lt;/code&amp;gt;. Note that the default is a list. &lt;br /&gt;
#progress bars: To have a running progress bar one can use the package &amp;lt;code&amp;gt;doSNOW&amp;lt;/code&amp;gt;. For the previous process we can add a progress bar with the following set of code &amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doSNOW)&lt;br /&gt;
cl &amp;lt;- makeCluster(numCores-4)&lt;br /&gt;
registerDoSNOW(cl)&lt;br /&gt;
pb &amp;lt;- txtProgressBar(max = length(my_k), style = 3)&lt;br /&gt;
progress &amp;lt;- function(n) setTxtProgressBar(pb, n)&lt;br /&gt;
opts &amp;lt;- list(progress = progress)&lt;br /&gt;
&lt;br /&gt;
storage_p &amp;lt;- foreach (k=my_k, .combine=c,&lt;br /&gt;
                      .options.snow = opts) %dopar% {&lt;br /&gt;
   my_simulation(k)&lt;br /&gt;
}&lt;br /&gt;
parallel::stopCluster(cl)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;|==============================================================        | 83%&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#memory and library passing: Sometimes you have a lot of functions or memory in your RAM before your run for parallelized for-loop. The &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function tries to reduce the amount memory used for each parallel process by constraining which functions are passed in. To specify which packages you’d like to use use the &amp;lt;code&amp;gt;.packages&amp;lt;/code&amp;gt; command in &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt;, for example: &amp;lt;code&amp;gt;.packages = c(&amp;quot;dplyr&amp;quot;, &amp;quot;tidyr&amp;quot;)&amp;lt;/code&amp;gt;. Additionally, you can constraint what data is passed into each process with the parameter &amp;lt;code&amp;gt;.export&amp;lt;/code&amp;gt; (which also uses strings relative to your global envirnoment). &lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
== additional resources ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf vignette]&lt;br /&gt;
&lt;br /&gt;
= lapply style: &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The second package we will look at is &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt;, which includes the function &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; which can be used very similar to an &amp;lt;code&amp;gt;lapply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== single core analysis ==&lt;br /&gt;
&lt;br /&gt;
For a single-core analysis of the same analysis seen in the previous section, we could run&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;&lt;br /&gt;
system.time({&lt;br /&gt;
  results &amp;lt;- lapply(my_k, my_simulation)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.819   0.641   2.467&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== multi core analysis ==&lt;br /&gt;
&lt;br /&gt;
Now, with the &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; function we can do basically the same thing:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;system.time({&lt;br /&gt;
  results &amp;lt;- mclapply(my_k, my_simulation, mc.cores = numCores)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   2.208   1.636   0.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== additional resources ==&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; [https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf manual] &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== &amp;#039;&amp;#039;&amp;#039;The fastest one as of February 2024:&amp;amp;nbsp;&amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;&amp;#039; ==&lt;br /&gt;
&lt;br /&gt;
All of the above are good solutions, but from practical experience we have seen that sometimes they can incur in high overheads. A consistently faster solution comes from a package developed and maintainted by the (huge) bioinformatics community that works with R:&amp;amp;nbsp;&amp;amp;nbsp;&amp;lt;code&amp;gt;BioConductor&amp;lt;/code&amp;gt;. The package is named &amp;lt;code&amp;gt;BiocParallel&amp;amp;nbsp;&amp;lt;/code&amp;gt;and its interface is almost exactly the same as the one for the&amp;amp;nbsp;&amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; package seen in the previous section. The only difference is that we need another package.&lt;br /&gt;
&lt;br /&gt;
First of all, install the bioconductor package manager via&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;install.packages(&amp;quot;BiocManager&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will make available an &amp;lt;code&amp;gt;install&amp;amp;nbsp;&amp;lt;/code&amp;gt;command, which we use as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;install(&amp;quot;BiocParallel&amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use a simple command. Assuming the function to run multiple times in parallel is named &amp;lt;code&amp;gt;foo&amp;lt;/code&amp;gt;, we type&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;library(&amp;#039;BiocParallel&amp;#039;)&lt;br /&gt;
system.time( &lt;br /&gt;
     y &amp;lt;- bplapply(seq_len(100), foo) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See this [https://lcolladotor.github.io/2016/03/07/biocparallel/ article] for more information.&lt;/div&gt;</summary>
		<author><name>Skipper</name></author>
	</entry>
</feed>