1. Parallel Programing with .Net<br />Table of Contents TOC quot;
1-3quot;
Parallel Programming PAGEREF _Toc284352133 3Data Parallelism PAGEREF _Toc284352134 3Task Parallelism PAGEREF _Toc284352135 5Create Tasks PAGEREF _Toc284352136 6Cancelling Task PAGEREF _Toc284352137 9Wait until Task Execution Completed PAGEREF _Toc284352138 11Cancelling Several Tasks PAGEREF _Toc284352139 12Monitoring Cancellation with a Delegate PAGEREF _Toc284352140 14<br />Parallel Programming<br />Now day’s computers are coming with multiple processors that enable multiple threads to be executed simultaneously to give performance of applications and we can expect significantly more CPUs in near future. If application is doing CPU intensive tasks and we find that one CPU is taking 100 %usage and others are idle. It might be situation when one thread is doing cpu intensive work and other threads are doing non cpu intensive work. In this case application is not utilizing all CPUs potential here. To get benefits all CPUs Microsoft launches Parallel Programming Library in DotNet Framework 4.0. <br />We can say “Programming to leverage multicores or multiple processors is called parallel programming”. This is a subset of the broader concept of multithreading.<br />To use your system’s CPU resources efficiently, you need to split your application into pieces that can run at the same time and we have CPU intensive task that should be breaks into parts like below:<br />Partition it into small chunks. <br />Execute those chunks in parallel via multithreading. <br />Collate the results as they become available, in a thread-safe and performant manner. <br />We can also achieve this in traditional way of multithreading but partitioning and collating of chunks can be nightmare because we would need to put locking for thread safety and lot of synchronizatopm to collate everything. Parallel programming library has been designed to help in such scenarios. You don’t need to worry about partitioning and collating of chunks. These chunks will run in parallel on different CPUs.<br />There can be two kind of strategy for partitioning work among threads:<br />Data Parallelism: This strategy fits in scenario in which same operation is performed concurrently on elements like collections, arrays etc. <br />Task Parallelism: This strategy suggests independent tasks running concurrently. <br />Data Parallelism<br />In this parallelism, collection or array is partitioned so that multiple threads can operate on different segments concurrently. DotNet supports data parallelism by introducing System.Threading.Tasks.Parallel static class. This class is having methods like Parallel.For, Parallel.ForEach etc. These methods automatically partitioned data on different threads, you don’t need to write explicit implementation of thread execution. <br />Below is simple example of Parallel.For Method and you can see how it has utilize all cores and you are not using any synchronization here:<br />Parallel.For(0, 20, t => { Console.WriteLine(quot;
Thread{0}:Value:{1}quot;
,Thread.CurrentThread.ManagedThreadId,t); });<br />Output <br />Thread10:Value:0 Thread7:Value:5 Thread10:Value:1 Thread6:Value:10 Thread10:Value:2 Thread10:Value:3 Thread10:Value:4 Thread10:Value:8 Thread10:Value:9 Thread10:Value:13 Thread10:Value:14 Thread10:Value:16 Thread10:Value:17 Thread10:Value:18 Thread10:Value:19 Thread12:Value:15 Thread6:Value:11 Thread6:Value:12 Thread7:Value:6 Thread7:Value:7 <br />Above example tells you how Parallel class is utilizing all cores. Now I am giving you example of performance of Parallel loop for CPU intensive tasks over sequential loop.<br />System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();<br /> //Sequential Loop<br /> stopwatch.Start();<br /> for(int i=0;i<20000;i++)<br /> {<br /> double temp = i * new Random().Next() + Math.PI;<br /> }<br /> stopwatch.Stop();<br /> Console.WriteLine(quot;
Total Time (in milliseconds) Taken by Sequential loop: {0}quot;
,stopwatch.ElapsedMilliseconds);<br /> stopwatch.Reset();<br /> //Parallel Loop<br /> stopwatch.Start();<br /> Parallel.For(0, 20000, t => <br /> {<br /> double temp = t * new Random().Next() +Math.PI;<br /> });<br /> stopwatch.Stop();<br /> Console.WriteLine(quot;
Total Time (in milliseconds) Taken by Parallel loop: {0}quot;
, stopwatch.ElapsedMilliseconds);<br />Output<br /> <br />Total Time (in milliseconds) Taken by Sequential loop: 159Total Time (in milliseconds) Taken by Parallel loop: 80<br />Task Parallelism<br />This is strategy to run independent task in parallel way. It focuses on distributing execution process (threads) across different parallel computing nodes.Task parallelism emphasizes the distributed (parallelized) nature of the processing (i.e. threads), as opposed to the data parallelism. Most real programs fall somewhere on a continuum between Task parallelism and Data parallelism. Workflow of task parallelism is below:<br /> <br />Dot Net Framework provides Task Parallel Library (TPL) to achieve Task Parallelism. This library provides two primary benefits:<br />More Efficient and more scalable use of system resources. <br />More programmatic control than is possible with a thread or work item.<br />Behind the scenes tasks are queued in ThreadPool, which has been enhanced with algorithms in .Net 4.0 that determine and adjust to the number of threads that maximizes throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. To complement this, widely-known work-stealing algorithms are employed to provide load-balancing.<br />This library provides more features to control tasks like: cancellation, continuation, waiting, robust exception handling, scheduling etc.<br />The classes for TaskParallelism are defined in System.Threading.Tasks:<br />ClassPurposeTaskFor running unit of work concurrently.Task<Result>For managing unit of work with return valueTaskFactoryFactory Class to create Task class Instance.TaskFactory<TResult>Factory Class to create Task class Instance and return value.TaskSchedulerfor scheduling tasks.TaskCompletionSourceFor manually controlling a task’s workflow<br />Create Tasks<br />Task Creation and Execution can be done by two ways: Implicit and Explicit.<br />Create and Execute Task Implicitly<br />Parallel.Invoke method helps to to run unit of work in parallel. you can just pass any number of Action delegates as parameters. The no. of tasks created by Invoke method is not necessarily equal to Action delegates provided because this method automatically does some optimization specially in this case.<br />Source Code<br /> private static void Run2()<br /> {<br /> Thread.Sleep(1000);<br /> Console.WriteLine(quot;
Run2: My Thread Id {0}quot;
, Thread.CurrentThread.ManagedThreadId);<br /> }<br /> private static void Run1()<br /> {<br /> Thread.Sleep(1000);<br /> Console.WriteLine(quot;
Run1: My Thread Id {0}quot;
, Thread.CurrentThread.ManagedThreadId);<br /> }<br /> static void Main(string[] args)<br /> {<br /> //Create and Run task implicitly<br /> Parallel.Invoke(() => Run1(), () => Run2());<br /> Console.ReadLine();<br /> }<br />Output<br />Run2: My Thread Id 11 Run1: My Thread Id 10<br />This approach of creating task does not give greater control over task execution, scheduling etc. Better approach is to create task by TaskFactory class.<br />Create and Execute Task Explicitly<br />You can create task by creating instance of task class and pass delegate which encapsulate the code that task will execute. These delegate can be anonyms, Action delegate, lambda express and method name etc.<br />Example of creating tasks:<br />Source Code:<br /> private static void Run2()<br /> {<br /> Thread.Sleep(1000);<br /> Console.WriteLine(quot;
Run2: My Thread Id {0}quot;
, Thread.CurrentThread.ManagedThreadId);<br /> }<br /> private static void Run1()<br /> {<br /> Thread.Sleep(1000);<br /> Console.WriteLine(quot;
Run1: My Thread Id {0}quot;
, Thread.CurrentThread.ManagedThreadId);<br /> }<br /> static void Main(string[] args)<br /> {<br /> // Create a task and supply a user delegate by using a lambda expression.<br /> // use an Action delegate and a named method<br /> Task task1 = new Task(new Action(Run1));<br /> // use a anonymous delegate<br /> Task task2 = new Task(delegate<br /> {<br /> Run1();<br /> });<br /> // use a lambda expression and a named method<br /> Task task3 = new Task(() => Run1());<br /> // use a lambda expression and an anonymous method<br /> Task task4 = new Task(() =><br /> {<br /> Run1();<br /> });<br /> task1.Start();<br /> task2.Start();<br /> task3.Start();<br /> task4.Start();<br /> Console.ReadLine();<br /> }<br />Output<br />Run1: My Thread Id 13 Run1: My Thread Id 12 Run1: My Thread Id 11 Run1: My Thread Id 14<br />If you don’t want to create and starting of task separated then you can use TaskFactory class. Task exposes “Factory” property which is instance of TaskFactory class.<br />Task task5= Task.Factory.StartNew(()=> {Run1();});<br />Task with Return Values <br />To get value from when task completes it execution, you can use generic version of Task class. <br /> public static void Main(string[] args)<br /> {<br /> //This will return string result<br /> Task task = Task.Factory.StartNew(() => ReturnString());<br /> Console.WriteLine(task.Result);// Wait for task to finish and fetch result.<br /> Console.ReadLine();<br /> <br /> }<br /> private static string ReturnString()<br /> {<br /> return quot;
Neerajquot;
;<br /> }<br />Task State <br />If you are running multiple tasks same time and you want to track progress of each task then using quot;
Statequot;
object is better approach. <br /> <br />public static void Main(string[] args)<br /> {<br /> //This will return string result<br /> for (int i = 0; i < 5; i++)<br /> {<br /> Task task = Task.Factory.StartNew(state => ReturnString(), i.ToString());<br /> //Show progress of task<br /> Console.WriteLine(quot;
Progress of this task {0}: {1}quot;
, i, task.AsyncState.ToString());<br /> }<br /> <br /> Console.ReadLine();<br /> }<br /> private static void ReturnString()<br /> {<br /> //DO something here<br /> // Console.WriteLine(quot;
helloquot;
);<br /> }<br />Output<br />Progress of this task 0: 0 Progress of this task 1: 1 Progress of this task 2: 2 Progress of this task 3: 3 Progress of this task 4: 4 <br />Task execution can be cancelled through use of cancellation Token which is new in DotNet Framework4.0. Task class supports Cancellation with the integration with System.Threading.CancellationTokenSource class and the System.Threading.CancellationToken class. Many of the constructors in the System.Threading.Tasks.Task class take a CancellationToken as an input parameter. Many of the StartNew overloads also take a CancellationToken.<br />CancellationTokenSource contains CancellationToken and Cancel method through which cancellation request can be raised. I’ll cover following type of cancellation here:<br />Cancelling a task. <br />Cancelling Many Tasks <br />Monitor tokens <br />Cancelling Task<br />Following steps will describe how to cancel a task:<br />First create instance of CancellationTokenSource class <br />Create instance of CancellationToken by setting Token property of CancellationTokenSource class. <br />Start task by TaskFactory.StartNew method or Task.Start(). <br />Check for token.IsCancellationRequested property or token.ThrowIfCancellationRequested() for Cancellation Request. <br />Execute Cancel method of CancellationTokenSource class to send cancellation request to Task. <br />SourceCode<br />[sourcecode language=quot;
csharpquot;
firstline=quot;
1quot;
padlinenumbers=quot;
falsequot;
] <br />CancellationTokenSource tokenSource = new CancellationTokenSource();<br /> CancellationToken token = tokenSource.Token;<br /> int i = 0;<br /> Console.WriteLine(quot;
Calling from Main Thread {0}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId);<br /> var task = Task.Factory.StartNew(() =><br /> {<br /> while (true)<br /> {<br /> if (token.IsCancellationRequested)<br /> {<br />Console.WriteLine(quot;
Task cancel detectedquot;
);<br />throw new OperationCanceledException(token);<br /> }<br /> }<br /> });<br /> Console.WriteLine(quot;
Cancelling taskquot;
);<br /> tokenSource.Cancel();<br />[/sourcecode] <br /> <br />When tokenSource.Cancel method execute then token.IsCancellationRequested property will gets true then you need to cancel execution of task. In above example I am throwing OperationCanceledException which should have parameter as token, but you need to catch this exception otherwise it will give error “Unhandled Exception”. If you don’t want to throw exception explicitly then you can use ThrowIfCancellationRequested method which internally throw OperationCanceledException and no need to explicitly check for token.IsCancellationRequested property.<br />[sourcecode language=quot;
csharpquot;
firstline=quot;
1quot;
padlinenumbers=quot;
falsequot;
] <br /> CancellationTokenSource tokenSource = new CancellationTokenSource();<br /> CancellationToken token = tokenSource.Token;<br /> int i = 0;<br /> Console.WriteLine(quot;
Calling from Main Thread {0}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId);<br /> var task = Task.Factory.StartNew(() =><br /> {<br /> while (true)<br /> {<br /> try<br /> {<br /> token.ThrowIfCancellationRequested();<br /> }<br /> catch (OperationCanceledException)<br /> {<br /> Console.WriteLine(quot;
Task cancel detectedquot;
);<br /> break;<br /> }<br /> //if (token.IsCancellationRequested)<br /> //{<br /> // Console.WriteLine(quot;
Task cancel detectedquot;
);<br /> // throw new OperationCanceledException(token);<br /> //}<br /> Console.WriteLine(quot;
Thread:{0} Printing: {1}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId, i++);<br /> }<br /> });<br /> Console.WriteLine(quot;
Cancelling taskquot;
);<br /> Thread.Sleep(10);<br /> tokenSource.Cancel();<br /> Console.WriteLine(quot;
Task Status:{0}quot;
, task.Status);<br />[/sourcecode] <br />Output<br />Calling from Main Thread 10 Cancelling task Thread:6 Printing: 0 Thread:6 Printing: 1 Thread:6 Printing: 2 Thread:6 Printing: 3 Thread:6 Printing: 4 Thread:6 Printing: 5 Thread:6 Printing: 6 Thread:6 Printing: 7 Thread:6 Printing: 8 Thread:6 Printing: 9 Task Status:Running Task cancel detected <br />Wait until Task Execution Completed<br />You can see TaskStatus is showing status as “Running'” in above output besides Cancel method fired before than task status. So to avoid execution next statement after cancel method we should wait for task to be in complete phase for this we can use Wait method of task class.<br />[sourcecode language=quot;
csharpquot;
firstline=quot;
1quot;
padlinenumbers=quot;
falsequot;
] <br /> Console.WriteLine(quot;
Cancelling taskquot;
);<br /> Thread.Sleep(10);<br /> tokenSource.Cancel();<br /> Console.WriteLine(quot;
Task Status:{0}quot;
, task.Status);<br /> task.Wait();//wait for thread to completes its execution<br /> Console.WriteLine(quot;
Task Status:{0}quot;
, task.Status);<br /> [/sourcecode] <br />Output<br />Calling from Main Thread 9 Cancelling task Thread:6 Printing: 0 Thread:6 Printing: 1 Thread:6 Printing: 2 Thread:6 Printing: 3 Thread:6 Printing: 4 Thread:6 Printing: 5 Task cancel detected Task Status:RanToCompletion<br />Cancelling Several Tasks<br />You can use one instance of token to cancel several tasks like in below example:<br />[sourcecode language=quot;
csharpquot;
firstline=quot;
1quot;
padlinenumbers=quot;
falsequot;
] <br />public void CancelSeveralTasks()<br /> {<br /> CancellationTokenSource tokenSource = new CancellationTokenSource();<br /> CancellationToken token = tokenSource.Token;<br /> int i = 0;<br /> Console.WriteLine(quot;
Calling from Main Thread {0}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId);<br /> Task t1 = new Task(() =><br /> {<br /> while (true)<br /> {<br /> try<br /> {<br /> token.ThrowIfCancellationRequested();<br /> }<br /> catch (OperationCanceledException)<br /> {<br /> Console.WriteLine(quot;
Task1 cancel detectedquot;
);<br /> break;<br /> }<br /> Console.WriteLine(quot;
Task1: Printing: {1}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId, i++);<br /> }<br /> }, token);<br /> Task t2 = new Task(() =><br /> {<br /> while (true)<br /> {<br /> try<br /> {<br /> token.ThrowIfCancellationRequested();<br /> }<br /> catch (OperationCanceledException)<br /> {<br /> Console.WriteLine(quot;
Task2 cancel detectedquot;
);<br /> break;<br /> }<br /> Console.WriteLine(quot;
Task2: Printing: {1}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId, i++);<br /> }<br /> });<br /> t1.Start();<br /> t2.Start();<br /> Thread.Sleep(100);<br /> tokenSource.Cancel();<br /> t1.Wait();//wait for thread to completes its execution<br /> t2.Wait();//wait for thread to completes its execution<br /> Console.WriteLine(quot;
Task1 Status:{0}quot;
, t1.Status);<br /> Console.WriteLine(quot;
Task2 Status:{0}quot;
, t1.Status);<br /> }<br />[/sourcecode] <br />Output<br />Calling from Main Thread 9 Task1: Printing: 0 Task1: Printing: 1 Task1: Printing: 2 Task1: Printing: 3 Task1: Printing: 4 Task1: Printing: 5 Task1: Printing: 6 Task1: Printing: 7 Task1: Printing: 8 Task1: Printing: 9 Task1: Printing: 10 Task1: Printing: 11 Task1: Printing: 12 Task1: Printing: 14 Task1: Printing: 15 Task1: Printing: 16 Task1: Printing: 17 Task1: Printing: 18 Task1: Printing: 19 Task2: Printing: 13 Task2: Printing: 21 Task2: Printing: 22 Task2: Printing: 23 Task2: Printing: 24 Task2: Printing: 25 Task2: Printing: 26 Task2: Printing: 27 Task2: Printing: 28 Task1 cancel detected Task2 cancel detected Task1 Status:RanToCompletion Task2 Status:RanToCompletion <br />Monitoring Cancellation with a Delegate<br />You can register delegate to get status of cancellation as callback. This is useful if your task is doing some other asynchronous operations. It can be useful in showing cancellation status on UI.<br />[sourcecode language=quot;
csharpquot;
firstline=quot;
1quot;
padlinenumbers=quot;
falsequot;
] <br />public void MonitorTaskwithDelegates()<br /> {<br /> CancellationTokenSource tokenSource = new CancellationTokenSource();<br /> CancellationToken token = tokenSource.Token;<br /> int i = 0;<br /> Console.WriteLine(quot;
Calling from Main Thread {0}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId);<br /> Task t1 = new Task(() =><br /> {<br /> while (true)<br /> {<br /> try<br /> {<br /> token.ThrowIfCancellationRequested();<br /> }<br /> catch (OperationCanceledException)<br /> {<br /> Console.WriteLine(quot;
Task1 cancel detectedquot;
);<br /> break;<br /> }<br /> Console.WriteLine(quot;
Task1: Printing: {1}quot;
, System.Threading.Thread.CurrentThread.ManagedThreadId, i++);<br /> }<br /> }, token);<br /> //Register Cancellation Delegate<br /> token.Register(new Action(GetStatus));<br /> t1.Start();<br /> Thread.Sleep(10);<br /> //cancelling task<br /> tokenSource.Cancel();<br /> }<br /> public void GetStatus()<br /> {<br /> Console.WriteLine(quot;
Cancelled calledquot;
);<br /> }<br />[/sourcecode]<br />Output <br />Calling from Main Thread 10 Task1: Printing: 0 Task1: Printing: 1 Task1: Printing: 2 Task1: Printing: 3 Task1: Printing: 4 Task1: Printing: 5 Task1: Printing: 6 Task1: Printing: 7 Task1: Printing: 8 Task1: Printing: 9 Task1: Printing: 10 Task1: Printing: 11 Task1: Printing: 12 Cancelled called Task1 cancel detected<br />