Shan Show
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    2017q3 Homework1 (phonebook) === contributed by < `shanshow` > ### 預期目標 * 適度修改 phonebook_opt.c 和 phonebook_opt.h 兩個檔案,使得這兩者執行時期的 cache miss 降低。請用 perf 驗證,而且改進的過程中,不能有功能方面的減損。 * 於是,先行理解和為 cache miss 及有何種方式使其降低。 * 閱讀老師提供的 [Programming small](https://hackmd.io/s/SkfffA-jZ),以及 [cache 原理和實驗](https://hackmd.io/s/S14L26-_l) * 其中,關於 struct 的大小計算並非一開始想像的直接把char內的數值相加,因為 struct 的空間分配不是直接使用連續空間的,為記憶體的對齊問題。 * [struct 結構佔記憶體容量計算](http://blog.csdn.net/myblog_dwh/article/details/13035005) ### cache miss: * 在 cache 裡找不到需要的指令或資料,因此須繼續往 main memory 找,而 latency (延遲)時間會愈長。 * 有三種類型:([參考投影片EECC551 - Shaaban](http://meseec.ce.rit.edu/eecc551-winter2001/551-1-30-2002.pdf)) #### Compulsory: ``` On the first access to a block; the block must be brought into the cache; also called cold start misses, or first reference misses. 第一次存取某尚未存在於 cache 內的 block 的時候發生,這無法避免,畢竟 你一定得先把 block 放到 cache 內才有意義。又稱 cold start miss ``` #### Capacity: ``` Occur because blocks are being discarded from cache because cache cannot contain all blocks needed for program execution (program working set is much larger than cache capacity). 因為在程式執行的期間, cache 沒辦法把所有需要的 block 一起放進去( capacity 不足), 所以縱使那個 block 稍後仍然需要,也得先行拋棄。 ``` #### Conflict: ``` In the case of set associative or direct mapped block placement strategies, conflict misses occur when several blocks are mapped to the same set or block frame; also called collision misses or interference misses. 在 set associative 跟 direct mapped block caches 內發生, 很多個 block 搶奪同一個 set 導致 miss ,又稱 called collision misses 或 interference misses 。 ``` * 其中, Capacity 與 Conflict 的概念容易混淆。 * 以[老師提供的文件](http://hackfoldr.org/cpu/https%253A%252F%252Fhackmd.io%252Fs%252FHkW3Dr1Rb)作為參考與概念釐清。 #### Capacity misses : 如果有一個 64KB 的陣列需要重複存取,因為數組大小遠大於 cache 大小,所以沒辦法全部放入 cache 。第一次存取陣列發生的 cache misses 全都是 compulsory misses 。但之後存取陣列發生的 cache misses 全都是 capacity misses ,這時的 cache 已存滿,容量不足以儲存全部的資料。 #### Conflict misses : 如果有兩個 8KB 的數組需要來回存取,但是這兩個數組都映射到相同的地址, cache 的大小足夠儲存全部的數據,但是因為相同地址發生衝突,需要來回替換,這時候發生的 cache misses 全都是 conflict misses(不過,第一次存取陣列發生的 cache misses 仍然都是 compulsory misses),這時 cache 並沒有存滿。 * 解決 conflict misses 的辦法: * LRU(least recently used,最近罕用):將集合中目前最不常用的的區塊加以取代 * 舉個例子,假設 block frame 為 3 ,進入 block 的順序為 1 3 2 3 5 1 * 1 3 2 先後進入 block,由左至右表示它的優先程度的遞減:2 3 1 。 * 而後 3 再次進入,導致 block 內的 3 優先度提升至最前: 3 2 1 。 * 5 進入,因為 1 的優先度最低,因此被拋棄: 5 3 2。以此類推。 * 可減少失誤次數,實作困難且成本高,常用在虛擬記憶體中 * FIFO(first in first out,先進先出) * 將集合中最早進入的區塊加以取代 * 設 block frame = 3 ,順序為 1 2 3 4 * 1 2 3 先後進入 block : 3 2 1 * 4 進入時,把最早進入的 1 捨棄: 4 3 2 * 實作較簡單,失誤較高 #### 額外啟發:快取分區方法: 我參閱[交通大學的碩士論文](https://ir.nctu.edu.tw/handle/11536/46632),裡面提到 當越來越多處理器共享一個快取時, 會使得處理器之間對於快取資源的競爭更加劇烈, 進而影響個別單一程序的效能。 * 快取分區方法是一個可以降低程序間互相競爭的方法,其通常將快取分割給各個處理器單獨使用,然而此方法在程序不平衡的存取快取時,會造成快取分區利用率不佳的問題。針對此問題,我們提出兩個方法做改善: * 1. 額外增加一塊共享分區供所有處理器共同使用 * 2. 針對每一個處理器,給予不同的索引方式。另外,我們針對所有分區,在一般常用於減少失誤的方法下(可變動區塊大小、可變動關聯度、改變替代策略)進行討論。 * 論文偏長,需持續精進閱讀與補充此條目。 ### 效能改善 作業要求如下 適度修改 phonebook_opt.c 和 phonebook_opt.h 兩個檔案, 使得這兩者執行時期的 cache miss 降低。 請用 perf 驗證,而且改進的過程中, 不能有功能方面的減損。 * 根據[AMAT的計算式](https://zh.wikipedia.org/wiki/CPU%E7%BC%93%E5%AD%98#.E9.99.8D.E4.BD.8E.E5.A4.B1.E6.95.88.E7.8E.87),可以看出優化快取可從三個方面入手: * 一、減少命中時間($T_{hit}$)。 * 二、降低失效率($MR$)。 * 三、減輕失效代價($MP$)。 * $AMAT=T_{hit}+MR\times MP$ * $T_{hit}$為 Hit Time :從定位快取塊、經標籤比較並選中,一直到傳回資料所需的時間。 * $MR$為 Miss Rate ,在特定次數的記憶體訪問中,發生 cache miss 次數所占的比重。 * $MP$為 Miss Penalty:從定位快取塊、經標籤比較判定失效,然後再從記憶體中定位資料並載入快取,最後直到把目標資料返回所需的時間。 * 首先觀看 phonebook_orig.c 檔案,可發現裡面使用的資料結構是 Linked list。 * 在[Programming small](https://hackmd.io/s/SkfffA-jZ)中提到一個 phone book 案例,有一個優化方式:使用體積較小的 struct 。 * 根據題目要求,我們只需要知道「有沒有找到 last name」,對於 email、phone number、address、zip 等等資訊是可以在搜尋過程中忽略不看。另外我又希望能不改變原本 phonebook entry 的結構,所以我另外設計一個 struct 只儲存 last name,並用一個指向 phonebook entry 的指標叫 *detail 來儲存詳細資訊,新的 struct 大小只有 32 bytes,這樣搜尋的過程中,cache 就可以塞進 (32 * 1024) / (32 * 8) = 128 個單字,增加 cache hit 的機率。 * 以此為參考設計程式: ```clike= typedef struct __LAST_NAME_ENTRY{ char lastName[MAX_LAST_NAME_SIZE]; entry *detail; struct __LAST_NAME_ENTRY *pNext; } lastNameEntry; ``` * 根據這個程式碼,將其餘不必要的參數用 __PHONE_BOOK_DETAIL 另行放置,然後改寫 __PHONE_BOOK_ENTRY 。 * 因為 findName 只會查詢 lastName ,然而最初的程式碼會把 Entry 總共 136 bytes 的資料都搬進去 cache 內,造成效率不足。 * 於是將一些不會用到的資料以 struct __PHONE__BOOK_DETAIL 的形式另外儲存,再用指標的方式讓原本的 Entry連過去,可以有效把 Entry 的大小縮至 32 bytes 。 * 進行 make cache-test ```clike= Performance counter stats for './phonebook_orig' (100 runs): 2,104,919 cache-misses # 91.069 % of all cache refs ( +- 0.04% ) 2,311,356 cache-references ( +- 0.05% ) 261,910,733 instructions # 1.18 insn per cycle ( +- 0.02% ) 221,715,446 cycles ( +- 0.32% ) 0.077304586 seconds time elapsed ( +- 1.00% ) ``` ```clike= Performance counter stats for './phonebook_opt' (100 runs): 446,366 cache-misses # 61.120 % of all cache refs ( +- 0.38% ) 730,312 cache-references ( +- 0.32% ) 244,530,784 instructions # 1.62 insn per cycle ( +- 0.02% ) 150,966,012 cycles ( +- 0.23% ) 0.048559588 seconds time elapsed ( +- 1.03% ) ``` * 原本的情形, cache misses 達到91.069 %,更改後的 則急遽減少至61.120 %。 ![](https://i.imgur.com/t6nER8t.png) ### 使用 hash table 的效能增長(待改進 * Linked list 搜尋時的時間複雜度為 O(N),在數目龐大時將損耗效能。 * 思考是否有其餘資料結構能夠用更精簡的時間複雜度。 * 依據 [Hash Table:Intro(簡介)](http://alrightchiu.github.io/SecondRound/hash-tableintrojian-jie.html) ,如果使用 Hash Table 結構,進行搜尋的時間複雜度將變為 O(1)。

    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