1 / 28

Task Parallel Library

Task Parallel Library. Task Parallel Library. What are we going to learn today ?. System.Threading.Tasks Task Represents an asynchronous operation Supports waiting, cancellation, continuations, … Parent/child relationships 1 st -class debugging support in Visual Studio 2010

suzuki
Download Presentation

Task Parallel Library

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. Task Parallel Library

  2. Task Parallel Library What are we going to learn today ? • System.Threading.Tasks • Task • Represents an asynchronous operation • Supports waiting, cancellation, continuations, … • Parent/child relationships • 1st-class debugging support in Visual Studio 2010 • Task<TResult> : Task • Tasks that return results • Task Exceptions • Task Scheduler

  3. In the year 2003, CPU performance growth had hit a wall. Up until then, most of us wrote sequential code and relayed on the CPU frequency to consistently raise (in 12 years it climbed from 400Hz to 3.8GHz) while futilely counting on Moore law (that the number of transistors on a chip will double about every two years) to keep on translating into faster processors

  4. Moore’s Law: Alive and Well? The number of transistors doubles every two years… More than 1 billiontransistors in 2006! http://upload.wikimedia.org/wikipedia/commons/2/25/Transistor_Count_and_Moore%27s_Law_-_2008_1024.png

  5. The number of transistors on Intel’s chips never stopped climbing, even after the year 2010. However, clock speed stopped on 2003 somewhere near 3GHz.

  6. Moore’s Law: But Different Frequencies will NOT get much faster! Maybe 5 to 10% every year or so, a few more times… And these modest gains would make the chips A LOThotter! http://www.tomshw.it/cpu.php?guide=20051121

  7. what we already know • Threading • Thread Pool • Asynchronous Calls

  8. Example: “Race Car Drivers” IEnumerable<RaceCarDriver> drivers = ...; varresults = new List<RaceCarDriver>(); foreach(var driver in drivers) { if (driver.Name == queryName && driver.Wins.Count >= queryWinCount) { results.Add(driver); } } results.Sort((b1, b2) => b1.Age.CompareTo(b2.Age));

  9. Manual Parallel Solution IEnumerable<RaceCarDriver> drivers = …; varresults = new List<RaceCarDriver>(); intpartitionsCount = Environment.ProcessorCount; intremainingCount = partitionsCount; varenumerator = drivers.GetEnumerator(); try { using (vardone = new ManualResetEvent(false)) { for(inti = 0; i < partitionsCount; i++) { ThreadPool.QueueUserWorkItem(delegate { while(true) { RaceCarDriver driver; lock (enumerator) { if (!enumerator.MoveNext()) break; driver = enumerator.Current; } if (driver.Name == queryName && driver.Wins.Count >= queryWinCount) { lock(results) results.Add(driver); } } if (Interlocked.Decrement(refremainingCount) == 0) done.Set(); }); } done.WaitOne(); results.Sort((b1, b2) => b1.Age.CompareTo(b2.Age)); } } finally { if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose(); }

  10. ThreadPool in .NET 3.5 Global Queue Worker Thread 1 Worker Thread 1 … Item 4 Item 5 Program Thread Item 1 Item 2 Item 3 Item 6 • Thread Management: • Starvation Detection • Idle Thread Retirement

  11. ThreadPool in .NET 4 Local Work-Stealing Queue Local Work-Stealing Queue Lock-Free Global Queue … Worker Thread 1 Worker Thread p … Task 6 Task 3 Program Thread Task 4 Task 1 Task 5 Task 2 • Thread Management: • Starvation Detection • Idle Thread Retirement • Hill-climbing

  12. Visual Studio 2010Tools, Programming Models, Runtimes Tools Programming Models .NET Framework 4 Visual C++ 10 Visual Studio IDE Parallel LINQ Parallel Debugger Tool Windows Parallel Pattern Library AgentsLibrary Task Parallel Library Data Structures Data Structures Concurrency Runtime • Profiler Concurrency • Analysis Task Scheduler ThreadPool Task Scheduler Resource Manager Resource Manager Operating System Windows Threads UMS Threads Key: Managed Native Tooling

  13. What is Task

  14. What is Task

  15. What is Task

  16. What is Task

  17. What is Task

  18. Working with Task

  19. Working with Task

  20. Working with Task

  21. Working with Task

  22. Working with Task

  23. Working with Task

  24. Working with Task

  25. Working with Task

  26. Amdahl’s Law Theoretical maximum speedup determined by amount of sequential code

  27. To Infinity And Beyond… • The “Manycore Shift” is happening • Parallelism in your code is inevitable • Visual Studio 2012 and .NET 4.5 will help • Parallel Computing Dev Center • http://msdn.com/concurrency • Download Beta 2 (“go-live” license) • http://go.microsoft.com/?linkid=9692084 • Team Blogs • Managed: http://blogs.msdn.com/pfxteam • Native: http://blogs.msdn.com/nativeconcurrency • Tools: http://blogs.msdn.com/visualizeconcurrency • Forums • http://social.msdn.microsoft.com/Forums/en-US/category/parallelcomputing We love feedback!

  28. Questions ??? • Shoot Up

More Related