Concurrency Algorithms and Theories 02 Memory Models

Memory Models

Why Memory Models ?

Sequential Consistency (SC) Model

Interleaving semantics

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lNwEL7X5-1668869667086)(https://s2.loli.net/2022/09/23/brFgsuhX9Bo1xpP.png)]

每一次读取的都是上一次写的值

The need of weak memory models

SC model prohibits many optimization

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BCljSdaV-1668869667086)(https://s2.loli.net/2022/09/23/FW6EVArtHReiCTx.png)]

Impossible in SC model, but allowed in x86 or Java (because of 编译优化)

Weak memory model allow more behaviors

Design Criteria

  • Usability: DRF guarantee
    • DRF programs have the same behaviors as in SC model
  • Not too strong
    • Allow common optimization techniques
    • Allow standard compilation schemes to major modern architectures
    • In some sense hijacked by the mainstream complier
  • Preserve type-safety and security guarantee
    • Cannot be too weak (out-of-thin-air)

Data-Race-Freedom(DRF)

Data-race: read-write / write-write conflicts

Data race

  • A data race occurs when we have two concurrent conflicting operations

    • Conflicting: the two operations both access the same memory location and at least one is a write
    • Concurrent ?
      • Differs across memory models
      • Java: the two operations are not ordered by “happens-before”

SC下 的 happens-before : P O ∪ S W PO\cup SW POSW

  • program-order

  • synchronizes-with

DRF Guarantee

  • DRF programs have the same behaviors as in SC
    • For DRF programs, the programmer does not need to worry that reorders will affect her code

Compiler Optimization Can Be Smart

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bN0iUCjg-1668869667087)(https://s2.loli.net/2022/09/23/EOWYhvDaP7qnUsM.png)]

Efforts for Java Memory Model (JMM)

  • First edition in Java Language Spec
  • Current JMM

Happens-Before Order

  • Program execution: a set of events, and some orders between them

**Happens-before order (hb): **transitive closure of p o ∪ s w po\cup sw posw

w 1 ⟶ h b w 2 w_1 \stackrel{hb}{\longrightarrow} w_2 w1hbw2

w 1 ⟶ h b r w_1 \stackrel{hb}{ \longrightarrow }r w1hbr

Happens-Before Memory Model (HMM)

Read can see

  1. the most recent write that happens-before it, or
  2. a write that has no happens-before relation

r could see both w 1 w_1 w1 ( which happens-before it )

and w 2 w_2 w2 ( with which there is no happens-before relation )

HMM - Relaxed Ordering

HMM - Examples with Global Analysis

HMM - Out-of-Thin-Air-Read

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uCbOZ89y-1668869667088)(https://s2.loli.net/2022/09/23/HLyMbXV3Eu98txB.png)]

  • Good speculation -> should allow
  • Bad speculation -> disallow!

JMM

  • Take HMM as the core, and try hard to distinguish good speculation from bad speculation!
  • Introduce 9 axioms to constrain causality.
  • Very complex, with surprising results and bugs.

Surprising Results

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RpA1bQio-1668869667088)(https://s2.loli.net/2022/09/23/MDBY7CIpPozTy2O.png)]

Inlining threads may increase behaviors!

More:

Re-ordering independent operations may change behaviors

Adding/removing redundant reads may change behaviors.

More Examples

Load buffering (LB)

Allowed!

Independent reads of independent writes (IRIW)

  • SC下不可能产生

  • HB下Allowed

Out of thin air read

OOTA的行为

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EHBY2CJJ-1668869667089)(https://s2.loli.net/2022/09/23/yjI7Yqk1PBWCR8u.png)]

如何判断 o u t − o f − t h i n − a i r out-of-thin-air outofthinair行为:具体分析

Allowed in HMM !

HMM does not have DRF-guarantee

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BR4NfbzA-1668869667089)(https://s2.loli.net/2022/09/23/iuXxcDGNLQtvUTw.png)]

Summary

  • Why need weak memory models
  • Design criteria of weak memory models
  • The happens-before memory model
  • Out-of-thin-air read
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值