Introducing Parallel Programming with the .NET Framework 4...
Transcript of Introducing Parallel Programming with the .NET Framework 4...
Igor Ostrovsky Software Engineer Microsoft Corporation
Parallel Programming with the .NET Framework 4 and Visual Studio 2010
Introducing
Agenda
Why
What Managed APIs/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 Library (TPL)
Parallel LINQ (PLINQ)
Thread-safe data structures and synchronization primitives
Enhanced ThreadPool
Parallel.For(0, N, i => { Compute(i); }); Parallel.ForEach(data, e => Compute(e));
Task Parallel Library (TPL) Loops
for (int i = 0; i < N; i++) { Compute(i); } foreach (T e in data) Compute(e);
Task Parallel Library (TPL) Loops
DEMO: Raytracer
ParallelOptions pops = new ParallelOptions()
{
MaxDegreeOfParallelism = 2,
CancellationToken = myToken
};
Parallel.Invoke(pops,
() => A(),
() => B(),
() => C());
Task Parallel Library (TPL) Regions
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 To Objects
LINQ To XML
LINQ To …
.AsParallel().WithDegreeOfParallelism(4) .WithCancellation(myToken) .WithMergeOptions(ParallelMergeOptions.NotBuffered) .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
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;
Aggregate(3)
All(1)
Any(2)
AsEnumerable(1)
Average(20)
Cast(1)
Concat(1)
Contains(2)
Count(2)
DefaultIfEmpty(2)
Distinct(2)
ElementAt(1)
ElementAtOrDefault(1)
Empty(1)
Except(2)
First(2)
FirstOrDefault(2)
GroupBy(8)
GroupJoin(2)
Intersect(2)
Join(2)
Last(2)
LastOrDefault(2)
LongCount(2)
Max(22)
Min(22)
OfType(1)
OrderBy(2)
OrderByDescending(2)
Range(1)
Repeat(1)
Reverse(1)
Select(2)
SelectMany(4)
SequenceEqual(2)
Single(2)
SingleOrDefault(2)
Skip(1)
SkipWhile(2)
Sum(20)
Take(1)
TakeWhile(2)
ThenBy(2)
ThenByDescending(2)
ToArray(1)
ToDictionary(4)
ToList(1)
ToLookup(4)
Union(2)
Where(2)
Zip(1)
In .NET 4, ~50 operators w/ ~175 overloads
Parallel LINQ (PLINQ) Supported Operators
Parallel LINQ (PLINQ)
DEMO: Baby Names
Thread-safe Data Structures and Sync Primitives
Thread-safe, scalable collections IProducerConsumerCollection<T>
ConcurrentQueue<T> ConcurrentStack<T> ConcurrentBag<T>
ConcurrentDictionary<TKey, TValue>
Phases and work exchange
Barrier BlockingCollection<T> CountdownEvent
Partitioning
{Orderable}Partitioner<T> Partitioner.Create(…)
Initialization
Lazy<T>
ThreadLocal<T>
Locks
ManualResetEventSlim
SemaphoreSlim
SpinLock
SpinWait
Cancellation
CancellationTokenSource
CancellationToken
ThreadPool in .NET 3.5
Global Queue
Worker Thread 0
Worker Thread N
…
Other Threads
Work Item 3
Work Item 2 Work Item 1
ThreadPool in .NET 4
Lock-free Global Queue
Worker Thread 0
Worker Thread N
…
Other Threads
Local Work-
Stealing Queue
Local Work-
Stealing Queue
Work Item 3 Work Item 4 Work Item 5
Work Item 2 Work Item 1
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”
Multiple call stacks in a single view Easy navigation to any executing method Rich UI (zooming, panning, tooltips, etc.)
Zoom
control Bird’s eye view
Tools Concurrency Visualizer
Tools Concurrency Visualizer: CPU Utilization View
Number of cores
Your Process
Idle Time
Other Processes
Tools Concurrency Visualizer: Threads View
Hide Uninteresting
threads
Active Legend
Call Stacks
Usage Hints
Measure time for interesting
segments
Tools Concurrency Visualizer: Cores View
Migration visualization
Color per thread
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.