SlideShare uma empresa Scribd logo
1 de 37
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 sec
4 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.
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.
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# ?
Thread
ThreadPool
Task
Parallel
Async
PLinq
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
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
Mutex
SpinLock
Semaphore
ReaderWriterLock
Semaphore Slim
ReaderWriterLockSlim
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 equivalent
ConcurrentStack<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ème
Tél. : 01 44 56 88 10 / Fax : 01 44 56 88 11
www.meritis.fr
Merci de votre
attention

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Erlang For Five Nines
Erlang For Five NinesErlang For Five Nines
Erlang For Five Nines
 
ZeroMQ with NodeJS
ZeroMQ with NodeJSZeroMQ with NodeJS
ZeroMQ with NodeJS
 
Open mp directives
Open mp directivesOpen mp directives
Open mp directives
 
Asynchronous programming in C#
Asynchronous programming in C#Asynchronous programming in C#
Asynchronous programming in C#
 
MPI Tutorial
MPI TutorialMPI Tutorial
MPI Tutorial
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
 
Asynchronous Programming in .NET
Asynchronous Programming in .NETAsynchronous Programming in .NET
Asynchronous Programming in .NET
 
Multicore
MulticoreMulticore
Multicore
 
Openmp
OpenmpOpenmp
Openmp
 
MPI in TNT for parallel processing
MPI in TNT for parallel processingMPI in TNT for parallel processing
MPI in TNT for parallel processing
 
Tensorflow on Android
Tensorflow on AndroidTensorflow on Android
Tensorflow on Android
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!ZeroMQ - Sockets on steroids!
ZeroMQ - Sockets on steroids!
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Multiprocessing in python
Multiprocessing in pythonMultiprocessing in python
Multiprocessing in python
 
OpenMP
OpenMPOpenMP
OpenMP
 
Event Driven with LibUV and ZeroMQ
Event Driven with LibUV and ZeroMQEvent Driven with LibUV and ZeroMQ
Event Driven with LibUV and ZeroMQ
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
An introduction to erlang
An introduction to erlangAn introduction to erlang
An introduction to erlang
 
Introduction to ZeroMQ - eSpace TechTalk
Introduction to ZeroMQ - eSpace TechTalkIntroduction to ZeroMQ - eSpace TechTalk
Introduction to ZeroMQ - eSpace TechTalk
 

Destaque

Outsourceit- At a glance
Outsourceit- At a glanceOutsourceit- At a glance
Outsourceit- At a glanceUma Baeko
 
Project presentation
Project presentationProject presentation
Project presentationdaitoguns
 
Outsourceit_changing the future of employment
Outsourceit_changing the future of employmentOutsourceit_changing the future of employment
Outsourceit_changing the future of employmentUma Baeko
 
MERIMeeting du 21 octobre 2014 - Financial Markets and Instruments overview
MERIMeeting du 21 octobre 2014  - Financial Markets and Instruments overviewMERIMeeting du 21 octobre 2014  - Financial Markets and Instruments overview
MERIMeeting du 21 octobre 2014 - Financial Markets and Instruments overviewOlivier NAVARRE
 
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014Hvordan lykkes med offshoring (outsourcing til utlandet) 2014
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014Uma Baeko
 
Science Thought for the Day
Science Thought for the DayScience Thought for the Day
Science Thought for the Dayannetteblack6
 
Jolly phonics ai words
Jolly phonics ai wordsJolly phonics ai words
Jolly phonics ai wordsannetteblack6
 
Jolly phonics ee digraph
Jolly phonics ee digraphJolly phonics ee digraph
Jolly phonics ee digraphannetteblack6
 
Jolly phonics letter sounds
Jolly phonics letter soundsJolly phonics letter sounds
Jolly phonics letter soundsannetteblack6
 

Destaque (14)

Outsourceit- At a glance
Outsourceit- At a glanceOutsourceit- At a glance
Outsourceit- At a glance
 
Project presentation
Project presentationProject presentation
Project presentation
 
Outsourceit_changing the future of employment
Outsourceit_changing the future of employmentOutsourceit_changing the future of employment
Outsourceit_changing the future of employment
 
