<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://boofcv.org/index.php?action=history&amp;feed=atom&amp;title=Concurrency</id>
	<title>Concurrency - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://boofcv.org/index.php?action=history&amp;feed=atom&amp;title=Concurrency"/>
	<link rel="alternate" type="text/html" href="https://boofcv.org/index.php?title=Concurrency&amp;action=history"/>
	<updated>2026-05-08T05:35:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.2</generator>
	<entry>
		<id>https://boofcv.org/index.php?title=Concurrency&amp;diff=2513&amp;oldid=prev</id>
		<title>Peter at 19:18, 8 March 2019</title>
		<link rel="alternate" type="text/html" href="https://boofcv.org/index.php?title=Concurrency&amp;diff=2513&amp;oldid=prev"/>
		<updated>2019-03-08T19:18:12Z</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 19:18, 8 March 2019&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 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;Concurrent algorithms were introduced into BoofCV in version 0.33. Concurrent or multi-threaded algorithms are algorithms which can solve problems with more than one processes running in parallel. This can result in very large speed improvements. On my desktop, each physical CPU core results in a speed up of about 1x for highly paralizable algorithms such as image convolution. Since my system has 4-cores that means a 4x speed up is typical. Not all algorithms have been made concurrent and not all algorithms can be made fully parallel. That means in a typical computer vision algorithm you can&amp;#039;t expect a 4x speed up.  &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;Concurrent algorithms were introduced into BoofCV in version 0.33. Concurrent or multi-threaded algorithms are algorithms which can solve problems with more than one processes running in parallel. This can result in very large speed improvements. On my desktop, each physical CPU core results in a speed up of about 1x for highly paralizable algorithms such as image convolution. Since my system has 4-cores that means a 4x speed up is typical. Not all algorithms have been made concurrent and not all algorithms can be made fully parallel. That means in a typical computer vision algorithm you can&amp;#039;t expect a 4x speed up.  &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; 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;By default concurrent algorithms are turned &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;off&lt;/del&gt;. &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;To &lt;/del&gt;turn them &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;on you need to add the following line to your code&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;By default concurrent algorithms are turned &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;on&lt;/ins&gt;. &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;If you wish to &lt;/ins&gt;turn them &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;off that is simple enough, see below&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;&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&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;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&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;BoofConcurrency.USE_CONCURRENT = &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;true&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;BoofConcurrency.USE_CONCURRENT = &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;false&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;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&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;/syntaxhighlight&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; 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;It&amp;#039;s highly recommended that you do this at or near the very first line in your program and leave it alone. In theory you can change it back to &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;false &lt;/del&gt;if you wish, but not all algorithms will respect that change.&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;It&amp;#039;s highly recommended that you do this at or near the very first line in your program and leave it alone. In theory you can change it back to &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;true &lt;/ins&gt;if you wish, but not all algorithms will respect that change.&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;By default the maximum number of threads created is defined by ```Runtime.getRuntime().availableProcessors()```. This is typically the total number of hyperthreads and not cores. It&amp;#039;s possible the manually specify the number of threads:&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;By default the maximum number of threads created is defined by ```Runtime.getRuntime().availableProcessors()```. This is typically the total number of hyperthreads and not cores. It&amp;#039;s possible the manually specify the number of threads:&lt;/div&gt;&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-l17&quot;&gt;Line 17:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 17:&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;That will set the maximum number of threads to 5. Again, it&amp;#039;s not a great idea to mess with this value after the program has launched unless you really know what you&amp;#039;re doing.&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;That will set the maximum number of threads to 5. Again, it&amp;#039;s not a great idea to mess with this value after the program has launched unless you really know what you&amp;#039;re doing.&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; 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;If threads &lt;/del&gt;are &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;so great and easy to use why weren&amp;#039;t they added to BoofCV earlier? Writing &lt;/del&gt;bug free &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;and efficient multi-threaded &lt;/del&gt;code is &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;difficult and debugging the code &lt;/del&gt;is extremely difficult. &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Code &lt;/del&gt;might &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;work perfectly on one system but crash horribly on another due to &lt;/del&gt;a &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;subtle type-o &lt;/del&gt;or &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;misunderstanding of how &lt;/del&gt;an &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;operation behaves&lt;/del&gt;. &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;More on this later&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;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Other Issues:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&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;# Some algorithms produce equivalent but non-deterministic output&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&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;# Concurrent algorithms &lt;/ins&gt;are &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;never as &amp;quot;&lt;/ins&gt;bug free&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;quot; as serial algorithms.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&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;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&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;An algorithm like image convolution should produce identical results. However, a feature detector might return the same features but in a different order. Your &lt;/ins&gt;code &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;shouldn&amp;#039;t depend on the order a feature &lt;/ins&gt;is &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;returned but often it &lt;/ins&gt;is&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;&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;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&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;Multi-threaded algorithms are &lt;/ins&gt;extremely difficult &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;to write correctly and robustly&lt;/ins&gt;. &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Bugs &lt;/ins&gt;might &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;only happen when a 1 in &lt;/ins&gt;a &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;million timing coincidence occur or only manifest in faster &lt;/ins&gt;or &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;slow machine. For this reason all concurrent algorithms in BoofCV have &lt;/ins&gt;an &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;untainted equivalent serial algorithm which makes no calls to threads at all&lt;/ins&gt;. &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;If you think there might be a bug you can switch to the serial version, then start debugging and preparing a bug report&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;== JMH Benchmark Results ==&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;== JMH Benchmark Results ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Peter</name></author>
	</entry>
	<entry>
		<id>https://boofcv.org/index.php?title=Concurrency&amp;diff=2509&amp;oldid=prev</id>
		<title>Peter: Created page with &quot;Concurrent algorithms were introduced into BoofCV in version 0.33. Concurrent or multi-threaded algorithms are algorithms which can solve problems with more than one processes...&quot;</title>
		<link rel="alternate" type="text/html" href="https://boofcv.org/index.php?title=Concurrency&amp;diff=2509&amp;oldid=prev"/>
		<updated>2019-02-24T03:55:57Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Concurrent algorithms were introduced into BoofCV in version 0.33. Concurrent or multi-threaded algorithms are algorithms which can solve problems with more than one processes...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Concurrent algorithms were introduced into BoofCV in version 0.33. Concurrent or multi-threaded algorithms are algorithms which can solve problems with more than one processes running in parallel. This can result in very large speed improvements. On my desktop, each physical CPU core results in a speed up of about 1x for highly paralizable algorithms such as image convolution. Since my system has 4-cores that means a 4x speed up is typical. Not all algorithms have been made concurrent and not all algorithms can be made fully parallel. That means in a typical computer vision algorithm you can&amp;#039;t expect a 4x speed up. &lt;br /&gt;
