300 likes | 640 Views
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
E N D
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
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
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
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.
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
what we already know • Threading • Thread Pool • Asynchronous Calls
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));
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(); }
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
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
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
Amdahl’s Law Theoretical maximum speedup determined by amount of sequential code
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!
Questions ??? • Shoot Up