MERIMeeting du 21 octobre 2014 - Financial Markets and Instruments overview
MERIMeeting du 21 octobre 2014  - Financial Markets and Instruments overviewMERIMeeting du 21 octobre 2014  - Financial Markets and Instruments overview
MERIMeeting du 21 octobre 2014 - Financial Markets and Instruments overview
 
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014Hvordan lykkes med offshoring (outsourcing til utlandet) 2014
Hvordan lykkes med offshoring (outsourcing til utlandet) 2014
 
Science Thought for the Day
Science Thought for the DayScience Thought for the Day
Science Thought for the Day
 
Present verb to be
Present  verb to bePresent  verb to be
Present verb to be
 
Metodo di Dimagrimento "GLIODIME"
Metodo di Dimagrimento "GLIODIME"Metodo di Dimagrimento "GLIODIME"
Metodo di Dimagrimento "GLIODIME"
 
LA GLICAZIONE
LA GLICAZIONELA GLICAZIONE
LA GLICAZIONE
 
MCNT Dr Salvatore Di Meglio
MCNT Dr Salvatore Di MeglioMCNT Dr Salvatore Di Meglio
MCNT Dr Salvatore Di Meglio
 
Disbiosi intestinale e DIETA GIFT
Disbiosi intestinale e DIETA GIFTDisbiosi intestinale e DIETA GIFT
Disbiosi intestinale e DIETA GIFT
 
Jolly phonics ai words
Jolly phonics ai wordsJolly phonics ai words
Jolly phonics ai words
 
Jolly phonics ee digraph
Jolly phonics ee digraphJolly phonics ee digraph
Jolly phonics ee digraph
 
Jolly phonics letter sounds
Jolly phonics letter soundsJolly phonics letter sounds
Jolly phonics letter sounds
 

Semelhante a MERIMeeting du 27 mai 2014 - Parallel Programming

Peyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futurePeyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futureTakayuki Muranushi
 
Simon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSimon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSkills Matter
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programmingUmeshwaran V
 
6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptxSimRelokasi2
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionPyData
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Eelco Visser
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingPraveen Prajapati
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsKonrad Malawski
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionEelco Visser
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spacesluccastera
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthingtonoscon2007
 
Evolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded ProgrammingEvolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded ProgrammingGlobalLogic Ukraine
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...Artur Trzop
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting StartedMartin Chapman
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJAX London
 

Semelhante a MERIMeeting du 27 mai 2014 - Parallel Programming (20)

Concurrency in c#
Concurrency in c#Concurrency in c#
Concurrency in c#
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Peyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futurePeyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_future
 
Simon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSimon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelism
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programming
 
6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx6-9-2017-slides-vFinal.pptx
6-9-2017-slides-vFinal.pptx
 
Os Lamothe
Os LamotheOs Lamothe
Os Lamothe
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle Introduction
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 
Os Worthington
Os WorthingtonOs Worthington
Os Worthington
 
Evolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded ProgrammingEvolution of JDK Tools for Multithreaded Programming
Evolution of JDK Tools for Multithreaded Programming
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...
Distributed locks in Ruby - Correctness vs Efficiency - Knapsack Pro case stu...
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting Started
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben EvansJava Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
 

Último

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesRAJNEESHKUMAR341697
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 

Último (20)

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 

MERIMeeting du 27 mai 2014 - Parallel Programming

  • 1. 1 MeetingsMERI Programmation Parallèle en .NET Animé par Sylvain Rescourio – Expert .NET Olivier Navarre – Directeur Technique Mardi 27 Mai 2014
  • 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 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 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 Why parallel programming ? Responsive UI Long Action Refresh interface Refresh interface Refresh interface Sequential Parallel See example Click Click Long Action
  • 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 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 How to do parallel programming in C# ? Thread ThreadPool Task Parallel Async PLinq
  • 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
  • 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.
  • 18. 18 How to synchronize in C# ? Lock Mutex SpinLock Semaphore ReaderWriterLock Semaphore Slim ReaderWriterLockSlim
  • 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 Lock Comparison of Different solutions Reference : http://www.albahari.com/threading/part2.aspx#_Locking
  • 24. 24 How to signal in C# ? Wait / Pulse Barrier
  • 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.
  • 28. 28 A quick word about atomicity in C#
  • 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
  • 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 How to store shared items in C# ?
  • 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>
  • 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 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 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