1 of 19

Linux 核心專題《Concurrency Primer》校訂和範例撰寫

Yu-Ting Shi 施宇庭

MS in Intelligent Computing, NCKU

2 of 19

Outline

  • 任務目標
  • 任務一:修訂並行程式設計:執行順序
  • 任務二:將執行順序納入《Concurrency Primer》

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

1

3 of 19

任務目標

  • 修訂《並行程式設計:執行順序》及《Concurrency Primer》
    • 閱讀並行程式設計系列教材並記錄問題
    • 閱讀 Russ Cox 所寫的〈Hardware Memory Models〉和〈Programming Language Memory Models〉並記錄認知
    • 改進上課教材《並行程式設計:執行順序》
    • 將改進納入《Concurrency Primer》

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

2

4 of 19

任務一:修訂並行程式設計 — 執行順序

  • Memory consistency model: contract between hardware, compilers, and programs

Memory Consistency Models

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

3

5 of 19

任務一:修訂並行程式設計 — 執行順序

  • 1970s, proposed by Leslie Lamport
    • A multiprocessor system is sequentially consistent if 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.

Seqeuntial Consistency

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

4

6 of 19

任務一:修訂並行程式設計 — 執行順序

  • Message passing litmus test
    • not possible for r1 = 1, r2 = 0
  • Hardware model
    • all threads share a single memory which can only serve one read/write operation at a time

Seqeuntial Consistency

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

5

7 of 19

任務一:修訂並行程式設計 — 執行順序

  • Hardware model
    • write to a FIFO buffer
    • read from shared memory
  • Store buffer litmus test
    • x86-TSO allows r1 = r2 = 0, which is forbidden in sequential consistency
  • IRIW litmus test
    • x86-TSO denies r1 = 1, r2 = 0, r3 = 1, and r4 = 0.

Hardware Memory Models – x86 Total Store Order (TSO)

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

6

8 of 19

任務一:修訂並行程式設計 — 執行順序

  • Hardware model
    • read/write from/to local memory

Hardware Memory Models – ARM Relaxed Memory Order

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

7

  • Litmus test
    • allows IRIW to be r1 = 1, r2 = 0, r3 = 1, and r4 = 0.
    • denies coherence test to be r1 = 1, r2 = 2, r3 = 2, and r4 = 1.

9 of 19

任務一:修訂並行程式設計 — 執行順序

The Math of Memory Ordering – Must-not-reorder Funct.

  •  

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

8

10 of 19

任務一:修訂並行程式設計 — 執行順序

  •  

The Math of Memory Ordering – Must-not-reorder Funct.

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

9

x86 total store order (TSO)

ARM relaxed memory model (RMO)

11 of 19

任務一:修訂並行程式設計 — 執行順序

  •  

The Math of Memory Ordering – Read-from Map Relation

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

10

12 of 19

任務一:修訂並行程式設計 — 執行順序

  •  

The Math of Memory Ordering – Happens-before

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

11

13 of 19

任務一:修訂並行程式設計 — 執行順序

  •  

The Math of Memory Ordering – Happens-before

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

12

14 of 19

任務一:修訂並行程式設計 — 執行順序

  •  

The Math of Memory Ordering – Memory Models

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

13

15 of 19

任務一:修訂並行程式設計 — 執行順序

  • Sarita Adve and Mark Hill proposed “weak ordering” in 1990
    • If no data race in software, then weakly ordered hardware appears sequential consistency, even thought the hardware has weak memory consistency model.
  • Hardware: provide synchronization mechanisms for software to prevent data races.
  • Software: no write of a thread happens when there are other threads reading/writing (data-race-free).
    • e.g. Dekkers algorithm, Petersons algorithm, Pthread, Atomic instructions

Data-Race-Free Sequential Consistency (DRF-SC)

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

14

Program may have data race

Program without data race

16 of 19

任務一:修訂並行程式設計 — 執行順序

  • Strong synchronization (sequentially consistent)
    • memory_order_seq_cst:沒有指令重排,確保 sequential consistency
  • Weak synchronization (acquire/release, coherence-only)
    • memory_order_acquire:用於讀取操作,後面的存取不能重排到此同步之前
    • memory_order_release:用於寫入操作,前面的存取不能重排到此同步之後
    • memory_order_acq_rel:用於 RMW 操作,同時有 acquire + release 語意
  • No synchronization (relaxed, hiding races)
    • memory_order_relaxed:只保證操作的 atomicity

C11/C++11 Memory Consistency Models

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

15

17 of 19

任務二:將執行順序納入《Concurrency Primer》

Preparation for Contribution to Concurrency Primer

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

16

18 of 19

任務二:將執行順序納入《Concurrency Primer》

  • Detailed describe the sequential consistent, x86-TSO, and ARM memory models.
  • Litmus tests to differentiate between different memory models

Pull Requests and Code Review

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

17

19 of 19

Thanks for listening

Linux 核心專題: 《Concurrency Primer》校訂和範例撰寫

18