1 Meetings MERI Programmation Parallèle en .NET Animé par Sylvain Rescourio – Expert .NET Olivier Navarre – Directeur Technique Mardi 27 Mai 2014
May 10, 2015
1
MeetingsMERI
Programmation Parallèle en .NET
Animé par Sylvain Rescourio – Expert .NET
Olivier Navarre – Directeur Technique
Mardi 27 Mai 2014
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
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
Why parallel programming ?
Do the same work but in less time
8 sec4 sec
Work 1 2 * (0,5 * Work 1) at the same time
Sequential
Parallel
Start
5
Why parallel programming ?
Responsive UI
Long Action
Refresh interface
Refresh interface Refresh interface
Sequential
Parallel
See example
Click Click Long Action
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.
DEADLOCK
Waiting for Task 2 to finish
Task 1
Task 2
End
Waiting for Task 1 to finish
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.
RACE CONDITION
Task 1
Task 2
Result 1
Task 1
Task 2
Result 2
Race Condition if Result 1 != Result 2
8
How to do parallel programming in C# ?
ThreadThreadPool
TaskParallel
AsyncPLinq
9
Thread
A is the basic unit to which an operating system allocates processor time to execute a sequence of instruction.
Thread
Process
Thread 1
Instruction 1
Instruction 2
Instruction n
Thread k
Instruction 1
Instruction 2
Instruction n
10
Thread
Code
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
ThreadPool
Code
13
Task
Code
14
Parallel
Code
15
Parallel Foreach
Code
16
Async
Code
See example
17
PLinq
Code
18
How to synchronize in C# ?Lock
MutexSpinLock
SemaphoreReaderWriterLock
Semaphore SlimReaderWriterLockSlim
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
Lock
Comparison of Different solutions
Reference : http://www.albahari.com/threading/part2.aspx#_Locking
21
Lock
Code
22
SemaphoreSlim
Code
23
ReaderWriterLockSlim
Code
24
How to signal in C# ?Wait / Pulse
Barrier
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
Wait / Pulse
Code
27
Barrier
Code
28
A quick word about atomicity in C#
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
DEMO
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
How to store shared items in C# ?
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
equivalentConcurrentStack<T> Stack<T>
ConcurrentQueue<T> Queue<T>
ConcurrentDictionary<TKey,TValue> Dictionary<TKey,TValue>
BlockingCollection<T>
ConcurrentBag<T>
34
Conclusion
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
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
Conseil et Ingénierie en Banque, Finance et Assurance
Siège Social : 14, rue Gaillon – Paris 2èmeTél. : 01 44 56 88 10 / Fax : 01 44 56 88 11www.meritis.fr
Merci de votreattention