mooyee0929
    • 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
    • Make a copy
    • 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 Make a copy 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
    # Programming Assignment II: Multi-thread Programming<font size=3>`109550039 楊富翔`</font> ##### Parallel Programming by Prof. Yi-Ping You @NYCU 2023,Fall ## Part1 ### Execution time ``` 109550039@pp7:~/HW2/HW2/part1$ time (./pi.out 3 100000000; ./pi.out 4 100000000) Pi: 3.141453 Pi: 3.141560 real 0m0.519s user 0m1.767s sys 0m0.004s ``` ## Part2 ### Execution code ```clike= void workerThreadStart(WorkerArgs *const args) { int height = args -> height / args -> numThreads; int startRow = height * (args -> threadId); mandelbrotSerial( args -> x0, args -> y0, args -> x1, args -> y1, args -> width, args -> height, startRow, height, args -> maxIterations, args -> output); } ``` **I split the image into n region along the Y-axis. Each thread copes with one sub-image like the following demonstration image.** ![image.png](https://hackmd.io/_uploads/Hk2W3u576.png) ### Q1: In your write-up, produce a graph of speedup compared to the reference sequential implementation as a function of the number of threads used FOR VIEW 1. Is speedup linear in the number of threads used? In your writeup hypothesize why this is (or is not) the case? ### 1. <font color=#008000 > Produce the speed up figure in View 1 and View 2. </font> * **View 1** ``` 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 2 [mandelbrot serial]: [460.360] ms [mandelbrot thread]: [236.090] ms (1.95x speedup from 2 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 3 [mandelbrot serial]: [461.012] ms [mandelbrot thread]: [283.135] ms (1.63x speedup from 3 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 4 [mandelbrot serial]: [460.847] ms [mandelbrot thread]: [193.291] ms (2.39x speedup from 4 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 5 [mandelbrot serial]: [460.599] ms [mandelbrot thread]: [189.624] ms (2.44x speedup from 5 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 6 [mandelbrot serial]: [460.843] ms [mandelbrot thread]: [148.733] ms (3.10x speedup from 6 threads) ``` * **View 2** ``` 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 2 -v 2 [mandelbrot serial]: [290.244] ms [mandelbrot thread]: [173.165] ms (1.68x speedup from 2 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 3 -v 2 [mandelbrot serial]: [290.353] ms [mandelbrot thread]: [131.689] ms (2.20x speedup from 3 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 4 -v 2 [mandelbrot serial]: [290.450] ms [mandelbrot thread]: [110.665] ms (2.62x speedup from 4 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 5 -v 2 [mandelbrot serial]: [290.294] ms [mandelbrot thread]: [96.621] ms (3.00x speedup from 5 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 6 -v 2 [mandelbrot serial]: [290.374] ms [mandelbrot thread]: [85.138] ms (3.41x speedup from 6 threads) ``` ### 2. <font color=#008000 > Compare with 2 line charts </font> <center class="half"> <img src="https://hackmd.io/_uploads/rJjrBOcXT.png" alt="drawing" width="350"/> <img src="https://hackmd.io/_uploads/ry3ZBuc7p.png" alt="drawing" width="350"/> </center> ### 3. <font color=#008000 > Observation </font> :::success 1. **In View 1, speedup is not linear in the number of threads.** Conversely, speedup decreases and hit a plateau when the number of threads are 3 and 5. 2. **In View 2, speedup is linear in the number of threads.** ::: ### 4. <font color=#008000 > Hypothesis </font> :::warning ![image.png](https://hackmd.io/_uploads/H1PUJK9mp.png) When we see the image, we can find the white pixels in View 1 are clustered in specific region, but they distribute evenly in View 2. Also, we can find light color needs more iterations to compute in code. As a result, I suggest that some threads are responsible for more white pixels, so they would spend more time on it. **The pixel distribution is not equivalent, which causes that some thread would cope with more pixels.** ::: ### Q2: How do your measurements explain the speedup graph you previously created? ### 1. <font color=#008000 >Compute the execution time in each thread</font> |2 threads|3 threads|4 threads|5 threads|6 threads| |---|---|---|---|---| |<img src="https://hackmd.io/_uploads/HkNXPF9Xp.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/ByKIvY9Qa.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/S1EjLK97T.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/Hk75vK5X6.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/Hyy6Pt5mp.png" alt="drawing" width="120"/>| |t 0 : 235.872 ms<br>t 1 : 236.397 ms<br>|t 0 : 93.219 ms<br>t 1 : 283.055 ms<br>t 2 : 93.853 ms|t 0 : 45.388 ms<br>t 1 : 192.433 ms<br>t 2 : 193.465 ms<br>t 3 : 45.878 ms|t 0 : 33.330 ms<br>t 1 : 122.878 ms<br>t 2 : 189.139 ms<br>t 3 : 123.187 ms<br>t 4 : 21.916 ms|t 0 : 12.742 ms<br>t 1 : 82.529 ms<br>t 2 : 145.539 ms<br>t 3 : 146.182 ms<br>t 4 : 86.487 ms<br>t 5 : 25.074 ms| |![image.png](https://hackmd.io/_uploads/S1PsoFcQ6.png)|![image.png](https://hackmd.io/_uploads/rkf9sYq7a.png)|![image.png](https://hackmd.io/_uploads/rkcOiF9XT.png)|![image.png](https://hackmd.io/_uploads/HJSPsY5m6.png)|![image.png](https://hackmd.io/_uploads/S1OSsFcXp.png)| |t 0 : 173.368 ms<br>t 1 : 124.481 ms|t 0 : 132.036 ms<br>t 1 : 87.179 ms<br>t 2 : 79.928 ms|t 0 : 110.538 ms<br>t 1 : 66.155 ms<br>t 2 : 64.885 ms<br>t 3 : 61.324 ms|t 0 : 96.214 ms<br>t 1 : 77.227 ms<br>t 2 : 55.711 ms<br>t 3 : 50.078 ms<br>t 4 : 65.314 ms|t 0 : 85.111 ms<br>t 1 : 78.099 ms<br>t 2 : 65.299 ms<br>t 3 : 44.933 ms<br>t 4 : 65.068 ms<br>t 5 : 50.818 ms| ### 2. <font color=#008000 >Clonclusion</font> :::success **In View 1, the middle threads always take more time because of the light pixels. On the other hand, because the light pixels equally distribute in View 2, the threads spend roughly equivalent time.** ::: ## Q3: In your write-up, describe your approach to parallelization and report the final 4-thread speedup obtained. ### Execution code ```clike= for (unsigned int i = args->threadId; i < args->height; i += args->numThreads){ mandelbrotSerial(args->x0, args->y0, args->x1, args->y1, args->width, args->height, i, 1,args->maxIterations, args->output); } ``` Because light pixels are clustered together, so I split the image into smaller pieces. **Each thread would be responsible for 1 row.** After the end of thread, I distribute new row to first thread. Following figure demonstrate what I am talking about. ![image.png](https://hackmd.io/_uploads/ByjBQocmT.png) * ### <font color=#008000 >Compute the execution time in each thread</font> |2 threads|3 threads|4 threads|5 threads|6 threads| |---|---|---|---|---| |<img src="https://hackmd.io/_uploads/HkNXPF9Xp.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/ByKIvY9Qa.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/S1EjLK97T.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/Hk75vK5X6.png" alt="drawing" width="120"/>|<img src="https://hackmd.io/_uploads/Hyy6Pt5mp.png" alt="drawing" width="120"/>| |t 0 : 234.891 ms<br>t 1 : 236.213 ms<br>|t 0 : 158.798 ms<br>t 1 : 159.235 ms<br>t 2 : 158.820 ms|t 0 : 120.948 ms<br>t 1 : 120.821 ms<br>t 2 : 120.988 ms<br>t 3 : 121.603 ms|t 0 : 143.729 ms<br>t 1 : 96.719 ms<br>t 2 : 97.622 ms<br>t 3 : 97.041 ms<br>t 4 : 135.771 ms|t 0 : 129.804 ms<br>t 1 : 80.821 ms<br>t 2 : 80.555 ms<br>t 3 : 80.701 ms<br>t 4 : 116.355 ms<br>t 5 : 127.804 ms| |![image.png](https://hackmd.io/_uploads/S1PsoFcQ6.png)|![image.png](https://hackmd.io/_uploads/rkf9sYq7a.png)|![image.png](https://hackmd.io/_uploads/rkcOiF9XT.png)|![image.png](https://hackmd.io/_uploads/HJSPsY5m6.png)|![image.png](https://hackmd.io/_uploads/S1OSsFcXp.png)| |t 0 : 148.404 ms<br>t 1 : 148.451 ms|t 0 : 100.442 ms<br>t 1 : 100.542 ms<br>t 2 : 100.334 ms|t 0 : 76.338 ms<br>t 1 : 76.563 ms<br>t 2 : 76.210 ms<br>t 3 : 76.147 ms|t 0 : 84.344 ms<br>t 1 : 61.299 ms<br>t 2 : 60.937 ms<br>t 3 : 61.064 ms<br>t 4 : 86.382 ms|t 0 : 78.769 ms<br>t 1 : 50.892 ms<br>t 2 : 50.873 ms<br>t 3 : 57.868 ms<br>t 4 : 68.840 ms<br>t 5 : 79.077 ms| * ### <font color=#008000 >Performance</font> :::success In this approach, the difference in max spending time and min spending time is smaller than first approach. Moreover, **the performance are better in both View 1 and View 2**. ::: <center class="half"> <img src="https://hackmd.io/_uploads/SJo_1sqX6.png" alt="drawing" width="350"/> <img src="https://hackmd.io/_uploads/H1WMys576.png" alt="drawing" width="350"/> </center> :::warning The performance decrease in thread 5 and 6 is relartive to Q4. Because the server only has 4 core, it can only parallelize for 4 threads at the same time. Extra threads won't improve performance. ::: ## Q4: Now run your improved code with eight threads. Is performance noticeably greater than when running with four threads? Why or why not? ``` 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 4 [mandelbrot serial]: [460.708] ms [mandelbrot thread]: [121.570] ms (3.79x speedup from 4 threads) 109550039@pp7:~/HW2/HW2/part2$ ./mandelbrot -t 8 [mandelbrot serial]: [460.600] ms [mandelbrot thread]: [124.621] ms (3.70x speedup from 8 threads) ``` :::success **As I mentioned aboved,the workstation has 4 cores and 4 threads, meaning that at any given time, only 4 threads can operate simultaneously. Therefore, even if there are 8 threads, only 4 can run concurrently, and the presence of 8 threads also introduces "context switch overhead", which results in slightly worse performance compared to having just 4 threads.** :::

    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