parallel programming in .net 4.0 tasks and threading

29
Parallel Programming in .NET 4.0 Tasks and Threading Ingo Rammer, thinktecture weblogs.thinktecture.com/ingo @ingorammer

Upload: elmer

Post on 23-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Parallel Programming in .NET 4.0 Tasks and Threading. Ingo Rammer, thinktecture weblogs.thinktecture.com/ingo @ingorammer. Ingo Rammer and thinktecture. Support and consulting for software architects and developers Architectural Consulting and Prototyping - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Parallel Programming in .NET 4.0  Tasks  and Threading

Parallel Programming in .NET 4.0 Tasks and ThreadingIngo Rammer, thinktectureweblogs.thinktecture.com/ingo@ingorammer

Page 2: Parallel Programming in .NET 4.0  Tasks  and Threading

Ingo Rammer and thinktecture

• Support and consulting for software architects and developers• Architectural Consulting and Prototyping• Developer-Coaching and -Mentoring• Application Optimization, Troubleshooting, Debugging• Architecture and Code Reviews

• Slides/Samples: http://weblogs.thinktecture.com/ingo• [email protected]

Page 3: Parallel Programming in .NET 4.0  Tasks  and Threading

The Problem

Page 4: Parallel Programming in .NET 4.0  Tasks  and Threading
Page 5: Parallel Programming in .NET 4.0  Tasks  and Threading
Page 6: Parallel Programming in .NET 4.0  Tasks  and Threading
Page 7: Parallel Programming in .NET 4.0  Tasks  and Threading

128 cores(Yes, this is a real screenshot)

Page 8: Parallel Programming in .NET 4.0  Tasks  and Threading

The future brings ...

... more cores @ lower speed

Page 9: Parallel Programming in .NET 4.0  Tasks  and Threading

Multithreading vs. Parallelism

Page 10: Parallel Programming in .NET 4.0  Tasks  and Threading

What do we need?• Parallelism, not just multithreading

• Partitioning of work• Queue management• Synchronization

• Today• Threads• ThreadPool (==> limited API)

Page 11: Parallel Programming in .NET 4.0  Tasks  and Threading

.NET <= 3.5• Manual management of parallelism• Important

• Threads: unit of scheduling, not unit of work!

• ThreadPool: limited API

Page 12: Parallel Programming in .NET 4.0  Tasks  and Threading

.NET 4.0• Fine-Grained Parallelism: Task-API and

coordination structures (the foundation of it all)

• Structured Parallelism: Parallel• Declarative Parallelism: PLINQ

• And some underlying optimizations in the ThreadPool

Page 13: Parallel Programming in .NET 4.0  Tasks  and Threading

Task API

Task t1 = Task.Factory.StartNew(DoSomething);Task t2 = Task.Factory.StartNew(delegate {DoSomething();});Task t3 = Task.Factory.StartNew(()=>DoSomething());

Something tmp = GetData(); // just dummy parameter

Task t4 = Task.Factory.StartNew(p=>((Something)p).DoIt(), tmp);Task t5 = Task.Factory.StartNew(()=>tmp.DoIt());

t1.Wait();Task.WaitAll(t2,t3,t4);Task.WaitAny(t3,t4,t6);

Page 14: Parallel Programming in .NET 4.0  Tasks  and Threading

Tasks with Results

Task<int> t1 = Task.Factory.StartNew<int>(() => data.Process());

int val = t1.Result; // blocks until t1 is finished

Page 15: Parallel Programming in .NET 4.0  Tasks  and Threading

Task Continuation

var firstTask = new Task<int>(() => First());var secondTask = firstTask.ContinueWith((t) => Second());

firstTask.Start();secondTask.Wait();

Page 16: Parallel Programming in .NET 4.0  Tasks  and Threading

Debugging: Parallel Tasks

Page 17: Parallel Programming in .NET 4.0  Tasks  and Threading

Debugging: Parallel Stacks

