Top Banner
1 Meetings MERI Programmation Parallèle en .NET Animé par Sylvain Rescourio – Expert .NET Olivier Navarre – Directeur Technique Mardi 27 Mai 2014
37

MERIMeeting du 27 mai 2014 - Parallel Programming

May 10, 2015

Download

Engineering

Olivier NAVARRE

Présentation sur la programmation parallèle en .NET
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MERIMeeting du 27 mai 2014 - Parallel Programming

1

MeetingsMERI

Programmation Parallèle en .NET

Animé par Sylvain Rescourio – Expert .NET

Olivier Navarre – Directeur Technique

Mardi 27 Mai 2014

Page 2: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 3: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 4: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 5: MERIMeeting du 27 mai 2014 - Parallel Programming

5

Why parallel programming ?

Responsive UI

Long Action

Refresh interface

Refresh interface Refresh interface

Sequential

Parallel

See example

Click Click Long Action

Page 6: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 7: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 8: MERIMeeting du 27 mai 2014 - Parallel Programming

8

How to do parallel programming in C# ?

ThreadThreadPool

TaskParallel

AsyncPLinq

Page 9: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 10: MERIMeeting du 27 mai 2014 - Parallel Programming

10

Thread

Code

Page 11: MERIMeeting du 27 mai 2014 - Parallel Programming

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.

Page 12: MERIMeeting du 27 mai 2014 - Parallel Programming

12

ThreadPool

Code

Page 13: MERIMeeting du 27 mai 2014 - Parallel Programming

13

Task

Code

Page 14: MERIMeeting du 27 mai 2014 - Parallel Programming

14

Parallel

Code

Page 15: MERIMeeting du 27 mai 2014 - Parallel Programming

15

Parallel Foreach

Code

Page 16: MERIMeeting du 27 mai 2014 - Parallel Programming

16

Async

Code

See example

Page 17: MERIMeeting du 27 mai 2014 - Parallel Programming

17

PLinq

Code

Page 18: MERIMeeting du 27 mai 2014 - Parallel Programming

18

How to synchronize in C# ?Lock

MutexSpinLock

SemaphoreReaderWriterLock

Semaphore SlimReaderWriterLockSlim

Page 19: MERIMeeting du 27 mai 2014 - Parallel Programming

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 !

Page 20: MERIMeeting du 27 mai 2014 - Parallel Programming

20

Lock

Comparison of Different solutions

Reference : http://www.albahari.com/threading/part2.aspx#_Locking

Page 21: MERIMeeting du 27 mai 2014 - Parallel Programming

21

Lock

Code

Page 22: MERIMeeting du 27 mai 2014 - Parallel Programming

22

SemaphoreSlim

Code

Page 23: MERIMeeting du 27 mai 2014 - Parallel Programming

23

ReaderWriterLockSlim

Code

Page 24: MERIMeeting du 27 mai 2014 - Parallel Programming

24

How to signal in C# ?Wait / Pulse

Barrier

Page 25: MERIMeeting du 27 mai 2014 - Parallel Programming

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.

Page 26: MERIMeeting du 27 mai 2014 - Parallel Programming

26

Wait / Pulse

Code

Page 27: MERIMeeting du 27 mai 2014 - Parallel Programming

27

Barrier

Code

Page 28: MERIMeeting du 27 mai 2014 - Parallel Programming

28

A quick word about atomicity in C#

Page 29: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 30: MERIMeeting du 27 mai 2014 - Parallel Programming

30

DEMO

Page 31: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 32: MERIMeeting du 27 mai 2014 - Parallel Programming

32

How to store shared items in C# ?

Page 33: MERIMeeting du 27 mai 2014 - Parallel Programming

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>

Page 34: MERIMeeting du 27 mai 2014 - Parallel Programming

34

Conclusion

Page 35: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 36: MERIMeeting du 27 mai 2014 - Parallel Programming

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

Page 37: MERIMeeting du 27 mai 2014 - Parallel Programming

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