Top Banner
1 Based on : The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction to Concurrent Programming Software & Hardware Basics Slides by Ofer Givoli
29

1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

Jan 21, 2016

Download

Documents

Jordan Fowler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

1

Based on:The art of multiprocessor programmingMaurice Herlihy and Nir Shavit, 2008• Appendix A – Software Basics• Appendix B – Hardware Basics

Introduction to Concurrent ProgrammingSoftware & Hardware Basics

Slides by Ofer Givoli

Page 2: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

2

Software Basics

Page 3: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

3

Threads in Java

• Executes a single, sequential program

• Subclass of: java.lang.Thread

Page 4: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

4

Taken from: The art of multiprocessor programming, by Maurice Herlihy and Nir Shavit, 2008 (modified)

Page 5: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

5

Monitors

• lock + waiting set

• every object is a monitor

• Critical section: using the synchronized keyword.

• Waiting: using the wait()method• Waking-up waiting threads, using the methods:

• notify()• notifyAll()

Page 6: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

6

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public void push(T obj) { innerStack.push(obj); }

public T pop() { return innerStack.pop(); }}

ConcurrentStack<Integer> s = ...s.push(1);

... = s.pop(); ... = s.pop();

Solution: mutual exclusion

Page 7: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

7

... = s.pop(); ... = s.pop();

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>(); private Object monitor = new Object();

public void push(T obj) { synchronized(monitor) { innerStack.push(obj); } }

public T pop() { synchronized(monitor) { return innerStack.pop(); } }}

BLOCKED

Page 8: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

8

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>(); private Object monitor = new Object();

public void push(T obj) { synchronized(monitor) { innerStack.push(obj); } }

public T pop() { synchronized(monitor) { return innerStack.pop(); } }}

Page 9: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

9

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public void push(T obj) { synchronized(this) { innerStack.push(obj); } }

public T pop() { synchronized(this) { return innerStack.pop(); } }}

Page 10: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

10

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { innerStack.push(obj); }

public synchronized T pop() { return innerStack.pop(); }}

New feature: waiting for pop()

Page 11: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

11

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {} return innerStack.pop(); }}

Problem?

Page 12: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

12

... = s.pop();

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {} return innerStack.pop(); }}

s.push(1);

BLOCKED

deadlock

Page 13: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

13

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {} return innerStack.pop(); }}

Page 14: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

14

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { if (innerStack.empty()) notifyAll(); innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {wait();} return innerStack.pop(); }}

Page 15: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

15

... = s.pop(); s.push(1);

BLOCKEDWAITING

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { if (innerStack.empty()) notifyAll(); innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {wait();} return innerStack.pop(); }}

BLOCKED

Page 16: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

16

... = s.pop();

WAITING

... = s.pop();

WAITING

public class ConcurrentStack<T> {

private Stack<T> innerStack = new Stack<T>();

public synchronized void push(T obj) { if (innerStack.empty()) notify(); innerStack.push(obj); }

public synchronized T pop() { while (innerStack.empty()) {wait();} return innerStack.pop(); }}

s.push(1);s.push(2);

lost wakeup

Page 17: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

17

Thread.yield();

Thread.sleep(t);

Page 18: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

18

Thread-Local Objects

class ThreadLocallD extends ThreadLocal<Integer> { protected Integer initialValue() { return …; }}

ThreadLocallD id = …;

id.set(…);…… = id.get();

id.set(…);…… = id.get();

Page 19: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

19

• Synchronization in C#• Pthreads

Page 20: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

20

Hardware Basics

Page 21: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

21

Taken from: https://software.intel.com/en-us/articles/optimizing-applications-for-numa

Page 22: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

22

L1Cache

Speed: Fastest Slowest Size: Smallest Biggest Cost: Highest Lowest Power: Highest Lowest

CPUL2

CacheL3

CacheMemory(DRAM)

Taken from: Computer Structure 2014 slides, by Lihu Rappoport and Adi Yoaz (modified)

Page 23: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

23

Processor 1

L1 cache

Processor 2

L1 cache

L2 cache (shared)

Memory

Taken from: Computer Structure 2014 slides, by Lihu Rappoport and Adi Yoaz

Page 24: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

24

SMP (symmetric multiprocessing)

NUMA (Non-uniform memory access)

Taken from: The art of multiprocessor programming, by Maurice Herlihy and Nir Shavit, 2008 (modified)

not scalable

Page 25: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

25

Cache Coherence

Cache-line states:• Modified• Exclusive• Shared• Invalid

Taken from: The art of multiprocessor programming, by Maurice Herlihy and Nir Shavit, 2008 (modified)

false sharing

Page 26: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

26

Spinning

SMPNUMA

Taken from: The art of multiprocessor programming, by Maurice Herlihy and Nir Shavit, 2008 (modified)

Page 27: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

27

• Execute instructions out-of-order/in parallel/speculatively. • write buffer

• reordering of reads-writes by compiler• memory barrier instruction (expensive)

• reads-writes reorder in Java• Volatile variables in Java

Multi-Core and Multi-Threaded Architectures

Taken from: The art of multiprocessor programming, by Maurice Herlihy and Nir Shavit, 2008 (modified)

Page 28: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

28

Hardware Synchronization Instructions

• compare-and-swap/set (CAS)• load-linked & store-conditional (LL/SC)

Page 29: 1 Based on: The art of multiprocessor programming Maurice Herlihy and Nir Shavit, 2008 Appendix A – Software Basics Appendix B – Hardware Basics Introduction.

29

Thanks!