&lt;br /&gt;
By default concurrent algorithms are turned off. To turn them on you need to add the following line to your code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
BoofConcurrency.USE_CONCURRENT = true;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s highly recommended that you do this at or near the very first line in your program and leave it alone. In theory you can change it back to false if you wish, but not all algorithms will respect that change.&lt;br /&gt;
&lt;br /&gt;
By default the maximum number of threads created is defined by ```Runtime.getRuntime().availableProcessors()```. This is typically the total number of hyperthreads and not cores. It&amp;#039;s possible the manually specify the number of threads:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
BoofConcurrency.setMaxThreads(5);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That will set the maximum number of threads to 5. Again, it&amp;#039;s not a great idea to mess with this value after the program has launched unless you really know what you&amp;#039;re doing.&lt;br /&gt;
&lt;br /&gt;
If threads are so great and easy to use why weren&amp;#039;t they added to BoofCV earlier? Writing bug free and efficient multi-threaded code is difficult and debugging the code is extremely difficult. Code might work perfectly on one system but crash horribly on another due to a subtle type-o or misunderstanding of how an operation behaves. More on this later. &lt;br /&gt;
&lt;br /&gt;
== JMH Benchmark Results ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Benchmark                       (concurrent)  (width)  Mode  Cnt     Score     Error  Units&lt;br /&gt;
BenchmarkBlurImageOps.gaussian          true     5000  avgt   10    84.100 ±   6.310  ms/op&lt;br /&gt;
BenchmarkBlurImageOps.gaussian         false     5000  avgt   10   328.401 ±   4.105  ms/op&lt;br /&gt;
BenchmarkBlurImageOps.mean              true     5000  avgt   10    32.907 ±   0.695  ms/op&lt;br /&gt;
BenchmarkBlurImageOps.mean             false     5000  avgt   10   127.657 ±   0.623  ms/op&lt;br /&gt;
BenchmarkBlurImageOps.median            true     5000  avgt   10   322.588 ±   6.403  ms/op&lt;br /&gt;
BenchmarkBlurImageOps.median           false     5000  avgt   10  1142.279 ± 120.221  ms/op&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The table above shows actual performance with and without threads for different image blur operations. My system has 4 cores and 8 hyperthreads. As a result the typical speedup is around 4x. When the number of concurrent threads exceeds the number of cores the gains go down significantly.&lt;br /&gt;
&lt;br /&gt;
== Writing Your Own Concurrent Algorithms  ==&lt;br /&gt;
&lt;br /&gt;
All build in concurrent algorithms use a single thread pool. Specifically, they use a single [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html ForkJoinPool]. You can access this pool for your use as it&amp;#039;s a static variable.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
BoofConcurrency.pool&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
However, it&amp;#039;s recommended that you stick to using function provided in BoofCV for parallelizing for loops. Most of the time you will want to use BoofConcurrency.loopFor(). Here&amp;#039;s an example of how it can be used. Below is function which sets the value of all pixels less than 200 to 0.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
for (int y = 0; y &amp;lt; input.height; y++) {&lt;br /&gt;
	int index = input.startIndex + y*input.stride;&lt;br /&gt;
	for (int x = 0; x &amp;lt; input.width; x++, index++ ) {&lt;br /&gt;
		if( input.data[index] &amp;lt; 200 )&lt;br /&gt;
			input.data[index] = 0;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The concurrent version looks very similar:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
BoofConcurrency.loopFor(0, input.height, y -&amp;gt; {&lt;br /&gt;
	int index = input.startIndex + y*input.stride;&lt;br /&gt;
	for (int x = 0; x &amp;lt; input.width; x++, index++ ) {&lt;br /&gt;
		if( input.data[index] &amp;lt; 200 )&lt;br /&gt;
			input.data[index] = 0;&lt;br /&gt;
	}&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Only one line needs to be changed! There are also other function in BoofConcurrency like the loopBlock variants. These are used when you want to run a block of values all in the same thread.&lt;br /&gt;
&lt;br /&gt;
== BoofCV Innards ==&lt;br /&gt;
&lt;br /&gt;
Internally BoofCV follows the following code practices:&lt;br /&gt;
&lt;br /&gt;
* Always first optimize single threaded algorithms fully first&lt;br /&gt;
* All algorithms must have a single thread version which contain no references to any threaded code&lt;br /&gt;
* Threaded/concurrent algorithms are tested for correctness against the equivalent single threaded code&lt;br /&gt;
&lt;br /&gt;
These code practices assume that multi-threaded can never be fully trusted. I&amp;#039;ve noticed a strong correlation between people who believe tutorials that claim to make concurrency easy and bug free, and how buggy their code is. By following the procedure above several subtle bugs were discovered and fixed in BoofCV.&lt;br /&gt;
&lt;br /&gt;
Automatically generated code is used heavily in concurrent algorithms in BoofCV. This is done by adding code comments with hints in them that tell a code generator how to make the algorithm parallel. An example of this is shown below:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
//CONCURRENT_BELOW BoofConcurrency.loopFor(0, input.height, y -&amp;gt; {&lt;br /&gt;
for (int y = 0; y &amp;lt; input.height; y++) {&lt;br /&gt;
	int index = input.startIndex + y*input.stride;&lt;br /&gt;
	for (int x = 0; x &amp;lt; input.width; x++, index++ ) {&lt;br /&gt;
		if( input.data[index] &amp;lt; 200 )&lt;br /&gt;
			input.data[index] = 0;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
//CONCURRENT_ABOVE });&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The //CONCURRENT comments indicate that either the line below or above it needs to be replaced with the remaining text. See AutocodeConcurrentApp for more details on this system.&lt;/div&gt;</summary>
		<author><name>Peter</name></author>
	</entry>
</feed>