LEE, CHUNG-HAU
    • 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
    phonebook concurrent === contributed by <`snoopy831002`> ## concurrency * concurrency 的開始:cpu的演進不再以提昇時脈為方向,多核與多執行序的時代來臨 * 未來的效能效能提昇方向: 1. hyperthread(多執行序並行處理task) 2. multicore(核心多自然處理的快) 3. cache(cache大小的增加與速度的提昇對效能影響顯著) * 未來程式的執行效能將不仰賴cpu的速度快慢,而是程式的平行化。 * lock 與 lock-free programming(核心目標為使程序間可互相溝通順利) 1. lock:包含mutex與semephore等,防止程序競爭同一記憶資源 2. lock-free:沒有使用mutex與semephore等,但能達到locK者 * concurrency v.s parallelism 1. concurrency指的是一次可以做很多事情:If two or more problems are solved by a single processor.(multi-thread) ![](https://i.imgur.com/iKokAPa.png) 2. parallelism指的是很多事情同時在發生:If one problem is solved by multiple processors. ![](https://i.imgur.com/e5upTL0.png) * concurrency所產生的問題: 1. 多執行序競爭同ㄧ資源 2. thread之間的不同步行為 * 使用mutex或semephore改善 * 確保在寫入或讀取共享記憶體資源之操作是atomic的 * 必要時取消編譯器優化,因可能會有memory reordering所產生的不同步行為 * coroutine * tasks are cooperatively multitasked by pausing and resuming functions at set points.(使原本單向執行的程式碼可以有交錯執行的結果) ## mmap 因為原始使用fget的緣故,城市無法平行化,所以造成append()的時間一直無法有效縮短。故吳彥寬同學使用了mmap ```clike= void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); ``` 1.addr可以設定起始位置 2.length決定mapping大小 3.flags可以決定是否可供其他thread看見 4.offset則由fd決定 ## pthread * Why not use a process? * threads are light weight processes * threads can be created with much less operating system overhead compared to processes. * Pthread 的建立 ```clike= pthread_t a_thread;//定義thread pthread_attr_t a_thread_attribute;//通常使用pthread_attr_default即可 void thread_function(void *argument);//thread 所要執行的函式 char *some_argument;//thread 所要執行的函式中塞入的參數 pthread_create( &a_thread, a_thread_attribute, (void *)&thread_function, (void *) &some_argument); ``` * Pthread 的結束 ```clike= pthread_exit() ``` * Pthread 的暫停(只影響單一thread,不影響整個process) ```clike= pthread_delay_np() ``` * Mutex ```clike= pthread_mutex_t mutex; pthread_mutex_init(&mutex, pthread_mutexattr_default);//定義mutex pthread_mutex_lock( &mutex );//鎖定共享資源 pthread_mutex_unlock( &mutex );//解所共享資源 pthread_mutex_destroy();//結束mutex ``` * Semaphores [實做後再補上] ## thread pool * 之前的實做著重在減少findname()的執行時間,現在我們可以利用thread pool的方式優化append()。 * 使用thread pool可以將我們之前多個thread的操作統整起來,現在我們要做的事情只要將工作丟入threadpool請它代為執行就好。在threadpool的操作下,thread的首要任務就是進入threadpool搶奪lock進行工作。 * 因為[threadpool-mbrossard](https://github.com/mbrossard/threadpool)本來就是以pthread寫的,所以在開始實做之前我把吳彥寬同學原本的pthread通通砍掉了 ![](https://i.imgur.com/U3BPBcm.png) * 實際操做 * 利用[threadpool-mbrossard](https://github.com/mbrossard/threadpool) * 引入的函式 * ```threadpool_t *pool=threadpool_create(THREAD_NUM,QUEUE SIZE,FLAG);``` 定義thread的多寡與queue大小,flag則設為NULL即可。這裡很像我們在使用pthread也要create一樣 * ```threadpool_add(threadpool_t *pool, void (*function)(void *)``` 將工作append()丟入threadpool中,此時thread就會開始搶工作了 *```int threadpool_destroy(threadpool_t *pool, int flags);```最後清理掉threadpool * 實做結果 * 原始pthread與threadpool的差異(使用4個 thread) ![](https://i.imgur.com/y4DCiQC.png) * threadpool使用不同thread的差異 ![](https://i.imgur.com/bLzDqfa.png) 沒想到使用一條thread竟然是最快的... # gnuplot補充 * plot語法 * datafile : 輸出的檔案 * <xcol>: 使用資料第幾列作圖(可以不用設定) * <ycol>: 使用資料第幾行作圖(必設定) * xticlable(<labelcol>)[等同xtic(<labelcol>)]:資料x座標標籤 * <plotstyle>:圖的種類(histogram長條圖) ``` plot ’datafile’ using <xcol>:<ycol>:xticlabels(<labelcol>) with <plotstyle> ``` * using語法 ```2:xtic(1)``` : 使用第二行資料與第一行資料作為xlabel作圖 ```using ($0-0.2):($2+0.001):2``` : 現在只有一維資料y(所以x使用$0表示label之x座標), $2是第二個column的資料座標, 2則是表示使用output.txt中的第二行資料作為資料來源。-0.2與+0.001則是座標的調整 ```clike= reset //清除先前設定 set ylabel 'time(sec)' //設定y軸資訊 set style fill solid set title 'perfomance comparison with different num of threads' //設定圖片標題 set term png enhanced font 'Verdana,10' set output 'runtime.png' //設定輸出檔名 plot [:][:0.02]'output.txt' using 2:xtic(1) with histogram title '1 thread', \ '' using ($0-0.2):($2+0.001):2 with labels title '', \ '' using 3:xtic(1) with histogram title '2 thread' , \ '' using ($0-0.04):($3+0.0015):3 with labels title '', \ '' using 4:xtic(1) with histogram title '4 thread' , \ '' using ($0+0.01):($4+0.0025):4 with labels title'',\ '' using 5:xtic(1) with histogram title '8 thread' , \ '' using ($0+0.2):($5+0.0021):5 with labels title ' ', \ '' using 6:xtic(1) with histogram title '16 thread', \ '' using ($0+0.45):($6+0.003):6 with labels title ' ', \ ```

    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