43
Concurrency & Parallel Programming by Ramazan AYYILDIZ rayyildiz .me @rayyildiz

Concurrency & Parallel Programming

Embed Size (px)

Citation preview

Page 1: Concurrency & Parallel Programming

Concurrency & Parallel

Programmingby Ramazan AYYILDIZ

rayyildiz.me@rayyildiz

Page 2: Concurrency & Parallel Programming

Gordon MooreIntel co-founder

Page 3: Concurrency & Parallel Programming

Moore’s Law"The number of transistors incorporated in a chip will approximately double every 24

months."

Page 4: Concurrency & Parallel Programming

Multi Core CPUs

Page 5: Concurrency & Parallel Programming

Concurrency vs

Parallel Programming

Page 6: Concurrency & Parallel Programming

Parallel Programming

• Uses a multiplicity of computational hardware (e.g., several processor cores) to perform a computation more quickly

Page 7: Concurrency & Parallel Programming

Concurrency

• A programming structuring technique in which there are multiple threads of control.

Page 8: Concurrency & Parallel Programming
Page 9: Concurrency & Parallel Programming
Page 10: Concurrency & Parallel Programming

Beyond the Multiple Cores

Page 11: Concurrency & Parallel Programming

Parallel Architecture• Instruction Level Parallelism

• 8 bit —> 16 bit —> 32 bit —> 64 bit —> …

• Data parallelism

• GPU

• Intel accured Altera ~17 Billion USD

Page 12: Concurrency & Parallel Programming
Page 13: Concurrency & Parallel Programming

Threads

Page 14: Concurrency & Parallel Programming

Threads

• Simple thread for java

• java.util.Thread

• Mutex, Deadlock, Race Condition, Lock

Page 15: Concurrency & Parallel Programming

Simple Thread

Page 16: Concurrency & Parallel Programming

• Why Thread.yield()

• a hint to schedule that the current thread is willing to yield its current use of processor

• Why Thread.join()

Page 17: Concurrency & Parallel Programming
Page 18: Concurrency & Parallel Programming

Race Condition

Page 19: Concurrency & Parallel Programming

Solution of Race Condition

• synronized keyword

• java.util.Lock

• Immutable

• Atomic Integer

Page 20: Concurrency & Parallel Programming

Multiple Locks?

Page 21: Concurrency & Parallel Programming

Dining Philosophers

• A philosopher is either hungry or thinking

• If he is hungry, he picks up a chopsticks on either side of him and eats for a while.

• When he is done, he puts them down

Page 22: Concurrency & Parallel Programming

DEADLOCKS

Page 23: Concurrency & Parallel Programming

• java 6 has java.util.concurrent package.

• This package has great features

• Use case :

• XBRL instance creation ( 5-6 financial table in parallel by Executer Tasks)

• Load tests 75 —> 150 concurrent user

Page 24: Concurrency & Parallel Programming

If it Hurts,Stop doing it

Page 25: Concurrency & Parallel Programming

FUNCTIONAL PROGRAMMING

Page 26: Concurrency & Parallel Programming

Functional Programming

• Imperative program has series of statements that changes global state when executed

• A functional programming ( aka FP) computation as evaluation of expression.

• Those expressions are built from pure mathematical functions

• Side effect free

Page 27: Concurrency & Parallel Programming

• FP is useful for concurrency because lack of side effect makes reasoning about thread safety much easier.

• FP allows parallelism to be represented directly.

• If data is immutable ( not shared mutable state) can be accessed by multiple threads without any kind of locking.

Page 28: Concurrency & Parallel Programming

History of FP• FP invented in 1930s, Lambda Calculus

• in 1950s, LISP was created by John McCarthy

• in 1970s, ML was created Robin Milner and David Turner.

• in 1987, Haskell began with a consensus (open standards)

Page 29: Concurrency & Parallel Programming

Haskell Basic Features

• Pure Function

• First Class and higher order function

• Recursion

• Immutable

• Lazy evaluation

Page 30: Concurrency & Parallel Programming
Page 31: Concurrency & Parallel Programming

Fibonacci Number in Haskell

Page 32: Concurrency & Parallel Programming

Use Case : Facebook Spam detection ( HAXL)

Page 33: Concurrency & Parallel Programming

Actor Model

Page 34: Concurrency & Parallel Programming
Page 35: Concurrency & Parallel Programming

Elixir• Created by Jose Valim in

2012

• Syntax is similar to Ruby

• Runs on Erlang Virtual Machine (BEAM)

• Functional

• Erlang process (actor)

Page 36: Concurrency & Parallel Programming
Page 37: Concurrency & Parallel Programming

Use Case : Pinterest

• 14000 notification per seconds

• Runs on 15 server( old one runs on 30 server)

• Pinterest API : response time is about 500 µs

https://engineering.pinterest.com/blog/introducing-new-open-source-tools-elixir-community

Page 38: Concurrency & Parallel Programming

Use Case : Whatsapp• Run on Erlang BEAM/OTP

• 1+ Million user signup per day

• 50 Million messages per day

• 14 Million active users per day

• Total 450 Million users

• >8000 cores

• > 70Million Erlang messages per day

• 2 Million connection per server

http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-billion.html

Page 39: Concurrency & Parallel Programming

Questions ?

Page 40: Concurrency & Parallel Programming

Bonus Sections

Page 41: Concurrency & Parallel Programming

Bonus - 1• Clojure

• Created by Rich Hickey in 2007

• Inspired by LISP

• Runs on JVM

• Clojure Agent: shared access to mutable state.

• No blocking receive

Page 42: Concurrency & Parallel Programming

Bonus - 2

• Is FP (Haskell) ready for industry ?

• Linq invented by Eric Meijer

• Scala, F# , Swift, even Java 8 has FP features

Page 43: Concurrency & Parallel Programming

Bonus - 3• I love imperative style,

which programming language may I use ?

• GOLANG :

• is invented after multi core

• Has Garbage Collector• Has go-routine & chain