MERIMeeting du 27 mai 2014 - Parallel Programming

292 views

Published on

Présentation sur la programmation parallèle en .NET

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
292
On SlideShare
0
From Embeds
0
Number of Embeds
73
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MERIMeeting du 27 mai 2014 - Parallel Programming

  1. 1. 1 MeetingsMERI Programmation Parallèle en .NET Animé par Sylvain Rescourio – Expert .NET Olivier Navarre – Directeur Technique Mardi 27 Mai 2014
  2. 2. 2  Introduction  What is parallel programming ?  Why parallel programming ?  The issues that come with parallel programming ?  How to do parallel programming in C# ?  Thread  ThreadPool  Task  Parallel  Async  PLinq  How to synchronize in C# ?  Lock  Mutex  SpinLock  Semaphore  ReaderWriterLock  Semaphore Slim  ReaderWriterLockSlim  How to signal in C# ?  Wait / Pulse  Barrier  How to store shared items in C# ?  Wait / Pulse  Barrier  Conclusion Sommaire
  3. 3. 3 What is parallel programming ? Parallel computing is the simultaneous use of multiple compute resources Problem Result Compute resource 1 Compute resource 2 Compute resource m Sub Problem 1 Sub Problem 2 Sub Problem n
  4. 4. 4 Why parallel programming ? Do the same work but in less time 8 sec 4 sec Work 1 2 * (0,5 * Work 1) at the same time Sequential Parallel Start
  5. 5. 5 Why parallel programming ? Responsive UI Long Action Refresh interface Refresh interface Refresh interface Sequential Parallel See example Click Click Long Action
  6. 6. 6 The issues that come with parallel programming A is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. Waiting for Task 2 to finish Task 1 Task 2 End Waiting for Task 1 to finish
  7. 7. 7 The issues that come with parallel programming A is the behavior of a system where the output is dependent on the sequence or timing of other uncontrollable events. Task 1 Task 2 Result 1 Task 1 Task 2 Result 2 Race Condition if Result 1 != Result 2
  8. 8. 8 How to do parallel programming in C# ? Thread ThreadPool Task Parallel Async PLinq
  9. 9. 9 Thread A is the basic unit to which an operating system allocates processor time to execute a sequence of instruction. Process Thread 1 Instruction 1 Instruction 2 Instruction n Thread k Instruction 1 Instruction 2 Instruction n
  10. 10. 10 Thread Code
  11. 11. 11 Solutions Thread, ThreadPool, Task, Parallel, Async, Plinq, … Differents solutions  ThreadPool : A pool of threads that can be used to execute tasks.  Task : Represents an asynchronous operation.  Parallel : Provides support for parallel loops.  Async : A keyword to specify that a method is asynchronous.  Plinq : Execute LINQ queries in parallel.
  12. 12. 12 ThreadPool Code
  13. 13. 13 Task Code
  14. 14. 14 Parallel Code
  15. 15. 15 Parallel Foreach Code
  16. 16. 16 Async Code See example
  17. 17. 17 PLinq Code
  18. 18. 18 How to synchronize in C# ? Lock Mutex SpinLock Semaphore ReaderWriterLock Semaphore Slim ReaderWriterLockSlim
  19. 19. 19 Different solutions  Lock  Protect a section of code.  Mutex  Locking across processes.  SpinLock  Locking few instructions (Sum, variable copy, …).  Semaphore  Cap the access of a resource to a fixed number of thread.  ReaderWriterLock  Control the access of a resource by multiple thread, allowing 1 writer or many reader at the same time. Lock Locking for a long time can degrade performances ! Be aware of deadlock !
  20. 20. 20 Lock Comparison of Different solutions Reference : http://www.albahari.com/threading/part2.aspx#_Locking
  21. 21. 21 Lock Code
  22. 22. 22 SemaphoreSlim Code
  23. 23. 23 ReaderWriterLockSlim Code
  24. 24. 24 How to signal in C# ? Wait / Pulse Barrier
  25. 25. 25 How to signal in C# ? Reference : http://www.albahari.com/threading/part2.aspx#_Signaling_with_Event_Wait_Handles Differents solutions  Wait / Pulse methods  Let a thread wait for a signal or send a signal (Pulse).  Barrier  Allow threads to wait for each other.
  26. 26. 26 Wait / Pulse Code
  27. 27. 27 Barrier Code
  28. 28. 28 A quick word about atomicity in C#
  29. 29. 29 A quick word about atomicity in C# An atomic operation  An atomic operation cannot be interrupted by other thread.  Example : Incrementing a variable is not atomic  Read the variable  Add 1  Store the result in the same variable  What if in the middle, another thread access this variable ?  Solution  Interlocked.Add, Interlocked.Increment  Interlocked.Exchange, Interlocked.CompareExchange  Interlocked.Read
  30. 30. 30 DEMO
  31. 31. 31 DEMO - Image crawler Image crawler : http://www.hdwallpapers.in GUI Processing Get image count Download pages Download images Update GUI Wait for all images to be downloaded
  32. 32. 32 How to store shared items in C# ?
  33. 33. 33 How to store shared items in C# ? Different Solutions  Standard Collection + Lock If you only add elements to the collection  Standard Collection + ReaderWriterLockSlim If you modify & read simultaneously the collection  Native ThreadSafe collections If you modify & read simultaneously the collection  For highly concurrent scenario Native ThreadSafe collections Concurrent collection Non Concurrent equivalent ConcurrentStack<T> Stack<T> ConcurrentQueue<T> Queue<T> ConcurrentDictionary<TKey,TValue> Dictionary<TKey,TValue> BlockingCollection<T> ConcurrentBag<T>
  34. 34. 34 Conclusion
  35. 35. 35 Parallel programming can help you to  Compute faster  Create responsive application  Parallel programming has a cost :  Increase code complexity.  Too much synchronization can lead to worse performance than single threaded application. Conclusion Don’t rush into parallel programming, profile your code if necessary
  36. 36. 36 Partenaire Microsoft MERITIS GOLD PARTNER 2015 Répondre aux attentes de nos clients et de nos collaborateurs Améliorer notre expertise sur les solutions Microsoft Accompagner nos clients sur ces domaines technologiques Être innovant autours des technologies Microsoft
  37. 37. 37 Conseil et Ingénierie en Banque, Finance et Assurance Siège Social : 14, rue Gaillon – Paris 2ème Tél. : 01 44 56 88 10 / Fax : 01 44 56 88 11 www.meritis.fr Merci de votre attention

×