Top Banner
25

The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

Apr 05, 2018

Download

Documents

lenhan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft
Page 2: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

The Future of Parallel Programming in

the .NET Framework

Danny Shih

Microsoft Corporation

Program Manager

Page 3: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

3 | The Future of Parallel Programming in the .NET Framework | June 2011

DISCLAIMER

This is a talk about the future…

– All content is subject to change.

– The technology being discussed…

…is almost entirely available in CTP form NOW.

… but may never actually ship (we’re doing the best we can).

Page 4: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

4 | The Future of Parallel Programming in the .NET Framework | June 2011

AGENDA

Present

– Recap of parallel programming in the .NET Framework 4

Future

– Visual Studio Async

– TPL Dataflow

Page 5: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

5 | The Future of Parallel Programming in the .NET Framework | June 2011

RECAP PARALLEL PROGRAMMING IN .NET 4

Feature Areas

– Task Parallel Library (TPL)

– Parallel LINQ (PLINQ)

– Thread-safe data structures and synchronization primitives

Pure .NET libraries

Page 6: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

6 | The Future of Parallel Programming in the .NET Framework | June 2011

AGENDA CHECKPOINT

Present

– Recap of parallel programming in the .NET Framework 4

Future

– Visual Studio Async

– TPL Dataflow

Page 7: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

7 | The Future of Parallel Programming in the .NET Framework | June 2011

VISUAL STUDIO ASYNC

Application Trends

– Increasingly connected

More latency (e.g. everything as a service)

More UI responsiveness problems

More scalability issues

– User -> =(

Page 8: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

8 | The Future of Parallel Programming in the .NET Framework | June 2011

// Synchronous

TResult Foo(...);

// Asynchronous Programming Model (APM)

IAsyncResult BeginFoo(..., AsyncCallback callback, object state);

TResult EndFoo(IAsyncResult asyncResult);

// Event-based Asynchronous Pattern (EAP)

public void FooAsync(...);

public event EventHandler<FooCompletedEventArgs> FooCompleted;

ASYNCHRONOUS PROGRAMMING IN .NET TODAY

Page 9: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

9 | The Future of Parallel Programming in the .NET Framework | June 2011

public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }

SOME SYNCHRONOUS CODE IN .NET 4

Page 10: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

10 | The Future of Parallel Programming in the .NET Framework | June 2011

public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }

public IAsyncResult BeginCopyStreamToStream( Stream source, Stream destination) { var tcs = new TaskCompletionSource<object>(); byte[] buffer = new byte[0x1000]; Action<IAsyncResult> readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) { switch (isRead) { case true: iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); }, null); if (!iar.CompletedSynchronously) return; break; case false: int numRead = source.EndRead(iar); if (numRead == 0) { tcs.TrySetResult(null); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } catch (Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public void EndCopyStreamToStream(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); }

AN EXPERT’S ASYNCHRONOUS CODE IN .NET 4

Page 11: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

11 | The Future of Parallel Programming in the .NET Framework | June 2011

public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }

public async Task CopyStreamToStreamAsync(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0) { await destination.WriteAsync(buffer, 0, numRead); } }

YOUR ASYNCHRONOUS CODE WITH THE VISUAL STUDIO ASYNC CTP

Page 12: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

12 | The Future of Parallel Programming in the .NET Framework | June 2011

VISUAL STUDIO ASYNC – LANGUAGE AND FRAMEWORK

Language

– “async” modifier marks a method or lambda as asynchronous

– “await” operator yields control until the await’ed Task completes

Framework

– Task and Task<TResult> represent ongoing operations

E.g. Async I/O, background work, etc.

Single object for status, result, and exceptions

– New APIs round out the experience

Page 13: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

13 | The Future of Parallel Programming in the .NET Framework | June 2011

// Synchronous

TResult Foo(...);

// Asynchronous Programming Model (APM)

IAsyncResult BeginFoo(..., AsyncCallback callback, object state);

TResult EndFoo(IAsyncResult asyncResult);

// Event-based Asynchronous Pattern (EAP)

public void FooAsync(...);

public event EventHandler<FooCompletedEventArgs> FooCompleted;

// Task-based Asynchronous Pattern (TAP)

Task<TResult> FooAsync(...);

ASYNCHRONOUS PROGRAMMING IN .NET TOMORROW

Page 14: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

14 | The Future of Parallel Programming in the .NET Framework | June 2011

VISUAL STUDIO ASYNC

DEMOS

Visual Studio Async CTP:

– http://msdn.microsoft.com/en-us/vstudio/async.aspx

Page 15: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

15 | The Future of Parallel Programming in the .NET Framework | June 2011

AGENDA CHECKPOINT

Present

– Recap of parallel programming in the .NET Framework 4

Future

– Visual Studio Async

– TPL Dataflow

Page 16: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

16 | The Future of Parallel Programming in the .NET Framework | June 2011

TPL DATAFLOW – COMPLEMENTING PARALLEL PROGRAMMING IN .NET 4

.NET parallel programming was proactive in nature

– Here’s the data. Now set up the computation.”

– Primitives for task and data parallelism

Missing the reactive piece

– Set up the computation. Now here’s the data.”

– Primitives for dataflow parallelism

Page 17: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

17 | The Future of Parallel Programming in the .NET Framework | June 2011

TPL DATAFLOW OVERVIEW

