1 / 32

Parallel Computing in . NET 4 and Visual Studio 2010

James Kolpack, InRAD LLC popcyclical.com. Parallel Computing in . NET 4 and Visual Studio 2010. CodeStock is proudly partnered with:. RecruitWise and Staff with Excellence - www.recruitwise.jobs. Send instant feedback on this session via Twitter:

kaili
Download Presentation

Parallel Computing in . NET 4 and Visual Studio 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. James Kolpack, InRADLLCpopcyclical.com Parallel Computingin.NET 4andVisual Studio 2010

  2. CodeStock is proudly partnered with: RecruitWise and Staff with Excellence - www.recruitwise.jobs Send instant feedback on this session via Twitter: Send a direct message with the room number to @CodeStock d codestock 406 This session is great! For more information on sending feedback using Twitter while at CodeStock, please see the “CodeStock README” in your CodeStock guide.

  3. Parallel computing

  4. “Free Lunch” is over - TANSTAAFL

  5. Outline • Parallel Extensions • ImperativeData Parallelization • ImperativeTaskParallelization • Declarative DataParallelization (PLINQ) • Thread-safe Data Structures • Visual Studio 2010 Concurrency Visualizations

  6. Parallel Extensions • Concurrency .NET Library • Lightweight user-mode runtime • Key benefits • Lightweight tasks, efficient scheduling • Programming model improvements • Unified exception models and scheduling • Great for “Delightfully” Parallel Problems • Computation-intensive processes for large data sets

  7. Decomposition • Breaking a problem into discrete parts • Data Decomposition • Iterations (for loops) over data • Simple! • Task Decomposition • Separate operations that can run independently of each other • …Or both!

  8. Data Decomposition • int[,] pixelData = newint[rowLen, colLen]; • for (int y = 0; y < rowLen; y++) • { • for (int x = 0; x < colLen; x++) • { • pixelData[y, x] = TraceRay(y, x); • } • }

  9. Sequential x1 x2 x3 x4 y1 y2 y3 y4 codeSample();

  10. Thread for each Row x1 x2 x3 x4 y1 y2 y3 y4 codeSample();

  11. Threading with 2 Partitions x1 x2 x3 x4 y1 y2 y3 y4 codeSample();

  12. Imbalanced Workload x1 x2 x3 x4 y1 y2 y3 y4

  13. Dynamic Partitioning x1 x2 x3 x4 y1 y2 y3 y4

  14. Partitioning Tradeoffs More Load-Balancing Fully Static Fully Dynamic Less Synchronization

  15. Partitioning Strategies

  16. Partitioning Strategies

  17. Sample Application • For Real Ray Tracer • Samples for Parallel Programming with .NET 4code.msdn.microsoft.com/ParExtSamples

  18. Parallel Extensions Architecture .NET Program Imperative Parallel Algorithms Task Parallel Library (TPL) Data Structures Compiler Concurrent Collections Synchronization Types Coordination Types Parallel Constructs C# VB F# Tasks and Tasks Scheduling C++ Threads (any other) Algorithms … Proc 1 Proc p IL

  19. Lambda Expressions (input parameters) => expression boolMyFunc(int i, string s) { returns.Length > i; } (inti, string s) => s.Length > i () => SomeMethod() (input parameters) => {statement;} void MyAction(inti, string s) { Console.WriteLine( “str: {0} int: {1}”, s, i); } (inti, string s) => { Console.WriteLine( “str: {0} int: {1}”, s, i); }

  20. Generic Delegates TResultFunc<outTResult>()TResultFunc<in T1, outTResult>(in T1) TResultFunc<in T1, in T2, outTResult>(inT1, inT2) Func<int, string, bool> Func<int, string, bool> f =(int i, string s) => s.Length > i; boolMyFunc(int i, string s){} Func<int, string, bool> f = MyFunc; void Action<>()void Action<in T1>(in T1) Void Action<inT1, in T2>(inT1, inT2) Action<int, string> a = (int i, string s) => { Console.WriteLine( “str: {0} int: {1}”, s, i); } void MyAction(inti, string s){} Action<int, string> a = MyAction;

  21. ImperativeData Parallelization • Parallel.For, Parallel.ForEach • Exception handling • Cancelling • Break and Stop • Thread-local state • Nested parallelism • Dynamic thread counts • Efficient load balancing Parallel.For(0, n, i => { // ... }); Parallel.ForEach(data, d => { // ... }); codeSample();

  22. ImperativeTask Parallelization • Tasks - Lighter weight than raw Threads • Intelligent scheduling using ThreadPool • Rich API for fine grained control • Waiting, cancellation, continuations, exceptions, etc • Parallel.Invoke( • () => DoComputation(), • () => DoAnotherCompuation() • ); codeSample();

  23. Concurrent Collections • Concurrent Collections • Thread-safe! • Time-outs and waits • Throttling • Cancellation • Classes • BlockingCollection<T> • ConcurrentDictionary<T> • ConcurrentQueue<T> • ConcurrentStack<T> • ConcurrentBag<T> codeSample();

  24. Parallel Extensions Architecture PLINQ Execution Engine Query Analysis .NET Program Data Partitioning Operator Types Merging Declarative Parallel Queries Imperative Parallel Algorithms Task Parallel Library (TPL) Data Structures Compiler Concurrent Collections Synchronization Types Coordination Types Parallel Constructs C# PLINQ VB F# Tasks and Tasks Scheduling C++ Threads (any other) Algorithms … Proc 1 Proc p IL

  25. Parallel LINQ (PLINQ) • DeclarativeDataParallelization • Describe what we want rather than how to accomplish it • For LINQ to Object queries • System.Linq.Parallel • .AsParallel() codeSample();

  26. PLINQ ForAll codeSample();

  27. PLINQ Performance Considerations • Computational cost of the overall work • The form of query execution • With ToArray or ToList, all results must be merged • The type of merge options • Buffered or Streaming • The kind of partitioning • Sequential mode fall-back varqueryA = fromnuminnumberList.AsParallel() selectExpensiveFunction(num); //good for PLINQvarqueryB = fromnuminnumberList.AsParallel() wherenum % 2 > 0 selectnum; //not so good for PLINQ

  28. Parallel API Review • ImperativeData Parallelization • System.Threading.Tasks.Parallel • ImperativeTask Parallelization • System.Threading.Tasks • DeclarativeDataParallelization (PLINQ) • System.Linq.Parallel • Thread-safe Data Structures • System.Collections.Concurrent Parallel.For(0, n, i => {}); Parallel.ForEach(data, i => {}); vart = Task<TResult> .Factory .StartNew(() => {}); t.Wait(); from n innums.AsParallel() selectExpressiveFunction(n); newBlockingCollection<string>();

  29. Parallel Patterns • Fork/Join • Recursive Decomposition • Aggregations • Dependencies • Producer/Consumer • MapReduce • Fold and Scan • Shared State • Anti-Patterns

  30. Concurrency Visualizers Cores View CPU Utilization View Threads View

  31. Links

More Related