Memory Models 17. September 2012
Here Be Dragons In addition, programming idioms used by some programmers and used within Sun s Java Development Kit is not guaranteed to be valid according the existing Java memory model. Fixing the Java Memory Model, William Pugh Compilerbau Systemprogrammierung High-Performance Code Effekte teilweise beobachtbar mit Wahrscheinlichkeit 10 9 http://www.cl.cam.ac.uk/ pes20/ppc-supplemental/ppc051.html
Warum und Wozu Out-of-Order Execution Speculative Reads Multiprocessor / -core Systeme 1 Takt für Registerzugriff 3 Takte für L1 Cache 12 Takte für L2 Cache 40 Takte für L3 Cache 200 Takte für RAM
Read Reordering movl $0, x movl $0, y movl x, %eax movl y, %ebx movl $1, y movl $1, x eax = 0 ebx = 0 eax = 1 ebx = 0 eax = 0 ebx = 1?
Read Reordering movl $0, x movl $0, y movl x, %eax movl y, %ebx movl $1, y movl $1, x eax = 0 ebx = 0 eax = 1 ebx = 0 eax = 0 ebx = 1 eax = 1 ebx = 1
Write Reordering movl $0, x movl $0, y movl $1, x movl y, %ebx lfence movl $1, y movl x, %eax eax = 0 ebx = 0 eax = 1 ebx = 0 eax = 1 ebx = 1?
Write Reordering movl $0, x movl $0, y movl $1, x movl y, %ebx lfence movl $1, y movl x, %eax eax = 0 ebx = 0 eax = 1 ebx = 0 eax = 1 ebx = 1 eax = 0 ebx = 1
Independent reads of independent writes movl $0, x movl $0, y movl $1, x movl x, %eax movl $1, y movl y, %ecx lfence lfence movl y, %ebx movl x, %edx. eax = 1 ebx = 0 ecx = 1 edx = 0.
Causality Loops A = 0 B = 0 a = A b = B if(a == 1) { if(b == 0) { B = 1 A = 1 } } if(b == 1) { A = 1 }. a = 1 b = 1.
Memory Models Mehrere Wortbedeutungen, hier nur bezogen auf Nebenläufigkeit Welche Verhaltensweisen sind zulässig? Welche Optimierungen darf der Prozessor vornehmen? Welche Optimierungen dürfen Compiler & Prozessor vornehmen? Unter welchen Bedingungen gelten welche Reihenfolgengarantien? Welche Arten von Abhängigkeiten sind für die Reihenfolge relevant?
Data Dependencies movl $1, %eax movl %eax, %ebx Das Ergebnis einer Operation hängt vom Ergebnis einer früheren ab Instruktionsreihenfolge wird von realen Prozessoren nicht garantiert
Control Dependencies andl $0, %eax jnz skip movl $1, %ebx skip: Die Ausführung einer Operation hängt vom Ergebnis einer früheren ab Instruktionsreihenfolge wird von realen Compilern nicht garantiert
Address Dependencies movl x, %eax movl (%eax), %ebx Ein Speicherzugriff einer Operation hängt vom Ergebnis einer früheren ab Instruktionsreihenfolge wird von realen Prozessoren momentan garantiert Relevanz für den Entwurf von lockfree und waitfree Algorithmen
Transitive Dependencies movl $0, addr1 movl $0, addr2 movl $0, %eax movl $0, %ebx movl $addr1, addr1 start1: start2: orl addr1, %eax orl addr2, %ebx jz start1 jz start2 movl (%eax), %eax movl (%ebx), %ebx movl %eax, addr2. ebx = 0.
Explicit Barriers lfence Kein Reordering von loads, lfence, mfence, serializing instructions sfence Kein Reordering von stores, sfence, mfence, serializing instructions mfence Kein Reordering von Speicherzugriff, lfence, sfence, mfence, clflush, serializing instructions ser.instr. Instruktion wird vollständig abgeschlossen lock-* Systemweit atomare Operation
Sequential Consistency... the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program. How to Make a Multiprocessor Computer That Correctly Executes Multiprocess Programs, Leslie Lamport
x86 Memory Model The following n6 example, due to Paul Loewenstein [14], shows a behaviour that is observable (e.g. on an Intel Core 2 duo), but that is disallowed by x86-cc, and by any interpretation we can make of IWP and AMD64-3.14. A Better x86 Memory Model: x86-tso, Scott Owens, Susmit Sarkar, Peter Sewell x86-total-store-order Jeder Kern hat einen eigenen Write-Buffer Ansonsten gibt es nur noch den globalen Speicher
PowerPC Memory Model To order stores to devices on two different PCI buses requires additional software protocols, such as performing a load from one device to ensure that a store has completed before permitting a store to a second device that is on a different PCI bus. PowerPC storage model and AIX programming, www.ibm.com/developerworks/systems/articles/powerpc.html Verschiedene Klassen von Speicher ( Memory, IO ) Address Dependencies werden beachtet sync Kumulative Barrier für alle Speicher eieio Barrier für IO-Speicher lwsync Barrier für RAM isync Instruktionsserialisierung
Java Memory Model happens-before-relation: ( Program Order Synchronizations ) + Ein Programm ist correctly synchronized wenn in Sequentially Consistent Ausführungen alle Read-/Writekonflikte durch happens-before geordnet sind Correctly synchronized Programme verhalten sich, als ob Sequential Consistency gelten würde Sonderregeln für volatile und final
C++11 Memory Model std::atomic und std::memory order memory order relaxed völlig ungeordnet memory order consume keine abhängigen Loads vorziehen memory order acquire, memory order release, memory order acq rel entsprechen lfence, sfence, mfence memory order seq cst Sequential Consistency volatile garantiert Dinge nur innerhalb eines Threads Über std::atomic keine völlig frei erfundenen Werte erzeugbar Normale Variablen: undefined behaviour