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

56

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

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

(c) 2011 Microsoft. All rights reserved.

FUTURE OF .NET PARALLEL PROGRAMMING

Joseph Albahari

SESSION CODE: DEV308

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

(c) 2011 Microsoft. All rights reserved.

JOE ALBAHARIwww.albahari.com

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

(c) 2011 Microsoft. All rights reserved.

Agenda

► Parallel Programming in Framework 4.0 recap

► TPL Dataflow in vNext

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

(c) 2011 Microsoft. All rights reserved.

CPU Clock Speeds over Past 25 Years

(logarithmic)

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

(c) 2011 Microsoft. All rights reserved.

Intel’s 80-core prototype

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

(c) 2011 Microsoft. All rights reserved.

Parallel APIs in Framework 4.0

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

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

(c) 2011 Microsoft. All rights reserved.

Fork/Join

Fork Join

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

(c) 2011 Microsoft. All rights reserved.

Parallel Programming APIs

API Fork JoinPLINQ

Parallel Loops

Task Parallelism

Functional

Imperative

Imperative

Task Parallel Library (TPL)

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

Task vs. Data Parallelism

(c) 2011 Microsoft. All rights reserved.

Tasks

Data

T1 T2 T3 T4

T1 T2 T3 T4

Parallel Loops

PLINQ

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

(c) 2011 Microsoft. All rights reserved.

PLINQ

► Highest-level parallel API► Data parallelism► Declarative► Transparently parallelizes LINQ queries

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

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

var query =    from n in names    where n.Contains ("a")     orderby n.Length    select 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());

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

(c) 2011 Microsoft. All rights reserved.

PLINQ – gotchas

► Query needs ‘meat’ ► Original ordering lost (by default)► Functional impurity can break queries► No work-stealing with range partitioning

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

Math.Sqrt (numbers.Average (n => n * n))

Root-mean-square

double mean = numbers.Average();double sdev = Math.Sqrt (numbers.Average (n =>              {                 double dif = n - mean;                 return dif * dif;              }));

Standard deviation

var numbers = new[] { 2, 3, 4, 5, 6 } .AsParallel();

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

(c) 2011 Microsoft. All rights reserved.

PLINQ – Tricks & Tips

► Can do I/O bound queries– but there are better ways

► .ForAll() defeats data collation► Aggregations are parallelizable

albahari.com/threading

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

(c) 2011 Microsoft. All rights reserved.

Parallel Loops

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

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

(c) 2011 Microsoft. All rights reserved.

Parallel Loops

Parallel.ForParallel.ForEach

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

(c) 2011 Microsoft. All rights reserved.

Parallel.For (1, 1000, i => Foo (i));

for (int i = 1; i < 1000; i++) Foo (i);

Sequential

Parallel

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

(c) 2011 Microsoft. All rights reserved.

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) { ... }}

Parallel Unit Testing

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

(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

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

(c) 2011 Microsoft. All rights reserved.

Task Parallelism

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

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

(c) 2011 Microsoft. All rights reserved.

Task Parallelism

►Parallel.Invoke(Action[] actions)

►Task / Task<T>

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

(c) 2011 Microsoft. All rights reserved.

Task and Task<TResult>

A task represents aconcurrent operation

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

(c) 2011 Microsoft. All rights reserved.

Tasks have two purposes

1. For multithreading– Including

Parallel programming– Gets you into the thread pool

2. A value-added signaling construct– I/O bound– Asynchronous

Pattern

Task

TaskScheduler

TaskCompletionSource

Task.Factory.StartNew()

new TaskCompletionSource<>()

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

(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

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

(c) 2011 Microsoft. All rights reserved.

MANDELBROT DEMO

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

(c) 2011 Microsoft. All rights reserved.

TPL Dataflow CTP

Tasks

Parallel LoopsPLINQ

Concurrent Collections

CLR Thread Pool

Dataflow

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

(c) 2011 Microsoft. All rights reserved.

TPL Dataflow

► High throughput, low-latency scenarios► Typical applications:– Manufacturing– Imaging– Biology– Oil & Gas– Finance– Robotics

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

TPL Dataflow

TPL Dataflow

AAL (C++)

CCR(MS Robotics) Axum

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

(c) 2011 Microsoft. All rights reserved.

Dataflow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

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

(c) 2011 Microsoft. All rights reserved.

ActionBlock<TInput>

Action<TInput> delegate

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

(c) 2011 Microsoft. All rights reserved.

Action<TInput> delegate

ActionBlock<TInput>

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

(c) 2011 Microsoft. All rights reserved.

Multiple Blocks

Action<TInput>

Action<TInput>

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

(c) 2011 Microsoft. All rights reserved.

Action<TInput>

MaxDegreeOfParallelism

MaxDegreeOfParallelism = 2

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

(c) 2011 Microsoft. All rights reserved.

TransformBlock<TInput, TOutput>

Func<TInput,TOutput>

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

(c) 2011 Microsoft. All rights reserved.

TransformManyBlock<TInput, TOutput>

Func<TInput,TOutput>

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

(c) 2011 Microsoft. All rights reserved.

TransformBlock + ActionBlock

Func<,> Action<>

Action<>

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

(c) 2011 Microsoft. All rights reserved.

Mandelbrot Pipeline

Func<Rect,Frame>

Func<Frame,Stream>

Action<Stream>

Render

Encode

Save

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

(c) 2011 Microsoft. All rights reserved.

Dataflow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

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

(c) 2011 Microsoft. All rights reserved.

BatchBlock<>

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

JoinBlock<>

(c) 2011 Microsoft. All rights reserved.

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

Joining Blocks: Greedy by default

(c) 2011 Microsoft. All rights reserved.

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

Non-greedy

(c) 2011 Microsoft. All rights reserved.

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

(c) 2011 Microsoft. All rights reserved.

DataFlow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

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

(c) 2011 Microsoft. All rights reserved.

BufferBlock<>

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

(c) 2011 Microsoft. All rights reserved.

BroadcastBlock<>

“overwrite buffer”

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

(c) 2011 Microsoft. All rights reserved.

WriteOnceBlock<>

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

(c) 2011 Microsoft. All rights reserved.

Dataflow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

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

(c) 2011 Microsoft. All rights reserved.

Extensibility & Interoperability

► Custom data blocks

► Rx:– AsObservable()– AsObserver()

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

Enrol in Microsoft Virtual Academy TodayWhy 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]

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

(c) 2011 Microsoft. All rights reserved.

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