Parallel Extentions to the .NET Framework

Preview:

DESCRIPTION

MikeTaulty_PFX_SlideShare

Citation preview

1

Parallel Extensions to the .NET Framework

Mike TaultyDeveloper & Platform GroupMicrosoft LtdMike.Taulty@microsoft.com http://www.mtaulty.com

Buying a new Processor

£100 - £300£100 - £300

2-3GHz2-3GHz

2 cores or 42 cores or 4

64-bit64-bit

CoreCore

CoreCore

Buying a new Processor

CoreCoreCoreCoreCoreCoreCoreCore£200 - £500£200 - £500

2-3GHz2-3GHz

4 cores with HT4 cores with HT

64-bit64-bit

QuickPath QuickPath InterconnectInterconnect

Memory ControllerMemory Controller

Where will it all end?

Unisys ES7000 (7600R) used with kind permission of Mr Henk var der Valk, Unisys, NL

“I’ll have a big one, please”

Was it a wise purchase?

Windows OSWindows OS

App 1App 1 App 2App 2 ......

App 1App 1

.NET CLR.NET CLR

.NET Framework.NET Framework

My CodeMy Code

Was it a wise purchase?

Some environments scale to take advantage of additional CPU cores (mostly server-side)

A lot of code does not (mostly client-side)This code will see little benefit from future hardware advances We want code to exhibit latent concurrency

ASP.NET Web Forms/ServicesASP.NET Web Forms/Services WCF ServicesWCF Services WF EngineWF Engine ......

.NET ThreadPool or Custom Threading Strategy.NET ThreadPool or Custom Threading Strategy

“The Answer” – System.Threading

System.Threading is one answerWriting correct, multi-threaded code is hard

Co-ordinating work and sharing data across threadsProne to races, deadlocks, livelocks, etc.

Minimal set of co-ordination data structures in System.ThreadingExplicitly managing threads is for “heroes”Implicitly managing threads == ThreadPool

9

Parallel sums on ThreadPool

“The Answer” – System.Threading

ThreadPool is a good start but;Programming model is awkwardMore than one ThreadPool?Cancelling work?Dependencies between work? etc.

Generally, in System.ThreadingFocus is on the thread and not the task

Makes it hard for the system to understand the intent and help with the implementation

Parallel Computing Initiative

Goal is to simplify parallel programmingLocal and distributed computingDifferent kinds of parallelism

Including work onOperating System, runtimes, frameworks, tools

First deliverables in VS 2010 ( .NET Fx V4.0 )Managed : Parallel Extensions to .NET Native: Parallel Pattern Library & Concurrency

RuntimeBoth: Parallel debugging and profiling tools

12

Parallel Sums Revisited

Parallel Extensions to .NET

System.ThreadinSystem.ThreadinggSystem.Collections.ConcurreSystem.Collections.Concurrentnt

System.dllSystem.dll

System.LinqSystem.Linq

System.Linq.ParallelSystem.Linq.Parallel

System.Core.dllSystem.Core.dll

System.Threading.TasksSystem.Threading.Tasks

System.ThreadingSystem.Threading

System.Collections.ConcurrentSystem.Collections.Concurrent

System.Threading.InternalSystem.Threading.Internal

mscorlib.dllmscorlib.dll

Task Parallel Library ( TPL )

TaskTask TaskFactoryTaskFactory TaskSchedulerTaskScheduler

Key ClassesKey Classes

Starting with Task

16

Starting with Task

Exploring Task

18

Exploring Task

Scheduling Tasks

TaskTask TaskSchedulerTaskScheduler

ThreadPoolTaskSchedulerThreadPoolTaskScheduler SynchronizationContextSynchronizationContextTaskSchedulerTaskScheduler CustomCustom

QueueTask(Task t)QueueTask(Task t)

20

Picking/Building a Scheduler

Default Scheduling

.NET ThreadPool ( with V4.0 modifications ).NET ThreadPool ( with V4.0 modifications )

Thread Thread Thread…

Work ItemWork ItemWork ItemWork Item

Work ItemWork Item

Work ItemWork Item

Thre

ad’s

Wor

k Q

ueue

Thre

ad’s

Wor

k Q

ueue

Thre

ad’s

Wor

k Q

ueue

Thre

ad’s

Wor

k Q

ueue

Thre

ad’s

Wor

k Q

ueue

Thre

ad’s

Wor

k Q

ueue

Work ItemWork Item Work ItemWork Item Work ItemWork Item

Glo

bal W

ork

Que

ueG

loba

l Wor

k Q

ueue

Task Exotica ( knobs )

23

Task exotica

Higher Level Constructs

Even with Task there are common patterns that build into higher level abstractions

The Parallel classInvoke, For, For<T>, Foreach

Care needs to be taken with state, ordering“This is not your Father’s for loop”

25

Parallel.Invoke, ForEach, For

Parallel LINQ ( PLINQ )

Extension method in System.LINQExtension method in System.LINQ

Extends Extends IEnumerable<T>IEnumerable<T>

LINQ to ObjectsLINQ to ObjectsLINQ to XMLLINQ to XML

not LINQ to SQL,EFnot LINQ to SQL,EF

Results in a Results in a ParallelQuery<T>ParallelQuery<T>““Usually” would beUsually” would be

EnumerableEnumerable Select()Select()Select()Select() Where()Where()Where()Where() Etc.Etc.Etc.Etc.

Parallel LINQ ( PLINQ )

IEnumerable<T>IEnumerable<T>

Could be IList<T> or Array

RangeChunkStriped?Hash

““Where”Where”““Where”Where” ““Where”Where”““Where”Where” ““Where”Where”““Where”Where”

““OrderBy”OrderBy”““OrderBy”OrderBy” ““OrderBy”OrderBy”““OrderBy”OrderBy” ““OrderBy”OrderBy”““OrderBy”OrderBy”

““Sum”Sum”““Sum”Sum” ““Sum”Sum”““Sum”Sum” ““Sum”Sum”““Sum”Sum”

Consumer( foreach … )Consumer( foreach … )

MergeMerge

Re-partitionRe-partition

TaskTask TaskTask TaskTaskQueryQuery QueryQuery QueryQuery

PartitionPartition

Parallel LINQ ( PLINQ )

Just like Parallel.For and friends, this sits on top of the Task Parallel LibraryCare still needs to be taken

Around shared stateAround orderingAround the mix between computation and I/O – not necessarily a performance win from parallelising a LINQ query

RememberPLINQ is nothing to do with databases

29

PLINQ

Co-ordination Data Structures

31

Co-ordination Data Structures

What Next?

Download VS 2010 CTPRemember to set the clock back

Experiment with runtime and APITeam is working on Visual Studio 2010 betaVery open to feedbackJoin in the discussion forums

Keep up with other concurrency topics at;http://msdn.microsoft.com/concurrency

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

Recommended