# Concurrency and Parallelism

## Concurrency and Parallelism

Concurrency is a composition of independently computing things.
Parallelism is a simultaniuse execution of multiple things.

Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.

Rob Pike, "Concurrency Is Not Parallelism", 2012

## Concurrency and Parallelism

### Process

``````
public class Counting {
public static void main(String[] args) throws InterruptedException {
class Counter {
private int count = 0;
public void increment() { ++count; }
public int getCount() { return count; }
}
final Counter counter = new Counter();

public void run() {
for(int x = 0; x < 10000; ++x)
counter.increment();
}
}

t1.start(); t2.start();
t1.join(); t2.join();

System.out.println(counter.getCount());
}
}
``````

### High-level synchronization

``````
class Counter {
private int count = 0;
public synchronized void increment() { ++count; }
public int getCount() { return count; }
}
``````

# Functional Programming

## Functional Programming: Sum

``````
(defn reduce-sum [numbers]
(reduce (fn [acc x] (+ acc x)) 0 numbers))
``````
``````
(defn sum [numbers]
(reduce + numbers))
``````
``````
(ns sum.core
(:require [clojure.core.reducers :as r]))

(defn parallel-sum [numbers]
(r/fold + numbers))
``````

## Functional Programming: Wikipedia parser

``````
(defn count-words-sequential [pages]
(frequencies (mapcat get-words pages)))
``````
``````
(pmap #(frequencies (get-words %)) pages)
``````
``````
(defn count-words-parallel [pages]
(reduce (partial merge-with +)
(pmap #(frequencies (get-words %)) pages)))
``````

## Functional Programming: Divide and Conquer

``````
(ns sum.core
(:require [clojure.core.reducers :as r]))

(defn parallel-sum [numbers]
(r/fold + numbers))
``````

## Functional Programming: Referential Transparency

``````
(+ (+ 1 2) (+ 3 4)) → (+ (+ 1 2) 7) → (+ 3 7) → 10
``````
``````
(+ (+ 1 2) (+ 3 4)) → (+ 3 (+ 3 4)) → (+ 3 7) → 10
``````

# Software Transactional Memory (STM)

## STM

``````
(defn transfer [from to amount]
(dosync
(alter from - amount)
(alter to + amount)))
``````
``````
=> (def user1 (ref 1000))

=> (def user2 (ref 2000))

=> (transfer user2 user1 100)
1100

=> @checking
1100

=> @savings
1900
``````

# Actor model

## Actor model

``````
defmodule Talker do
def loop do
{:greet, name} -> IO.puts("Hello, #{name}")
{:bye, status, name} -> IO.puts("Bye, #{status} #{name}")
end
loop
end
end
``````
``````
pid = spawn(&Talker.loop/0)

send(pid, {:greet, "Gopher"})
send(pid, {:bye, "Mrs", "Pike"})

sleep(1000)
``````
``````
Hello, Gopher
Bye, Mrs Pike
``````

# Communicating Sequential Processes (CSP)

## Communicating Sequential Processes (CSP)

### Focus on the channels

Do not communicate by sharing memory, instead share memory by communicating

Rob Pike

## CSP

### Goroutines

#### Cooperative scheduler vs Preemptive scheduler

``````
go func()
``````
Just looked at a Google-internal Go server with 139K goroutines serving over 68K active network connections. Concurrency wins.

@rob_pike

## CSP: Channels

#### Channels – first class object

``````
// Declaring and initializing
var ch chan int
ch = make(chan int)
// or
ch := make(chan int)
// Buffering
ch := make(chan int, 100)
``````
``````
// Sending on a channel
ch <- 1
``````
``````
// Receiving from a channel
value = <- ch
``````

## CSP

### Example

``````
func main() {
jobs := make(chan Job)
done := make(chan bool, len(jobList))

go func() {
for _, job := range jobList {
jobs <- job // Blocks waiting for a receive
}
close(jobs)
}()

go func() {
for job := range jobs { // Blocks waiting for a send
fmt.Println(job) // Do one job
done <- true
}
}()

for i := 0; i < len(jobList); i++ {
<-done // Blocks waiting for a receive
}
}
``````

## Wrapping Up

### Don't write Django/RoR by Go/Clojure/Erlang

#### Books:

• “Seven Concurrency Models in Seven Weeks”, 2014, by Paul Butcher
• “Communicating Sequential Processes”, 1978, C. A. R. Hoare

## The end

### Thank you for attention!

• Vasyl Nakvasiuk
• Email: vaxxxa@gmail.com
• Github: vaxXxa