<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Reed Copsey, Jr. &#187; Parallelism</title>
	<atom:link href="http://reedcopsey.com/category/algorithms/parallelism/feed/" rel="self" type="application/rss+xml" />
	<link>http://reedcopsey.com</link>
	<description>Thoughts on C#, WPF, .NET, and programming for Scientific Visualization</description>
	<lastBuildDate>Mon, 28 Nov 2011 20:42:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ConcurrentDictionary&lt;TKey,TValue&gt; used with Lazy&lt;T&gt;</title>
		<link>http://reedcopsey.com/2011/01/16/concurrentdictionarytkeytvalue-used-with-lazyt/</link>
		<comments>http://reedcopsey.com/2011/01/16/concurrentdictionarytkeytvalue-used-with-lazyt/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 21:04:03 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2011/01/16/concurrentdictionarytkeytvalue-used-with-lazyt/</guid>
		<description><![CDATA[In a recent thread on the MSDN forum for the TPL, Stephen Toub suggested mixing ConcurrentDictionary&#60;T,U&#62; with Lazy&#60;T&#62;.&#160; This provides a fantastic model for creating a thread safe dictionary of values where the construction of the value type is expensive.&#160; This is an incredibly useful pattern for many operations, such as value caches. The ConcurrentDictionary&#60;TKey, [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/e350f7d0-b860-482e-9b84-8dba12267d25">recent thread</a> on the <a href="http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/threads">MSDN forum for the TPL</a>, Stephen Toub suggested mixing ConcurrentDictionary&lt;T,U&gt; with Lazy&lt;T&gt;.&#160; This provides a fantastic model for creating a thread safe dictionary of values where the construction of the value type is expensive.&#160; This is an incredibly useful pattern for many operations, such as value caches.</p>
<p>  <span id="more-295"></span>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd287191.aspx">ConcurrentDictionary&lt;TKey, TValue&gt; class</a> was added in .NET 4, and provides a thread-safe, lock free collection of key value pairs.&#160; While this is a fantastic replacement for Dictionary&lt;TKey, TValue&gt;, it has a potential flaw when used with values where construction of the value class is expensive.</p>
<p>The typical way this is used is to call a method such as <a href="http://msdn.microsoft.com/en-us/library/ee378677.aspx">GetOrAdd</a> to fetch or add a value to the dictionary.&#160; It handles all of the thread safety for you, but as a result, if two threads call this simultaneously, two instances of TValue can easily be constructed.</p>
<p>If TValue is very expensive to construct, or worse, has side effects if constructed too often, this is less than desirable.&#160; While you can easily work around this with locking, Stephen Toub provided a very clever alternative – using Lazy&lt;TValue&gt; as the value in the dictionary instead.</p>
<p>This looks like the following.&#160; Instead of calling:</p>
<pre class="csharpcode">MyValue <span class="kwrd">value</span> = dictionary.GetOrAdd(
                             key,
                             () =&gt; <span class="kwrd">new</span> MyValue(key));</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>We would instead use a ConcurrentDictionary&lt;TKey, Lazy&lt;TValue&gt;&gt;, and write:</p>
<pre class="csharpcode">MyValue <span class="kwrd">value</span> = dictionary.GetOrAdd(
                             key,
                             () =&gt; <span class="kwrd">new</span> Lazy&lt;MyValue&gt;(
                                 () =&gt; <span class="kwrd">new</span> MyValue(key)))
                          .Value;</pre>
<p>This simple change dramatically changes how the operation works.&#160; Now, if two threads call this simultaneously, instead of constructing two MyValue instances, we construct two Lazy&lt;MyValue&gt; instances.</p>
<p>However, the Lazy&lt;T&gt; class is very cheap to construct.&#160; Unlike “MyValue”, we can safely afford to construct this twice and “throw away” one of the instances.</p>
<p>We then call Lazy&lt;T&gt;.Value at the end to fetch our “MyValue” instance.&#160; At this point, GetOrAdd will always return the same instance of Lazy&lt;MyValue&gt;.&#160; Since Lazy&lt;T&gt; doesn’t construct the MyValue instance until requested, the actual MyClass instance returned is only constructed once.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2011/01/16/concurrentdictionarytkeytvalue-used-with-lazyt/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 20, Using Task with Existing APIs</title>
		<link>http://reedcopsey.com/2010/10/27/parallelism-in-net-part-20-using-task-with-existing-apis/</link>
		<comments>http://reedcopsey.com/2010/10/27/parallelism-in-net-part-20-using-task-with-existing-apis/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 20:15:31 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/10/27/parallelism-in-net-part-20-using-task-with-existing-apis/</guid>
		<description><![CDATA[Although the Task class provides a huge amount of flexibility for handling asynchronous actions, the .NET Framework still contains a large number of APIs that are based on the previous asynchronous programming model.&#160; While Task and Task&#60;T&#62; provide a much nicer syntax as well as extending the flexibility, allowing features such as continuations based on [...]]]></description>
			<content:encoded><![CDATA[<p align="left">Although the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx">Task class</a> provides a huge amount of flexibility for handling asynchronous actions, the .NET Framework still contains a large number of APIs that are based on the previous <a href="http://msdn.microsoft.com/en-us/library/ms228963.aspx">asynchronous programming model</a>.&#160; While Task and Task&lt;T&gt; provide a much nicer syntax as well as extending the flexibility, allowing features such as <a href="http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/">continuations based on multiple tasks</a>, the existing APIs don’t directly support this workflow. </p>
<p>  <span id="more-288"></span>
<p align="left">There is a method in the <a href="http://msdn.microsoft.com/en-us/library/dd321401.aspx">TaskFactory class</a> which can be used to adapt the existing APIs to the new Task class: <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync(v=VS.100).aspx">TaskFactory.FromAsync</a>.&#160; This method provides a way to convert from the BeginOperation/EndOperation method pair syntax common through .NET Framework directly to a Task&lt;T&gt; containing the results of the operation in the task’s Result parameter.</p>
<p align="left">While this method does exist, it unfortunately comes at a cost – the method overloads are far from simple to decipher, and the resulting code is not always as easily understood as newer code based directly on the Task class.&#160; For example, a single call to handle WebRequest.BeginGetResponse/EndGetReponse, one of the easiest “pairs” of methods to use, looks like the following:</p>
<pre class="csharpcode">var task = Task.Factory.FromAsync&lt;WebResponse&gt;(
                            request.BeginGetResponse,
                            request.EndGetResponse,
                            <span class="kwrd">null</span>);</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>The compiler is unfortunately unable to infer the correct type, and, as a result, the WebReponse must be explicitly mentioned in the method call.&#160; As a result, I typically recommend wrapping this into an extension method to ease use.&#160; For example, I would place the above in an extension method like:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> WebRequestExtensions
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> Task&lt;WebResponse&gt; GetReponseAsync(<span class="kwrd">this</span> WebRequest request)
    {
        <span class="kwrd">return</span> Task.Factory.FromAsync&lt;WebResponse&gt;(
                        request.BeginGetResponse,
                        request.EndGetResponse,
                        <span class="kwrd">null</span>);
    }
}</pre>
<p>This dramatically simplifies usage.&#160; For example, if we wanted to asynchronously check to see if this blog supported XHTML 1.0, and report that in a text box to the user, we could do:</p>
<pre class="csharpcode">var webRequest = WebRequest.Create(<span class="str">&quot;http://www.reedcopsey.com&quot;</span>);
webRequest.GetReponseAsync().ContinueWith(t =&gt;
    {
        <span class="kwrd">using</span> (var sr = <span class="kwrd">new</span> StreamReader(t.Result.GetResponseStream()))
        {
            <span class="kwrd">string</span> str = sr.ReadLine();;
            <span class="kwrd">this</span>.textBox1.Text = <span class="kwrd">string</span>.Format(<span class="str">&quot;Page at {0} supports XHTML 1.0: {1}&quot;</span>,
                t.Result.ResponseUri,
                str.Contains(<span class="str">&quot;XHTML 1.0&quot;</span>));
        }
    }, TaskScheduler.FromCurrentSynchronizationContext());</pre>
<p>&#160;</p>
<p>By using a continuation with a TaskScheduler based on the current synchronization context, we can keep this request asynchronous, check based on the first line of the response string, and report the results back on our UI directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/10/27/parallelism-in-net-part-20-using-task-with-existing-apis/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 19, TaskContinuationOptions</title>
		<link>http://reedcopsey.com/2010/10/26/parallelism-in-net-part-19-taskcontinuationoptions/</link>
		<comments>http://reedcopsey.com/2010/10/26/parallelism-in-net-part-19-taskcontinuationoptions/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 00:30:53 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/10/26/parallelism-in-net-part-19-taskcontinuationoptions/</guid>
		<description><![CDATA[My introduction to Task continuations demonstrates continuations on the Task class.&#160; In addition, I’ve shown how continuations allow handling of multiple tasks in a clean, concise manner.&#160; Continuations can also be used to handle exceptional situations using a clean, simple syntax. In addition to standard Task continuations , the Task class provides some options for [...]]]></description>
			<content:encoded><![CDATA[<p align="left">My <a href="http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/" target="_blank">introduction to Task continuations</a> demonstrates continuations on the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task class</a>.&#160; In addition, I’ve shown how <a href="http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/" target="_blank">continuations allow handling of multiple tasks</a> in a clean, concise manner.&#160; Continuations can also be used to handle exceptional situations using a clean, simple syntax.</p>
<p>  <span id="more-284"></span>
<p align="left">In addition to standard <a href="http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/" target="_blank">Task continuations</a> , the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task class</a> provides some options for filtering continuations automatically.&#160; This is handled via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx" target="_blank">TaskContinationOptions</a> enumeration, which provides hints to the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx" target="_blank">TaskScheduler</a> that it should only continue based on the operation of the antecedent task.</p>
<p align="left">This is especially useful when dealing with exceptions.&#160; For example, we can extend <a href="http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/" target="_blank">the sample from our earlier continuation discussion</a> to include support for handling exceptions thrown by the Factorize method:</p>
<pre class="csharpcode"><span class="rem">// Get a copy of the UI-thread task scheduler up front to use later</span>
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

<span class="rem">// Start our task</span>
var factorize = Task.Factory.StartNew(
    () =&gt;
        {
            <span class="kwrd">int</span> primeFactor1 = 0;
            <span class="kwrd">int</span> primeFactor2 = 0;
            <span class="kwrd">bool</span> result = Factorize(10298312, <span class="kwrd">ref</span> primeFactor1, <span class="kwrd">ref</span> primeFactor2);
            <span class="kwrd">return</span> <span class="kwrd">new</span> {
                           Result = result,
                           Factor1 = primeFactor1,
                           Factor2 = primeFactor2
                       };
        });

<span class="rem">// When we succeed, report the results to the UI</span>
factorize.ContinueWith(task =&gt; textBox1.Text = <span class="kwrd">string</span>.Format(<span class="str">&quot;{0}/{1}  [Succeeded {2}]&quot;</span>,
                                 task.Result.Factor1,
                                 task.Result.Factor2,
                                 task.Result.Result),
                        CancellationToken.None,
                        TaskContinuationOptions.NotOnFaulted,
                        uiScheduler);

<span class="rem">// When we have an exception, report it</span>
factorize.ContinueWith(task =&gt;
                             textBox1.Text = <span class="kwrd">string</span>.Format(<span class="str">&quot;Error: {0}&quot;</span>, task.Exception.Message),
                        CancellationToken.None,
                        TaskContinuationOptions.OnlyOnFaulted,
                        uiScheduler);</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>The above code works by using a combination of features.&#160; First, we schedule our task, the same way as in <a href="http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/" target="_blank">the previous example</a>.&#160; However, in this case, we use a different overload of <a href="http://msdn.microsoft.com/en-us/library/dd991174.aspx" target="_blank">Task.ContinueWith</a> which allows us to specify both a specific TaskScheduler (in order to have your continuation run on the UI’s synchronization context) as well as a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx" target="_blank">TaskContinuationOption</a>.&#160; </p>
<p>In the first continuation, we tell the continuation that we only want it to run when there was not an exception by specifying <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx" target="_blank">TaskContinuationOptions.NotOnFaulted</a>.&#160; When our factorize task completes successfully, this continuation will automatically run on the UI thread, and provide the appropriate feedback.</p>
<p>However, if the factorize task has an exception – for example, if the Factorize method throws an exception due to an improper input value, the second continuation will run.&#160; This occurs due to the specification of <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx" target="_blank">TaskContinuationOptions.OnlyOnFaulted</a> in the options.&#160; In this case, we’ll report the error received to the user.</p>
<p>We can use <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcontinuationoptions.aspx" target="_blank">TaskContinuationOptions</a> to filter our continuations by whether or not an exception occurred and whether or not a task was cancelled.&#160; This allows us to handle many situations, and is especially useful when trying to maintain a valid application state without ever blocking the user interface.&#160; The same concepts can be extended even further, and allow you to chain together many tasks based on the success of the previous ones.&#160; Continuations can even be used to <a href="http://blogs.msdn.com/b/pfxteam/archive/2010/02/09/9960735.aspx" target="_blank">create a state machine</a> with full error handling, all without blocking the user interface thread.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/10/26/parallelism-in-net-part-19-taskcontinuationoptions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 18, Task Continuations with Multiple Tasks</title>
		<link>http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/</link>
		<comments>http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 01:18:37 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/</guid>
		<description><![CDATA[In my introduction to Task continuations I demonstrated how the Task class provides a more expressive alternative to traditional callbacks.&#160; Task continuations provide a much cleaner syntax to traditional callbacks, but there are other reasons to switch to using continuations… Task continuations provide a clean syntax, and a very simple, elegant means of synchronizing asynchronous [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/" target="_blank">introduction to Task continuations</a> I demonstrated how the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task class</a> provides a more expressive alternative to <a href="http://en.wikipedia.org/wiki/Callback_(computer_science)" target="_blank">traditional callbacks</a>.&#160; <a href="http://msdn.microsoft.com/en-us/library/ee372288.aspx" target="_blank">Task continuations</a> provide a much cleaner syntax to traditional callbacks, but there are other reasons to switch to using continuations…</p>
<p> <span id="more-282"></span>
<p><a href="http://msdn.microsoft.com/en-us/library/ee372288.aspx" target="_blank">Task continuations</a> provide a clean syntax, and a very simple, elegant means of synchronizing asynchronous method results with the user interface.&#160; In addition, continuations provide a very simple, elegant means of working with collections of tasks.</p>
<p>Prior to .NET 4, working with multiple related asynchronous method calls was very tricky.&#160; If, for example, we wanted to run two asynchronous operations, followed by a single method call which we wanted to run when the first two methods completed, we’d have to program all of the handling ourselves.&#160; We would likely need to take some approach such as using a shared callback which synchronized against a common variable, or using a <a href="http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx" target="_blank">WaitHandle</a> shared within the callbacks to allow one to wait for the second.&#160; Although this could be accomplished easily enough, it requires manually placing this handling into every algorithm which requires this form of blocking.&#160; This is error prone, difficult, and can easily lead to subtle bugs.</p>
<p>Similar to how the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task class</a> static methods providing a way to <a href="http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/" target="_blank">block until multiple tasks have completed</a>, <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.aspx" target="_blank">TaskFactory</a> contains static methods which allow a continuation to be scheduled upon the completion of multiple tasks: <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.continuewhenall.aspx" target="_blank">TaskFactory.ContinueWhenAll</a>.</p>
<p>This allows you to easily specify a single delegate to run when a collection of tasks has completed.&#160; For example, suppose we have a class which fetches data from the network.&#160; This can be a long running operation, and potentially fail in certain situations, such as a server being down.&#160; As a result, we have three separate servers which we will “query” for our information.&#160; Now, suppose we want to grab data from all three servers, and verify that the results are the same from all three.</p>
<p>With traditional asynchronous programming in .NET, this would require using three separate callbacks, and managing the synchronization between the various operations ourselves.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" target="_blank">Task</a> and <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.aspx" target="_blank">TaskFactory</a> classes simplify this for us, allowing us to write:</p>
<pre class="csharpcode">var server1 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(firstServer) );
var server2 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(secondServer) );
var server3 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(thirdServer) );

var result = Task.Factory.ContinueWhenAll( <span class="kwrd">new</span>[] {server1, server2, server3 },
                 (tasks) =&gt;
                 {
                       <span class="rem">// Propogate exceptions (see below)</span>
                       Task.WaitAll(tasks);

                       return <span class="kwrd">this</span>.CompareTaskResults(
                               tasks[0].Result,
                               tasks[1].Result,
                               tasks[2].Result);
                  });</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This is clean, simple, and elegant.&#160; The one complication is the Task.WaitAll(tasks); statement.</p>
<p>Although the continuation will not complete until all three tasks (server1, server2, and server3) have completed, there is a potential snag.&#160; If the networkClass.GetResults method fails, and raises an exception, we want to make sure to handle it cleanly.&#160; By using Task.WaitAll, any exceptions raised within any of our original tasks will get wrapped into a single <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx" target="_blank">AggregateException</a> by the <a href="http://msdn.microsoft.com/en-us/library/dd270695.aspx" target="_blank">WaitAll</a> method, providing us a simplified means of handling the exceptions.&#160; If we wait on the continuation, we can trap this <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx" target="_blank">AggregateException</a>, and handle it cleanly.&#160; Without this line, it’s possible that an exception could remain uncaught and <em>unhandled </em>by a task, which later might trigger a nasty <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception.aspx" target="_blank">UnobservedTaskException</a>.&#160; This would happen any time two of our original tasks failed.</p>
<p>Just as we can schedule a continuation to occur when an entire collection of tasks has completed, we can just as easily setup a continuation to run when any single task within a collection completes.&#160; If, for example, we didn’t need to compare the results of all three network locations, but only use one, we could still schedule three tasks.&#160; We could then have our completion logic work on the first task which completed, and ignore the others.&#160; This is done via <a href="http://msdn.microsoft.com/en-us/library/dd321530.aspx" target="_blank">TaskFactory.ContinueWhenAny</a>:</p>
<pre class="csharpcode">var server1 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(firstServer) );
var server2 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(secondServer) );
var server3 = Task.Factory.StartNew(
                 () =&gt; networkClass.GetResults(thirdServer) );

var result = Task.Factory.ContinueWhenAny( <span class="kwrd">new</span>[] {server1, server2, server3 },
                 (firstTask) =&gt;
                 {
                       <span class="kwrd">return</span> <span class="kwrd">this</span>.ProcessTaskResult(firstTask.Result);
                  });</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, instead of working with all three tasks, we’re just using the first task which finishes.&#160; This is very useful, as it allows us to easily work with results of multiple operations, and “throw away” the others.&#160; However, you must take care when using <a href="http://msdn.microsoft.com/en-us/library/dd321530.aspx" target="_blank">ContinueWhenAny</a> to properly handle exceptions.&#160; At some point, you should always wait on each task (or use the Task.Result property) in order to propogate any exceptions raised from within the task.&#160; Failing to do so can lead to an <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception.aspx" target="_blank">UnobservedTaskException</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/07/19/parallelism-in-net-part-18-task-continuations-with-multiple-tasks/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 17, Think Continuations, not Callbacks</title>
		<link>http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/</link>
		<comments>http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 20:27:40 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/</guid>
		<description><![CDATA[In traditional asynchronous programming, we’d often use a callback to handle notification of a background task’s completion.&#160; The Task class in the Task Parallel Library introduces a cleaner alternative to the traditional callback: continuation tasks. Asynchronous programming methods typically required callback functions.&#160; For example, MSDN’s Asynchronous Delegates Programming Sample shows a class that factorizes a [...]]]></description>
			<content:encoded><![CDATA[<p>In traditional asynchronous programming, we’d often use a <a href="http://en.wikipedia.org/wiki/Callback_(computer_science)" target="_blank">callback</a> to handle notification of a background task’s completion.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> in the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> introduces a cleaner alternative to the traditional <a href="http://en.wikipedia.org/wiki/Callback_(computer_science)" target="_blank">callback</a>: <a href="http://msdn.microsoft.com/en-us/library/ee372288.aspx" target="_blank">continuation tasks</a>.</p>
<p> <span id="more-272"></span>
<p>Asynchronous programming methods typically required callback functions.&#160; For example, MSDN’s <a href="http://msdn.microsoft.com/en-us/library/h80ttd5f(VS.90).aspx" target="_blank">Asynchronous Delegates Programming Sample</a> shows a class that factorizes a number.&#160; The original method in the example has the following signature:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">bool</span> Factorize(<span class="kwrd">int</span> number, <span class="kwrd">ref</span> <span class="kwrd">int</span> primefactor1, <span class="kwrd">ref</span> <span class="kwrd">int</span> primefactor2)
{
 //...</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>However, calling this is quite “tricky”, even if we modernize the sample to use lambda expressions via C# 3.0.&#160; </p>
<p>Normally, we could call this method like so:</p>
<pre class="csharpcode"><span class="kwrd">int</span> primeFactor1 = 0;
<span class="kwrd">int</span> primeFactor2 = 0;

<span class="kwrd">bool</span> answer = Factorize(10298312, <span class="kwrd">ref</span> primeFactor1, <span class="kwrd">ref</span> primeFactor2);
Console.WriteLine(<span class="str">&quot;{0}/{1}  [Succeeded {2}]&quot;</span>, primeFactor1, primeFactor2, answer);</pre>
<p>If we want to make this operation run in the background, and report to the console via a callback, things get tricker.&#160; First, we need a delegate definition: </p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">bool</span> AsyncFactorCaller(
     <span class="kwrd">int</span> number,
     <span class="kwrd">ref</span> <span class="kwrd">int</span> primefactor1,
     <span class="kwrd">ref</span> <span class="kwrd">int</span> primefactor2);</pre>
<p>Then we need to use BeginInvoke to run this method asynchronously:</p>
<pre class="csharpcode"><span class="kwrd">int</span> primeFactor1 = 0;
<span class="kwrd">int</span> primeFactor2 = 0;

AsyncFactorCaller caller  = <span class="kwrd">new</span> AsyncFactorCaller(Factorize);
caller.BeginInvoke(10298312, <span class="kwrd">ref</span> primeFactor1, <span class="kwrd">ref</span> primeFactor2,
   result =&gt;
       {
           <span class="kwrd">int</span> factor1 = 0;
           <span class="kwrd">int</span> factor2 = 0;
           <span class="kwrd">bool</span> answer = caller.EndInvoke(<span class="kwrd">ref</span> factor1, <span class="kwrd">ref</span> factor2, result);
           Console.WriteLine(<span class="str">&quot;{0}/{1}  [Succeeded {2}]&quot;</span>, factor1, factor2, answer);
       }, <span class="kwrd">null</span>);</pre>
<p>This works, but is quite difficult to understand from a conceptual standpoint.&#160; To combat this, the framework added the <a href="http://msdn.microsoft.com/en-us/library/wewwczdw(VS.90).aspx" target="_blank">Event-based Asynchronous Pattern</a>, but it isn’t much easier to understand or author. </p>
<p>Using .NET 4’s new Task&lt;T&gt; class and a continuation, we can dramatically simplify the implementation of the above code, as well as make it much more understandable.&#160; We do this via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.continuewith.aspx" target="_blank">Task.ContinueWith</a> method.&#160; This method will schedule a new Task upon completion of the original task, and provide the original Task (including its Result if it’s a <a href="http://msdn.microsoft.com/en-us/library/dd321424.aspx" target="_blank">Task&lt;T&gt;</a>) as an argument.&#160; Using Task, we can eliminate the delegate, and rewrite this code like so:</p>
<pre class="csharpcode">var background = Task.Factory.StartNew(
    () =&gt;
        {
            <span class="kwrd">int</span> primeFactor1 = 0;
            <span class="kwrd">int</span> primeFactor2 = 0;
            <span class="kwrd">bool</span> result = Factorize(10298312, <span class="kwrd">ref</span> primeFactor1, <span class="kwrd">ref</span> primeFactor2);
            <span class="kwrd">return</span> <span class="kwrd">new </span>{
                           Result = result,
                           Factor1 = primeFactor1,
                           Factor2 = primeFactor2
                       };
        });
background.ContinueWith(task =&gt; Console.WriteLine(<span class="str">&quot;{0}/{1}  [Succeeded {2}]&quot;</span>,
                                 task.Result.Factor1,
                                 task.Result.Factor2,
                                 task.Result.Result));</pre>
<p>This is much simpler to understand, in my opinion.&#160; Here, we’re explicitly asking to start a new task, then <strong>continue</strong> the task with a resulting task.&#160; In our case, our method used ref parameters (this was from the MSDN Sample), so there is a little bit of extra boiler plate involved, but the code is at least easy to understand.</p>
<p>That being said, this isn’t dramatically shorter when compared with our C# 3 port of the MSDN code above.&#160; However, if we were to extend our requirements a bit, we can start to see more advantages to the Task based approach.&#160; For example, supposed we need to report the results in a user interface control instead of reporting it to the Console.&#160; This would be a common operation, but now, we have to think about marshaling our calls back to the user interface.&#160; This is probably going to require calling Control.Invoke or Dispatcher.Invoke within our callback, forcing us to specify a delegate within the delegate.&#160; The maintainability and ease of understanding drops.&#160; However, just as a standard <a href="http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/" target="_blank">Task can be created with a TaskScheduler that uses the UI synchronization context</a>, so too can we continue a task with a specific context.&#160; There are <a href="http://msdn.microsoft.com/en-us/library/dd321307.aspx" target="_blank">Task.ContinueWith method overloads</a> which allow you to provide a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx" target="_blank">TaskScheduler</a>.&#160; This means you can schedule the continuation to run on the UI thread, by simply doing:</p>
<pre class="csharpcode">Task.Factory.StartNew(
    () =&gt;
        {
            <span class="kwrd">int</span> primeFactor1 = 0;
            <span class="kwrd">int</span> primeFactor2 = 0;
            <span class="kwrd">bool</span> result = Factorize(10298312, <span class="kwrd">ref</span> primeFactor1, <span class="kwrd">ref</span> primeFactor2);
            <span class="kwrd">return</span> <span class="kwrd">new</span> {
                           Result = result,
                           Factor1 = primeFactor1,
                           Factor2 = primeFactor2
                       };
        }).ContinueWith(task =&gt; textBox1.Text = <span class="kwrd">string</span>.Format(<span class="str">&quot;{0}/{1}  [Succeeded {2}]&quot;</span>,
                                 task.Result.Factor1,
                                 task.Result.Factor2,
                                 task.Result.Result),
                        TaskScheduler.FromCurrentSynchronizationContext());</pre>
<p>This is far more understandable than the alternative.&#160; By using <a href="http://msdn.microsoft.com/en-us/library/dd321307.aspx" target="_blank">Task.ContinueWith</a> in conjunction with <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.fromcurrentsynchronizationcontext.aspx" target="_blank">TaskScheduler.FromCurrentSynchronizationContext()</a>, we get a simple way to push any work onto a background thread, and update the user interface on the proper UI thread.&#160; This technique works with Windows Presentation Foundation as well as Windows Forms, with no change in methodology.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/04/19/parallelism-in-net-part-17-think-continuations-not-callbacks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 16, Creating Tasks via a TaskFactory</title>
		<link>http://reedcopsey.com/2010/03/26/parallelism-in-net-part-16-creating-tasks-via-a-taskfactory/</link>
		<comments>http://reedcopsey.com/2010/03/26/parallelism-in-net-part-16-creating-tasks-via-a-taskfactory/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 00:51:58 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/03/26/parallelism-in-net-part-16-creating-tasks-via-a-taskfactory/</guid>
		<description><![CDATA[The Task class in the Task Parallel Library supplies a large set of features.&#160; However, when creating the task, and assigning it to a TaskScheduler, and starting the Task, there are quite a few steps involved.&#160; This gets even more cumbersome when multiple tasks are involved.&#160; Each task must be constructed, duplicating any options required, [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> in the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> supplies a large set of features.&#160; However, when <a href="http://reedcopsey.com/2010/03/17/parallelism-in-net-part-14-the-different-forms-of-task/" target="_blank">creating the task</a>, and <a href="http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/" target="_blank">assigning it to a TaskScheduler</a>, and starting the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a>, there are quite a few steps involved.&#160; This gets even more cumbersome when multiple tasks are involved.&#160; Each task must be constructed, duplicating any options required, then started individually, potentially on a specific scheduler.&#160; At first glance, this makes the new <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> seem like more work than <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx" target="_blank">ThreadPool.QueueUserWorkItem</a> in .NET 3.5.</p>
<p>In order to simplify this process, and make Tasks simple to use in simple cases, without sacrificing their power and flexibility, the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> added a new class: <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a>.</p>
<p> <span id="more-263"></span>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory class</a> is intended to “Provide support for creating and scheduling Task objects.”&#160; Its entire purpose is to simplify development when working with <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instances.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> class provides access to the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a> via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.factory(VS.100).aspx" target="_blank">Task.Factory</a> static property.&#160; By default, <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a> uses the <a href="http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/" target="_blank">default TaskScheduler</a> to schedule tasks on a ThreadPool thread.&#160; By using Task.Factory, we can automatically create and start a task in a single “fire and forget” manner, similar to how we did with <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx" target="_blank">ThreadPool.QueueUserWorkItem</a>:</p>
<pre class="csharpcode">Task.Factory.StartNew(() =&gt; <span class="kwrd">this</span>.ExecuteBackgroundWork(myData) );</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This provides us with the same level of simplicity we had with <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx" target="_blank">ThreadPool.QueueUserWorkItem</a>, but even more power.&#160; For example, we can now easily wait on the task:</p>
<pre class="csharpcode"><span class="rem">// Start our task on a background thread</span>
var task = Task.Factory.StartNew(() =&gt; <span class="kwrd">this</span>.ExecuteBackgroundWork(myData) );

<span class="rem">// Do other work on the main thread, </span>
<span class="rem">// while the task above executes in the background</span>
<span class="kwrd">this</span>.ExecuteWorkSynchronously();

<span class="rem">// Wait for the background task to finish</span>
task.Wait();</pre>
<p><a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a> simplifies creation and startup of simple background tasks dramatically.</p>
<p>In addition to using the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a>, it’s often useful to construct a custom <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a>.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory class</a> includes an entire <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.taskfactory(VS.100).aspx" target="_blank">set of constructors</a> which allow you to specify the default configuration for every <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instance created by that factory.&#160; </p>
<p>This is particularly useful when <a href="http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/" target="_blank">using a custom TaskScheduler</a>.&#160; For example, look at the <a href="http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/" target="_blank">sample code for starting a task on the UI thread in Part 15</a>:</p>
<pre class="csharpcode"><span class="rem">// Given the following, constructed on the UI thread</span>
<span class="rem">// TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();</span>

<span class="rem">// When inside a background task, we can do</span>
<span class="kwrd">string</span> status = GetUpdatedStatus();

(<span class="kwrd">new</span> Task(() =&gt;
    {
        statusLabel.Text = status;
    }))
.Start(uiScheduler);</pre>
<p>This is actually quite a bit more complicated than necessary.&#160; When we create the <em>uiScheduler</em> instance, we can use that to construct a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a> that will automatically schedule tasks on the UI thread.&#160; To do that, we’d create the following on our main thread, prior to constructing our background tasks:</p>
<pre class="csharpcode"><span class="rem">// Construct a task scheduler from the current SynchronizationContext (UI thread)</span>
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
<span class="rem">// Construct a new TaskFactory using our UI scheduler</span>
var uiTaskFactory = <span class="kwrd">new</span> TaskFactory(uiScheduler);</pre>
<p>If we do this, when we’re on a background thread, we can use this new <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a> to marshal a Task back onto the UI thread.&#160; Our previous code simplifies to:</p>
<pre class="csharpcode"><span class="rem">// When inside a background task, we can do</span>
<span class="kwrd">string</span> status = GetUpdatedStatus();

<span class="rem">// Update our UI</span>
uiTaskFactory.StartNew( () =&gt; statusLabel.Text = status);</pre>
<p>Notice how much simpler this becomes!&#160; By taking advantage of the convenience provided by a custom <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory(VS.100).aspx" target="_blank">TaskFactory</a>, we can now marshal to set data on the UI thread in a single, clear line of code!</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/03/26/parallelism-in-net-part-16-creating-tasks-via-a-taskfactory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 15, Making Tasks Run: The TaskScheduler</title>
		<link>http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/</link>
		<comments>http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 01:10:46 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/</guid>
		<description><![CDATA[In my introduction to the Task class, I specifically made mention that the Task class does not directly provide it’s own execution.&#160; In addition, I made a strong point that the Task class itself is not directly related to threads or multithreading.&#160; Rather, the Task class is used to implement our decomposition of tasks.&#160; Once [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/" target="_blank">introduction to the Task class</a>, I specifically made mention that the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> does not directly provide it’s own execution.&#160; In addition, I made a strong point that the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> itself is not directly related to threads or multithreading.&#160; Rather, the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> is used to implement our <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">decomposition of tasks</a>.&#160; </p>
<p>Once we’ve implemented our tasks, we need to execute them.&#160; In the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a>, the execution of <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Tasks</a> is handled via an instance of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler class</a>.</p>
<p> <span id="more-249"></span>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler class</a> is an abstract class which provides a single function: it schedules the tasks and executes them within an appropriate context.&#160; This class is the class which actually runs individual <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instances.&#160; The .NET Framework provides two (internal) implementations of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler class</a>.</p>
<p>Since a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a>, based on our <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">decomposition</a>, should be a self-contained piece of code, parallel execution makes sense when executing tasks.&#160; The default implementation of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler class</a>, and the one most often used, is based on the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>.&#160; This can be retrieved via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.default(v=VS.100).aspx" target="_blank">TaskScheduler.Default</a> property, and is, by default, what is used when we just start a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instance with <a href="http://msdn.microsoft.com/en-us/library/dd270682(v=VS.100).aspx" target="_blank">Task.Start()</a>.</p>
<p>Normally, when a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> is started by the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a>, the task will be treated as a single work item, and run on a <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a> thread.&#160; This pools tasks, and provides <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instances all of the advantages of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>, including thread pooling for reduced resource usage, and an upper cap on the number of work items.&#160; In addition, <a href="http://www.developerfusion.com/media/31201/erika-parsons-and-eric-eilebrecht-clr-4-inside-the-thread-pool/" target="_blank">.NET 4 brings us a much improved thread pool</a>, providing work stealing and reduced locking within the thread pool queues.&#160; By using the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a>, our Tasks are run asynchronously on the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>.</p>
<p>There is one notable exception to my above statements when using the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a>.&#160; If a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> is created with the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcreationoptions(v=VS.100).aspx" target="_blank">TaskCreationOptions</a> set to TaskCreationOptions.LongRunning, the default <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> will generate a new thread for that <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a>, at least <a href="http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/8304b44f-0480-488c-93a4-ec419327183b/" target="_blank">in the current implementation</a>.&#160; This is useful for <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Tasks</a> which will persist for most of the lifetime of your application, since it prevents your <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> from starving the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a> of one of it’s work threads.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> provides one other implementation of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> class.&#160; In addition to providing a way to schedule tasks on the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>, the framework allows you to create a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> which works within a specified <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" target="_blank">SynchronizationContext</a>.&#160; This scheduler can be retrieved within a thread that provides a valid <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" target="_blank">SynchronizationContext</a> by calling the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.fromcurrentsynchronizationcontext(v=VS.100).aspx" target="_blank">TaskScheduler.FromCurrentSynchronizationContext()</a> method.</p>
<p>This implementation of <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> is intended for use with user interface development.&#160; <a href="http://msdn.microsoft.com/en-us/library/ms950424.aspx" target="_blank">Windows Forms</a> and <a href="http://msdn.microsoft.com/en-us/library/ms754130.aspx" target="_blank">Windows Presentation Foundation</a> both require any access to user interface controls to occur on the same thread that created the control.&#160; For example, if you want to set the text within a Windows Forms TextBox, and you’re working on a background thread, that UI call must be marshaled back onto the UI thread.&#160; The most common way this is handled depends on the framework being used.&#160; In Windows Forms, <a href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx" target="_blank">Control.Invoke</a> or <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke(v=VS.90).aspx" target="_blank">Control.BeginInvoke</a> is most often used.&#160; In WPF, the equivelent calls are <a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx" target="_blank">Dispatcher.Invoke</a> or <a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.begininvoke(v=VS.90).aspx" target="_blank">Dispatcher.BeginInvoke</a>.</p>
<p>As an example, say we’re working on a background thread, and we want to update a TextBlock in our user interface with a status label.&#160; The code would typically look something like:</p>
<pre class="csharpcode"><span class="rem">// Within background thread work...</span>
<span class="kwrd">string</span> status = GetUpdatedStatus();

Dispatcher.BeginInvoke(DispatcherPriority.Normal,
    <span class="kwrd">new</span> Action( () =&gt;
        {
            statusLabel.Text = status;
        }));

<span class="rem">// Continue on in background method</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This works fine, but forces your method to take a dependency on WPF or Windows Forms.&#160; There is an alternative option, however.&#160; Both Windows Forms and WPF, when initialized, setup a <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" target="_blank">SynchronizationContext</a> in their thread, which is available on the UI thread via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.current(v=VS.90).aspx" target="_blank">SynchronizationContext.Current</a> property.&#160; This context is used by classes such as <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" target="_blank">BackgroundWorker</a> to marshal calls back onto the UI thread in a framework-agnostic manner.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> provides the same functionality via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.fromcurrentsynchronizationcontext(v=VS.100).aspx" target="_blank">TaskScheduler.FromCurrentSynchronizationContext()</a> method.&#160; When setting up our Tasks, as long as we’re working on the UI thread, we can construct a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> via:</p>
<pre class="csharpcode">TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();</pre>
<p>We then can use this scheduler on any thread to marshal data back onto the UI thread.&#160; For example, our code above can then be rewritten as:</p>
<pre class="csharpcode"><span class="kwrd">string</span> status = GetUpdatedStatus();

(<span class="kwrd">new</span> Task(() =&gt;
    {
        statusLabel.Text = status;
    }))
.Start(uiScheduler);

<span class="rem">// Continue on in background method</span></pre>
<p>This is nice since it allows us to write code that isn’t tied to Windows Forms or WPF, but is still fully functional with those technologies.&#160; I’ll discuss even more uses for the <a href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" target="_blank">SynchronizationContext</a> based <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> when I demonstrate task continuations, but even without continuations, this is a very useful construct.</p>
<p>In addition to the two implementations provided by the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a>, it is possible to implement your own <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a>.&#160; The <a href="http://code.msdn.microsoft.com/ParExtSamples" target="_blank">ParallelExtensionsExtras project within the Samples for Parallel Programming</a> provides nine sample <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler(VS.100).aspx" target="_blank">TaskScheduler</a> implementations.&#160; These include schedulers which restrict the maximum number of concurrent tasks, run tasks on a single threaded apartment thread, use a new thread per task, and more.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/03/18/parallelism-in-net-part-15-making-tasks-run-the-taskscheduler/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 14, The Different Forms of Task</title>
		<link>http://reedcopsey.com/2010/03/17/parallelism-in-net-part-14-the-different-forms-of-task/</link>
		<comments>http://reedcopsey.com/2010/03/17/parallelism-in-net-part-14-the-different-forms-of-task/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 00:56:57 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/03/17/parallelism-in-net-part-14-the-different-types-of-tasks/</guid>
		<description><![CDATA[Before discussing Task creation and actual usage in concurrent environments, I will briefly expand upon my introduction of the Task class and provide a short explanation of the distinct forms of Task.&#160; The Task Parallel Library includes four distinct, though related, variations on the Task class. In my introduction to the Task class, I focused [...]]]></description>
			<content:encoded><![CDATA[<p>Before discussing <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> creation and actual usage in concurrent environments, I will briefly expand upon <a href="http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/" target="_blank">my introduction of the Task class</a> and provide a short explanation of the distinct forms of Task.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> includes four distinct, though related, variations on the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a>.</p>
<p> <span id="more-242"></span>In <a href="http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/" target="_blank">my introduction to the Task class</a>, I focused on the most basic version of <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a>.&#160; This version of Task, the standard <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a>, is most often used with an <a href="http://msdn.microsoft.com/en-us/library/system.action(v=VS.100).aspx" target="_blank">Action delegate</a>.&#160; This allows you to implement for each task within the <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">task decomposition</a> as a single delegate.
</p>
<p>Typically, when using the new threading constructs in .NET 4 and the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a>, we use <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expressions</a> to define anonymous methods.&#160; The advantage of using a <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expression</a> is that it allows the <a href="http://msdn.microsoft.com/en-us/library/system.action(v=VS.100).aspx" target="_blank">Action delegate</a> to directly use variables in the calling scope.&#160; This eliminates the need to make separate Task classes for <a href="http://msdn.microsoft.com/en-us/library/018hxwa8(v=VS.100).aspx" target="_blank">Action&lt;T&gt;</a>, <a href="http://msdn.microsoft.com/en-us/library/bb549311(v=VS.100).aspx" target="_blank">Action&lt;T1,T2&gt;</a>, and all of the other Action&lt;…&gt; delegate types.&#160; As an example, suppose we wanted to make a Task to handle the <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank"><em>”Show Splash”</em> task from our earlier decomposition</a>.&#160; Even if this task required parameters, such as a message to display, we could still use an <a href="http://msdn.microsoft.com/en-us/library/system.action(v=VS.100).aspx" target="_blank">Action delegate</a> specified via a lambda:</p>
<pre class="csharpcode"><span class="rem">// Store this as a local variable</span>
<span class="kwrd">string</span> messageForSplashScreen = GetSplashScreenMessage();
<span class="rem">// Create our task</span>
Task showSplashTask = <span class="kwrd">new</span> Task(
    () =&gt;
        {
            <span class="rem">// We can use variables in our outer scope, </span>
            <span class="rem">// as well as methods scoped to our class!</span>
            <span class="kwrd">this</span>.DisplaySplashScreen(messageForSplashScreen);
        });</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->This provides a huge amount of flexibility.&#160; We can use this single form of task for any task which performs an operation, provided the only information we need to track is whether the task has completed successfully or not.&#160; This leads to my first observation:</p>
<p><strong>Use a Task with a System.Action delegate for any task for which no result is generated.</strong></p>
<p>This observation leads to an obvious corollary: we also need a way to define a task which generates a result.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> provides this via the <a href="http://msdn.microsoft.com/en-us/library/dd321424(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt; class</a>.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd321424(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt;</a> subclasses the standard <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a>, providing one additional feature – the ability to return a value back to the user of the task.&#160; This is done by switching from providing an <a href="http://msdn.microsoft.com/en-us/library/system.action(v=VS.100).aspx" target="_blank">Action delegate</a> to providing a <a href="http://msdn.microsoft.com/en-us/library/bb534960(v=VS.100).aspx" target="_blank">Func&lt;TResult&gt; delegate</a>.&#160; If we decompose our problem, and we realize we have one task where its result is required by a future operation, this can be handled via <a href="http://msdn.microsoft.com/en-us/library/dd321424(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt;</a>.&#160; For example, suppose we want to make a task for our <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">“<em>Check for Update</em>” task</a>, we could do:</p>
<pre class="csharpcode">Task&lt;<span class="kwrd">bool</span>&gt; checkForUpdateTask = <span class="kwrd">new</span> Task&lt;<span class="kwrd">bool</span>&gt;(
    () =&gt;
        {
            <span class="kwrd">return</span> <span class="kwrd">this</span>.CheckWebsiteForUpdate();
        });</pre>
<p>
  <br />Later, we would start this task, and perform some other work.&#160; At any point in the future, we could get the value from the <a href="http://msdn.microsoft.com/en-us/library/dd321468(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt;.Result</a> property, which will cause our thread to block until the task has finished processing:</p>
<p></p>
<pre class="csharpcode"><span class="rem">// This uses Task&lt;bool&gt; checkForUpdateTask generated above...</span>
<span class="rem">// Start the task, typically on a background thread</span>
checkForUpdateTask.Start();

<span class="rem">// Do some other work on our current thread</span>
<span class="kwrd">this</span>.DoSomeWork();

<span class="rem">// Discover, from our background task, whether an update is available</span>
<span class="rem">// This will block until our task completes</span>
<span class="kwrd">bool</span> updateAvailable = checkForUpdateTask.Result;</pre>
<p>This leads me to my second observation:</p>
<p><strong>Use a Task&lt;TResult&gt; with a System.Func&lt;TResult&gt; delegate for any task which generates a result.</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> and <a href="http://msdn.microsoft.com/en-us/library/dd321424(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt;</a> provide a much cleaner alternative to the previous <a href="http://msdn.microsoft.com/en-us/library/ms228969(v=VS.90).aspx" target="_blank">Asynchronous Programming design patterns</a> in the .NET framework.&#160; Instead of trying to implement <a href="http://msdn.microsoft.com/en-us/library/system.iasyncresult(v=VS.90).aspx" target="_blank">IAsyncResult</a>, and providing BeginXXX() and EndXXX() methods, implementing an asynchronous programming API can be as simple as creating a method that returns a Task or Task&lt;TResult&gt;.&#160; The client side of the pattern also is dramatically simplified – the client can call a method, then either choose to call <a href="http://msdn.microsoft.com/en-us/library/dd235635(v=VS.100).aspx" target="_blank">task.Wait()</a> or use <a href="http://msdn.microsoft.com/en-us/library/dd321468(v=VS.100).aspx" target="_blank">task.Result</a> when it needs to wait for the operation’s completion.</p>
<p>While this provides a much cleaner model for future APIs, there is quite a bit of infrastructure built around the current <a href="http://msdn.microsoft.com/en-us/library/ms228969(v=VS.90).aspx" target="_blank">Asynchronous Programming design patterns</a>.&#160; In order to provide a model to work with existing APIs, two other forms of Task exist.&#160; There is a constructor for <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> which takes an <a href="http://msdn.microsoft.com/en-us/library/018hxwa8(v=VS.100).aspx" target="_blank">Action&lt;Object&gt;</a> and a state parameter.&#160; In addition, there is a constructor for creating a <a href="http://msdn.microsoft.com/en-us/library/dd321424(v=VS.100).aspx" target="_blank">Task&lt;TResult&gt;</a> which takes a <a href="http://msdn.microsoft.com/en-us/library/bb549151(v=VS.100).aspx" target="_blank">Func&lt;Object, TResult&gt;</a> as well as a state parameter.&#160; When using these constructors, the state parameter is stored in the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.asyncstate(v=VS.100).aspx" target="_blank">Task.AsyncState property</a>.</p>
<p>While these two overloads exist, and are usable directly, I strongly recommend avoiding this for new development.&#160; The two forms of Task which take an object state parameter exist primarily for <a href="http://msdn.microsoft.com/en-us/library/dd997423(v=VS.100).aspx" target="_blank">interoperability with traditional .NET Asynchronous Programming methodologies</a>.&#160; Using <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expressions</a> to capture variables from the scope of the creator is a much cleaner approach than using the untyped state parameters, since <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expressions</a> provide full type safety without introducing new variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/03/17/parallelism-in-net-part-14-the-different-forms-of-task/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 13, Introducing the Task class</title>
		<link>http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/</link>
		<comments>http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 20:47:30 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/</guid>
		<description><![CDATA[Once we’ve used a task-based decomposition to decompose a problem, we need a clean abstraction usable to implement the resulting decomposition.&#160; Given that task decomposition is founded upon defining discrete tasks, .NET 4 has introduced a new API for dealing with task related issues, the aptly named Task class. The Task class is a wrapper [...]]]></description>
			<content:encoded><![CDATA[<p>Once we’ve used a <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">task-based decomposition</a> to decompose a problem, we need a clean abstraction usable to implement the resulting decomposition.&#160; Given that <a href="http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/" target="_blank">task decomposition is founded upon defining discrete tasks</a>, .NET 4 has introduced a new API for dealing with task related issues, the aptly named <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a>.</p>
<p> <span id="more-217"></span>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> is a wrapper for a delegate representing a single, discrete task within your decomposition.&#160; We will go into various methods of construction for tasks later, but, when reduced to its fundamentals, an instance of a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> is nothing more than a wrapper around a delegate with some utility functionality added.&#160; </p>
<p>In order to fully understand the Task class within the new <a href="http://msdn.microsoft.com/en-us/library/dd460717(v=VS.100).aspx" target="_blank">Task Parallel Library</a>, it is important to realize that a task really is just a delegate – nothing more.&#160; In particular, note that I never mentioned threading or parallelism in my description of a <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a>.&#160; Although the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class </a>exists in the new <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks(v=VS.100).aspx" target="_blank">System.Threading.Tasks</a> namespace:</p>
<p><strong>Tasks are not directly related to threads or multithreading.</strong></p>
<p>Of course, <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task</a> instances will typically be used in our implementation of concurrency within an application, but the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> itself does not provide the concurrency used.&#160; The Task API supports using Tasks in an entirely single threaded, synchronous manner.</p>
<p>Tasks are very much like standard delegates.&#160; You can execute a task synchronously via <a href="http://msdn.microsoft.com/en-us/library/dd321435(v=VS.100).aspx" target="_blank">Task.RunSynchronously()</a>, or you can use <a href="http://msdn.microsoft.com/en-us/library/dd270682(v=VS.100).aspx" target="_blank">Task.Start()</a> to schedule a task to run, typically asynchronously.&#160; This is very similar to using delegate.Invoke to execute a delegate synchronously, or using delegate.BeginInvoke to execute it asynchronously.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> adds some nice functionality on top of a standard delegate which improves usability in both synchronous and multithreaded environments.</p>
<p>The first addition provided by Task is a means of handling cancellation via the new <a href="http://msdn.microsoft.com/en-us/library/dd997364(VS.100).aspx" target="_blank">unified cancellation mechanism of .NET 4</a>.&#160; If the wrapped delegate within a Task raises an <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception(v=VS.100).aspx" target="_blank">OperationCanceledException</a> during it’s operation, which is typically generated via calling <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.throwifcancellationrequested(v=VS.100).aspx" target="_blank">ThrowIfCancellationRequested</a> on a <a href="http://msdn.microsoft.com/en-us/library/dd384802(v=VS.100).aspx" target="_blank">CancellationToken</a>, or if the <a href="http://msdn.microsoft.com/en-us/library/dd384802(v=VS.100).aspx" target="_blank">CancellationToken</a> used to construct a Task instance is flagged as canceled, the Task’s <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.iscanceled(v=VS.100).aspx" target="_blank">IsCanceled property</a> will be set to true automatically.&#160; This provides a clean way to determine whether a Task has been canceled, often without requiring specific exception handling.</p>
<p>Tasks also provide a clean API which can be used for waiting on a task.&#160; Although the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class explicitly implements IAsyncResult</a>, Tasks provide a nicer usage model than the <a href="http://msdn.microsoft.com/en-us/library/ms228963.aspx" target="_blank">traditional .NET Asynchronous Programming Model</a>.&#160; Instead of needing to track an IAsyncResult handle, you can just directly call <a href="http://msdn.microsoft.com/en-us/library/dd235635(v=VS.100).aspx" target="_blank">Task.Wait()</a> to block until a Task has completed.&#160; Overloads exist for providing a timeout, a CancellationToken, or both to prevent waiting indefinitely.&#160; In addition, the <a href="http://msdn.microsoft.com/en-us/library/dd235678(v=VS.100).aspx" target="_blank">Task class</a> provides static methods for waiting on multiple tasks – <a href="http://msdn.microsoft.com/en-us/library/dd270695(v=VS.100).aspx" target="_blank">Task.WaitAll</a> and <a href="http://msdn.microsoft.com/en-us/library/dd270672(v=VS.100).aspx" target="_blank">Task.WaitAny</a>, again with overloads providing time out options.&#160; This provides a very simple, clean API for waiting on single or multiple tasks.</p>
<p>Finally, Tasks provide a much nicer model for Exception handling.&#160; If the delegate wrapped within a Task raises an exception, the exception will automatically get wrapped into an <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception(v=VS.100).aspx" target="_blank">AggregateException</a> and exposed via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.exception(v=VS.100).aspx" target="_blank">Task.Exception property</a>.&#160; This exception is stored with the Task directly, and does not tear down the application.&#160; Later, when <a href="http://msdn.microsoft.com/en-us/library/dd235635(v=VS.100).aspx" target="_blank">Task.Wait()</a> (or <a href="http://msdn.microsoft.com/en-us/library/dd270695(v=VS.100).aspx" target="_blank">Task.WaitAll</a> or <a href="http://msdn.microsoft.com/en-us/library/dd270672(v=VS.100).aspx" target="_blank">Task.WaitAny</a>) is called on this task, an AggregateException will be raised at that point if any of the tasks raised an exception.&#160; </p>
<p>For example, suppose we have the following code:</p>
<pre class="csharpcode">Task taskOne = <span class="kwrd">new</span> Task(
                       () =&gt;
                       {
                           <span class="kwrd">throw</span> <span class="kwrd">new</span> ApplicationException(<span class="str">&quot;Random Exception!&quot;</span>);
                       });
Task taskTwo = <span class="kwrd">new</span> Task(
                       () =&gt;
                       {
                           <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;Different exception here&quot;</span>);
                       });

<span class="rem">// Start the tasks</span>
taskOne.Start();
taskTwo.Start();

<span class="kwrd">try</span>
{
    Task.WaitAll(<span class="kwrd">new</span>[] { taskOne, taskTwo });
}
<span class="kwrd">catch</span> (AggregateException e)
{
    Console.WriteLine(e.InnerExceptions.Count);
    <span class="kwrd">foreach</span> (var inner <span class="kwrd">in</span> e.InnerExceptions)
        Console.WriteLine(inner.Message);
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, our routine will print:</p>
<pre>2
Different exception here
Random Exception!</pre>
<p>Note that we had two separate tasks, each of which raised two distinctly different types of exceptions.&#160; We can handle this cleanly, with very little code, in a much nicer manner than the <a href="http://msdn.microsoft.com/en-us/library/ms228969(v=VS.90).aspx" target="_blank">Asynchronous Programming API</a>.&#160; We no longer need to handle <a href="http://msdn.microsoft.com/en-us/library/system.reflection.targetinvocationexception.aspx" target="_blank">TargetInvocationException</a> or worry about implementing the <a href="http://msdn.microsoft.com/en-us/library/wewwczdw(v=VS.90).aspx" target="_blank">Event-based Asynchronous Pattern</a> properly by setting the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.error(v=VS.90).aspx" target="_blank">AsyncCompletedEventArgs.Error</a> property.&#160; Instead, we just raise our exception as normal, and handle <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception(v=VS.100).aspx" target="_blank">AggregateException</a> in a single location in our calling code.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/03/15/parallelism-in-net-part-13-introducing-the-task-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 12, More on Task Decomposition</title>
		<link>http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/</link>
		<comments>http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 01:09:47 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/</guid>
		<description><![CDATA[Many tasks can be decomposed using a Data Decomposition approach, but often, this is not appropriate.&#160; Frequently, decomposing the problem into distinctive tasks that must be performed is a more natural abstraction. However, as I mentioned in Part 1, Task Decomposition tends to be a bit more difficult than data decomposition, and can require a [...]]]></description>
			<content:encoded><![CDATA[<p>Many tasks can be <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decomposed</a> using a Data Decomposition approach, but often, this is not appropriate.&#160; Frequently, decomposing the problem into distinctive tasks that must be performed is a more natural abstraction.</p>
<p>However, as I mentioned in <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">Part 1</a>, Task Decomposition tends to be a bit more difficult than data decomposition, and can require a bit more effort.&#160; Before we being parallelizing our algorithm based on the tasks being performed, we need to decompose our problem, and take special care of certain considerations such as ordering and grouping of tasks.</p>
<p> <span id="more-216"></span>
<p>Up to this point in this series, I’ve focused on parallelization techniques which are most appropriate when a problem space can be decomposed by data.&#160; Using <a href="http://msdn.microsoft.com/en-us/library/dd460688(v=VS.100).aspx" target="_blank">PLINQ</a> and the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(v=VS.100).aspx" target="_blank">Parallel class</a>, I’ve shown how problem spaces where there is a collection of data, and each element needs to be processed, can potentially be parallelized.</p>
<p>However, there are many other routines where this is not appropriate.&#160; Often, instead of working on a collection of data, there is a single piece of data which must be processed using an algorithm or series of algorithms.&#160; Here, there is no collection of data, but there may still be opportunities for parallelism.</p>
<p>As I mentioned before, in cases like this, the approach is to look at your overall routine, and <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decompose your problem space based on tasks</a>.&#160; The idea here is to look for discrete “tasks,” individual pieces of work which can be conceptually thought of as a single operation.</p>
<p>Let’s revisit the example I used in <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">Part 1</a>, an application startup path.&#160; Say we want our program, at startup, to do a bunch of individual actions, or “tasks”.&#160; The following is our list of duties we must perform right at startup:</p>
<ul>
<li>Display a splash screen </li>
<li>Request a license from our license manager </li>
<li>Check for an update to the software from our web server </li>
<li>If an update is available, download it </li>
<li>Setup our menu structure based on our current license </li>
<li>Open and display our main, welcome Window </li>
<li>Hide the splash screen </li>
</ul>
<p><strong>The first step in Task Decomposition is breaking up the problem space into discrete tasks.</strong></p>
<p>This, naturally, can be abstracted as seven discrete tasks.&#160; In the serial version of our program, if we were to diagram this, the general process would appear as:</p>
<p><a href="http://reedcopsey.com/blog/wp-content/uploads/2010/03/serial_w.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="serial_w" border="0" alt="serial_w" src="http://reedcopsey.com/blog/wp-content/uploads/2010/03/serial_w_thumb.png" width="176" height="535" /></a>These tasks, obviously, provide some opportunities for parallelism.&#160; Before we can parallelize this routine, we need to analyze these tasks, and find any dependencies between tasks.&#160; In this case, our dependencies include:</p>
<ul>
<li>The splash screen must be displayed first, and as quickly as possible.</li>
<li>We can’t download an update before we see whether one exists.</li>
<li>Our menu structure depends on our license, so we must check for the license before setting up the menus.</li>
<li>Since our welcome screen will notify the user of an update, we can’t show it until we’ve downloaded the update.</li>
<li>Since our welcome screen includes menus that are customized based off the licensing, we can’t display it until we’ve received a license.</li>
<li>We can’t hide the splash until our welcome screen is displayed.</li>
</ul>
<p>By listing our dependencies, we start to see the natural ordering that must occur for the tasks to be processed correctly.</p>
<p><strong>The second step in Task Decomposition is determining the dependencies between tasks, and ordering tasks based on their dependencies.</strong></p>
<p>Looking at these tasks, and looking at all the dependencies, we quickly see that even a simple decomposition such as this one can get quite complicated.&#160; In order to simplify the problem of defining the dependencies, it’s often a useful practice to group our tasks into larger, discrete tasks.&#160; The goal when grouping tasks is that you want to make each task “group” have as few dependencies as possible to other tasks or groups, and then work out the dependencies within that group.&#160; Typically, this works best when any external dependency is based on the “last” task within the group when it’s ordered, although that is not a firm requirement.&#160; This process is often called <strong>Grouping Tasks</strong>.&#160; In our case, we can easily group together tasks, effectively turning this into four discrete task groups:</p>
<p><strong>1. Show our splash screen – </strong></p>
<p>This needs to be left as its own task.&#160; First, multiple things depend on this task, mainly because we want this to start before any other action, and start as quickly as possible.</p>
<p><strong>2. Check for Update and Download the Update if it Exists -</strong></p>
<p>These two tasks logically group together.&#160; We know we only download an update if the update exists, so that naturally follows.&#160; This task has one dependency as an input, and other tasks only rely on the final task within this group.</p>
<p><strong>3. Request a License, and then Setup the Menus &#8211; </strong></p>
<p>Here, we can group these two tasks together.&#160; Although we mentioned that our welcome screen depends on the license returned, it also depends on setting up the menu, which is the final task here.&#160; Setting up our menus cannot happen until after our license is requested.&#160; By grouping these together, we further reduce our problem space.</p>
<p><strong>4. Display welcome and hide splash -</strong></p>
<p>Finally, we can display our welcome window and hide our splash screen.&#160; This task group depends on all three previous task groups – it cannot happen until all three of the previous groups have completed.</p>
<p>By grouping the tasks together, we reduce our problem space, and can naturally see a pattern for how this process can be parallelized.&#160; The diagram below shows one approach:</p>
<p><a href="http://reedcopsey.com/blog/wp-content/uploads/2010/03/parallel_w.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="parallel_w" border="0" alt="parallel_w" src="http://reedcopsey.com/blog/wp-content/uploads/2010/03/parallel_w_thumb.png" width="522" height="518" /></a> </p>
<p>The orange boxes show each task group, with each task represented within.&#160; We can, now, effectively take these tasks, and run a large portion of this process in parallel, including the portions which may be the most time consuming.&#160; We’ve now created two parallel paths which our process execution can follow, hopefully speeding up the application startup time dramatically.</p>
<p>The main point to remember here is that, when decomposing your problem space by tasks, you need to:</p>
<ul>
<li>Define each discrete action as an individual Task</li>
<li>Discover dependencies between your tasks</li>
<li>Group tasks based on their dependencies</li>
<li>Order the tasks and groups of tasks</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/03/05/parallelism-in-net-part-12-more-on-task-decomposition/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 11, Divide and Conquer via Parallel.Invoke</title>
		<link>http://reedcopsey.com/2010/02/26/parallelism-in-net-part-11-divide-and-conquer-via-parallel-invoke/</link>
		<comments>http://reedcopsey.com/2010/02/26/parallelism-in-net-part-11-divide-and-conquer-via-parallel-invoke/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 00:00:31 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/02/26/parallelism-in-net-part-11-divide-and-conquer-via-parallel-invoke/</guid>
		<description><![CDATA[Many algorithms are easily written to work via recursion.&#160; For example, most data-oriented tasks where a tree of data must be processed are much more easily handled by starting at the root, and recursively “walking” the tree.&#160; Some algorithms work this way on flat data structures, such as arrays, as well.&#160; This is a form [...]]]></description>
			<content:encoded><![CDATA[<p>Many algorithms are easily written to work via <a href="http://en.wikipedia.org/wiki/Recursion" target="_blank">recursion</a>.&#160; For example, most data-oriented tasks where a tree of data must be processed are much more easily handled by starting at the root, and recursively “walking” the tree.&#160; Some algorithms work this way on flat data structures, such as arrays, as well.&#160; This is a form of <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a>: an <a href="http://en.wikipedia.org/wiki/Algorithm_design" target="_blank">algorithm design</a> which is based around breaking up a set of work recursively, “dividing” the total work in each recursive step, and “conquering” the work when the remaining work is small enough to be solved easily.</p>
<p>Recursive algorithms, especially ones based on a form of <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a>, are often a very good candidate for parallelization.</p>
<p> <span id="more-209"></span>
<p>This is apparent from a common sense standpoint.&#160; Since we’re dividing up the total work in the algorithm, we have an obvious, <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/" target="_blank">built-in partitioning scheme</a>.&#160; Once partitioned, the data can be worked upon independently, so there is good, clean isolation of data.</p>
<p>Implementing this type of algorithm is fairly simple.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(VS.100).aspx" target="_blank">Parallel class</a> in .NET 4 includes a method suited for this type of operation: <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.invoke(VS.100).aspx" target="_blank">Parallel.Invoke</a>.&#160; This method works by taking any number of delegates defined as an <a href="http://msdn.microsoft.com/en-us/library/system.action(VS.100).aspx" target="_blank">Action</a>, and operating them all in parallel.&#160; The method returns when every delegate has completed:</p>
<pre class="csharpcode">Parallel.Invoke(
    () =&gt;
        {
            Console.WriteLine(<span class="str">&quot;Action 1 executing in thread {0}&quot;</span>,
                                                 Thread.CurrentThread.ManagedThreadId);
        },
    () =&gt;
        {
            Console.WriteLine(<span class="str">&quot;Action 2 executing in thread {0}&quot;</span>,
                                                 Thread.CurrentThread.ManagedThreadId);
        },
    () =&gt;
        {
            Console.WriteLine(<span class="str">&quot;Action 3 executing in thread {0}&quot;</span>,
                                                 Thread.CurrentThread.ManagedThreadId);
        }
    );</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Running this simple example demonstrates the ease of using this method.&#160; For example, on my system, I get three separate thread IDs when running the above code.&#160; By allowing any number of delegates to be executed directly, concurrently, the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.invoke(VS.100).aspx" target="_blank">Parallel.Invoke</a> method provides us an easy way to parallelize any algorithm based on <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a>.&#160; We can divide our work in each step, and execute each task in parallel, recursively.</p>
<p>For example, suppose we wanted to implement our own <a href="http://en.wikipedia.org/wiki/Quicksort" target="_blank">quicksort</a> routine.&#160; The <a href="http://en.wikipedia.org/wiki/Quicksort" target="_blank">quicksort</a> algorithm can be designed based on <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a>.&#160; In each iteration, we pick a pivot point, and use that to partition the total array.&#160; We swap the elements around the pivot, then recursively sort the lists on each side of the pivot.&#160; </p>
<p>For example, let’s look at this simple, sequential implementation of <a href="http://en.wikipedia.org/wiki/Quicksort" target="_blank">quicksort</a>:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> QuickSort&lt;T&gt;(T[] array) <span class="kwrd">where</span> T : IComparable&lt;T&gt;
{
    QuickSortInternal(array, 0, array.Length - 1);
}

<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> QuickSortInternal&lt;T&gt;(T[] array, <span class="kwrd">int</span> left, <span class="kwrd">int</span> right)
    <span class="kwrd">where</span> T : IComparable&lt;T&gt;
{
    <span class="kwrd">if</span> (left &gt;= right)
    {
        <span class="kwrd">return</span>;
    }

    SwapElements(array, left, (left + right) / 2);
    <span class="kwrd">int</span> last = left;
    <span class="kwrd">for</span> (<span class="kwrd">int</span> current = left + 1; current &lt;= right; ++current)
    {
        <span class="kwrd">if</span> (array[current].CompareTo(array[left]) &lt; 0)
        {
            ++last;
            SwapElements(array, last, current);
        }
    }

    SwapElements(array, left, last);

    QuickSortInternal(array, left, last - 1);
    QuickSortInternal(array, last + 1, right);
}

<span class="kwrd">static</span> <span class="kwrd">void</span> SwapElements&lt;T&gt;(T[] array, <span class="kwrd">int</span> i, <span class="kwrd">int</span> j)
{
    T temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}</pre>
<p>Here, we implement <a href="http://en.wikipedia.org/wiki/Quicksort" target="_blank">the quicksort algorithm</a> in a very common, <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a> approach.&#160; Running this against the built-in <a href="http://msdn.microsoft.com/en-us/library/system.array.sort.aspx" target="_blank">Array.Sort</a> routine shows that we get the exact same answers (although the framework’s sort routine is slightly faster).&#160; On my system, for example, I can use framework’s sort to sort ten million random doubles in about 7.3s, and this implementation takes about 9.3s on average.</p>
<p>Looking at this routine, though, there is a clear opportunity to parallelize.&#160; At the end of QuickSortInternal, we recursively call into QuickSortInternal with each partition of the array after the pivot is chosen.&#160; This can be rewritten to use <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.invoke(VS.100).aspx" target="_blank">Parallel.Invoke</a> by simply changing it to:</p>
<pre class="csharpcode">    <span class="rem">// Code above is unchanged...</span>
    SwapElements(array, left, last);

    Parallel.Invoke(
        () =&gt; QuickSortInternal(array, left, last - 1),
        () =&gt; QuickSortInternal(array, last + 1, right)
    );
}</pre>
<p>This routine will now run in parallel.&#160; When executing, we now see the CPU usage across all cores spike while it executes.&#160; </p>
<p>However, there is a significant problem here – by parallelizing this routine, we took it from an execution time of 9.3s to an execution time of approximately <strong>14 seconds!</strong>&#160; We’re using more resources as seen in the CPU usage, but the overall result is a dramatic slowdown in overall processing time.</p>
<p>This occurs because parallelization adds overhead.&#160; Each time we split this array, we spawn two new tasks to parallelize this algorithm!&#160; This is far, far too many tasks for our cores to operate upon at a single time.&#160; In effect, we’re “over-parallelizing” this routine.&#160; This is a common problem when working with <a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank">divide and conquer</a> algorithms, and leads to an important observation:</p>
<p><strong>When parallelizing a recursive routine, take special care not to add more tasks than necessary to fully utilize your system.</strong></p>
<p>This can be done with a few different approaches, in this case.&#160; Typically, the way to handle this is to stop parallelizing the routine at a certain point, and revert back to the serial approach.&#160; Since the first few recursions will all still be parallelized, our “deeper” recursive tasks will be running in parallel, and can take full advantage of the machine.&#160; This also dramatically reduces the overhead added by parallelizing, since we’re only adding overhead for the first few recursive calls.&#160; </p>
<p>There are two basic approaches we can take here.&#160; The first approach would be to look at the total work size, and if it’s smaller than a specific threshold, revert to our serial implementation.&#160; In this case, we could just check right-left, and if it’s under a threshold, call the methods directly instead of using Parallel.Invoke.</p>
<p>The second approach is to track how “deep” in the “tree” we are currently at, and if we are below some number of levels, stop parallelizing.&#160; This approach is a more general-purpose approach, since it works on routines which parse trees as well as routines working off of a single array, but may not work as well if a poor partitioning strategy is chosen or the tree is not balanced evenly.</p>
<p>This can be written very easily.&#160; If we pass a <em>maxDepth</em> parameter into our internal routine, we can restrict the amount of times we parallelize by changing the recursive call to:</p>
<pre class="csharpcode"><span class="rem">// Code above is unchanged...</span>
SwapElements(array, left, last);

<span class="kwrd">if</span> (maxDepth &lt; 1)
{
    QuickSortInternal(array, left, last - 1, maxDepth);
    QuickSortInternal(array, last + 1, right, maxDepth);
}
<span class="kwrd">else</span>
{
    --maxDepth;
    Parallel.Invoke(
        () =&gt; QuickSortInternal(array, left, last - 1, maxDepth),
        () =&gt; QuickSortInternal(array, last + 1, right, maxDepth));
}</pre>
<p>We no longer allow this to parallelize indefinitely – only to a specific depth, at which time we revert to a serial implementation.&#160; By starting the routine with a maxDepth equal to <a href="http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx" target="_blank">Environment.ProcessorCount</a>, we can restrict the total amount of parallel operations significantly, but still provide adequate work for each processing core.</p>
<p>With this final change, my timings are much better.&#160; On average, I get the following timings:</p>
<ul>
<li>Framework via Array.Sort: 7.3 seconds </li>
<li>Serial Quicksort Implementation: 9.3 seconds </li>
<li>Naive Parallel Implementation: 14 seconds </li>
<li>Parallel Implementation Restricting Depth: <strong>4.7 seconds</strong> </li>
</ul>
<p>Finally, we are now faster than the framework’s Array.Sort implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/02/26/parallelism-in-net-part-11-divide-and-conquer-via-parallel-invoke/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 10, Cancellation in PLINQ and the Parallel class</title>
		<link>http://reedcopsey.com/2010/02/17/parallelism-in-net-part-10-cancellation-in-plinq-and-the-parallel-class/</link>
		<comments>http://reedcopsey.com/2010/02/17/parallelism-in-net-part-10-cancellation-in-plinq-and-the-parallel-class/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 01:35:29 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/02/17/parallelism-in-net-part-10-cancellation-in-plinq-and-the-parallel-class/</guid>
		<description><![CDATA[Many routines are parallelized because they are long running processes.&#160; When writing an algorithm that will run for a long period of time, its typically a good practice to allow that routine to be cancelled.&#160; I previously discussed terminating a parallel loop from within, but have not demonstrated how a routine can be cancelled from [...]]]></description>
			<content:encoded><![CDATA[<p>Many routines are parallelized because they are long running processes.&#160; When writing an algorithm that will run for a long period of time, its typically a good practice to allow that routine to be cancelled.&#160; I previously discussed <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/" target="_blank">terminating a parallel loop from within</a>, but have not demonstrated how a routine can be cancelled from the caller’s perspective.&#160; Cancellation in <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> and the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> is handled through a new, <a href="http://msdn.microsoft.com/en-us/library/dd997364(VS.100).aspx" target="_blank">unified cooperative cancellation model</a> introduced with .NET 4.0.</p>
<p> <span id="more-206"></span>
<p>Cancellation in .NET 4 is based around a new, lightweight struct called <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.100).aspx" target="_blank">CancellationToken</a>.&#160; A <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.100).aspx" target="_blank">CancellationToken</a> is a small, thread-safe value type which is generated via a <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource(VS.100).aspx" target="_blank">CancellationTokenSource</a>.&#160; There are many goals which led to this design.&#160; For our purposes, we will focus on a couple of specific design decisions:</p>
<ul>
<li>Cancellation is cooperative.&#160; A calling method can request a cancellation, but it’s up to the processing routine to terminate &#8211; it is not forced.</li>
<li>Cancellation is consistent.&#160; A single method call requests a cancellation on every copied CancellationToken in the routine.</li>
</ul>
<p>Let’s begin by looking at how we can cancel a <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> query.&#160; Supposed we wanted to provide the option to cancel our <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/" target="_blank">query from Part 6</a>:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection
                .AsParallel()
                .Min(item =&gt; item.PerformComputation());</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>We would rewrite this to allow for cancellation by adding a call to <a href="http://msdn.microsoft.com/en-us/library/dd413259(VS.100).aspx" target="_blank">ParallelEnumerable.WithCancellation</a> as follows:</p>
<pre class="csharpcode">var cts = <span class="kwrd">new</span> CancellationTokenSource();

<span class="rem">// Pass cts here to a routine that could, </span>
<span class="rem">// in parallel, request a cancellation</span>

<span class="kwrd">try</span>
{
    <span class="kwrd">double</span> min = collection
                    .AsParallel()
                    .WithCancellation(cts.Token)
                    .Min(item =&gt; item.PerformComputation());
}
<span class="kwrd">catch</span> (OperationCanceledException e)
{
    <span class="rem">// Query was cancelled before it finished</span>
}</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, if the user calls <a href="http://msdn.microsoft.com/en-us/library/dd321955(VS.100).aspx" target="_blank">cts.Cancel()</a> before the PLINQ query completes, the query will stop processing, and an <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx" target="_blank">OperationCanceledException</a> will be raised.&#160; </p>
<p>Be aware, however, that cancellation will not be instantaneous.&#160; When cts.Cancel() is called, the query will only stop after the current item.PerformComputation() elements all finish processing.&#160; cts.Cancel() will prevent PLINQ from scheduling a new task for a new element, but will not stop items which are currently being processed.&#160; This goes back to the first goal I mentioned – Cancellation is cooperative.&#160; Here, we’re requesting the cancellation, but it’s up to PLINQ to terminate.</p>
<p>If we wanted to allow cancellation to occur within our routine, we would need to change our routine to accept a <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.100).aspx" target="_blank">CancellationToken</a>, and modify it to handle this specific case:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">void</span> PerformComputation(CancellationToken token)
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> i=0; i&lt;<span class="kwrd">this</span>.iterations; ++i)
    {
        <span class="rem">// Add a check to see if we've been canceled</span>
        <span class="rem">// If a cancel was requested, we'll throw here</span>
        token.ThrowIfCancellationRequested();

        <span class="rem">// Do our processing now</span>
        <span class="kwrd">this</span>.RunIteration(i);
    }
}</pre>
<p>With this overload of PerformComputation, each internal iteration checks to see if a cancellation request was made, and will throw an <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx" target="_blank">OperationCanceledException</a> at that point, instead of waiting until the method returns.&#160; This is good, since it allows us, as developers, to plan for cancellation, and terminate our routine in a clean, safe state. </p>
<p>This is handled by changing our PLINQ query to:</p>
<pre class="csharpcode"><span class="kwrd">try</span>
{
    <span class="kwrd">double</span> min = collection
                    .AsParallel()
                    .WithCancellation(cts.Token)
                    .Min(item =&gt; item.PerformComputation(cts.Token));
}
<span class="kwrd">catch</span> (OperationCanceledException e)
{
    <span class="rem">// Query was cancelled before it finished</span>
}</pre>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is very good about handling this exception, as well.&#160; There is a very good chance that multiple items will raise this exception, since the entire purpose of PLINQ is to have multiple items be processed concurrently.&#160; <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> will take all of the <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx" target="_blank">OperationCanceledException</a> instances raised within these methods, and merge them into a single <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx" target="_blank">OperationCanceledException</a> in the call stack.&#160; This is done internally because we added the call to <a href="http://msdn.microsoft.com/en-us/library/dd413259(VS.100).aspx" target="_blank">ParallelEnumerable.WithCancellation</a>.</p>
<p>If, however, a different exception is raised by any of the elements, the <a href="http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx" target="_blank">OperationCanceledException</a> as well as the other Exception will be merged into a single <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception(VS.100).aspx" target="_blank">AggregateException</a>.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> uses the same cancellation model, as well.&#160; Here, we supply our <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.100).aspx" target="_blank">CancellationToken</a> as part of the <a href="http://reedcopsey.com/2010/02/11/parallelism-in-net-part-9-configuration-in-plinq-and-tpl/" target="_blank">configuration</a>.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions(VS.100).aspx" target="_blank">ParallelOptions class</a> contains a property for the <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken(VS.100).aspx" target="_blank">CancellationToken</a>.&#160; This allows us to cancel a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for(VS.100).aspx" target="_blank">Parallel.For</a> or <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach(VS.100).aspx" target="_blank">Parallel.ForEach</a> routine in a very similar manner to our PLINQ query.&#160; As an example, we could rewrite our <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">Parallel.ForEach loop from Part 2</a> to support cancellation by changing it to:</p>
<pre class="csharpcode"><span class="kwrd">try</span>
{
    var cts = <span class="kwrd">new</span> CancellationTokenSource();
    var options = <span class="kwrd">new</span> ParallelOptions()
                      {
                          CancellationToken = cts.Token
                      };
    Parallel.ForEach(customers, options, customer =&gt;
    {
        <span class="rem">// Run some process that takes some time...</span>
        DateTime lastContact = theStore.GetLastContact(customer);
        TimeSpan timeSinceContact = DateTime.Now - lastContact;

        <span class="rem">// Check for cancellation here</span>
        options.CancellationToken.ThrowIfCancellationRequested();

        <span class="rem">// If it's been more than two weeks, send an email, and update...</span>
        <span class="kwrd">if</span> (timeSinceContact.Days &gt; 14)
        {
           theStore.EmailCustomer(customer);
           customer.LastEmailContact = DateTime.Now;
        }
    });
}
<span class="kwrd">catch</span> (OperationCanceledException e)
{
    <span class="rem">// The loop was cancelled</span>
}</pre>
<p>Notice that here we use the same approach taken in <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a>.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> will automatically handle our cancellation in the same manner as PLINQ, providing a clean, unified model for cancellation of any parallel routine.&#160; The TPL performs the same aggregation of the cancellation exceptions as PLINQ, as well, which is why a single exception handler for OperationCanceledException will cleanly handle this scenario.&#160; This works because we’re using the same CancellationToken provided in the ParallelOptions.&#160; If a different exception was thrown by one thread, or a CancellationToken from a different CancellationTokenSource was used to raise our exception, we would instead receive all of our individual exceptions merged into one <a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception(VS.100).aspx" target="_blank">AggregateException</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/02/17/parallelism-in-net-part-10-cancellation-in-plinq-and-the-parallel-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 9, Configuration in PLINQ and TPL</title>
		<link>http://reedcopsey.com/2010/02/11/parallelism-in-net-part-9-configuration-in-plinq-and-tpl/</link>
		<comments>http://reedcopsey.com/2010/02/11/parallelism-in-net-part-9-configuration-in-plinq-and-tpl/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 01:12:42 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/02/11/parallelism-in-net-part-9-configuration-in-plinq-and-tpl/</guid>
		<description><![CDATA[Parallel LINQ and the Task Parallel Library contain many options for configuration.&#160; Although the default configuration options are often ideal, there are times when customizing the behavior is desirable.&#160; Both frameworks provide full configuration support. When working with Data Parallelism, there is one primary configuration option we often need to control – the number of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">Parallel LINQ</a> and the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> contain many options for configuration.&#160; Although the default configuration options are often ideal, there are times when customizing the behavior is desirable.&#160; Both frameworks provide full configuration support.</p>
<p> <span id="more-203"></span>
<p>When working with <a href="http://msdn.microsoft.com/en-us/library/dd537608(VS.100).aspx">Data Parallelism</a>, there is one primary configuration option we often need to control – the number of threads we want the system to use when parallelizing our routine.&#160; By default, PLINQ and the TPL both use the ThreadPool to schedule tasks.&#160; Given the <a href="http://www.danielmoth.com/Blog/2008/11/new-and-improved-clr-4-thread-pool.html" target="_blank">major improvements in the ThreadPool in CLR 4</a>, this default behavior is often ideal.&#160; </p>
<p>However, there are times that the default behavior is not appropriate.&#160; For example, if you are working on multiple threads simultaneously, and want to schedule parallel operations from within both threads, you might want to consider restricting each parallel operation to using a subset of the processing cores of the system.&#160; Not doing this might over-parallelize your routine, which leads to inefficiencies from having too many context switches.</p>
<p>In the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a>, configuration is handled via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions(VS.100).aspx" target="_blank">ParallelOptions class</a>.&#160; All of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel_methods(VS.100).aspx" target="_blank">methods of the Parallel class</a> have an overload which accepts a <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions(VS.100).aspx" target="_blank">ParallelOptions</a> argument.</p>
<p>We configure the Parallel class by setting the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(VS.100).aspx" target="_blank">ParallelOptions.MaxDegreeOfParallelism</a> property.&#160; For example, let’s revisit one of the simple data parallel examples from <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">Part 2</a>:</p>
<pre class="csharpcode">Parallel.For(0, pixelData.GetUpperBound(0), row =&gt;
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> col=0; col &lt; pixelData.GetUpperBound(1); ++col)
    {
        pixelData[row, col] = AdjustContrast(pixelData[row, col], minPixel, maxPixel);
    }
});</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, we’re looping through an image, and calling a method on each pixel in the image.&#160; If this was being done on a separate thread, and we knew another thread within our system was going to be doing a similar operation, we likely would want to restrict this to using half of the cores on the system.&#160; This could be accomplished easily by doing:</p>
<pre class="csharpcode">var options = <span class="kwrd">new</span> ParallelOptions();
options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1);

Parallel.For(0, pixelData.GetUpperBound(0), options, row =&gt;
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> col=0; col &lt; pixelData.GetUpperBound(1); ++col)
    {
        pixelData[row, col] = AdjustContrast(pixelData[row, col], minPixel, maxPixel);
    }
});</pre>
<p>Now, we’re restricting this routine to using no more than half the cores in our system.&#160; Note that I included a check to prevent a single core system from supplying zero; without this check, we’d potentially cause an exception.&#160; I also did not hard code a specific value for the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(VS.100).aspx" target="_blank">MaxDegreeOfParallelism</a> property.&#160; One of our goals when parallelizing a routine is allowing it to scale on better hardware.&#160; Specifying a hard-coded value would contradict that goal.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">Parallel LINQ</a> also supports configuration, and in fact, has quite a few more options for configuring the system.&#160; The main configuration option we most often need is the same as our TPL option: we need to supply the maximum number of processing threads.&#160; In <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a>, this is done via a new extension method on ParallelQuery&lt;T&gt;: <a href="http://msdn.microsoft.com/en-us/library/dd383719(VS.100).aspx" target="_blank">ParallelEnumerable.WithDegreeOfParallelism</a>.</p>
<p>Let’s revisit our <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/" target="_blank">declarative data parallelism sample from Part 6</a>:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection.AsParallel().Min(item =&gt; item.PerformComputation());</pre>
<p>Here, we’re performing a computation on each element in the collection, and saving the minimum value of this operation.&#160; If we wanted to restrict this to a limited number of threads, we would add our new extension method:</p>
<pre class="csharpcode"><span class="kwrd">int</span> maxThreads = Math.Max(Environment.ProcessorCount / 2, 1);
<span class="kwrd">double</span> min = collection
                 .AsParallel()
                 .WithDegreeOfParallelism(maxThreads)
                 .Min(item =&gt; item.PerformComputation());</pre>
<p>This automatically restricts the <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> query to half of the threads on the system.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> provides some additional configuration options.&#160; By default, PLINQ will occasionally revert to processing a query in parallel.&#160; This occurs because many queries, if parallelized, typically actually cause an overall slowdown compared to a serial processing equivalent.&#160; By analyzing the “shape” of the query, PLINQ often decides to run a query serially instead of in parallel.&#160; This can occur for (<a href="http://msdn.microsoft.com/en-us/library/dd997399(VS.100).aspx" target="_blank">taken from MSDN</a>):</p>
<ul>
<li>Queries that contain a Select, indexed Where, indexed SelectMany, or ElementAt clause after an ordering or filtering operator that has removed or rearranged original indices. </li>
<li>Queries that contain a Take, TakeWhile, Skip, SkipWhile operator and where indices in the source sequence are not in the original order. </li>
<li>Queries that contain Zip or SequenceEquals, unless one of the data sources has an originally ordered index and the other data source is indexable (i.e. an array or IList(T)). </li>
<li>Queries that contain Concat, unless it is applied to indexable data sources. </li>
<li>Queries that contain Reverse, unless applied to an indexable data source. </li>
</ul>
<p>If the specific query follows these rules, PLINQ will run the query on a single thread.&#160; However, none of these rules look at the specific work being done in the delegates, only at the “shape” of the query.&#160; There are cases where running in parallel may still be beneficial, even if the shape is one where it typically parallelizes poorly.&#160; In these cases, you can override the default behavior by using the <a href="http://msdn.microsoft.com/en-us/library/dd642145(VS.100).aspx" target="_blank">WithExecutionMode</a> extension method.&#160; This would be done like so:</p>
<pre class="csharpcode">var reversed = collection
                  .AsParallel()
                  .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                  .Select(i =&gt; i.PerformComputation())
                  .Reverse();</pre>
<p>Here, the default behavior would be to not parallelize the query unless collection implemented IList&lt;T&gt;.&#160; We can force this to run in parallel by adding the <a href="http://msdn.microsoft.com/en-us/library/dd642145(VS.100).aspx" target="_blank">WithExecutionMode</a> extension method in the method chain.</p>
<p>Finally, <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> has the ability to configure how results are returned.&#160; When a query is filtering or selecting an input collection, the results will need to be streamed back into a single IEnumerable&lt;T&gt; result.&#160; For example, the method above returns a new, reversed collection.&#160; In this case, the processing of the collection will be done in parallel, but the results need to be streamed back to the caller serially, so they can be enumerated on a single thread.</p>
<p>This streaming introduces overhead.&#160; IEnumerable&lt;T&gt; isn’t designed with thread safety in mind, so the system needs to handle merging the parallel processes back into a single stream, which introduces synchronization issues.&#160; There are two extremes of how this could be accomplished, but both extremes have disadvantages.</p>
<p>The system could watch each thread, and whenever a thread produces a result, take that result and send it back to the caller.&#160; This would mean that the calling thread would have access to the data as soon as data is available, which is the benefit of this approach.&#160; However, it also means that every item is introducing synchronization overhead, since each item needs to be merged individually.</p>
<p>On the other extreme, the system could wait until all of the results from all of the threads were ready, then push all of the results back to the calling thread in one shot.&#160; The advantage here is that the least amount of synchronization is added to the system, which means the query will, on a whole, run the fastest.&#160; However, the calling thread will have to wait for all elements to be processed, so this could introduce a long delay between when a parallel query begins and when results are returned.</p>
<p>The default behavior in <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is actually between these two extremes.&#160; By default, PLINQ maintains an internal buffer, and chooses an optimal buffer size to maintain.&#160; Query results are accumulated into the buffer, then returned in the IEnumerable&lt;T&gt; result in chunks.&#160; This provides reasonably fast access to the results, as well as good overall throughput, in most scenarios.</p>
<p>However, if we know the nature of our algorithm, we may decide we would prefer one of the other extremes.&#160; This can be done by using the <a href="http://msdn.microsoft.com/en-us/library/dd438099(VS.100).aspx" target="_blank">WithMergeOptions</a> extension method.&#160; For example, if we know that our PerformComputation() routine is very slow, but also variable in runtime, we may want to retrieve results as they are available, with no bufferring.&#160; This can be done by changing our above routine to:</p>
<pre class="csharpcode">var reversed = collection
                  .AsParallel()
                  .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                  .WithMergeOptions(ParallelMergeOptions.NotBuffered)
                  .Select(i =&gt; i.PerformComputation())
                  .Reverse();</pre>
<p>On the other hand, if are already on a background thread, and we want to allow the system to maximize its speed, we might want to allow the system to fully buffer the results:</p>
<pre class="csharpcode">var reversed = collection
                  .AsParallel()
                  .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                  .WithMergeOptions(ParallelMergeOptions.FullyBuffered)
                  .Select(i =&gt; i.PerformComputation())
                  .Reverse();</pre>
<p>Notice, also, that you can specify multiple configuration options in a parallel query.&#160; By chaining these extension methods together, we generate a query that will always run in parallel, and will always complete before making the results available in our IEnumerable&lt;T&gt;.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/02/11/parallelism-in-net-part-9-configuration-in-plinq-and-tpl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 8, PLINQ&#8217;s ForAll Method</title>
		<link>http://reedcopsey.com/2010/02/03/parallelism-in-net-part-8-plinqs-forall-method/</link>
		<comments>http://reedcopsey.com/2010/02/03/parallelism-in-net-part-8-plinqs-forall-method/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 01:56:25 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/02/03/parallelism-in-net-part-8-plinqs-forall-method/</guid>
		<description><![CDATA[Parallel LINQ extends LINQ to Objects, and is typically very similar.&#160; However, as I previously discussed, there are some differences.&#160; Although the standard way to handle simple Data Parellelism is via Parallel.ForEach, it’s possible to do the same thing via PLINQ. PLINQ adds a new method unavailable in standard LINQ which provides new functionality… LINQ [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">Parallel LINQ</a> extends <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a>, and is typically very similar.&#160; However, as I previously discussed, there are <a href="http://reedcopsey.com/2010/01/28/parallelism-in-net-part-7-some-differences-between-plinq-and-linq-to-objects/" target="_blank">some differences</a>.&#160; Although the standard way to handle <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">simple Data Parellelism is via Parallel.ForEach</a>, it’s possible to do the same thing via PLINQ. </p>
<p>PLINQ adds a new method unavailable in standard LINQ which provides new functionality…</p>
<p> <span id="more-173"></span>
<p><a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ</a> is designed to provide a much simpler way of handling querying, including filtering, ordering, grouping, and many other benefits.&#160; Reading the description in <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects on MSDN</a>, it becomes clear that the thinking behind LINQ deals with retrieval of data.&#160; LINQ works by adding a <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">functional programming</a> style on top of .NET, allowing us to express filters in terms of predicate functions, for example.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is, generally, very similar.&#160; Typically, when using PLINQ, we write declarative statements to <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/" target="_blank">filter a dataset or perform an aggregation.</a>&#160; However, PLINQ adds one new method, which provides a very different purpose: <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a>.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a> method is defined on <a href="http://msdn.microsoft.com/en-us/library/system.linq.parallelenumerable(VS.100).aspx" target="_blank">ParallelEnumerable</a>, and will work upon any <a href="http://msdn.microsoft.com/en-us/library/dd383736(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a>.&#160; Unlike the sequence operators in LINQ and PLINQ, ForAll is<strong> intended to cause side effects</strong>.&#160; It does not filter a collection, but rather invokes an action on each element of the collection.</p>
<p>At first glance, this seems like a bad idea.&#160; For example, <a href="http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx" target="_blank">Eric Lippert clearly explained two philosophical objections to providing an IEnumerable&lt;T&gt;.ForEach extension method</a>, one of which still applies when parallelized.&#160; The sole purpose of this method is to cause side effects, and as such, I agree that the <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a> method “<em>violates the functional programming principles that all the other sequence operators are based upon</em>”, in exactly the same manner an IEnumerable&lt;T&gt;.ForEach extension method would violate these principles.&#160; Eric Lippert’s second reason for disliking a ForEach extension method does not necessarily apply to <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a> – replacing <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a> with a call to <a href="http://msdn.microsoft.com/en-us/library/dd991870(VS.100).aspx" target="_blank">Parallel.ForEach</a> has the same closure semantics, so there is no loss there.</p>
<p>Although <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a> may have philosophical issues, there is a pragmatic reason to include this method.&#160; Without <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a>, we would take a fairly serious performance hit in many situations.&#160; Often, we need to perform some filtering or grouping, then perform an action using the results of our filter.&#160; </p>
<p>Using a standard <a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6(VS.100).aspx" target="_blank">foreach statement</a> to perform our action would avoid this philosophical issue:</p>
<pre class="csharpcode"><span class="rem">// Filter our collection</span>
var filteredItems = collection.AsParallel().Where( i =&gt; i.SomePredicate() );

<span class="rem">// Now perform an action</span>
<span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> filteredItems)
{
    <span class="rem">// These will now run serially</span>
    item.DoSomething();
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This would cause a loss in performance, since we lose any parallelism in place, and cause all of our actions to be run serially.</p>
<p>We could easily use a <a href="http://msdn.microsoft.com/en-us/library/dd991870(VS.100).aspx" target="_blank">Parallel.ForEach</a> instead, which adds parallelism to the actions:</p>
<pre class="csharpcode"><span class="rem">// Filter our collection</span>
var filteredItems = collection.AsParallel().Where( i =&gt; i.SomePredicate() );

<span class="rem">// Now perform an action once the filter completes</span>
Parallel.ForEach(filteredItems, item =&gt;
{
    <span class="rem">// These will now run in parallel</span>
    item.DoSomething();
});</pre>
<p>This is a noticeable improvement, since both our filtering and our actions run parallelized.&#160; However, there is still a large bottleneck in place here.&#160; </p>
<p>The problem lies with my comment “<em>perform an action once the filter completes</em>”.&#160; Here, we’re parallelizing the filter, then collecting all of the results, blocking until the filter completes.&#160; Once the filtering of every element is completed, we then <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/" target="_blank">repartition</a> the results of the filter, reschedule into multiple threads, and perform the action on each element.&#160; By moving this into two separate statements, we potentially double our parallelization overhead, since we’re forcing the work to be partitioned and scheduled twice as many times.</p>
<p>This is where the pragmatism comes into play.&#160; By <a href="http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx" target="_blank">violating our functional principles</a>, we gain the ability to avoid the overhead and cost of rescheduling the work:</p>
<pre class="csharpcode"><span class="rem">// Perform an action on the results of our filter</span>
collection
    .AsParallel()
    .Where( i =&gt; i.SomePredicate() )
    .ForAll( i =&gt; i.DoSomething() );</pre>
<p>The ability to avoid the scheduling overhead is a compelling reason to use <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a>.&#160; This really goes back to one of the <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">key points I discussed in data parallelism</a>: Partition your problem in a way to place the most work possible into each task.&#160; Here, this means leaving the statement attached to the expression, even though it causes side effects and is not standard usage for LINQ.</p>
<p>This leads to my one guideline for using <a href="http://msdn.microsoft.com/en-us/library/dd383744(VS.100).aspx" target="_blank">ForAll</a>:</p>
<p><strong>The ForAll extension method should only be used to process the results of a parallel query, as returned by a PLINQ expression.</strong></p>
<p>Any other usage scenario should use <a href="http://msdn.microsoft.com/en-us/library/dd991870(VS.100).aspx" target="_blank">Parallel.ForEach</a>, instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/02/03/parallelism-in-net-part-8-plinqs-forall-method/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 7, Some Differences between PLINQ and LINQ to Objects</title>
		<link>http://reedcopsey.com/2010/01/28/parallelism-in-net-part-7-some-differences-between-plinq-and-linq-to-objects/</link>
		<comments>http://reedcopsey.com/2010/01/28/parallelism-in-net-part-7-some-differences-between-plinq-and-linq-to-objects/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 21:02:03 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/28/parallelism-in-net-part-7-some-differences-between-plinq-and-linq-to-objects/</guid>
		<description><![CDATA[In my previous post on Declarative Data Parallelism, I mentioned that PLINQ extends LINQ to Objects to support parallel operations.&#160; Although nearly all of the same operations are supported, there are some differences between PLINQ and LINQ to Objects.&#160; By introducing Parallelism to our declarative model, we add some extra complexity.&#160; This, in turn, adds [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/" target="_blank">previous post on Declarative Data Parallelism</a>, I mentioned that <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> extends <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> to support parallel operations.&#160; Although nearly all of the same operations are supported, there are some differences between PLINQ and LINQ to Objects.&#160; By introducing Parallelism to our declarative model, we add some extra complexity.&#160; This, in turn, adds some extra requirements that must be addressed.</p>
<p>In order to illustrate the main differences, and why they exist, let’s begin by discussing some differences in how the two technologies operate, and look at the underlying types involved in <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> and <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> .</p>
<p> <span id="more-166"></span>
<p><a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> is mainly built upon a single class: <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx" target="_blank">Enumerable</a>.&#160; The <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx" target="_blank">Enumerable class</a> is a static class that defines a large set of <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable_methods.aspx" target="_blank">extension methods</a>, nearly all of which work upon an <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>.&#160; Many of these methods return a new <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, allowing the methods to be chained together into a <a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_blank">fluent style interface</a>.&#160; This is what allows us to write statements that chain together, and lead to the nice declarative programming model of LINQ:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection
                .Where(item =&gt; item.SomeProperty &gt; 6 &amp;&amp; item.SomeProperty &lt; 24)
                .Min(item =&gt; item.PerformComputation());</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><a href="http://msdn.microsoft.com/en-us/library/bb397926.aspx" target="_blank">Other LINQ variants</a> work in a similar fashion.&#160; For example, most data-oriented LINQ providers are built upon an implementation of <a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx" target="_blank">IQueryable&lt;T&gt;</a>, which allows the database provider to turn a LINQ statement into an underlying SQL query, to be performed directly on the remote database.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is similar, but instead of being built upon the <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aspx" target="_blank">Enumerable class</a>, most of <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is built upon a new static class: <a href="http://msdn.microsoft.com/en-us/library/system.linq.parallelenumerable(VS.100).aspx" target="_blank">ParallelEnumerable</a>.&#160; When using <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a>, you typically begin with any collection which implements <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, and convert it to a new type using an extension method defined on ParallelEnumerable: <a href="http://msdn.microsoft.com/en-us/library/dd413602(VS.100).aspx" target="_blank">AsParallel()</a>.&#160; This method takes any <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, and converts it into a <a href="http://msdn.microsoft.com/en-us/library/dd383736(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a>, the core class for PLINQ.&#160; There is a similar <a href="http://msdn.microsoft.com/en-us/library/system.linq.parallelquery(VS.100).aspx" target="_blank">ParallelQuery</a> class for working with non-generic IEnumerable implementations.</p>
<p>This brings us to our first subtle, but important difference between PLINQ and LINQ – </p>
<p><strong>PLINQ always works upon specific types, which must be explicitly created.</strong> </p>
<p>Typically, the type you’ll use with PLINQ is <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a>, but it can sometimes be a <a href="http://msdn.microsoft.com/en-us/library/system.linq.parallelquery(VS.100).aspx" target="_blank">ParallelQuery</a> or an <a href="http://msdn.microsoft.com/en-us/library/dd413755(VS.100).aspx" target="_blank">OrderedParallelQuery&lt;T&gt;</a>.&#160; Instead of dealing with an interface, implemented by an unknown class, we’re dealing with a specific class type.&#160; This works seamlessly from a usage standpoint &#8211; <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a> implements <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, so you can always “switch back” to an <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>.&#160; </p>
<p>The difference only arises at the beginning of our parallelization.&#160; When we’re using LINQ, and we want to process a normal collection via PLINQ, we need to explicitly convert the collection into a <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a> by calling <a href="http://msdn.microsoft.com/en-us/library/dd413602(VS.100).aspx" target="_blank">AsParallel()</a>.&#160; There is an important consideration here – <a href="http://msdn.microsoft.com/en-us/library/dd413602(VS.100).aspx" target="_blank">AsParallel()</a> does not need to be called on your specific collection, but rather any <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>.&#160; This allows you to place it anywhere in the chain of methods involved in a LINQ statement, not just at the beginning.&#160; This can be useful if you have an operation which will not parallelize well or is not thread safe.&#160; For example, the following is perfectly valid, and similar to our previous examples:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection
                .AsParallel()
                .Select(item =&gt; item.SomeOperation())
                .Where(item =&gt; item.SomeProperty &gt; 6 &amp;&amp; item.SomeProperty &lt; 24)
                .Min(item =&gt; item.PerformComputation());</pre>
<p>However, if <em>SomeOperation()</em> is not thread safe, we could just as easily do:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection
                .Select(item =&gt; item.SomeOperation())
                .AsParallel()
                .Where(item =&gt; item.SomeProperty &gt; 6 &amp;&amp; item.SomeProperty &lt; 24)
                .Min(item =&gt; item.PerformComputation());</pre>
<p>In this case, we’re using standard <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> for the <a href="http://msdn.microsoft.com/en-us/library/bb548891.aspx" target="_blank">Select(…)</a> method, then converting the results of that map routine to a <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a>, and processing our filter (the <a href="http://msdn.microsoft.com/en-us/library/dd384150(VS.100).aspx" target="_blank">Where</a> method) and our aggregation (the <a href="http://msdn.microsoft.com/en-us/library/dd383728(VS.100).aspx" target="_blank">Min</a> method) in parallel.</p>
<p>PLINQ also provides us with a way to convert a <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a> back into a standard <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, forcing sequential processing via standard <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a>.&#160; If <em>SomeOperation() </em>was thread-safe, but <em>PerformComputation()</em> was not thread-safe, we would need to handle this by using the <a href="http://msdn.microsoft.com/en-us/library/dd413587(VS.100).aspx" target="_blank">AsEnumerable()</a> method:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection
                .AsParallel()
                .Select(item =&gt; item.SomeOperation())
                .Where(item =&gt; item.SomeProperty &gt; 6 &amp;&amp; item.SomeProperty &lt; 24)
                .AsEnumerable()
                .Min(item =&gt; item.PerformComputation());</pre>
<p>Here, we’re converting our collection into a <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;,</a> doing our map operation (the <a href="http://msdn.microsoft.com/en-us/library/bb548891.aspx" target="_blank">Select(…)</a> method) and our filtering in parallel, then converting the collection back into a standard <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, which causes our aggregation via Min() to be performed sequentially.</p>
<p>This could also be written as two statements, as well, which would allow us to use the language integrated syntax for the first portion:</p>
<pre class="csharpcode">var tempCollection = from item <span class="kwrd">in</span> collection.AsParallel()
                     let e = item.SomeOperation()
                     <span class="kwrd">where</span> (e.SomeProperty &gt; 6 &amp;&amp; e.SomeProperty &lt; 24)
                     select e;
<span class="kwrd">double</span> min = tempCollection.AsEnumerable().Min(item =&gt; item.PerformComputation());</pre>
<p>This allows us to use the standard LINQ style language integrated query syntax, but control whether it’s performed in parallel or serial by adding <a href="http://msdn.microsoft.com/en-us/library/dd413602(VS.100).aspx" target="_blank">AsParallel()</a> and <a href="http://msdn.microsoft.com/en-us/library/dd413587(VS.100).aspx" target="_blank">AsEnumerable()</a> appropriately.</p>
<p>The second important difference between PLINQ and LINQ deals with <a href="http://msdn.microsoft.com/en-us/library/dd460677(VS.100).aspx" target="_blank">order preservation</a>.&#160; </p>
<p><strong>PLINQ, by default, does not preserve the order of of source collection.</strong></p>
<p>This is by design.&#160; In order to process a collection in parallel, the system needs to naturally deal with multiple elements at the same time.&#160; Maintaining the original ordering of the sequence adds overhead, which is, in many cases, unnecessary.&#160; Therefore, by default, the system is allowed to completely change the order of your sequence during processing.&#160; If you are doing a standard query operation, this is usually not an issue.&#160; However, there are times when keeping a specific ordering in place is important.&#160; If this is required, you can explicitly request the ordering be preserved throughout all operations done on a <a href="http://msdn.microsoft.com/en-us/library/dd413700(VS.100).aspx" target="_blank">ParallelQuery&lt;T&gt;</a> by using the <a href="http://msdn.microsoft.com/en-us/library/dd413357(VS.100).aspx" target="_blank">AsOrdered()</a> extension method.&#160; This will cause our sequence ordering to be preserved.</p>
<p>For example, suppose we wanted to take a collection, perform an expensive operation which converts it to a new type, and display the first 100 elements.&#160; In <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a>, our code might look something like:</p>
<pre class="csharpcode"><span class="rem">// Using IEnumerable&lt;SourceClass&gt; collection</span>
IEnumerable&lt;ResultClass&gt; results = collection
                                       .Select(e =&gt; e.CreateResult())
                                       .Take(100);</pre>
<p>If we just converted this to a parallel query naively, like so:</p>
<pre class="csharpcode">IEnumerable&lt;ResultClass&gt; results = collection
                                       .AsParallel()
                                       .Select(e =&gt; e.CreateResult())
                                       .Take(100);</pre>
<p>We could very easily get a very different, and non-reproducable, set of results, since the ordering of elements in the input collection is not preserved.&#160; To get the same results as our original query, we need to use:</p>
<pre class="csharpcode">IEnumerable&lt;ResultClass&gt; results = collection
                                       .AsParallel()
                                       .AsOrdered()
                                       .Select(e =&gt; e.CreateResult())
                                       .Take(100);</pre>
<p>This requests that PLINQ process our sequence in a way that verifies that our resulting collection is ordered as if it were processed serially.&#160; This will cause our query to run slower, since there is overhead involved in maintaining the ordering.&#160; However, in this case, it is required, since the ordering is required for correctness.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> is incredibly useful.&#160; It allows us to easily take nearly any <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> query and run it in parallel, using the same methods and syntax we’ve used previously.&#160; There are some important differences in operation that must be considered, however – it is not a free pass to parallelize everything.&#160; When using PLINQ in order to parallelize your routines declaratively, the same <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/" target="_blank">guideline I mentioned before</a> still applies:</p>
<p><strong>Parallelization is something that should be handled with care and forethought, added by design, and not just introduced casually.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/28/parallelism-in-net-part-7-some-differences-between-plinq-and-linq-to-objects/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 6, Declarative Data Parallelism</title>
		<link>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/</link>
		<comments>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 01:26:01 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/</guid>
		<description><![CDATA[When working with a problem that can be decomposed by data, we have a collection, and some operation being performed upon the collection.&#160; I’ve demonstrated how this can be parallelized using the Task Parallel Library and imperative programming using imperative data parallelism via the Parallel class.&#160; While this provides a huge step forward in terms [...]]]></description>
			<content:encoded><![CDATA[<p>When working with a problem that can be <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decomposed by data</a>, we have a collection, and some operation being performed upon the collection.&#160; I’ve demonstrated how this can be parallelized using the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> and <a href="http://en.wikipedia.org/wiki/Imperative_programming" target="_blank">imperative programming</a> using <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">imperative data parallelism</a> via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(VS.100).aspx" target="_blank">Parallel class</a>.&#160; While this provides a huge step forward in terms of power and capabilities, in many cases, <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/" target="_blank">special care must still be given for relative common scenarios</a>.</p>
<p>C# 3.0 and Visual Basic 9.0 introduced a new, <a href="http://en.wikipedia.org/wiki/Declarative_programming" target="_blank">declarative programming model</a> to .NET via the <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" target="_blank">LINQ Project</a>.&#160; When working with collections, we can now write software that describes <em>what</em> we want to occur without having to explicitly state <em>how </em>the program should accomplish the task.&#160; By taking advantage of <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" target="_blank">LINQ</a>, many operations become much shorter, more elegant, and easier to understand and maintain.&#160; Version 4.0 of the .NET framework extends this concept into the parallel computation space by introducing <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">Parallel LINQ</a>.</p>
<p> <span id="more-162"></span>
<p>Before we delve into PLINQ, let’s begin with a short discussion of LINQ.&#160; LINQ, the extensions to the .NET Framework which implement language integrated query, set, and transform operations, is <a href="http://msdn.microsoft.com/en-us/library/bb397926.aspx" target="_blank">implemented in many flavors</a>.&#160; For our purposes, we are interested in <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a>.&#160; When dealing with parallelizing a routine, we typically are dealing with in-memory data storage.&#160; More data-access oriented LINQ variants, such as <a href="http://msdn.microsoft.com/en-us/library/bb386976.aspx" target="_blank">LINQ to SQL</a> and <a href="http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx#ado.netenfrmovw_topic4" target="_blank">LINQ to Entities in the Entity Framework</a> fall outside of our concern, since the parallelism there is the concern of the data base engine processing the query itself.</p>
<p>LINQ (<a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank">LINQ to Objects</a> in particular) works by implementing a series of extension methods, most of which work on <a href="http://msdn.microsoft.com/en-us/library/19e6zeyy.aspx" target="_blank">IEnumerable&lt;T&gt;</a>.&#160; The language enhancements use these extension methods to create a very concise, readable alternative to using traditional <a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6(VS.80).aspx" target="_blank">foreach</a> statement.&#160; For example, let’s revisit our <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/" target="_blank">minimum aggregation routine we wrote in Part 4</a>:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
<span class="kwrd">foreach</span>(var item <span class="kwrd">in</span> collection)
{
    <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
    min = System.Math.Min(min, <span class="kwrd">value</span>);
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, we’re doing a very simple computation, but writing this in an imperative style.&#160; This can be loosely translated to English as:</p>
<pre>Create a very large number, and save it in min
Loop through each item in the collection.
For every item:
    Perform some computation, and save the result
    If the computation is less than min, set min to the computation </pre>
<p>Although this is fairly easy to follow, it’s quite a few lines of code, and it requires us to read through the code, step by step, line by line, in order to understand the intention of the developer.</p>
<p>We can rework this same statement, using LINQ:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection.Min(item =&gt; item.PerformComputation());</pre>
<p>Here, we’re after the same information.&#160; However, this is written using a declarative programming style.&#160; When we see this code, we’d naturally translate this to English as:</p>
<pre>Save the Min value of collection, determined via calling item.PerformComputation() </pre>
<p>That’s it – instead of multiple logical steps, we have one single, declarative <em>request</em>.&#160; This makes the developer’s intentions very clear, and very easy to follow.&#160; The system is free to implement this using whatever method required.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">Parallel LINQ (PLINQ)</a> extends LINQ to Objects to support parallel operations.&#160; This is a perfect fit in many cases when you have a problem that can be <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decomposed by data</a>.&#160; To show this, let’s again refer to our <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/" target="_blank">minimum aggregation routine from Part 4</a>, but this time, let’s review our final, parallelized version:</p>
<pre class="csharpcode"><span class="rem">// Safe, and fast!</span>
<span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
<span class="rem">// Make a &quot;lock&quot; object</span>
<span class="kwrd">object</span> syncObject = <span class="kwrd">new</span> <span class="kwrd">object</span>();
Parallel.ForEach(
    collection,
    <span class="rem">// First, we provide a local state initialization delegate.</span>
    () =&gt; <span class="kwrd">double</span>.MaxValue,
    <span class="rem">// Next, we supply the body, which takes the original item, loop state,</span>
    <span class="rem">// and local state, and returns a new local state</span>
    (item, loopState, localState) =&gt;
    {
        <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
        <span class="kwrd">return</span> System.Math.Min(localState, <span class="kwrd">value</span>);
    },
    <span class="rem">// Finally, we provide an Action&lt;TLocal&gt;, to &quot;merge&quot; results together</span>
    localState =&gt;
    {
        <span class="rem">// This requires locking, but it's only once per used thread</span>
        <span class="kwrd">lock</span>(syncObj)
            min = System.Math.Min(min, localState);
    }
);</pre>
<p>Here, we’re doing the same computation as above, but fully parallelized.&#160; Describing this in English becomes quite a feat: </p>
<pre>Create a very large number, and save it in min
Create a temporary object we can use for locking
Call Parallel.ForEach, specifying three delegates
    For the first delegate:
        Initialize a local variable to hold the local state to a very large number
    For the second delegate:
        For each item in the collection, perform some computation, save the result
        If the result is less than our local state, save the result in local state
    For the final delegate:
        Take a lock on our temporary object to protect our min variable
        Save the min of our min and local state variables</pre>
<p>Although this solves our problem, and does it in a very efficient way, we’ve created a set of code that is quite a bit more difficult to understand and maintain.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a> provides us with a very nice alternative.&#160; In order to use <a href="http://msdn.microsoft.com/en-us/library/dd460688(VS.100).aspx" target="_blank">PLINQ</a>, we need to learn one new extension method that works on <a href="http://msdn.microsoft.com/en-us/library/19e6zeyy.aspx" target="_blank">IEnumerable&lt;T&gt;</a> – <a href="http://msdn.microsoft.com/en-us/library/dd413602(VS.100).aspx" target="_blank">ParallelEnumerable.AsParallel()</a>.</p>
<p>That’s all we need to learn in order to use PLINQ<strong>: one single method</strong>.&#160; We can write our minimum aggregation in PLINQ very simply:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = collection.AsParallel().Min(item =&gt; item.PerformComputation());</pre>
<p>By simply adding “.AsParallel()” to our LINQ to Objects query, we converted this to using PLINQ and running this computation in parallel!&#160; </p>
<p>This can be loosely translated into English easily, as well:</p>
<pre>Process the collection in parallel
Get the Minimum value, determined by calling PerformComputation on each item </pre>
<p>Here, our intention is very clear and easy to understand.&#160; We just want to perform the same operation we did in serial, but run it “as parallel”.&#160; PLINQ completely extends LINQ to Objects: the entire functionality of LINQ to Objects is available.&#160; By simply adding a call to AsParallel(), we can specify that a collection should be processed in parallel.&#160; This is simple, safe, and incredibly useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-6-declarative-data-parallelism/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 5, Partitioning of Work</title>
		<link>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/</link>
		<comments>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 17:34:56 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/</guid>
		<description><![CDATA[When parallelizing any routine, we start by decomposing the problem.&#160; Once the problem is understood, we need to break our work into separate tasks, so each task can be run on a different processing element.&#160; This process is called partitioning. Partitioning our tasks is a challenging feat.&#160; There are opposing forces at work here: too [...]]]></description>
			<content:encoded><![CDATA[<p>When parallelizing any routine, we start by <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decomposing the problem</a>.&#160; Once the problem is understood, we need to break our work into separate tasks, so each task can be run on a different processing element.&#160; This process is called partitioning.</p>
<p>Partitioning our tasks is a challenging feat.&#160; There are opposing forces at work here: too many partitions adds overhead, too few partitions leaves processors idle.&#160; Trying to work the perfect balance between the two extremes is the goal for which we should aim.&#160; Luckily, the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> automatically handles much of this process.&#160; However, there are situations where the default partitioning may not be appropriate, and knowledge of our routines may allow us to guide the framework to making better decisions.</p>
<p> <span id="more-161"></span>
<p>First off, I’d like to say that this is a more advanced topic.&#160; It is perfectly acceptable to use the parallel constructs in the framework without considering the partitioning taking place.&#160; The default behavior in the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> is very well-behaved, even for unusual work loads, and should rarely be adjusted.&#160; I have found few situations where the default partitioning behavior in the TPL is not as good or better than my own hand-written partitioning routines, and recommend using the defaults unless there is a strong, measured, and profiled reason to avoid using them.&#160; However, understanding partitioning, and how the TPL partitions your data, helps in understanding the proper usage of the TPL.</p>
<p>I indirectly mentioned partitioning while discussing <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/" target="_blank">aggregation</a>.&#160; Typically, our systems will have a limited number of <em>Processing Elements (PE)</em>, which is the terminology used for hardware capable of processing a stream of instructions.&#160; For example, in a standard Intel i7 system, there are four processor cores, each of which has two potential hardware threads due to Hyperthreading.&#160; This gives us a total of 8 PEs – theoretically, we can have up to eight operations occurring concurrently within our system.</p>
<p>In order to fully exploit this power, we need to partition our work into <em>Tasks.</em>&#160; A task is a simple set of instructions that can be run on a PE.&#160; Ideally, we want to have at least one task per PE in the system, since fewer tasks means that some of our processing power will be sitting idle.&#160; A naive implementation would be to just take our data, and partition it with one element in our collection being treated as one task.&#160; When we loop through our collection in parallel, using this approach, we’d just process one item at a time, then reuse that thread to process the next, etc.&#160; There’s a flaw in this approach, however.&#160; It will tend to be slower than necessary, often slower than processing the data serially.</p>
<p>The problem is that there is overhead associated with each task.&#160; When we take a simple foreach loop body and implement it using the TPL, we add overhead.&#160; First, we change the body from a simple statement to a delegate, which must be invoked.&#160; In order to invoke the delegate on a separate thread, the delegate gets added to the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>’s current work queue, and the ThreadPool must pull this off the queue, assign it to a free thread, then execute it.&#160; If our collection had one million elements, the overhead of trying to spawn one million tasks would destroy our performance.</p>
<p>The answer, here, is to partition our collection into groups, and have each group of elements treated as a single <em>task</em>.&#160; By adding a partitioning step, we can break our total work into small enough tasks to keep our processors busy, but large enough tasks to avoid overburdening the ThreadPool.&#160; There are two clear, opposing goals here:</p>
<p><strong>Always try to keep each processor working, but also try to keep the individual partitions as large as possible.</strong></p>
<p>When using <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for(VS.100).aspx" target="_blank">Parallel.For</a>, the partitioning is always handled automatically.&#160; At first, partitioning here seems simple.&#160; A naive implementation would merely split the total element count up by the number of PEs in the system, and assign a chunk of data to each processor.&#160; Many hand-written partitioning schemes work in this exactly manner.&#160; This perfectly balanced, static partitioning scheme works very well if the amount of work is constant for each element.&#160; However, this is rarely the case.&#160; Often, the length of time required to process an element grows as we progress through the collection, especially if we’re doing numerical computations.&#160; In this case, the first PEs will finish early, and sit idle waiting on the last chunks to finish.&#160; Sometimes, work can decrease as we progress, since previous computations may be used to speed up later computations.&#160; In this situation, the first chunks will be working far longer than the last chunks.&#160; In order to balance the workload, many implementations create many small chunks, and reuse threads.&#160; This adds overhead, but does provide better load balancing, which in turn improves performance.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx" target="_blank">Task Parallel Library</a> handles this more elaborately.&#160; Chunks are determined at runtime, and start small.&#160; They grow slowly over time, getting larger and larger.&#160; This tends to lead to a near optimum load balancing, even in odd cases such as increasing or decreasing workloads.&#160; </p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd991870(VS.100).aspx" target="_blank">Parallel.ForEach</a> is a bit more complicated, however. When working with a generic <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, the number of items required for processing is not known in advance, and must be discovered at runtime.&#160; In addition, since we don’t have direct access to each element, the scheduler must enumerate the collection to process it.&#160; Since <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a> is not thread safe, it must lock on elements as it enumerates, create temporary collections for each chunk to process, and schedule this out.&#160; By default, it uses a partitioning method similar to the one described above.&#160; We can see this directly by looking at the <strong>Visual Partitioning</strong> sample shipped by the Task Parallel Library team, and available as part of the <a href="http://code.msdn.microsoft.com/ParExtSamples" target="_blank">Samples for Parallel Programming</a>.&#160; When we run the sample, with four cores and the default, Load Balancing partitioning scheme, we see this:</p>
<p><a href="http://reedcopsey.com/blog/wp-content/uploads/2010/01/foreach_paritioning.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="foreach_paritioning" border="0" alt="foreach_paritioning" src="http://reedcopsey.com/blog/wp-content/uploads/2010/01/foreach_paritioning_thumb.png" width="500" height="633" /></a> </p>
<p>The colored bands represent each processing core.&#160; You can see that, when we started (at the top), we begin with very small bands of color.&#160; As the routine progresses through the <a href="http://msdn.microsoft.com/en-us/library/dd991870(VS.100).aspx" target="_blank">Parallel.ForEach</a>, the chunks get larger and larger (seen by larger and larger stripes).</p>
<p>Most of the time, this is fantastic behavior, and most likely will out perform any custom written partitioning.&#160; However, if your routine is not scaling well, it may be due to a failure in the default partitioning to handle your specific case.&#160; With prior knowledge about your work, it may be possible to partition data more meaningfully than the default Partitioner.</p>
<p>There is the option to use an <a href="http://msdn.microsoft.com/en-us/library/dd783404(VS.100).aspx" target="_blank">overload of Parallel.ForEach</a> which takes a <a href="http://msdn.microsoft.com/en-us/library/dd381768(VS.100).aspx" target="_blank">Partitioner&lt;T&gt;</a> instance.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd381768(VS.100).aspx" target="_blank">Partitioner&lt;T&gt;</a> class is an abstract class which allows for both static and dynamic partitioning.&#160; By overriding <a href="http://msdn.microsoft.com/en-us/library/dd381743(VS.100).aspx" target="_blank">Partitioner&lt;T&gt;.SupportsDynamicPartitions</a>, you can specify whether a dynamic approach is available.&#160; If not, your custom Partitioner&lt;T&gt; subclass would override <a href="http://msdn.microsoft.com/en-us/library/dd381767(VS.100).aspx" target="_blank">GetPartitions(int)</a>, which returns a list of IEnumerator&lt;T&gt; instances.&#160; These are then used by the Parallel class to split work up amongst processors.&#160; When dynamic partitioning is available, <a href="http://msdn.microsoft.com/en-us/library/dd381959(VS.100).aspx" target="_blank">GetDynamicPartitions()</a> is used, which returns an IEnumerable&lt;T&gt; for each partition.&#160; If you do decide to implement your own Partitioner&lt;T&gt;, keep in mind the goals and tradeoffs of different partitioning strategies, and design appropriately.</p>
<p>The <a href="http://code.msdn.microsoft.com/ParExtSamples" target="_blank">Samples for Parallel Programming</a> project includes a ChunkPartitioner class in the <strong>ParallelExtensionsExtras</strong> project.&#160; This provides example code for implementing your own, custom allocation strategies, including a static allocator of a given chunk size.&#160; Although implementing your own Partitioner&lt;T&gt; is possible, as I mentioned above, this is rarely required or useful in practice.&#160; The default behavior of the TPL is very good, often better than any hand written partitioning strategy.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/26/parallelism-in-net-part-5-partitioning-of-work/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 4, Imperative Data Parallelism: Aggregation</title>
		<link>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/</link>
		<comments>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 01:14:19 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/</guid>
		<description><![CDATA[In the article on simple data parallelism, I described how to perform an operation on an entire collection of elements in parallel.&#160; Often, this is not adequate, as the parallel operation is going to be performing some form of aggregation. Simple examples of this might include taking the sum of the results of processing a [...]]]></description>
			<content:encoded><![CDATA[<p>In the article on <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">simple data parallelism</a>, I described how to perform an operation on an entire collection of elements in parallel.&#160; Often, this is not adequate, as the parallel operation is going to be performing some form of <a href="http://en.wikipedia.org/wiki/Aggregate_function" target="_blank">aggregation</a>.</p>
<p>Simple examples of this might include taking the sum of the results of processing a function on each element in the collection, or finding the minimum of the collection given some criteria.&#160; This can be done using the techniques described in <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">simple data parallelism</a>, however, special care needs to be taken into account to <a href="http://en.wikipedia.org/wiki/Synchronization_(computer_science)" target="_blank">synchronize the shared data appropriately</a>.&#160; The Task Parallel Library has tools to assist in this synchronization.</p>
<p> <span id="more-157"></span>
<p>The main issue with <a href="http://en.wikipedia.org/wiki/Aggregate_function" target="_blank">aggregation</a> when parallelizing a routine is that you need to handle <a href="http://en.wikipedia.org/wiki/Synchronization_(computer_science)" target="_blank">synchronization of data</a>.&#160; Since multiple threads will need to write to a shared portion of data.&#160; Suppose, for example, that we wanted to parallelize a simple loop that looked for the minimum value within a dataset:</p>
<pre class="csharpcode"><span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
<span class="kwrd">foreach</span>(var item <span class="kwrd">in</span> collection)
{
    <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
    min = System.Math.Min(min, <span class="kwrd">value</span>);
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This seems like a good candidate for parallelization, but there is a problem here.&#160; If we just wrap this into a call to <a href="http://msdn.microsoft.com/en-us/library/dd992001(VS.100).aspx" target="_blank">Parallel.ForEach</a>, we’ll introduce a <a href="http://en.wikipedia.org/wiki/Race_condition" target="_blank">critical race condition</a>, and get the wrong answer.&#160; Let’s look at what happens here:</p>
<pre class="csharpcode"><span class="rem">// Buggy code!  Do not use!</span>
<span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
Parallel.ForEach(collection, item =&gt;
{
    <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
    min = System.Math.Min(min, <span class="kwrd">value</span>);
});</pre>
<p>This code has a fatal flaw: <strong>min</strong> will be checked, then set, by multiple threads simultaneously.&#160; Two threads may perform the check at the same time, and set the wrong value for min.&#160; Say we get a value of 1 in thread 1, and a value of 2 in thread 2, and these two elements are the first two to run.&#160; If both hit the min check line at the same time, both will determine that min should change, to 1 and 2 respectively.&#160; If element 1 happens to set the variable first, then element 2 sets the min variable, we’ll detect a min value of 2 instead of 1.&#160; This can lead to wrong answers.</p>
<p>Unfortunately, fixing this, with the <a href="http://msdn.microsoft.com/en-us/library/dd992001(VS.100).aspx" target="_blank">Parallel.ForEach</a> call we’re using, would require adding locking.&#160; We would need to rewrite this like:</p>
<pre class="csharpcode"><span class="rem">// Safe, but slow</span>
<span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
<span class="rem">// Make a &quot;lock&quot; object</span>
<span class="kwrd">object</span> syncObject = <span class="kwrd">new</span> <span class="kwrd">object</span>();
Parallel.ForEach(collection, item =&gt;
{
    <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
    <span class="kwrd">lock</span>(syncObject)
        min = System.Math.Min(min, <span class="kwrd">value</span>);
});</pre>
<p>This will potentially add a huge amount of overhead to our calculation.&#160; Since we can potentially block while waiting on the lock for every single iteration, we will most likely slow this down to where it is actually quite a bit slower than our serial implementation.&#160; The problem is the lock statement – any time you use <em>lock(object), </em>you’re almost assuring reduced performance in a parallel situation.&#160; This leads to two observations I’ll make:</p>
<p><strong>When parallelizing a routine, try to avoid locks.</strong></p>
<p>That being said:</p>
<p><strong>Always add any and all required synchronization to avoid </strong><a href="http://en.wikipedia.org/wiki/Race_condition" target="_blank"><strong>race conditions</strong></a><strong>.</strong></p>
<p>These two observations tend to be opposing forces – we often need to synchronize our algorithms, but we also want to avoid the synchronization when possible.&#160; Looking at our routine, there is no way to directly avoid this lock, since each element is potentially being run on a separate thread, and this lock is necessary in order for our routine to function correctly every time.</p>
<p>However, this isn’t the only way to design this routine to implement this algorithm.&#160; Realize that, although our collection may have thousands or even millions of elements, we have a limited number of <em>Processing Elements (PE)</em>.&#160; Processing Element is the standard term for a hardware element which can process and execute instructions.&#160; This typically is a core in your processor, but many modern systems have <a href="http://en.wikipedia.org/wiki/Hyper-threading" target="_blank">multiple hardware execution threads per core</a>.&#160; The Task Parallel Library will not execute the work for each item in the collection as a separate work item. Instead, when Parallel.ForEach executes, it will partition the collection into larger “chunks” which get processed on different threads via the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx" target="_blank">ThreadPool</a>.&#160; This helps reduce the threading overhead, and help the overall speed.&#160; In general, the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(VS.100).aspx" target="_blank">Parallel class</a> will only use one thread per PE in the system.</p>
<p>Given the fact that there are typically fewer threads than work items, we can rethink our algorithm design.&#160; We can parallelize our algorithm more effectively by approaching it differently.&#160; Because the basic aggregation we are doing here (Min) is <a href="http://en.wikipedia.org/wiki/Commutative" target="_blank">communitive</a>, we do not need to perform this in a given order.&#160; We knew this to be true already – otherwise, we wouldn’t have been able to parallelize this routine in the first place.&#160; With this in mind, we can treat each thread’s work independently, allowing each thread to serially process many elements with no locking, then, after all the threads are complete, “merge” together the results.</p>
<p>This can be accomplished via a different set of overloads in the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(VS.100).aspx" target="_blank">Parallel class</a>: <a href="http://msdn.microsoft.com/en-us/library/dd990270(VS.100).aspx" target="_blank">Parallel.ForEach&lt;TSource,TLocal&gt;</a>.&#160; The idea behind these overloads is to allow each thread to begin by initializing some local state (TLocal).&#160; The thread will then process an entire set of items in the source collection, providing that state to the delegate which processes an individual item.&#160; Finally, at the end, a separate delegate is run which allows you to handle merging that local state into your final results.</p>
<p>To rewriting our routine using <a href="http://msdn.microsoft.com/en-us/library/dd990270(VS.100).aspx" target="_blank">Parallel.ForEach&lt;TSource,TLocal&gt;</a>, we need to provide three delegates instead of one.&#160; The most basic version of this function is declared as:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> ParallelLoopResult ForEach&lt;TSource, TLocal&gt;(
    IEnumerable&lt;TSource&gt; source,
    Func&lt;TLocal&gt; localInit,
    Func&lt;TSource, ParallelLoopState, TLocal, TLocal&gt; body,
    Action&lt;TLocal&gt; localFinally
)</pre>
<p>The first delegate (the <em>localInit</em> argument) is defined as Func&lt;TLocal&gt;.&#160; This delegate initializes our local state.&#160; It should return some object we can use to track the results of a <strong>single thread’s operations</strong>.</p>
<p>The second delegate (the <em>body </em>argument) is where our main processing occurs, although now, instead of being an Action&lt;T&gt;, we actually provide a Func&lt;TSource, ParallelLoopState, TLocal, TLocal&gt; delegate.&#160; This delegate will receive three arguments: our original element from the collection (TSource), a <a href="http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/" target="_blank">ParallelLoopState which we can use for early termination</a>, and the instance of our local state we created (TLocal).&#160; It should do whatever processing you wish to occur per element, then <strong>return the value of the local state </strong>after processing is completed.</p>
<p>The third delegate (the <em>localFinally</em> argument) is defined as Action&lt;TLocal&gt;.&#160; This delegate is passed our local state after it’s been processed by all of the elements this thread will handle.&#160; This is where you can merge your final results together.&#160; This may require synchronization, but now, instead of synchronizing once per element (potentially millions of times), you’ll only have to synchronize <em>once per thread</em>, which is an ideal situation.</p>
<p>Now that I’ve explained <em>how </em>this works, lets look at the code:</p>
<pre class="csharpcode"><span class="rem">// Safe, and fast!</span>
<span class="kwrd">double</span> min = <span class="kwrd">double</span>.MaxValue;
<span class="rem">// Make a &quot;lock&quot; object</span>
<span class="kwrd">object</span> syncObject = <span class="kwrd">new</span> <span class="kwrd">object</span>();
Parallel.ForEach(
    collection,
    <span class="rem">// First, we provide a local state initialization delegate.</span>
    () =&gt; <span class="kwrd">double</span>.MaxValue,
    <span class="rem">// Next, we supply the body, which takes the original item, loop state,</span>
    <span class="rem">// and local state, and returns a new local state</span>
    (item, loopState, localState) =&gt;
    {
        <span class="kwrd">double</span> <span class="kwrd">value</span> = item.PerformComputation();
        <span class="kwrd">return</span> System.Math.Min(localState, <span class="kwrd">value</span>);
    },
    <span class="rem">// Finally, we provide an Action&lt;TLocal&gt;, to &quot;merge&quot; results together</span>
    localState =&gt;
    {
        <span class="rem">// This requires locking, but it's only once per used thread</span>
        <span class="kwrd">lock</span>(syncObj)
            min = System.Math.Min(min, localState);
    }
);</pre>
<p>Although this is a bit more complicated than the previous version, it is now both thread-safe, and has minimal locking.&#160; </p>
<p>This same approach can be used by Parallel.For, although now, it’s <a href="http://msdn.microsoft.com/en-us/library/dd783299(VS.100).aspx" target="_blank">Parallel.For&lt;TLocal&gt;</a>.&#160; When working with <a href="threadsafe" target="_blank">Parallel.For&lt;TLocal&gt;</a>, you use the same triplet of delegates, with the same purpose and results.</p>
<p>Also, many times, you can completely avoid locking by using a method of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.aspx" target="_blank">Interlocked</a> class to perform the final aggregation in an atomic operation.&#160; The <a href="http://msdn.microsoft.com/en-us/library/dd460703(VS.100).aspx" target="_blank">MSDN example demonstrating this same technique using Parallel.For</a> uses the Interlocked class instead of a lock, since they are doing a sum operation on a long variable, which is possible via <a href="http://msdn.microsoft.com/en-us/library/x629ff68.aspx" target="_blank">Interlocked.Add</a>.</p>
<p>By taking advantage of local state, we can use the Parallel class methods to parallelize algorithms such as <a href="http://en.wikipedia.org/wiki/Aggregate_function" target="_blank">aggregation</a>, which, at first, may seem like poor candidates for parallelization.&#160; Doing so requires careful consideration, and often requires a slight redesign of the algorithm, but the performance gains can be significant if handled in a way to avoid excessive synchronization.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 3, Imperative Data Parallelism: Early Termination</title>
		<link>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/</link>
		<comments>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 19:08:37 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/</guid>
		<description><![CDATA[Although simple data parallelism allows us to easily parallelize many of our iteration statements, there are cases that it does not handle well.&#160; In my previous discussion, I focused on data parallelism with no shared state, and where every element is being processed exactly the same. Unfortunately, there are many common cases where this does [...]]]></description>
			<content:encoded><![CDATA[<p>Although <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">simple data parallelism</a> allows us to easily parallelize many of our <a href="http://msdn.microsoft.com/en-us/library/32dbftby.aspx" target="_blank">iteration statements</a>, there are cases that it does not handle well.&#160; In my previous discussion, I focused on data parallelism with no shared state, and where every element is being processed exactly the same.</p>
<p>Unfortunately, there are many common cases where this does not happen.&#160; If we are dealing with a loop that requires <a href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx" target="_blank">early termination</a>, extra care is required when parallelizing.</p>
<p> <span id="more-156"></span>
<p>Often, while processing in a loop, once a certain condition is met, it is no longer necessary to continue processing.&#160; This may be a matter of finding a specific element within the collection, or reaching some error case.&#160; The important distinction here is that, it is often impossible to know until runtime, what set of elements needs to be processed.</p>
<p>In my <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">initial discussion of data parallelism</a>, I mentioned that this technique is a candidate when you can <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decompose</a> the problem based on the data involved, and you wish to apply a single operation concurrently on all of the elements of a collection.&#160; This covers many of the potential cases, but sometimes, after processing some of the elements, we need to stop processing.</p>
<p>As an example, lets go back to our <a href="http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/" target="_blank">previous Parallel.ForEach example</a> with contacting a customer.&#160; However, this time, we’ll change the requirements slightly.&#160; In this case, we’ll add an extra condition – if the store is unable to email the customer, we will exit gracefully.&#160; The thinking here, of course, is that if the store is currently unable to email, the next time this operation runs, it will handle the same situation, so we can just skip our processing entirely.&#160; The original, serial case, with this extra condition, might look something like the following:</p>
<pre class="csharpcode"><span class="kwrd">foreach</span>(var customer <span class="kwrd">in</span> customers)
{
    <span class="rem">// Run some process that takes some time...</span>
    DateTime lastContact = theStore.GetLastContact(customer);
    TimeSpan timeSinceContact = DateTime.Now - lastContact;

    <span class="rem">// If it's been more than two weeks, send an email, and update...</span>
    <span class="kwrd">if</span> (timeSinceContact.Days &gt; 14)
    {
         <span class="rem">// Exit gracefully if we fail to email, since this </span>
         <span class="rem">// entire process can be repeated later without issue.</span>
         <span class="kwrd">if</span> (theStore.EmailCustomer(customer) == <span class="kwrd">false</span>)
             <span class="kwrd">break</span>;
         customer.LastEmailContact = DateTime.Now;
    }
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Here, we’re processing our loop, but at any point, if we fail to send our email successfully, we just abandon this process, and assume that it will get handled correctly the next time our routine is run.&#160; If we try to parallelize this using Parallel.ForEach, as we did previously, we’ll run into an error almost immediately: the <a href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx" target="_blank">break statement</a> we’re using is only valid when enclosed within an <a href="http://msdn.microsoft.com/en-us/library/32dbftby.aspx" target="_blank">iteration statement</a>, such as <a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx" target="_blank">foreach</a>.&#160; When we switch to Parallel.ForEach, we’re no longer within an iteration statement – we’re a delegate running in a method.</p>
<p>This needs to be handled slightly differently when parallelized.&#160; Instead of using the <a href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx" target="_blank">break</a> statement, we need to utilize a new class in the Task Parallel Library: <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate(VS.100).aspx" target="_blank">ParallelLoopState</a>.&#160; The ParallelLoopState class is intended to allow concurrently running loop bodies a way to interact with each other, and provides us with a way to break out of a loop.&#160; In order to use this, we will use a different overload of <a href="http://msdn.microsoft.com/en-us/library/dd992198(VS.100).aspx" target="_blank">Parallel.ForEach</a> which takes an IEnumerable&lt;T&gt; and an Action&lt;T, ParallelLoopState&gt; instead of an Action&lt;T&gt;.&#160; Using this, we can parallelize the above operation by doing:</p>
<pre class="csharpcode">Parallel.ForEach(customers, (customer, parallelLoopState) =&gt;
{
    <span class="rem">// Run some process that takes some time...</span>
    DateTime lastContact = theStore.GetLastContact(customer);
    TimeSpan timeSinceContact = DateTime.Now - lastContact;

    <span class="rem">// If it's been more than two weeks, send an email, and update...</span>
    <span class="kwrd">if</span> (timeSinceContact.Days &gt; 14)
    {
         <span class="rem">// Exit gracefully if we fail to email, since this </span>
         <span class="rem">// entire process can be repeated later without issue.</span>
         <span class="kwrd">if</span> (theStore.EmailCustomer(customer) == <span class="kwrd">false</span>)
             parallelLoopState.Break();
         <span class="kwrd">else</span>
             customer.LastEmailContact = DateTime.Now;
    }
});</pre>
<p>There are a couple of important points here.&#160; First, we didn’t actually instantiate the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate_members(VS.100).aspx" target="_blank">ParallelLoopState</a> instance.&#160; It was provided directly to us via the Parallel class.&#160; All we needed to do was change our <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expression</a> to reflect that we want to use the loop state, and the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(VS.100).aspx" target="_blank">Parallel class</a> creates an instance for our use.&#160; We also needed to change our logic slightly when we call Break().&#160; Since Break() doesn’t stop the program flow within our block, we needed to add an else case to only set the property in customer when we succeeded.&#160; This <a href="http://msdn.microsoft.com/en-us/library/dd460721(VS.100).aspx" target="_blank">same technique can be used to break out of a Parallel.For loop</a>.</p>
<p>That being said, there is a huge difference between using ParallelLoopState to cause early termination and to use break in a standard iteration statement.&#160; When dealing with a loop serially, <a href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx" target="_blank">break</a> will immediately terminate the processing within the closest enclosing loop statement.&#160; Calling ParallelLoopState.Break(), however, has a very different behavior.</p>
<p>The issue is that, now, we’re no longer processing one element at a time.&#160; If we break in one of our threads, there are other threads that will likely still be executing.&#160; This leads to an important observation about termination of parallel code:</p>
<p><strong>Early termination in parallel routines is not immediate.&#160; Code will continue to run after you request a termination.</strong></p>
<p>This may seem problematic at first, but it is something you just need to keep in mind while designing your routine.&#160; <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">ParallelLoopState.Break()</a> should be thought of as a request.&#160; We are telling the runtime that no elements that were in the collection past the element we’re currently processing need to be processed, and leaving it up to the runtime to decide how to handle this as gracefully as possible.&#160; Although this may seem problematic at first, it is a good thing.&#160; If the runtime tried to immediately stop processing, many of our elements would be partially processed.&#160; It would be like putting a return statement in a random location throughout our loop body – which could have horrific consequences to our code’s maintainability.</p>
<p>In order to understand and effectively write parallel routines, we, as developers, need a subtle, but profound shift in our thinking.&#160; We can no longer think in terms of sequential processes, but rather need to think in terms of requests to the system that may be handled differently than we’d first expect.&#160; This is more natural to developers who have dealt with asynchronous models previously, but is an important distinction when moving to concurrent programming models.</p>
<p>As an example, I’ll discuss the Break() method.&#160; <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">ParallelLoopState.Break()</a> functions in a way that may be unexpected at first.&#160; When you call <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">Break()</a> from a loop body, the runtime will continue to process <em>all elements of the collection that were found prior to the element that was being processed when the Break() method was called</em>.&#160; This is done to keep the behavior of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">Break()</a> method as close to the behavior of the <a href="http://msdn.microsoft.com/en-us/library/adbctzc4.aspx" target="_blank">break</a> statement as possible. We can see the behavior in this simple code:</p>
<pre class="csharpcode">var collection = Enumerable.Range(0, 20);
var pResult = Parallel.ForEach(collection, (element, state) =&gt;
{
    <span class="kwrd">if</span> (element &gt; 10)
    {
        Console.WriteLine(<span class="str">&quot;Breaking on {0}&quot;</span>, element);
        state.Break();
    }
    Console.WriteLine(element);
});</pre>
<p>If we run this, we get a result that may seem unexpected at first:</p>
<pre>0
2
1
5
6
3
4
10
Breaking on 11
11
Breaking on 12
12
9
Breaking on 13
13
7
8
Breaking on 15
15</pre>
<p>What is occurring here is that we loop until we find the first element where the element is greater than 10.&#160; In this case, this was found, the first time, when one of our threads reached element 11.&#160; It requested that the loop stop by calling <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">Break()</a> at this point.&#160; However, the loop continued processing until all of the elements less than 11 were completed, then terminated.&#160; This means that it will guarantee that elements 9, 7, and 8 are completed before it stops processing.&#160; You can see our other threads that were running each tried to break as well, but since Break() was called on the element with a value of 11, it decides which elements (0-10) must be processed.</p>
<p>If this behavior is not desirable, there is another option.&#160; Instead of calling <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(VS.100).aspx" target="_blank">ParallelLoopState.Break()</a>, you can call <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.stop(VS.100).aspx" target="_blank">ParallelLoopState.Stop()</a>.&#160; The Stop() method requests that the runtime terminate as soon as possible , without guaranteeing that any other elements are processed.&#160; Stop() will not stop the processing within an element, so elements already being processed will continue to be processed.&#160; It will prevent new elements, even ones found earlier in the collection, from being processed.&#160; Also, when Stop() is called, the ParallelLoopState’s <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.isstopped(VS.100).aspx" target="_blank">IsStopped</a> property will return true.&#160; This lets longer running processes poll for this value, and return after performing any necessary cleanup.</p>
<p>The basic rule of thumb for choosing between Break() and Stop() is the following.</p>
<ul>
<li>Use ParallelLoopState.Stop() when possible, since it terminates more quickly.&#160; This is particularly useful in situations where you are searching for an element or a condition in the collection.&#160; Once you’ve found it, you do not need to do any other processing, so Stop() is more appropriate. </li>
<li>Use ParallelLoopState.Break() if you need to more closely match the behavior of the C# break statement. </li>
</ul>
<p>Both methods behave differently than our C# break statement.&#160; Unfortunately, when parallelizing a routine, more thought and care needs to be put into every aspect of your routine than you may otherwise expect.&#160; This is due to my second observation: </p>
<p><strong>Parallelizing a routine will almost always change its behavior.</strong></p>
<p>This sounds crazy at first, but it’s a concept that’s so simple its easy to forget.&#160; We’re purposely telling the system to process more than one thing at the same time, which means that the sequence in which things get processed is no longer deterministic.&#160; It is easy to change the behavior of your routine in very subtle ways by introducing parallelism.&#160; Often, the changes are not avoidable, even if they don’t have any adverse side effects.&#160; This leads to my final observation for this post:</p>
<p><strong>Parallelization is something that should be handled with care and forethought, added by design, and not just introduced casually.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/22/parallelism-in-net-part-3-imperative-data-parallelism-early-termination/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 2, Simple Imperative Data Parallelism</title>
		<link>http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/</link>
		<comments>http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 01:15:46 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/</guid>
		<description><![CDATA[In my discussion of Decomposition of the problem space, I mentioned that Data Decomposition is often the simplest abstraction to use when trying to parallelize a routine.&#160; If a problem can be decomposed based off the data, we will often want to use what MSDN refers to as Data Parallelism as our strategy for implementing [...]]]></description>
			<content:encoded><![CDATA[<p>In my discussion of <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/">Decomposition</a> of the problem space, I mentioned that Data Decomposition is often the simplest abstraction to use when trying to parallelize a routine.&#160; If a problem can be decomposed based off the data, we will often want to use what MSDN refers to as <a href="http://msdn.microsoft.com/en-us/library/dd537608(VS.100).aspx">Data Parallelism</a> as our strategy for implementing our routine.&#160; The Task Parallel Library in .NET 4 makes implementing Data Parallelism, for most cases, very simple.</p>
<p> <span id="more-155"></span>
<p>Data Parallelism is the main technique we use to parallelize a routine which can be decomposed based off data.&#160; Data Parallelism refers to taking a single collection of data, and having a single operation be performed concurrently on elements in the collection.&#160; </p>
<p>One side note here: Data Parallelism is also sometimes referred to as the <em>Loop Parallelism Pattern</em> or <em>Loop-level Parallelism.&#160; </em>In general, for this series, I will try to use the terminology used in the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx">MSDN Documentation for the Task Parallel Library</a>.&#160; This should make it easier to investigate these topics in more detail.</p>
<p>Once we’ve determined we have a problem that, potentially, can be decomposed based on data, implementation using Data Parallelism in the TPL is quite simple.&#160; Let’s take our example from the <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/">Data Decomposition discussion</a> – a simple contrast stretching filter.&#160; Here, we have a collection of data (pixels), and we need to run a simple operation on each element of the pixel.&#160; Once we know the minimum and maximum values, we most likely would have some simple code like the following:</p>
<pre class="csharpcode"><span class="kwrd">for</span> (<span class="kwrd">int</span> row=0; row &lt; pixelData.GetUpperBound(0); ++row)
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> col=0; col &lt; pixelData.GetUpperBound(1); ++col)
    {
        pixelData[row, col] = AdjustContrast(pixelData[row, col], minPixel, maxPixel);
    }
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>This simple routine loops through a two dimensional array of pixelData, and calls the AdjustContrast routine on each pixel.</p>
<p>As I mentioned, when you’re <a href="http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/" target="_blank">decomposing a problem space</a>, most <a href="http://msdn.microsoft.com/en-us/library/32dbftby.aspx" target="_blank">iteration statements</a> are potentially candidates for data decomposition.&#160; Here, we’re using two for loops – one looping through rows in the image, and a second nested loop iterating through the columns.&#160; We then perform one, independent operation on each element based on those loop positions.</p>
<p>This is a prime candidate – we have no shared data, no dependencies on anything but the pixel which we want to change.&#160; Since we’re using a for loop, we can easily parallelize this using the <a href="http://msdn.microsoft.com/en-us/library/dd783539(VS.100).aspx" target="_blank">Parallel.For</a> method in the TPL:</p>
<pre class="csharpcode">Parallel.For(0, pixelData.GetUpperBound(0), row =&gt;
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> col=0; col &lt; pixelData.GetUpperBound(1); ++col)
    {
        pixelData[row, col] = AdjustContrast(pixelData[row, col], minPixel, maxPixel);
    }
});</pre>
<p>Here, by simply changing our first for loop to a call to <a href="http://msdn.microsoft.com/en-us/library/dd783539(VS.100).aspx" target="_blank">Parallel.For</a>, we can parallelize this portion of our routine.&#160; Parallel.For works, as do many methods in the TPL, by creating a <a href="http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx" target="_blank">delegate</a> and using it as an argument to a method.&#160; In this case, our for loop iteration block becomes a delegate creating via a <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expression</a>.&#160; This lets you write code that, superficially, looks similar to the familiar for loop, but functions quite differently at runtime.</p>
<p>We could easily do this to our second for loop as well, but that may not be a good idea.&#160; There is a balance to be struck when writing parallel code.&#160; We want to have enough work items to keep all of our processors busy, but the more we partition our data, the more overhead we introduce.&#160; In this case, we have an image of data – most likely hundreds of pixels in both dimensions.&#160; By just parallelizing our first loop, each row of pixels can be run as a single task.&#160; With hundreds of rows of data, we are providing fine enough granularity to keep all of our processors busy.</p>
<p>If we parallelize both loops, we’re potentially creating millions of independent tasks.&#160; This introduces extra overhead with no extra gain, and will actually reduce our overall performance.&#160; This leads to my first guideline when writing parallel code:</p>
<p><strong>Partition your problem into enough tasks to keep each processor busy throughout the operation, but not more than necessary to keep each processor busy.</strong></p>
<p>Also note that I parallelized the outer loop.&#160; I could have just as easily partitioned the inner loop.&#160; However, partitioning the inner loop would have led to many more discrete work items, each with a smaller amount of work (operate on one pixel instead of one row of pixels).&#160; My second guideline when writing parallel code reflects this:</p>
<p><strong>Partition your problem in a way to place the most work possible into each task.</strong></p>
<p>This typically means, in practice, that you will want to parallelize the routine at the “highest” point possible in the routine, typically the outermost loop.&#160; If you’re looking at parallelizing methods which call other methods, you’ll want to try to partition your work high up in the stack – as you get into lower level methods, the performance impact of parallelizing your routines may not overcome the overhead introduced.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd783539(VS.100).aspx" target="_blank">Parallel.For</a> works great for situations where we know the number of elements we’re going to process in advance.&#160; If we’re iterating through an <a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx" target="_blank">IList&lt;T&gt;</a> or an array, this is a typical approach.&#160; However, there are other <a href="http://msdn.microsoft.com/en-us/library/32dbftby.aspx" target="_blank">iteration statements</a> common in C#.&#160; In many situations, we’ll use <a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx" target="_blank">foreach</a> instead of a for loop.&#160; This can be more understandable and easier to read, but also has the advantage of working with collections which only implement <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" target="_blank">IEnumerable&lt;T&gt;</a>, where we do not know the number of elements involved in advance.</p>
<p>As an example, lets take the following situation.&#160; Say we have a collection of Customers, and we want to iterate through each customer, check some information about the customer, and if a certain case is met, send an email to the customer and update our instance to reflect this change.&#160; Normally, this might look something like:</p>
<pre class="csharpcode"><span class="kwrd">foreach</span>(var customer <span class="kwrd">in</span> customers)
{
    <span class="rem">// Run some process that takes some time...</span>
    DateTime lastContact = theStore.GetLastContact(customer);
    TimeSpan timeSinceContact = DateTime.Now - lastContact;

    <span class="rem">// If it's been more than two weeks, send an email, and update...</span>
    <span class="kwrd">if</span> (timeSinceContact.Days &gt; 14)
    {
       theStore.EmailCustomer(customer);
       customer.LastEmailContact = DateTime.Now;
    }
}</pre>
<p>Here, we’re doing a fair amount of work for each customer in our collection, but we don’t know how many customers exist.&#160; If we assume that theStore.GetLastContact(customer) and theStore.EmailCustomer(customer) are both side-effect free, thread safe operations, we could parallelize this using <a href="http://msdn.microsoft.com/en-us/library/dd992001(VS.100).aspx" target="_blank">Parallel.ForEach</a>:</p>
<pre class="csharpcode">Parallel.ForEach(customers, customer =&gt;
{
    <span class="rem">// Run some process that takes some time...</span>
    DateTime lastContact = theStore.GetLastContact(customer);
    TimeSpan timeSinceContact = DateTime.Now - lastContact;

    <span class="rem">// If it's been more than two weeks, send an email, and update...</span>
    <span class="kwrd">if</span> (timeSinceContact.Days &gt; 14)
    {
       theStore.EmailCustomer(customer);
       customer.LastEmailContact = DateTime.Now;
    }
});</pre>
<p>Just like Parallel.For, we rework our loop into a method call accepting a delegate created via a <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expression</a>.&#160; This keeps our new code very similar to our original iteration statement, however, this will now execute in parallel.&#160; The same guidelines apply with Parallel.ForEach as with Parallel.For.</p>
<p>The other iteration statements, <a href="http://msdn.microsoft.com/en-us/library/370s1zax.aspx" target="_blank">do</a> and <a href="http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx" target="_blank">while</a>, do not have direct equivalents in the Task Parallel Library.&#160; These, however, are very easy to <a href="http://blogs.msdn.com/pfxteam/archive/2009/08/12/9867246.aspx" target="_blank">implement using Parallel.ForEach</a> and the <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx" target="_blank">yield keyword</a>.</p>
<p>Most applications can benefit from implementing some form of <a href="http://msdn.microsoft.com/en-us/library/dd537608(VS.100).aspx" target="_blank">Data Parallelism</a>.&#160; Iterating through collections and performing “work” is a very common pattern in nearly every application.&#160; When the problem can be decomposed by data, we often can parallelize the workload by merely changing foreach statements to Parallel.ForEach method calls, and for loops to Parallel.For method calls.&#160; Any time your program operates on a collection, and does a set of work on each item in the collection where that work is not dependent on other information, you very likely have an opportunity to parallelize your routine.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Part 1, Decomposition</title>
		<link>http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/</link>
		<comments>http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 23:50:21 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/</guid>
		<description><![CDATA[The first step in designing any parallelized system is Decomposition.&#160; Decomposition is nothing more than taking a problem space and breaking it into discrete parts.&#160; When we want to work in parallel, we need to have at least two separate things that we are trying to run.&#160; We do this by taking our problem and [...]]]></description>
			<content:encoded><![CDATA[<p>The first step in designing any parallelized system is <a href="http://en.wikipedia.org/wiki/Decomposition_(computer_science)"><strong>Decomposition</strong></a>.&#160; Decomposition is nothing more than taking a problem space and breaking it into discrete parts.&#160; When we want to work in parallel, we need to have at least two separate things that we are trying to run.&#160; We do this by taking our problem and decomposing it into parts.</p>
<p>There are two common abstractions that are useful when discussing parallel decomposition: <strong>Data Decomposition </strong>and <strong>Task Decomposition</strong>.&#160; These two abstractions allow us to think about our problem in a way that helps leads us to correct decision making in terms of the algorithms we’ll use to parallelize our routine.</p>
<p> <span id="more-154"></span>
<p>To start, I will make a couple of minor points.</p>
<ul>
<li>I’d like to stress that Decomposition has nothing to do with specific algorithms or techniques.&#160; It’s about how you approach and think about the problem, not how you solve the problem using a specific tool, technique, or library.&#160; Decomposing the problem is about constructing the appropriate mental model: once this is done, you can choose the appropriate design and tools, which is a subject for future posts.</li>
<li>Decomposition, being unrelated to tools or specific techniques, is not specific to .NET in any way.&#160; This should be the first step to parallelizing a problem, and is valid using any framework, language, or toolset.&#160; However, this gives us a starting point – without a proper understanding of decomposition, it is difficult to understand the proper usage of specific classes and tools within the .NET framework.</li>
</ul>
<p><strong>Data Decomposition </strong>is often the simpler abstraction to use when trying to parallelize a routine.&#160; In order to decompose our problem domain by data, we take our entire set of data and break it into smaller, discrete portions, or chunks.&#160; We then work on each chunk in the data set in parallel.</p>
<p>This is particularly useful if we can process each element of data independently of the rest of the data.&#160; In a situation like this, there are some wonderfully simple techniques we can use to take advantage of our data.&#160; By decomposing our domain by data, we can very simply parallelize our routines.&#160; In general, we, as developers, should be always searching for data that can be decomposed.</p>
<p>Finding data to decompose if fairly simple, in many instances.&#160; Data decomposition is typically used with collections of data.&#160; Any time you have a collection of items, and you’re going to perform work on or with each of the items, you potentially have a situation where parallelism can be exploited.&#160; This is fairly easy to do in practice: look for <a href="http://msdn.microsoft.com/en-us/library/32dbftby.aspx">iteration statements</a> in your code, such as <a href="http://msdn.microsoft.com/en-us/library/ch45axte.aspx">for</a> and <a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx">foreach</a>.</p>
<p>Granted, every for loop is not a candidate to be parallelized.&#160; If the collection is being modified as it’s iterated, or the processing of elements depends on other elements, the iteration block may need to be processed in serial.&#160; However, if this is not the case, data decomposition may be possible.</p>
<p>Let’s look at one example of how we might use data decomposition.&#160; Suppose we were working with an image, and we were applying a simple <a href="http://www.ph.tn.tudelft.nl/Courses/FIP/noframes/fip-istogram.html">contrast stretching filter</a>.&#160; When we go to apply the filter, once we know the minimum and maximum values, we can apply this to each pixel independently of the other pixels.&#160; This means that we can easily decompose this problem based off <em>data</em> – we will do the same operation, in parallel, on individual chunks of data (each pixel).</p>
<p><strong>Task Decomposition</strong>, on the other hand, is focused on the individual tasks that need to be performed instead of focusing on the data.&#160; In order to decompose our problem domain by tasks, we need to think about our algorithm in terms of discrete operations, or tasks, which can then later be parallelized.</p>
<p>Task decomposition, in practice, can be a bit more tricky than data decomposition.&#160; Here, we need to look at what our algorithm actually does, and how it performs its actions.&#160; Once we have all of the basic steps taken into account, we can try to analyze them and determine whether there are any constraints in terms of shared data or ordering.&#160; There are no simple things to look for in terms of finding tasks we can decompose for parallelism; every algorithm is unique in terms of its tasks, so every algorithm will have unique opportunities for task decomposition.</p>
<p>For example, say we want our software to perform some customized actions on startup, prior to showing our main screen.&#160; Perhaps we want to check for proper licensing, notify the user if the license is not valid, and also check for updates to the program.&#160; Once we verify the license, and that there are no updates, we’ll start normally.&#160; In this case, we can decompose this problem into <em>tasks </em>– we have a few tasks, but there are at least two discrete, independent tasks (check licensing, check for updates) which we can perform in parallel.&#160; Once those are completed, we will continue on with our other tasks.</p>
<p>One final note – Data Decomposition and Task Decomposition are not mutually exclusive.&#160; Often, you’ll mix the two approaches while trying to parallelize a single routine.&#160; It’s possible to decompose your problem based off data, then further decompose the processing of each element of data based on tasks.&#160; </p>
<p>This just provides a framework for thinking about our algorithms, and for discussing the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/19/parallelism-in-net-part-1-decomposition/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Parallelism in .NET &#8211; Introduction</title>
		<link>http://reedcopsey.com/2010/01/19/parallelism-in-net-introduction/</link>
		<comments>http://reedcopsey.com/2010/01/19/parallelism-in-net-introduction/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 20:50:24 +0000</pubDate>
		<dc:creator>Reed</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Parallelism]]></category>
		<category><![CDATA[.NET 4]]></category>
		<category><![CDATA[C# 4]]></category>

		<guid isPermaLink="false">http://reedcopsey.com/2010/01/19/parallelism-in-net-introduction/</guid>
		<description><![CDATA[Parallel programming is something that every professional developer should understand, but is rarely discussed or taught in detail in a formal manner.&#160; Software users are no longer content with applications that lock up the user interface regularly, or take large amounts of time to process data unnecessarily.&#160; Modern development requires the use of parallelism.&#160; There [...]]]></description>
			<content:encoded><![CDATA[<p>Parallel programming is something that every professional developer should understand, but is rarely discussed or taught in detail in a formal manner.&#160; Software users are no longer content with applications that lock up the user interface regularly, or take large amounts of time to process data unnecessarily.&#160; Modern development requires the use of parallelism.&#160; There is no longer any excuses for us as developers.</p>
<p>Learning to write parallel software is challenging.&#160; It requires more than reading that one chapter on parallelism in our programming language book of choice…</p>
<p> <span id="more-153"></span>
<p>Today’s systems are no longer getting faster with each generation; in many cases, newer computers are actually slower than previous generation systems.&#160; Modern hardware is shifting towards conservation of power, with processing scalability coming from having multiple computer cores, not faster and faster CPUs.&#160; Our CPU frequencies no longer double on a regular basis, but <a href="http://en.wikipedia.org/wiki/Moores_law">Moore’s Law</a> is still holding strong.&#160; Now, however, instead of scaling transistors in order to make processors faster, hardware manufacturers are scaling the transistors in order to add more discrete hardware processing threads to the system.</p>
<p>This changes how we should think about software.&#160; In order to take advantage of modern systems, we need to <a href="http://en.wikipedia.org/wiki/Moores_law#Parallelism_and_Moore.27s_law">redesign and rewrite our algorithms to work in parallel</a>.&#160; As with any design domain, it helps tremendously to have a common language, as well as a common set of patterns and tools.</p>
<p>For .NET developers, this is an exciting time for parallel programming.&#160; Version 4 of the .NET Framework is adding the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx">Task Parallel Library</a>.&#160; This has been back-ported to .NET 3.5sp1 as part of the <a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx">Reactive Extensions for .NET</a>, and is available for use today in both .NET 3.5 and .NET 4.0 beta.</p>
<p>In order to fully utilize the <a href="http://msdn.microsoft.com/en-us/library/dd460717(VS.100).aspx">Task Parallel Library</a> and parallelism, both in .NET 4 and previous versions, we need to understand the proper terminology.&#160; For this series, I will provide an introduction to some of the basic concepts in parallelism, and relate them to the tools available in .NET.</p>
]]></content:encoded>
			<wfw:commentRss>http://reedcopsey.com/2010/01/19/parallelism-in-net-introduction/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
 
