Parallel Programming
Upcoming SlideShare
Loading in...5
×
 

Parallel Programming

on

  • 245 views

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 ...

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.

Statistics

Views

Total Views
245
Views on SlideShare
245
Embed Views
0

Actions

Likes
2
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Parallel Programming Parallel Programming Presentation Transcript

  • PARALLEL PROGRAMMING Programming to leverage multicores or multiple processors is called parallel programming. This is a subset of the broader concept of multithreading. - http://www.albahari.com/ ANUJ KUMAR SAHU
  • AGENDA • Introduction and Concept • PLINQ • TPL - Task Parallelism • Parallel Class • Demos
  • 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).
  • Image Source - http://www.albahari.com/
  • 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…
  • 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.
  • PLINQ - CODE
  • 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)
  • 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
  • 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.
  • 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….
  • ADDITONAL POINTS • Date Race condition • Debugging //example was built by Microsoft.
  • NOT COVERED • Error handling • Cancellation • New .NET 4.5 async and await Features
  • REFRENCES • http://www.albahari.com/threading/part5.aspx • MSDN • Plural Sight • http://www.blackwasp.co.uk/ParallelProgramming. aspx • Code Project • Samples