Concurrency & Parallel Programming

Post on 12-Apr-2017

268 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

Transcript

Concurrency & Parallel

Programmingby Ramazan AYYILDIZ

rayyildiz.me@rayyildiz

Gordon MooreIntel co-founder

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

months."

Multi Core CPUs

Concurrency vs

Parallel Programming

Parallel Programming

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

Concurrency

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

Beyond the Multiple Cores

Parallel Architecture• Instruction Level Parallelism

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

• Data parallelism

• GPU

• Intel accured Altera ~17 Billion USD

Threads

Threads

• Simple thread for java

• java.util.Thread

• Mutex, Deadlock, Race Condition, Lock

Simple Thread

• Why Thread.yield()

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

• Why Thread.join()

Race Condition

Solution of Race Condition

• synronized keyword

• java.util.Lock

• Immutable

• Atomic Integer

Multiple Locks?

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

DEADLOCKS

• 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

If it Hurts,Stop doing it

FUNCTIONAL 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

• 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.

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)

Haskell Basic Features

• Pure Function

• First Class and higher order function

• Recursion

• Immutable

• Lazy evaluation

Fibonacci Number in Haskell

Use Case : Facebook Spam detection ( HAXL)

Actor Model

Elixir• Created by Jose Valim in

2012

• Syntax is similar to Ruby

• Runs on Erlang Virtual Machine (BEAM)

• Functional

• Erlang process (actor)

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

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

Questions ?

Bonus Sections

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

Bonus - 2

• Is FP (Haskell) ready for industry ?

• Linq invented by Eric Meijer

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

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

top related