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

Preview:

Citation preview

(c) 2011 Microsoft. All rights reserved.

FUTURE OF .NET PARALLEL PROGRAMMING

Joseph Albahari

SESSION CODE: DEV308

(c) 2011 Microsoft. All rights reserved.

JOE ALBAHARIwww.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

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

(c) 2011 Microsoft. All rights reserved.

Fork/Join

Fork Join

(c) 2011 Microsoft. All rights reserved.

Parallel Programming APIs

API Fork JoinPLINQ

Parallel Loops

Task Parallelism

Functional

Imperative

Imperative

Task Parallel Library (TPL)

Task vs. Data Parallelism

(c) 2011 Microsoft. All rights reserved.

Tasks

Data

T1 T2 T3 T4

T1 T2 T3 T4

Parallel Loops

PLINQ

(c) 2011 Microsoft. All rights reserved.

PLINQ

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

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());

(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

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();

(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

(c) 2011 Microsoft. All rights reserved.

Parallel Loops

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

(c) 2011 Microsoft. All rights reserved.

Parallel Loops

Parallel.ForParallel.ForEach

(c) 2011 Microsoft. All rights reserved.

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

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

Sequential

Parallel

(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

(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

Tasks

Parallel Loops

PLINQ

Concurrent Collections

CLR Thread Pool

(c) 2011 Microsoft. All rights reserved.

Task Parallelism

►Parallel.Invoke(Action[] actions)

►Task / Task<T>

(c) 2011 Microsoft. All rights reserved.

Task and Task<TResult>

A task represents aconcurrent operation

(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<>()

(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

Tasks

Parallel LoopsPLINQ

Concurrent Collections

CLR Thread Pool

Dataflow

(c) 2011 Microsoft. All rights reserved.

TPL Dataflow

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

TPL Dataflow

TPL Dataflow

AAL (C++)

CCR(MS Robotics) Axum

(c) 2011 Microsoft. All rights reserved.

Dataflow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

(c) 2011 Microsoft. All rights reserved.

ActionBlock<TInput>

Action<TInput> delegate

(c) 2011 Microsoft. All rights reserved.

Action<TInput> delegate

ActionBlock<TInput>

(c) 2011 Microsoft. All rights reserved.

Multiple Blocks

Action<TInput>

Action<TInput>

(c) 2011 Microsoft. All rights reserved.

Action<TInput>

MaxDegreeOfParallelism

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

Func<,> Action<>

Action<>

(c) 2011 Microsoft. All rights reserved.

Mandelbrot Pipeline

Func<Rect,Frame>

Func<Frame,Stream>

Action<Stream>

Render

Encode

Save

(c) 2011 Microsoft. All rights reserved.

Dataflow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

(c) 2011 Microsoft. All rights reserved.

BatchBlock<>

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.

(c) 2011 Microsoft. All rights reserved.

DataFlow Blocks

Buffering Execution Joining

BufferBlock

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

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

BroadcastBlock

WriteOnceBlock

ActionBlock

TransformBlock

TransformManyBlock

BatchBlock

JoinBlock

BatchedJoinBlock

(c) 2011 Microsoft. All rights reserved.

Extensibility & Interoperability

► Custom data blocks

► Rx:– AsObservable()– AsObserver()

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. TellTheDean@microsoft.com

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

Recommended