Page 18: Parallel Programming in .NET 4.0  Tasks  and Threading

Structured Parallelism

• Parallel.Invoke

Parallel.Invoke( () => Foo(), () => Bar(), () => Baz());

Page 19: Parallel Programming in .NET 4.0  Tasks  and Threading

Structured Parallelism• Parallel.ForEach

string[] foo = {"bar","baz","qux"};

Parallel.ForEach(foo, (p) =>{ DoIt(p);});

// OR ...to support stopping:

Parallel.ForEach(foo, (p,s) =>{ DoIt(p); if (p == "baz") s.Stop();});

// s is implicitly of type ParallelLoopState

Page 20: Parallel Programming in .NET 4.0  Tasks  and Threading

int sum2 = lst .Where(p=>p.Index>3123 && p.Index<5892) .Sum(p => p.Process());

List<DemoData> lst = ...;var lst = from p in lst.AsParallel() where p.Index > 3123 && p.Index < 5892 select p;

Declarative Parallelism: PLINQ

Page 21: Parallel Programming in .NET 4.0  Tasks  and Threading

PLINQ Options

lst.AsParallel() .WithDegreeOfParallelism(10) .AsOrdered() .WithMergeOptions(ParallelMergeOptions.FullyBuffered)

Page 22: Parallel Programming in .NET 4.0  Tasks  and Threading

Cancellation Support• Unified cancellation with

CancellationSource and CancellationToken• Tasks can be manually cancelled, or

automatically when parent is cancelled• PLINQ-Queries can specify a

CancellationToken

CancellationSource src = new CancellationSource();

lst.AsParallel().WithCancellation(src.Token).Sum(...);

src.Cancel();

Page 23: Parallel Programming in .NET 4.0  Tasks  and Threading

BlockingCollection<T>

void ThreadProc() { while (!_workitems.IsCompleted) { var itm = _workitems.Take(); Process(itm); }}

static BlockingCollection<int> _workItems;

void EnqueueProc(){ _workItems.Add(123); _workItems.CompleteAdding();}

Page 24: Parallel Programming in .NET 4.0  Tasks  and Threading

ThreadPool Optimizations

• Local queues• Work stealing• Locality by LIFO

Page 25: Parallel Programming in .NET 4.0  Tasks  and Threading

What you get with .NET 4.0

• Fine-Grained Parallelism: Task, Task<T>• Structured Parallelism: Parallel.Invoke,

Parallel.ForEach• Declarative Parallelism:

PLINQ // .AsParallel()

Page 26: Parallel Programming in .NET 4.0  Tasks  and Threading

Next steps…Think of possible ways to add parallel capabilities to your existing applications. Otherwise you'll only ever use 1/128th of That Big Machine.

• Resources• Daniel Moth's weblog:

http://www.danielmoth.com/Blog/• PFX Team blog:

http://blogs.msdn.com/pfxteam/• Home: http://msdn.microsoft.com/concurrency/

Page 27: Parallel Programming in .NET 4.0  Tasks  and Threading

Stay up to date with MSDN Belux

• Register for our newsletters and stay up to date:http://www.msdn-newsletters.be• Technical updates• Event announcements and registration• Top downloads

• Follow our bloghttp://blogs.msdn.com/belux

• Join us on Facebookhttp://www.facebook.com/msdnbehttp://www.facebook.com/msdnbelux

• LinkedIn: http://linkd.in/msdnbelux/ • Twitter: @msdnbelux

Download MSDN/TechNet Desktop Gadget

http://bit.ly/msdntngadget

Page 28: Parallel Programming in .NET 4.0  Tasks  and Threading

TechDays 2011 On-Demand

• Watch this session on-demand via Channel9http://channel9.msdn.com/belux

• Download to your favorite MP3 or video player• Get access to slides and recommended resources by the speakers

Page 29: Parallel Programming in .NET 4.0  Tasks  and Threading

THANK YOU