33
1 Parallel Extensions to the .NET Framework Mike Taulty Developer & Platform Group Microsoft Ltd [email protected] http://www.mtaulty.com

Parallel Extentions to the .NET Framework

  • Upload
    ukdpe

  • View
    2.917

  • Download
    0

Embed Size (px)

DESCRIPTION

MikeTaulty_PFX_SlideShare

Citation preview

Page 1: Parallel Extentions to the .NET Framework

1

Parallel Extensions to the .NET Framework

Mike TaultyDeveloper & Platform GroupMicrosoft [email protected] http://www.mtaulty.com

Page 2: Parallel Extentions to the .NET Framework

Buying a new Processor

£100 - £300£100 - £300

2-3GHz2-3GHz

2 cores or 42 cores or 4

64-bit64-bit

CoreCore

CoreCore

Page 3: Parallel Extentions to the .NET Framework

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

Page 4: Parallel Extentions to the .NET Framework

Where will it all end?

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

Page 5: Parallel Extentions to the .NET Framework

“I’ll have a big one, please”

Page 6: Parallel Extentions to the .NET Framework

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

Page 7: Parallel Extentions to the .NET Framework

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

Page 8: Parallel Extentions to the .NET Framework

“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

Page 9: Parallel Extentions to the .NET Framework

9

Parallel sums on ThreadPool

Page 10: Parallel Extentions to the .NET Framework

“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

Page 11: Parallel Extentions to the .NET Framework

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

Page 12: Parallel Extentions to the .NET Framework

12

Parallel Sums Revisited

Page 13: Parallel Extentions to the .NET Framework

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

Page 14: Parallel Extentions to the .NET Framework

Task Parallel Library ( TPL )

TaskTask TaskFactoryTaskFactory TaskSchedulerTaskScheduler

Key ClassesKey Classes

Page 15: Parallel Extentions to the .NET Framework

Starting with Task

Page 16: Parallel Extentions to the .NET Framework

16

Starting with Task

Page 17: Parallel Extentions to the .NET Framework

Exploring Task

Page 18: Parallel Extentions to the .NET Framework

18

Exploring Task

Page 19: Parallel Extentions to the .NET Framework

Scheduling Tasks

TaskTask TaskSchedulerTaskScheduler

ThreadPoolTaskSchedulerThreadPoolTaskScheduler SynchronizationContextSynchronizationContextTaskSchedulerTaskScheduler CustomCustom

QueueTask(Task t)QueueTask(Task t)

Page 20: Parallel Extentions to the .NET Framework

20

Picking/Building a Scheduler

Page 21: Parallel Extentions to the .NET Framework

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

Page 22: Parallel Extentions to the .NET Framework

Task Exotica ( knobs )

Page 23: Parallel Extentions to the .NET Framework

23

Task exotica

Page 24: Parallel Extentions to the .NET Framework

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”

Page 25: Parallel Extentions to the .NET Framework

25

Parallel.Invoke, ForEach, For

Page 26: Parallel Extentions to the .NET Framework

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.

Page 27: Parallel Extentions to the .NET Framework

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

Page 28: Parallel Extentions to the .NET Framework

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

Page 29: Parallel Extentions to the .NET Framework

29

PLINQ

Page 30: Parallel Extentions to the .NET Framework

Co-ordination Data Structures

Page 31: Parallel Extentions to the .NET Framework

31

Co-ordination Data Structures

Page 32: Parallel Extentions to the .NET Framework

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

Page 33: Parallel Extentions to the .NET Framework

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