parallel programming 101 - sdd conference · parallel programming 101 tiberiu covaci senior...

33
Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Upload: others

Post on 19-Jul-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Parallel Programming 101Tiberiu Covaci

Senior Software Developer

Level: Intermediate

Page 2: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Who am I?

Tiberiu ’Tibi’ Covaci

Software engineer, 25 years experience

CTO for Learning Connexions

MCT since 2004, teaching .NET

Telerik MVP & Insider

MVP in Azure/ASP.NET

Geek

@tibor19 / #vslive

Page 3: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Agenda

Parallel Programing

Definitions

Why

Laws

As-is

To-be

Page 4: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Definitions

Pillars of Concurrency

Responsiveness and Isolation

Asynchronicity

Throughput and scalability

Concurrency

Consistency

Sharing

Page 5: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Definitions

Asynchronous

Active waiting

Concurrency

Several stuff at the same time

Multithreading

Multiple threads of executions

Parallel

Multiple computations concurrently

Page 6: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Why Concurrent

Programming?

The free lunch is over

We need to do more with more

We have to take responsibility for the way we write

our code

Page 7: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Laws affecting Parallel Programming

Moore’s Law

Amdahl’s Law

Gustafson’s Law

Page 8: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Moore’s Law (1965)

The complexity for minimum component cost has

increased at a rate of roughly a factor 2 every 2

years

Page 9: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Amdahl’s Law (1967)

The speedup of a program using multiple processors

in parallel computing is limited by the time needed

for the sequential fraction of the program.

SpeedUp = 1/(S+P/N)

If S = 10% => Max Speedup = x10

Prepare the foodBuy the

ingredientsCook it

Page 10: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Gustafson’s Law (1988)• Any sufficiently large problem can be efficiently parallelized.

Is your problem large enough?

Prepare the foodBuy the

ingredientsCook it

Page 11: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Parallel Programming today

Threads

Single threaded processes

Multi threaded processes

Thread pools

Synchronizations Objects

Page 12: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Threads

The unit of execution in a process

Heavyweight

Resource intense

Simultaneous threads cause context switches

Page 13: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Single threaded processes

One application at the time

To achieve multitasking

Hardware Interrupts

Terminate and Stay Resident Programs

Page 14: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 15: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Multi threaded processes

Several threads in a process

Single Core - Time sharing

Multi Core - Machine aided parallelism

Page 16: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 17: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Thread pools

Threads are pre-created

Fewer threads - Less thread overhead

Less control - Threads are bundled

Page 18: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Synchronization Objects

Monitors (lock / SynkLock)

Manual/Auto Reset Events

The Interlock class

Mutexes

Semaphores

Reader Writer Locks

Page 19: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 20: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Parallel Programming

tomorrow

Task parallelism

Imperative data parallelism

Declarative data parallelism

Asynchronous programming

Synchronization/coordination objects

Page 21: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Task parallelism

Task/Task<T>

AggregateException

Page 22: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 23: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Imperative data parallelism

Parallel.For

Parallel.ForEach

Parallel.Invoke

Page 24: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 25: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Declarative data parallelism

Parallel Linq (PLinq)

AsParallel

AsOrdered

Page 26: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate
Page 27: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Asynchronous programming

• APM

BeginInvoke/EndInvoke

• EAP

MethodAsync/MethodCompleted event

• Async/Await

The compiler will rewrite your code to take care of the

asynchronous portion of your program

Page 28: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Coordination objects

SpinLock

SpinWait

SemaphoreSlim

ManualResetEventSlim

LazyInit

CountdownEvent

WriteOnce

Page 29: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Synchronization

Memory synchronization / fences

Barriers

Mutual exclusion

Page 30: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Sharing State and Synchronization

Don’t share!

Read only data

Data isolation

Synchronization

Page 31: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Deadlocks

Mutual exclusion

Hold and wait

No preemption

Circular wait

Page 32: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Agenda

Parallel Programing

Definitions

Why

Laws

As-is

To-be

Page 33: Parallel Programming 101 - SDD Conference · Parallel Programming 101 Tiberiu Covaci Senior Software Developer Level: Intermediate

Thank you!

• Please fill out the evaluation!

• Contact me by

Email [email protected]

Twitter tibor19

And Don’t Forget to Write!