Primitives for in-progress message passing

– Blocks that can buffer and process data

– Can be linked together to create networks

– Useful in agent/actor paradigm

Inspired by

– Decades of computer science research/history

– Related Microsoft technologies

Asynchronous Agents Library in Visual C++ 2010

CCR from Microsoft Robotics

Axum incubation project

Page 18: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

18 | The Future of Parallel Programming in the .NET Framework | June 2011

ActionBlock<int>

Message Queue

0 1 2 3

Process(0); Process(1); Process(2); Process(3);

4

Process(4);

var c = new ActionBlock<int>(i =>

{

Process(i);

});

for(int i = 0; i < 5; i++)

{

c.Post(i);

}

ASYNC POSTING EXAMPLE

Page 19: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

19 | The Future of Parallel Programming in the .NET Framework | June 2011

IDataflowBlock

ISourceBlock<TOuput> (a source of data)

ITargetBlock<TInput> (a target for data)

IPropagatorBlock<> (a source and target)

Built-in blocks for Buffering and Propagation

Built-in blocks for Executing

Built-in blocks for Joining

TPL DATAFLOW BLOCK HIERARCHY

Page 20: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

20 | The Future of Parallel Programming in the .NET Framework | June 2011

BLOCKS FOR BUFFERING AND PROPAGATION

BufferBlock<T>

– Delivers each input element to at most 1

target

WriteOnceBlock<T>

– Accepts and buffers only 1 element, ever

– Delivers the 1 element to all targets

BroadcastBlock<T>

– Broadcasts each input element to all

targets

BufferBlock<T>

Input

OriginalTask

WriteOnceBlock<T>

Input

Copy

Sole Value

Copy

Copy

Copy

Task

BroadcastBlock<T>

Input CopyCurrent

Copy

TaskCopy

Copy

Page 21: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

21 | The Future of Parallel Programming in the .NET Framework | June 2011

BLOCKS FOR EXECUTING

ActionBlock<TInput>

– Executes an action for each input element

TransformBlock<TInput, TOuput>

– Transforms each input element to an

output element

TransformManyBlock<TInput, TOuput>

– Transforms each input element to a

collection of output elements

ActionBlock<TInput>

Input

Task

TransformBlock<Tinput,Toutput>

Input

Task OutputTask

TransformManyBlock<Tinput,Toutput>

Input

Task OutputTask

Page 22: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

22 | The Future of Parallel Programming in the .NET Framework | June 2011

BLOCKS FOR JOINING

BatchBlock<T>

– Batches multiple input elements into

batches (arrays)

JoinBlock<T1, T2>

– Joins pairs of input elements

into tuples

BatchedJoinBlock<T1, T2>

– Joins pairs of collections of input

elements into tuples

BatchBlock<T>

Input

Task OutputTask

JoinBlock<T1,T2,…>

InputT2 Task OutputTask

InputT1

BatchedJoinBlock<T1,T2,…>

InputT2 TaskOutput

Task

InputT1

Page 24: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

24 | The Future of Parallel Programming in the .NET Framework | June 2011

Disclaimer & Attribution The information presented in this document is for informational purposes only and may contain technical inaccuracies, omissions

and typographical errors.

The information contained herein is subject to change and may be rendered inaccurate for many reasons, including but not limited

to product and roadmap changes, component and motherboard version changes, new model and/or product releases, product

differences between differing manufacturers, software changes, BIOS flashes, firmware upgrades, or the like. There is no

obligation to update or otherwise correct or revise this information. However, we reserve the right to revise this information and to

make changes from time to time to the content hereof without obligation to notify any person of such revisions or changes.

NO REPRESENTATIONS OR WARRANTIES ARE MADE WITH RESPECT TO THE CONTENTS HEREOF AND NO

RESPONSIBILITY IS ASSUMED FOR ANY INACCURACIES, ERRORS OR OMISSIONS THAT MAY APPEAR IN THIS

INFORMATION.

ALL IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE ARE EXPRESSLY

DISCLAIMED. IN NO EVENT WILL ANY LIABILITY TO ANY PERSON BE INCURRED FOR ANY DIRECT, INDIRECT, SPECIAL

OR OTHER CONSEQUENTIAL DAMAGES ARISING FROM THE USE OF ANY INFORMATION CONTAINED HEREIN, EVEN IF

EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

AMD, the AMD arrow logo, and combinations thereof are trademarks of Advanced Micro Devices, Inc. All other names used in

this presentation are for informational purposes only and may be trademarks of their respective owners.

The contents of this presentation were provided by individual(s) and/or company listed on the title page. The information and

opinions presented in this presentation may not represent AMD’s positions, strategies or opinions. Unless explicitly stated, AMD is

not responsible for the content herein and no endorsements are implied.

Page 25: The Future of Parallel Programming in - Home - AMDdeveloper.amd.com/wordpress/media/2013/06/2082_final.pdf · The Future of Parallel Programming in the .NET Framework Danny Shih Microsoft

25 | The Future of Parallel Programming in the .NET Framework | June 2011

DataflowBlockOptions TaskScheduler

MaxMessagesPerTask CancellationToken BoundedCapacity

ExecutionDataflowBlockOptions MaxDegreeOfParallelism

GroupingDataflowBlockOptions Greedy

MaxNumberOfGroups

OPTIONS AND UTILITY FUNCTIONS