Parallel extensions in .Net 4.0


Published on

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Parallel extensions in .Net 4.0

  1. 1. Parallel Extensions in .Net 4.0<br />Dmytro Maleev for Lviv.Net User Group<br />
  2. 2. Agenda<br />Куда катится мир?<br />Hello, Parallel Extensions!<br />Parallel vs Multithreading<br />Parallel Loops<br />Tasks<br />PLINQ<br />CDS & Thread Enchantments<br />References<br />Q&A<br />
  3. 3. Куда катится мир<br />Forget about CPU frequency increase! There are a lot of issues with this, which will be fixed in future!<br />1998 <br />Intel 80486<br />80Mhz<br />1 Core<br />2007<br />Core 2 Duo 2.13 GHz<br />2 Core<br />2000<br />Celeron 400Mhz<br />1 Core<br />2010<br />Intel i5 3.3GHz<br />2 Core 4 Threads<br />2003<br />Athlon XP 1.6+GHz<br />1 Core<br />?<br />
  4. 4. Куда катится мир<br />For now…ENLARGE YOUR…<br />CPU CORE COUNT!<br />PEN<br />PEN<br />
  5. 5. Hello, Parallel Extensions!<br />Parallel Extensions, previously known as the Parallel Framework Extensions or PFX, is a managed concurrency library being developed by a collaboration between Microsoft Research and the CLR team at Microsoft. It is composed of two parts: Parallel LINQ (PLINQ) andTask Parallel Library (TPL).It also consists of a set of coordination data structures (CDS) – sets of data structures used to synchronize and co-ordinate the execution of concurrent tasks.Thelibrary was released as a CTP on November 29, 2007and refreshed again in December 2007 and June 2008. Microsoft has announced that the Parallel Extensions to .NET will release as part of the .NET 4.0 Framework release.<br />©Wikipedia<br />
  6. 6. Hello, Parallel Extensions!<br />Visual Studio Debugging and profiling support<br />Parallel Extensions<br />PLINQ<br />Task Parallel Library<br />Task Scheduler<br />CDS<br />
  7. 7. Parallel vs Multithreading<br />Multithreaded!=parallelization<br />“ If on a single core machine you are using threads and it makes perfect sense for your scenario, then you are not "doing parallelism", you are just doing multithreading”<br />”On a single core you can use threads and you can have concurrency, but to achieve<br />parallelism on a multi-core box you have to identify in your code the exploitable<br />concurrency: the portions of your code that can truly run at the same time.”<br />Daniel Moth<br />
  8. 8. Parallel vs Multithreading<br />
  9. 9. Dark Side of Paprallelization and Multithreading<br />Race conditions<br /><br />2. Deadlocks<br /><br /><br />Thread starvation<br />Difficult to code and debug<br />Environmental<br />
  10. 10. Народная мудрость!<br />
  11. 11. Parallel Loops<br />Parallel.For()<br />Parallel.For(0, 100, i =><br /> {<br /> Console.WriteLine("This is I-I-I-I-I-I: {0}", i);<br /> });<br />Parallel.ForEach()<br />Parallel.ForEach(ThisIsCollection, collectionItem =><br />{<br />collectionItem.Hello();<br />});<br />Parallel Options<br />Parallel.Invoke()<br />
  12. 12. Parallel Loops. Use Force wisely!<br />Parallelization Can Hurt Performance<br /><br /><br />
  13. 13. DEMO<br />AntiSocialRobots<br />
  14. 14. Task - Task Scheduler<br />Task is a new class that represents the work you want completed. There are methods to create, schedule, and synchronize tasks in your application.<br />Tasks are controlled by task scheduler.<br />Tasks scheduler works with thread pool.<br />
  15. 15. Task In Deep<br />Tasks can:<br />Task can be created<br />Task can wait! Task can simply Wait(), WaitAll() or WaitAny().<br />Task knows when it is completed ( IsCompleted property)<br />Task can ContinueWith()<br />Task can return value<br />Task has Options and Status<br />
  16. 16. Demo<br />Strassen algorithm<br />
  17. 17. PLINQ<br />PLINQ is just parallelized version of LINQ<br />Not parallelized: LINQ-to-SQL & LINQ-to-Entity.<br />LINQ:<br />var query = from s in someCollection<br />let result = CoolService.CallService(s)<br />select result;<br />PLINQ:<br />varquery = from s in someCollection.AsParallel()<br />let result = CoolService.CallService(s)<br />select result;<br />
  18. 18. PLINQ. How it works?<br />
  19. 19. PLINQ. Overview <br />Ordering Results<br />AsOrdered()<br />ForAll Operator()<br />AsSequential()<br />WithMergeOptions<br />Parallel Performance Analyzer. Just for rich<br />
  20. 20. DEMO <br />Baby Names<br />
  21. 21. CDS & Thread Enchantments<br />Thread Enchantments:<br />Thread.Yield()<br />Monitor.Enter()<br />Concurrent Collections<br />ConcurrentStack (LIFO)<br />ConcurrentQueue (FIFO)<br />ConcurrentDictionary<br />ConcurrentBag<br />BlockingCollection<br />
  22. 22. Synchronization Primitives<br />Barrier<br />“Let’s meet near monument and then go to have a beer”<br />Cancellation Tokens<br />CountDownEvent<br />ManualResetEventSlim and SemaphoreSlim<br />SpinLock<br />ThreadLocal<T><br />
  23. 23. References <br />Parallel Programming with .NET<br /><br />Wiki<br /><br />Introducing .NET 4.0<br /><br />Lviv.Net User Group<br /><br />
  24. 24. Q&A<br />?<br />
  25. 25. If you still have a questions <br />Mail me:<br /><br />Skype me:<br />hmmidma<br />Twitter:<br />dimko1<br />