Introducing Parallel Programming with the .NET Framework 4 and Visual Studio 2010 Igor Ostrovsky Software Engineer Corporation Agenda

Why What Managed /runtime (.NET 4) Tools (in the VS2010 IDE) Why The Manycore Shift Why Problem and Goals

Free lunch is over How much will you pay?

Multithreaded programming is hard today

Goals Developer productivity Performance Agenda Checkpoint

Why What Managed APIs/runtime (.NET 4) a.k.a. Parallel Extensions to .NET Tools (in the VS2010 IDE) Parallel Extensions to .NET Overview

Pure .NET libraries Feature areas Task Parallel (TPL) Parallel LINQ (PLINQ) -safe data structures and synchronization primitives Enhanced ThreadPool Task Parallel Library (TPL) Loops for (int i = 0; i < N; i++) { Compute(i); } foreach (T e in data) Compute(e);

Parallel.For(0, N, i => { Compute(i); }); Parallel.ForEach(data, e => Compute(e)); Task Parallel Library (TPL) Loops

DEMO: Raytracer Task Parallel Library (TPL) Regions

A(); B(); C();

ParallelOptions pops = new ParallelOptions() { MaxDegreeOfParallelism = 2, CancellationToken = myToken };

Parallel.Invoke(pops, () => A(), () => B(), () => C()); Task Parallel Library (TPL) Tasks

DEMO: Task examples

DEMO: Reversi Parallel LINQ (PLINQ) Language Integrated Query (LINQ) Overview

Visual Basic C# Others…

.NET Standard Query Operators

LINQ-enabled data sources

LINQ LINQ LINQ To Objects To XML To … Parallel LINQ (PLINQ) LINQ-To-Objects Parallelized var results = from i in arrayOfIntegers where i % 2 == 0 select i; var results = from i in arrayOfIntegers.AsParallel() where i % 2 == 0 select i;

.AsParallel().WithDegreeOfParallelism(4) .WithCancellation(myToken) .WithMergeOptions(ParallelMergeOptions.NotBuffered) .WithExecutionMode(ParallelExecutionMode.ForceParallelism) Parallel LINQ (PLINQ) Supported Operators In .NET 4, ~50 operators w/ ~175 overloads

Aggregate(3) GroupBy(8) SequenceEqual(2) All(1) GroupJoin(2) Single(2) Any(2) Intersect(2) SingleOrDefault(2) AsEnumerable(1) Join(2) Skip(1) Average(20) Last(2) SkipWhile(2) Cast(1) LastOrDefault(2) Sum(20) Concat(1) LongCount(2) Take(1) Contains(2) Max(22) TakeWhile(2) Count(2) Min(22) ThenBy(2) DefaultIfEmpty(2) OfType(1) ThenByDescending(2) Distinct(2) OrderBy(2) ToArray(1) ElementAt(1) OrderByDescending(2) ToDictionary(4) ElementAtOrDefault(1) Range(1) ToList(1) Empty(1) Repeat(1) ToLookup(4) Except(2) Reverse(1) Union(2) First(2) Select(2) Where(2) FirstOrDefault(2) SelectMany(4) Zip(1) Parallel LINQ (PLINQ)

DEMO: Baby Names Thread-safe Data Structures and Sync Primitives

Thread-safe, scalable collections Initialization IProducerConsumerCollection Lazy ConcurrentQueue ConcurrentStack ThreadLocal ConcurrentBag ConcurrentDictionary Locks

ManualResetEventSlim Phases and work exchange Barrier SemaphoreSlim BlockingCollection SpinLock CountdownEvent SpinWait

Partitioning Cancellation {Orderable}Partitioner Partitioner.Create(…) CancellationTokenSource CancellationToken ThreadPool in .NET 3.5

Global Queue Worker Worker Work Item 3 … Thread 0 Thread N

OtherWork Threads Item 21 ThreadPool in .NET 4

Local Local Work- Work- Lock-free Stealing Stealing Global Queue Queue Queue Worker Worker WorkWork ItemItem 453 … Thread 0 Thread N

OtherWork Threads Item 21 Agenda Checkpoint

Why What Managed APIs/runtime (.NET 4) a.k.a. Parallel Extensions to .NET Tools (in the VS2010 IDE) Debugging: Parallel Tasks and Parallel Stacks Concurrency Visualizer Tools Parallel Debugging

Two new debugger toolwindows

“Parallel Tasks” “Parallel Stacks” Tools “Parallel Tasks”

What threads are executing my Tasks? Where are my Tasks running (location, call stack)? What is the status of my Tasks?

Tools “Parallel Stacks”

Zoom Bird’s eye view control

Multiple call stacks in a single view Easy navigation to any executing method Rich UI (zooming, panning, tooltips, etc.)

Tools Concurrency Visualizer Tools Concurrency Visualizer: CPU Utilization View

Other Processes

Number Idle of cores Time Your Tools Concurrency Visualizer: Threads View

Measure time Hide for interesting Uninteresting segments threads

Active Legend

Call Usage Stacks Hints Tools Concurrency Visualizer: Cores View

Color per thread

Migration visualization Related Content

Dev Center: http://msdn.microsoft.com/concurrency Forum: http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/threads Blog: http://blogs.msdn.com/pfxteam/ © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.