t5i0m7
    • 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
    # 2018q1 Homework1 (phonebook) contributed by < `t5i0m7` > ### Reviewed by `Wiilemchen` * 實做 hash function 後的效能比較可以加入改 entry size 的版本 * 若有新檔案要提交,可以用`git add <file>` ### Reviewed by `BroLeaf` * 中文跟英文間要用空格隔開 * Commit Message 只有標題,應該在內文中敘述做了什麼,詳細可以參考老師給的 [如何寫一個 Git Commit Message] * 關於 Cache 的大小,`dmidecode` 上面顯示的是 CPU 總共有多少 cache 所以還要除以你的 CPU 上有多少 core 才會是對的,可以參考 [連結](http://debugo.com/ls-cpu-dmidecode/) 上面的算法 ``` L1d 快取: 16K L1i 快取: 64K L2 快取: 2048K ``` * 從 lscpu 得到的才是正確的值 ## 前言 這個課程有很多工具都是第一次接觸,一開始難免有很多不懂的,但現在對我來講 邊看邊學編寫就對了,這是我認為學習東西最快的方法。在此先感謝教授的影片與`ryanpatiency`同學 看著影片與同學的共筆讓我學到了許多。前言之就是我的開發紀錄。 ## linux系統安裝 在一開始使用usb安裝linux使自己的電腦變成雙系統就出了點問題,這跟筆電的BIOS有關 當我使用UEFI而非傳統BIOS時,這時會發生讀不到USB的情況。在花了許多白工之後,我決定 將本來要廢棄的電腦拿來安裝linux,並用putty靠SSH做遠端操控來繼續我的作業。 ## CPU 配置 * CPU ``` Architecture: x86_64 CPU 作業模式: 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 每核心執行緒數:2 每通訊端核心數:2 Socket(s): 1 NUMA 節點: 1 供應商識別號: AuthenticAMD CPU 家族: 21 型號: 16 Model name: AMD A10-5800K APU with Radeon(tm) HD Graphics 製程: 1 CPU MHz: 1400.000 CPU max MHz: 3800.0000 CPU min MHz: 1400.0000 BogoMIPS: 7599.19 虛擬: AMD-V L1d 快取: 16K L1i 快取: 64K L2 快取: 2048K NUMA node0 CPU(s): 0-3 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core perfctr_nb cpb hw_pstate retpoline retpoline_amd rsb_ctxsw vmmcall bmi1 arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold ``` * chache ``` # dmidecode 3.0 Getting SMBIOS data from sysfs. SMBIOS 2.7 present. Handle 0x002F, DMI type 7, 19 bytes Cache Information Socket Designation: L1 CACHE Configuration: Enabled, Not Socketed, Level 1 Operational Mode: Write Back Location: Internal Installed Size: 192 kB Maximum Size: 192 kB Supported SRAM Types: Pipeline Burst Installed SRAM Type: Pipeline Burst Speed: 1 ns Error Correction Type: Multi-bit ECC System Type: Unified Associativity: 2-way Set-associative Handle 0x0030, DMI type 7, 19 bytes Cache Information Socket Designation: L2 CACHE Configuration: Enabled, Not Socketed, Level 2 Operational Mode: Write Back Location: Internal Installed Size: 4096 kB Maximum Size: 4096 kB Supported SRAM Types: Pipeline Burst Installed SRAM Type: Pipeline Burst Speed: 1 ns Error Correction Type: Multi-bit ECC System Type: Unified Associativity: 16-way Set-associative ``` ## 開始進入作業一 (phonebook) 這次的作業要修改phonebook裡的code,將phonebook_orig做一個最佳化的版本。 借由實作phonebook_opt降低cache miss,而以下我參考很多人的想法。 #### 方法一 降低 entry 大小 * 降到 32bytes entry 留 lastName , zip[5] , pNext ![](https://i.imgur.com/fFlPcsZ.png) ``` Performance counter stats for './phonebook_orig' (100 runs): 724,422 cache-misses # 14.698 % of all cache refs ( +- 0.52% ) (74.46%) 4,928,566 cache-references ( +- 0.57% ) (50.81%) 265,940,300 instructions # 0.58 insn per cycle ( +- 0.21% ) (75.87%) 458,223,017 cycles ( +- 0.86% ) (75.66%) 0.132371343 seconds time elapsed ( +- 2.43% ) ``` ``` Performance counter stats for './phonebook_opt' (100 runs): 270,950 cache-misses # 10.407 % of all cache refs ( +- 0.84% ) (74.41%) 2,603,570 cache-references ( +- 0.78% ) (51.14%) 245,928,903 instructions # 0.66 insn per cycle ( +- 0.26% ) (76.17%) 372,881,696 cycles ( +- 1.02% ) (75.67%) 0.107570050 seconds time elapsed ( +- 2.86% ) ``` * 降到 28bytes entry 留 lastName , pNext ![](https://i.imgur.com/NYGOu2S.png) ``` Performance counter stats for './phonebook_orig' (100 runs): 679,209 cache-misses # 13.959 % of all cache refs ( +- 0.51% ) (74.43%) 4,865,670 cache-references ( +- 0.67% ) (51.31%) 264,981,539 instructions # 0.59 insn per cycle ( +- 0.18% ) (76.05%) 452,444,619 cycles ( +- 0.92% ) (75.18%) 0.132400425 seconds time elapsed ( +- 3.31% )`` ``` ``` Performance counter stats for './phonebook_opt' (100 runs): 184,417 cache-misses # 9.483 % of all cache refs ( +- 1.22% ) (74.08%) 1,944,692 cache-references ( +- 0.82% ) (51.87%) 242,371,815 instructions # 0.77 insn per cycle ( +- 0.29% ) (76.76%) 316,335,496 cycles ( +- 1.15% ) (75.05%) 0.102210667 seconds time elapsed ( +- 9.41% ) ``` ::: info 問題 1 :降低entry的大小不一樣會造成 findName 的時間下降,像是我做的第一個嘗試,將entry大小降為32bytes entry 留 lastName、zip[5]、pNext,findName時間反而phonebook_orig的實作還來的多。但將entry改為降到 28bytes entry 留 lastName、pNext就能將低findName的時間。 ::: ::: info 問題 2 :Cache miss的機率太詭異了,從phonebook_orig的版本就蠻奇怪的,不使用任何演算改變實作其cahce miss的機率只有14%,這數字跟其他人的比起來相當詭異。 ::: :::success 後來我看了自己CPU的資訊,我看到了自己CPU的L1 cache是 192 kB 、L2 cache 4096 kB,我認為是cache太大導致cache miss較低,而search的時間還是很久。 ::: #### 方法二 使用DJB hash function ![](https://i.imgur.com/LdATZgM.png) ``` Performance counter stats for './phonebook_orig' (100 runs): 634,523 cache-misses # 13.076 % of all cache refs ( +- 0.33% ) (74.59%) 4,852,419 cache-references ( +- 0.48% ) (50.69%) 263,565,254 instructions # 0.71 insn per cycle ( +- 0.21% ) (75.95%) 371,318,558 cycles ( +- 0.79% ) (75.79%) 0.101866438 seconds time elapsed ( +- 1.88% ) ``` ``` Performance counter stats for './phonebook_opt' (100 runs): 154,175 cache-misses # 8.831 % of all cache refs ( +- 0.44% ) (73.86%) 1,745,748 cache-references ( +- 0.79% ) (50.76%) 233,546,335 instructions # 1.03 insn per cycle ( +- 0.21% ) (77.00%) 227,029,862 cycles ( +- 0.47% ) (76.98%) 0.062459330 seconds time elapsed ( +- 0.80% ) ``` 我更改了phonebook_opt.c,並在裡面時作一個DJB hash function,並在phonebook_opt.h裡加入了hashtable以便實作。而在產生碰撞時要作的處理,append和findname都要做處理。 由上可以看的出來,在serach時間有顯著的降低,cache miss也比只改entry大小的方式還來的小。 ``` // my DJB Hash Function unsigned int DJBHash(char lastName[]) { unsigned int hash = 5381; while (*lastName) { hash += (hash << 5) + (*lastName++); } return (hash & 0x3FF); } ``` :::info 小插曲:沒注意到commit首位大寫的警告,導致之前的code都沒傳上去,最後再回github看才注意到,這點以後再寫commit要注意。 ::: ## 參考資料 * [Ubuntu 14.04 + xRDP + Xfce 实现Windows远程桌面连接](http://www.cnblogs.com/platero/p/4123720.html) * [各种字符串Hash函数比较](https://www.byvoid.com/zhs/blog/string-hash-compare)

    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