楊惟晶
    • 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
    # 2016q3 Homework3 (mergesort-concurrent) contribute by <`heathcliffYang`> [Github](https://github.com/heathcliffYang/mergesort-concurrent) ## 目標 * 作為 [concurrency](/s/H10MXXoT) 的展示案例 * 學習 POSIX Thread Programming,特別是 [synchronization object](https://docs.oracle.com/cd/E19683-01/806-6867/6jfpgdcnd/index.html) * 為日後效能分析和 scalability 研究建構基礎建設 * 學習程式品質分析和相關的開發工具 ## 理解Code ### list.h llist_t 有head指向第一個node_t,而非llist_t本身的指標指向第一個node & size (list大小) **node_t :** datatype為intptr_t的data > 若在不同的平台其長度也不相同,那就不懂使用intptr_t的用意 >> 其長度都會恰好與使用平台的指標長度相同,因此該變數可以拿來儲存數值,也可以拿來儲存指標 [name=Hua] >> ### threadpool.h **task_t :** 單一工作,有待執行function的pointer,傳入的變數,指向下一個task_t的next,指向上一個工作的last **tqueue_t :** 是工作佇列,紀錄頭尾的pointer,==mutex (thread搶奪到時方能執行task_t) 與 cond (工作通知)== **tpool_t :** 放置threads & queue ### threadpool.c **task_free :** 釋放單一工作 ==記得用完要釋放!!== **tqueue_pop & tqueue_push :** 從tail取工作,回傳工作指標;從head把工作塞入 ==在更動queue裡的東西必要注意mutex== **tqueue_free :** 用迴圈把一整串queue free掉 ==實驗只free head 與此的差別== ### main.c ==--- global ---== **tmp_list** , **the_list** , thread_count , data_count , max_cut & pool **data_context :** mutex & cut_thread_count (紀錄thread切分list的次數) ==--- global ---== **merge_list() :** merge兩個a list & b list,並回傳排序好的_lsit ```c llist_t *small = (llist_t*) ((intptr_t) a * (a->head->data <= b->head->data) + (intptr_t) b * (a->head->data > b->head->data)); ``` small為a或b裡data較小者的list > 為什麼要intptr_t? > ==a & b 是 pointer,用(intptr_t)強制轉換datatype,使其能與判斷式的結果(int)匹配(不會出現addr與int相乘的error也不會出現warning:cast from ptr to int)而可以相乘== --- from Hua 提供的資訊與試驗 >> 程式碼是寫給你改的,不是給你背誦用,覺得有疑惑,就大膽去實驗,比方說改為 `(void *)` 再觀察看看 [name=jserv] >> 好的,謝謝老師 **merge_sort() :** 將傳入的list分兩半(用`list_nth()`找出要的mid的list位置),並且處理前半段left list的最後一個node_t的指標與size **merge() :** `_list->size < (uint32) data_count` 成立,代表list還沒全部merge_sort完,則用_list與_t(也就是已有東西的tmp_list)做新工作排入task queue;若不成立,則將_list放到the_list ==tmp_list的意義 : 待被`merge_list`的list== **cut_func() :** ==成立條件 : list->size > 1 且 cut_count < max_cut==,將傳入的list切半之後分別輸入task queue **task_run() :** 從queue取出新工作並執行 **main() :** 1. argv[]-->輸入分別為thread_count跟data_count,max_cut取min(thread_count, data_count) 2. 初始化threadpool, task ---> create thread_count個thread,並傳入task_run 3. 製作`cut->func`的task並傳入queue,由某個thread搶到這個工作後,開始切分原始的list,並製造left & right的新的切分工作傳入queue,直到達到max_cut 4. 開始執行`merge`裡的`merge_sort`把list成兩半直到left&right都只剩1個node,再開始`merge_list`再回傳,直到merge完所有的data 5. ==處理資料輸入 :== > 與hugikun999討論到,atoi()不能轉換整個字串,而我做了一個小實驗,發現對於字串的指標,shift一個byte的話,他照樣會記錄後面的字串,而不是其中某個字元;所以還在思考到底要用甚麼方法,才能一個一個地將字元轉換;之後也考慮到mmap()時,對於一個pointer來說,他怎麼知道他是要取這樣一個長度?是取決於一開始對該pointer宣告的datatype?(實驗中) > ```c > char word[16]="apple"; > printf("%s",word+1); -->會印出pple > ``` > 其中也找到了一個函式:`getc( ) //從某一個檔案讀取一個字元` > with hugikun999's help, I use mmap() successfully!! ### Makefile (not yet) `$ (for i in {1..8}; do echo $RANDOM; done) | ./sort 4 8` [資料](http://linux.vbird.org/linux_basic/0320bash.php) ## Start My Try!! ### deal with my data set After I get the mmap pointer of the data set, how can I use it to turn the character one by one? Try to print what the map pointer point to. ```c char *map = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fp_orig, 0); printf("%s\n", map); ``` It print all words... with "enter!" my first word is kurupi ```c char *map = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fp_orig, 0); char getline = *(map+7); printf("%s\n", &getline); ----------------------------------------------------------------- output is : s // with a strange symbol but I don't know why ``` With this, I know data in virtual memory is continuouns and ignore the "enter." I know I need align my data (more desirable) or separate them.. `read(fp_orig, read_line, 1)` won't ignore the "enter." ---> method 1 : read one by one fopen the same file is OK. ---> method 2 : read line by line ==****use this== ### analysis 1. each word will be a `node_t`, with a little array consisting of characters. ---> no need to be merge 2. the llist_t consists of words! ### orignal method's time set structure time; sort time:0.354496,1.233266 ## Pthread * [Synchronization的函式](https://docs.oracle.com/cd/E19683-01/806-6867/6jfpgdcnd/index.html) 1. `int pthread_join(thread_t tid, void **status);` When status is not NULL, it points to a location that is set to the exit status of the terminated thread when pthread_join() returns successfully. If multiple threads wait for the same thread to terminate, they all wait until the target thread terminates, than one thread returns successfully and the others fail with an error of ESRCH. > 不太懂status的部分的作用,以及先前是從哪裡設定的 2. The pthread_detach() function is used to indicate to the implementation that storage for the thread tid can be reclaimed when the thread terminates. > detach的實際作用? 3. Thread-specific data is maintained on a per-thread basis. TSD is the only way to define and refer to data that is private to a thread. Each thread-specific data item is associated with a key that is global to all threads in the process. Using the key, a thread can access a pointer (void *) that is maintained per-thread. 4. The per-thread binding is deallocated when a thread terminates if the key was created with a destructor function. 5. When a key has a non-NULL destructor function and the thread has a non-NULL value associated with that key, the destructor function is called with the current associated value when the thread exits. The order in which the destructor functions are called is unspecified. 6. Signal Mask ?? 7. When threads in two or more processes can use a single synchronization object jointly. Note that the synchronization object should be initialized by only one of the cooperating processes, because reinitializing a synchronization object sets it to the unlocked state. 8. Block on a condition variable ---> `pthread_cond_wait(3THR)` > thread waiting的同時也把mutex的lock解除???被誰??

    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