SESSION CODE: DEV308 Joseph Albahari FUTURE OF .NET PARALLEL PROGRAMMING (c) 2011 Microsoft. All rights reserved.

Download Report

Transcript SESSION CODE: DEV308 Joseph Albahari FUTURE OF .NET PARALLEL PROGRAMMING (c) 2011 Microsoft. All rights reserved.

SESSION CODE: DEV308
Joseph Albahari
FUTURE OF .NET PARALLEL
PROGRAMMING
(c) 2011 Microsoft. All rights reserved.
JOE ALBAHARI
www.albahari.com
(c) 2011 Microsoft. All rights reserved.
Agenda
► Parallel Programming in Framework 4.0 recap
► TPL Dataflow in vNext
(c) 2011 Microsoft. All rights reserved.
CPU Clock Speeds over Past 25 Years
(logarithmic)
(c) 2011 Microsoft. All rights reserved.
Intel’s 80-core
prototype
(c) 2011 Microsoft. All rights reserved.
Parallel APIs in Framework 4.0
PLINQ
Parallel
Loops
Tasks
CLR Thread Pool
(c) 2011 Microsoft. All rights reserved.
Concurrent
Collections
Fork/Join
Join
Fork
(c) 2011 Microsoft. All rights reserved.
Parallel Programming APIs
API
PLINQ
Parallel Loops
Fork Join
Functional
Imperative
Task Parallelism
Imperative
Task Parallel Library (TPL)
(c) 2011 Microsoft. All rights reserved.
Task vs. Data Parallelism
PLINQ
T1
T2
T3
T4
T1
T2
T3
T4
Parallel
Loops
Data
Tasks
(c) 2011 Microsoft. All rights reserved.
PLINQ
► Highest-level parallel API
► Data parallelism
► Declarative
► Transparently parallelizes LINQ queries
(c) 2011 Microsoft. All rights reserved.
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var query =
from
where
orderby
select
n in names
n.Contains ("a")
n.Length
n.ToUpper();
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var query = names
.Where
(n => n.Contains ("a"))
.OrderBy (n => n.Length)
.Select (n => n.ToUpper());
PLINQ – gotchas
► Query needs ‘meat’
► Original ordering lost (by default)
► Functional impurity can break queries
► No work-stealing with range partitioning
(c) 2011 Microsoft. All rights reserved.
var numbers = new[] { 2, 3, 4, 5, 6 }
.AsParallel();
Root-mean-square
Math.Sqrt (numbers.Average (n => n * n))
Standard deviation
double mean = numbers.Average();
double sdev = Math.Sqrt (numbers.Average (n =>
{
double dif = n - mean;
return dif * dif;
}));
PLINQ – Tricks & Tips
► Can do I/O bound queries
– but there are better ways
► .ForAll() defeats data collation
► Aggregations are parallelizable
albahari.com/threading
(c) 2011 Microsoft. All rights reserved.
Parallel Loops
PLINQ
Parallel
Loops
Tasks
CLR Thread Pool
(c) 2011 Microsoft. All rights reserved.
Concurrent
Collections
Parallel Loops
Parallel.For
Parallel.ForEach
(c) 2011 Microsoft. All rights reserved.
Sequential
for (int i = 1; i < 1000; i++)
Foo (i);
Parallel
Parallel.For (1, 1000, i =>
Foo (i)
);
(c) 2011 Microsoft. All rights reserved.
Parallel Unit Testing
var toTest = new TypeToTest();
var methodsToTest =
from m in toTest.GetType().GetMethods()
where m.GetCustomAttributes (false)
.OfType<TestAttribute>().Any()
select m;
Parallel.ForEach (methodsToTest, m =>
{
try { m.Invoke (instanceToTest, null)); }
catch (Exception ex) { ... }
}
(c) 2011 Microsoft. All rights reserved.
Tricks & Tips
► Best with moderately sized work items
– 1µs to 100ms
► Avoid for I/O bound work
► Parallel.For = range partitioning + work stealing
(c) 2011 Microsoft. All rights reserved.
Task Parallelism
PLINQ
Parallel
Loops
Tasks
CLR Thread Pool
(c) 2011 Microsoft. All rights reserved.
Concurrent
Collections
Task Parallelism
►Parallel.Invoke(Action[] actions)
►Task / Task<T>
(c) 2011 Microsoft. All rights reserved.
Task and Task<TResult>
A task represents a
concurrent operation
(c) 2011 Microsoft. All rights reserved.
Tasks have two purposes
1. For multithreading
–
–
Task.Factory.StartNew()
TaskScheduler
Including
Parallel programming
Gets you into the thread pool
Task
2. A value-added signaling construct
–
–
I/O bound
Asynchronous
Pattern
TaskCompletionSource
new TaskCompletionSource<>()
(c) 2011 Microsoft. All rights reserved.
Tasks: Tricks and Tips
► Be careful with continuations
– Consider async CTP if you need them
– Conditional continuations are particularly nasty
► Exception-handle ‘set-and-forget’ tasks
(c) 2011 Microsoft. All rights reserved.
MANDELBROT DEMO
(c) 2011 Microsoft. All rights reserved.
TPL Dataflow CTP
PLINQ
Parallel
Loops
Dataflow
Tasks
CLR Thread Pool
(c) 2011 Microsoft. All rights reserved.
Concurrent
Collections
TPL Dataflow
► High throughput, low-latency scenarios
► Typical applications:
–
–
–
–
–
–
Manufacturing
Imaging
Biology
Oil & Gas
Finance
Robotics
(c) 2011 Microsoft. All rights reserved.
TPL Dataflow
AAL
(C++)
CCR
(MS Robotics)
TPL
Dataflow
Axum
Dataflow Blocks
Buffering
Execution
Joining
BufferBlock
ActionBlock
BatchBlock
BroadcastBlock
TransformBlock
JoinBlock
WriteOnceBlock TransformManyBlock BatchedJoinBlock
(c) 2011 Microsoft. All rights reserved.
ActionBlock<TInput>
Action<TInput> delegate
(c) 2011 Microsoft. All rights reserved.
ActionBlock<TInput>
Action<TInput> delegate
(c) 2011 Microsoft. All rights reserved.
Multiple Blocks
Action<TInput>
Action<TInput>
(c) 2011 Microsoft. All rights reserved.
MaxDegreeOfParallelism
Action<TInput>
MaxDegreeOfParallelism = 2
(c) 2011 Microsoft. All rights reserved.
TransformBlock<TInput, TOutput>
Func<TInput,TOutput>
(c) 2011 Microsoft. All rights reserved.
TransformManyBlock<TInput, TOutput>
Func<TInput,TOutput>
(c) 2011 Microsoft. All rights reserved.
TransformBlock + ActionBlock
Action<>
Func<,>
Action<>
(c) 2011 Microsoft. All rights reserved.
Mandelbrot Pipeline
Func<Rect,Frame>
Render
Func<Frame,Stream>
Encode
Action<Stream>
Save
(c) 2011 Microsoft. All rights reserved.
Dataflow Blocks
Buffering
Execution
Joining
BufferBlock
ActionBlock
BatchBlock
BroadcastBlock
TransformBlock
JoinBlock
WriteOnceBlock TransformManyBlock BatchedJoinBlock
(c) 2011 Microsoft. All rights reserved.
BatchBlock<>
(c) 2011 Microsoft. All rights reserved.
JoinBlock<>
(c) 2011 Microsoft. All rights reserved.
Joining Blocks: Greedy by default
(c) 2011 Microsoft. All rights reserved.
Non-greedy
(c) 2011 Microsoft. All rights reserved.
DataFlow Blocks
Buffering
Execution
Joining
BufferBlock
ActionBlock
BatchBlock
BroadcastBlock
TransformBlock
JoinBlock
WriteOnceBlock TransformManyBlock BatchedJoinBlock
(c) 2011 Microsoft. All rights reserved.
BufferBlock<>
(c) 2011 Microsoft. All rights reserved.
BroadcastBlock<>
“overwrite buffer”
(c) 2011 Microsoft. All rights reserved.
WriteOnceBlock<>
(c) 2011 Microsoft. All rights reserved.
Dataflow Blocks
Buffering
Execution
Joining
BufferBlock
ActionBlock
BatchBlock
BroadcastBlock
TransformBlock
JoinBlock
WriteOnceBlock TransformManyBlock BatchedJoinBlock
(c) 2011 Microsoft. All rights reserved.
Extensibility & Interoperability
► Custom data blocks
► Rx:
– AsObservable()
– AsObserver()
(c) 2011 Microsoft. All rights reserved.
Enrol in Microsoft Virtual Academy Today
Why Enroll, other than it being free?
The MVA helps improve your IT skill set and advance your career with a free, easy to access training
portal that allows you to learn at your own pace, focusing on Microsoft technologies.
What Do I get for enrolment?
►
►
►
Free training to make you become the Cloud-Hero in my Organization
Help mastering your Training Path and get the recognition
Connect with other IT Pros and discuss The Cloud
Where do I Enrol?
www.microsoftvirtualacademy.com
Then tell us what you think. [email protected]
© 2010 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.
(c) 2011 Microsoft. All rights reserved.