1 / 29

Parallel Programming in .NET 4.0 Tasks and Threading

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

elmer
Download Presentation

Parallel Programming in .NET 4.0 Tasks and Threading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 128 cores (Yes, this is a real screenshot)

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

  6. Multithreading vs. Parallelism

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

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

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

  10. 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);

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

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

  13. Debugging: Parallel Tasks

  14. Debugging: Parallel Stacks

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

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

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

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

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

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

  21. ThreadPool Optimizations • Local queues • Work stealing • Locality by LIFO

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

  23. 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/

  24. 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 DownloadMSDN/TechNet Desktop Gadgethttp://bit.ly/msdntngadget

  25. TechDays 2011 On-Demand • Watchthis 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

  26. THANK YOU

More Related