Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parallel Programming in.NET 4.0 Tasks and Threading Ingo Rammer, thinktecture

Similar presentations


Presentation on theme: "Parallel Programming in.NET 4.0 Tasks and Threading Ingo Rammer, thinktecture"— Presentation transcript:

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

2 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 ingo.rammer@thinktecture.com

3 The Problem

4

5

6

7 128 cores (Yes, this is a real screenshot) 128 cores (Yes, this is a real screenshot)

8 The future brings...... more cores @ lower speed

9 Multithreading vs. Parallelism

10 What do we need? Parallelism, not just multithreading Partitioning of work Queue management Synchronization Today Threads ThreadPool (==> limited API)

11 .NET <= 3.5 Manual management of parallelism Important Threads: unit of scheduling, not unit of work! ThreadPool: limited API

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

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

14 Tasks with Results Task t1 = Task.Factory.StartNew (() => data.Process()); int val = t1.Result; // blocks until t1 is finished Task t1 = Task.Factory.StartNew (() => data.Process()); int val = t1.Result; // blocks until t1 is finished

15 Task Continuation var firstTask = new Task (() => First()); var secondTask = firstTask.ContinueWith((t) => Second()); firstTask.Start(); secondTask.Wait(); var firstTask = new Task (() => First()); var secondTask = firstTask.ContinueWith((t) => Second()); firstTask.Start(); secondTask.Wait();

16 Debugging: Parallel Tasks

17 Debugging: Parallel Stacks

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

19 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 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

20 int sum2 = lst.Where(p=>p.Index>3123 && p.Index<5892).Sum(p => p.Process()); int sum2 = lst.Where(p=>p.Index>3123 && p.Index<5892).Sum(p => p.Process()); List lst =...; var lst = from p in lst.AsParallel() where p.Index > 3123 && p.Index < 5892 select p; List lst =...; var lst = from p in lst.AsParallel() where p.Index > 3123 && p.Index < 5892 select p; Declarative Parallelism: PLINQ

21 PLINQ Options lst.AsParallel().WithDegreeOfParallelism(10).AsOrdered().WithMergeOptions(ParallelMergeOptions.FullyBuffered) lst.AsParallel().WithDegreeOfParallelism(10).AsOrdered().WithMergeOptions(ParallelMergeOptions.FullyBuffered)

22 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(); CancellationSource src = new CancellationSource(); lst.AsParallel().WithCancellation(src.Token).Sum(...); src.Cancel();

23 BlockingCollection void ThreadProc() { while (!_workitems.IsCompleted) { var itm = _workitems.Take(); Process(itm); } void ThreadProc() { while (!_workitems.IsCompleted) { var itm = _workitems.Take(); Process(itm); } static BlockingCollection _workItems; void EnqueueProc() { _workItems.Add(123); _workItems.CompleteAdding(); } static BlockingCollection _workItems; void EnqueueProc() { _workItems.Add(123); _workItems.CompleteAdding(); }

24 ThreadPool Optimizations Local queues Work stealing Locality by LIFO

25 What you get with.NET 4.0 Fine-Grained Parallelism: Task, Task Structured Parallelism: Parallel.Invoke, Parallel.ForEach Declarative Parallelism: PLINQ //.AsParallel()

26 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/

27 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 blog http://blogs.msdn.com/belux Join us on Facebook http://www.facebook.com/msdnbe http://www.facebook.com/msdnbelux LinkedIn: http://linkd.in/msdnbelux/ Twitter: @msdnbelux Download MSDN/TechNet Desktop Gadget http://bit.ly/msdntngadget

28 TechDays 2011 On-Demand Watch this session on-demand via Channel9 http://channel9.msdn.com/belux Download to your favorite MP3 or video player Get access to slides and recommended resources by the speakers

29 THANK YOU


Download ppt "Parallel Programming in.NET 4.0 Tasks and Threading Ingo Rammer, thinktecture"

Similar presentations


Ads by Google