Parallel Programming


Published on

Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. This presentation is all about parallel programming and its features.

Published in: Software
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Parallel Programming

  1. 1. PARALLEL PROGRAMMING Programming to leverage multicores or multiple processors is called parallel programming. This is a subset of the broader concept of multithreading. - ANUJ KUMAR SAHU
  2. 2. AGENDA • Introduction and Concept • PLINQ • TPL - Task Parallelism • Parallel Class • Demos
  3. 3. INTRODUCTION • Many personal computers and workstations have two or four cores (that is, CPUs) that enable multiple threads to be executed simultaneously. • To take advantage of the hardware of today and tomorrow, you can parallelize your code to distribute work across multiple processors. • In the past, parallelization required low-level manipulation of threads and locks. • Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. • These APIs are collectively known (loosely) as PFX (Parallel Framework).
  4. 4. Image Source -
  5. 5. CONCEPT Two strategies for partitioning work among threads: data parallelism and task parallelism  Data parallelism - When a set of tasks must be performed on many data values, we can parallelize by having each thread perform the (same) set of tasks on a subset of values. • PLINQ • Parallel Class  Task parallelism - partition the tasks i.e. each thread perform a different task. • is the lowest-level approach to parallelization with PFX. • tuned for leveraging multicores. • Classes - Task ; Task<TResult> ; TaskFactory etc…
  6. 6. PLINQ • PLINQ automatically parallelizes local LINQ queries. • To use PLINQ, simply call AsParallel() on the input sequence and then continue the LINQ query as usual. • PLINQ is only for local collections: it doesn’t work with LINQ to SQL or Entity Framework because in those cases the LINQ translates into SQL which then executes on a database server. • If you need order preservation, you can force it by calling AsOrdered() after AsParallel(); however incurs a performance hit with large numbers of elements. • Does not works - Take, TakeWhile, Skip, and SkipWhile; indexed versions of Select, SelectMany, and ElementAt.
  7. 7. PLINQ - CODE
  8. 8. PARALLEL CLASS • Three static methods in the Parallel class o Parallel.Invoke - can execute several delegates in parallel. It takes array of tasks (Actions) as parameters. o Parallel.For - fromInclusive, toExclusive and step. o Parallel.ForEach - is a multi-threaded implementation of a common loop construct in C#, the foreach loop. It has numerous overloads; the most commonly used has the following signature: public static ParallelLoopResult ForEach<TSource>( IEnumerable<TSource> source, Action<TSource> body)
  9. 9. TPL – TASK PARALLEL LIBRARY Class Purpose Task For managing a unit for work Task<TResult> For managing a unit for work with a return value TaskFactory For creating tasks TaskFactory<TResult> For creating tasks and continuations with the same return type TaskScheduler For managing the scheduling of tasks TaskCompletionSource For manually controlling a task’s workflow
  10. 10. WHY TASK AND NOT THREADS? • Reference • The creation of a thread comes with a huge cost. Creating a huge number of Threads within your application also comes with an overhead of Context Switching. In a single core environment, it might lead to a bad performance as well, since we have a single core which serves various threads.The task on the other hand, dynamically calculates if it needs to create different threads of execution or not. It uses the ThreadPool under the hood, in order to distribute the work, without going through the overhead of Thread creation/or un-necessary context switching if not required.
  11. 11. TPL Syntax • Creating a task o Task.Factory.StartNew (() => Console.WriteLine ("Hello from a task!")); o var task = new Task (() => Console.Write ("Hello")); ... task.Start(); • Waiting on Tasks o Wait o WaitAny o WaitAll • Continuations o ContinueWith o ContinueWhenAll • And many mores….
  12. 12. ADDITONAL POINTS • Date Race condition • Debugging //example was built by Microsoft.
  13. 13. NOT COVERED • Error handling • Cancellation • New .NET 4.5 async and await Features
  14. 14. REFRENCES • • MSDN • Plural Sight • aspx • Code Project • Samples