![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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022062315/5697c0301a28abf838cdaf87/html5/thumbnails/29.jpg)
29
Thanks!