FANFNA
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Mem Exploiting Memory Hierarchy [TOC] ## 5.0 Out Line <font color="#f00"></font> - What are the challenges of building big, fast memory systems? - What is a cache? - Why caches work? - How are caches organized? - Where do we put things -and- how do we find them? ## 5.1 Direct Mapped Cache ### Large storage memory hierache ![](https://i.imgur.com/mWsEINn.png) - SRAM - Static RAM - DRAM - Dynamic RAM - Disk - Non-volatile 非揮發 - Flash Storage - NOR flash: NOR gate - NAND flash - DRAM(Dynamic Random Access Memory) - rectangular array - Burst mode: faster - Double data rate(DDR) - Quad data rate(QDR) -![](https://i.imgur.com/vLQRTCs.png) ![](https://i.imgur.com/17lDkGp.png) ### 基本Cache存取相關名詞 - Block : unit of copying , may be multiple words - Hit : access satisfied by upper level - data in block b is in cache - Miss : block copied from lower level - data in block b is not in cache go to mem take - Hit ratio : **hits/accesses** - Miss ratio : misses/accesses = **1 – hit ratio** - ### ie - consider - cache hit time of 1 cycle - Miss penalty 1000 cycle - AVG acess time: - 97 hit: 1cycle +.03 * 100 cycle =4 - 99 hit: 1cycle +.01 * 100 cycle=2 ### 使用cache有效增加效率的原因(Locality) ###### Locality 因為program 很常會使用的 `data` and `指令`所以我們會把他的`addr` 放在鄰近的擺在附近 **==must use in time or in space usually==** - Temporal locality: - recently use data or instraction ![](https://i.imgur.com/eSxHFbd.png) - instructions in a <font color="#f00">loop</font>, induction variables - ![](https://i.imgur.com/hp3MPk9.png) - Spatial locality: - nearby addr ![](https://i.imgur.com/RTozhjg.png) - sequential <font color="#f00">instruction access</font>, array data - from locations i + 1, i + 2 or i + 3 - ![](https://i.imgur.com/QkN7znj.png) if in data:(class) - ![](https://i.imgur.com/0zgSEHy.png) - Direct mapped: - 只有==hash==和定址而得到的一個項,沒有搜尋 - ![](https://i.imgur.com/PHt0eGK.png) ### Address的內容 - <font color="#f00">[tag]-[index]-[byte offset]</font> ![](https://i.imgur.com/lxEyFXT.png) - tag : 記錄現在在cache block中的是memory的哪一個block - index : 用來對每一個block定址(global) - offset : 用來對每一個byte/word定址(local) ### Cache Memory的內容 - <font color="#f00">[Index]-[Valid bit]-[Tag]-[Data]</font> ![](https://i.imgur.com/d2rFfCj.png) - Valid bit : 1 = present, 0 = not present ###### Direct mapped example: ![](https://i.imgur.com/97RRxxz.png) - 若cache要存放的資料量為16Kbyte, address為32 bits, 每一個block大小為4 words, 則Cache大小為多少? 1. 首先求Entry數目,Entry數目=1k=2^10,因此 index=10 2. 再求block內的word數目,4=2²,因此 word offset=2 3. 再求word內的byte數目,很明顯是,4=2²,因此 byte offset=2 4. 最後求tag,為 *address−(index+offset)* *tag=32–10(index)−2(word offset)−2(byte offset)* - 最終得Cache大小為 *2^10×(1(valid)+18(tag)+4×32(blocksize))≒147000bits* 2^10(19*128)=1024*(147) - 當題目有提到word的字眼 就用block offset,沒有的話,就只用byte offset即可 1. entry數 => Index bit 2. Block的word數 => block offset 3. N-bit Address => byte offset ![](https://i.imgur.com/l0lWok8.png) ![](https://i.imgur.com/vOAG3C3.png) ###### example: ![](https://i.imgur.com/pfJUFQM.png) ## 5.2 Cache Miss ### Block size issue - 當block Size越來越大,會降低 miss rate,因為==spatial locality==。 - block越大miss penalty越嚴重,因為要搬更多東西。 - 在固定的cache size下,太大的block Size : 1. 會使block數目變少而miss rate上升 2. 會讓沒有物盡其用的情況變嚴重,白白複製一堆東西到cache ![](https://i.imgur.com/aieesRm.png) ### Data Write issue - <font color="#f00">write-through:</font> 同時更改cache和memory的的資料,效能差但可用write buffer使CPU繼續執行來改善,當發生write miss時,有兩種處理方式。 1. write around : 不存取cache直接去寫main memory 2. allocate on miss : 存取cache一起改 - <font color="#f00">write-back :</font>先寫到cache並註記為dirty,當dirty block整個被替換時才寫回memory ### Cache Performance: Average memory access time (AMAT) ,<font color="#f00"> AMAT = Hit time + Miss rate × Miss penalty </font> ## 5.3 Cache Miss improvement <font color="#f00"></font> - <font color="#f00">Associative Cache:</font>增加空間使用的彈性,引入search的概念 (原本只有map和hash) ,使miss rate降低,硬體可平行做search。 - <font color="#f00">direct mapped :</font> hash 的概念 (block 位址直接mod n) - <font color="#f00">2-way set associative : </font> 半hash、半search - <font color="#f00">fully associative :</font> 全search - 在vm的時候都是用full assoc (因為不用搜尋所有的存儲空間) - 因為關係到search的查找模式,所以有replacement policy - Least-recently used (LRU) - Random ![](https://i.imgur.com/opaHgw3.png) ![](https://i.imgur.com/B5xxI1R.png) ![](https://i.imgur.com/pb6hZrQ.png) - When the CPU tries to read from memory, the address will be sent to a cache controller. ![](https://i.imgur.com/dsdjG4f.png) - multi-level caches 1. 第一層 cache:希望將 hit time 減到最小 2. 第二層 cache:希望將miss rate減到最小避免查找主記憶體,Hit time 影響較小 - 實務上的結果 - L-1 cache 大小< single cache 大小 - L-1 block 大小 < L-2 block 大小 ###### example:考慮一個兩層架構的cache Access time = 5 ns, Main memory access time = 100ns Clock cycle = 1/4GHz = 0.25ns (算次數) Global miss rate to main memory = 0.5 % Miss rate/instruction = 2% (算出現機率) CPI = 1 + 0.02 × 20 + 0.005 × 400 = 3.4 - memory hierarchy interaction with software - Radix Sort 複雜度較小,花的instruction少 - Radix Sort 要用bocket額外暫存,miss rate 較高 - 所以,總和起來還是quick sort快

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully