datbv
    • 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
    --- tags: Multi-threading in Java --- # 001: Parallel computing hardware P1 Bài viết nằm trong series [Multithread từ hardware tới software với Java](https://hackmd.io/@datbv/r1uoC54Kd). ## 1) Sequential vs Parallel computing Như chúng ta đã biết (hoặc có thể bạn chưa biết), một chương trình chạy trên máy tính là một tập các chỉ dẫn (instruction), giống như các bước cần thực hiện để có thể làm một đĩa salad tươi ngon. Công thức đã có rất nhanh trên google, bao gồm: - Gọt rửa hoa quả, rau xanh. - Cắt các loại rau củ quả. - Trộn tất cả lên và thêm nước sốt mong muốn. - Trình bày ra đĩa và thưởng thức thôi. ![](https://i.imgur.com/VXrEKpK.png) Mình sẽ đóng vai trò đầu bếp, thực hiện tất cả các công đoạn trên để hoàn thành đĩa salad. Đó là ví dụ của `single processor` (đơn nhân), có duy nhất mình thực hiện việc làm đĩa salad theo một công thức được cho trước từ đầu đến cuối (đã tách nhỏ thành các bước và thực hiện tuần tự). Ngoài ra, mình chỉ có thể thực hiện 1 hành động (rửa rau, cắt kiwi...) tại một thời điểm. Có phải bạn đang nghĩ hoàn toàn làm được nhiều việc cùng lúc, nhưng thực tế trong lúc cắt kiwi bạn không thể rửa rau được (bàn luận cụ thể ở bài sau). Bài toán làm salad ở trên là ví dụ của `sequential programming` (lập trình tuần tự), cách mà chúng ta vẫn được học vì nó dễ để tiếp cận. Nhưng nó cũng là hạn chế, thời gian để hoàn thành công việc sẽ phụ thuộc vào tốc độ làm việc của mình và sẽ có giới hạn. Việc rửa rau, cắt kiwi càng nhanh thì món salad sẽ càng nhanh chóng được hoàn thành. Sau rất nhiều nỗ lực, mình đã hoàn thành món salad trong... 5 phút. > Máy tính cũng hoạt động vậy, thời gian để một chương trình chạy xong sẽ phụ thuộc vào tốc độ của bộ xử lý (CPU/processor). **Kết luận đầu tiên**: tốc độ processor càng nhanh thì chương trình hoạt động càng nhanh. ![](https://i.imgur.com/LnuCfmK.png) Tất nhiên 1 đĩa salad là không đủ, mình tiếp tục vào bếp làm đĩa thứ hai. Để tăng tốc, mình gọi thêm một đầu bếp khác vào làm cùng. Chúng mình quyết định phân chia công việc để hoàn thành đĩa salad nhanh hơn. Mình sẽ cắt kiwi, trộn sốt, bạn còn lại sẽ cắt rau củ, cà rốt. Một lúc sau, mình đã cắt xong nguyên liệu... tuy nhiên bạn còn lại chưa thực hiện xong. Mình phải chờ cho đến khi bạn ấy hoàn thành xong mới có thể tiếp tục. ![](https://i.imgur.com/hezjC0k.png) Ví dụ trên chính là `parallel execution` (xử lý song song), chúng ta sẽ chia công việc có sẵn thành các phần có thể thực hiện cùng lúc và mỗi người sẽ lựa chọn task sau đó thực hiện cùng nhau. Bước cuối cùng (trộn sốt) sẽ thực hiện khi tất cả các công việc trước đó được hoàn thành. > **Rút ra kết luận thứ hai**: có nhiều processor thì chương trình hoạt động nhanh hơn so với ít processor. > **Lưu ý**, không phải cứ nhiều processor thì sẽ nhanh hơn, mình sẽ giải thích ở bài sau. Đĩa salad thứ 2 đã hoàn thành trong vòng 3 phút. **Hãy chú ý**, khi có thêm một người nữa làm cùng không có nghĩa là thời gian được giảm đi một nửa. Thực tế, có thêm người tức là có thêm sự phức tạp, chúng ta cần nhiều effort hơn cho việc giao tiếp để hoàn thành công việc. Một lý do nữa, không phải việc nào cũng có thể chia ra làm cùng nhau, đôi khi sẽ phải chờ lẫn nhau để tiếp tục một công việc nào đó (trộn salad, thêm sốt). > Sẽ có rất nhiều thứ cần phải xử lý, đó chính là khó khăn khi lập trình multi thread so với single thread. Tuy nhiên, lợi ích mà nó đem lại sẽ rất lớn nếu biết áp dụng đúng cách, nhớ là đúng cách nhé. Và khi thực hiện đúng cách, nó sẽ giúp: > - Hoàn thành 1 công việc sớm hơn (so với single thread programming). > - Hoàn thành được nhiều công việc hơn trong cùng 1 khoảng thời gian (so với single thread programming). ## 2) Parallel computing architecture Các CPU (bộ vi xử lý) ngày này bao gồm rất nhiều processors (đa nhân) bên trong, chúng ta thường nay nghe đến Intel i5, i7 với 4 nhân (core), 8 luồng (thread) và hyper-threading (sẽ có giải thích thuật ngữ ở các bài tiếp theo). Nhờ có nhiều processor (core) như vậy nên chúng thật sự có thể xử lý các bài toán song song với nhau. Có một câu hỏi đặt ra, vậy thì với các máy tính đời single core, làm thế nào để chúng có thể thực hiện được nhiều thứ đến vậy: lướt web, nghe nhạc, xem phim? Đi sâu hơn một chút, chúng ta tiếp tục tìm hiểu về các kiến trúc xử lý song song mà máy tính đang có. Dựa trên cách phân loại Flynn (**Flynn's taxonomy**), phân chia với 2 yếu tố: - Số lượng của các luồng chỉ dẫn (instruction) (the number of concurrent struction or control streams). - Số lượng của các luồng dữ liệu (data) (the number of data streams). Từ hai yếu tố trên, sẽ có 4 loại kiến trúc: - SISD: Single instruction stream, single data stream. - SIMD: Single instruction stream, multiple data stream. - MISD: Multiple instruction stream, single data stream. - MIMD: Multiple instruction stream, multiple data stream. ![](https://i.imgur.com/GwjhINZ.png) Bắt đầu có nhiều thuật ngữ hơn rồi đây. Vậy thì 4 loại kiến trúc trên thực sự là gì, ví dụ thực tế như thế nào và chúng khác nhau ra sao? ### 2.1) SISD Đầu tiên với kiến trúc SISD, trong bất kì thời điểm nào, chỉ có thể xử lý duy nhất một instruction và tương tác với duy nhất một nguồn data. Ví dụ, mình là SISD computer, mình chỉ có thể thực hiện duy nhất một hành động cắt (single instruction) với quả kiwi (single data) tại một thời điểm. Đây là kiến trúc được áp dụng phổ biến cho các máy tính đời đầu. ![](https://i.imgur.com/dO3q5tW.png) ### 2.2) SIMD Tiếp tục đến SIMD, một loại parallel computing với nhiều processor (core), hay chúng ta gọi là nhân của hệ điều hành (multiple processing units). Tại một thời điểm bất kì, tất cả các processors đều xử lý chung một instruction nhưng sẽ tương tác với nhiều nguồn data khác nhau. Ví dụ, SIMD bao gồm hai processors là 2 đầu bếp, cả 2 đều sẽ thực hiện hành động cắt (single instruction), tuy nhiên mỗi processor sẽ xử lý với nguồn data khác nhau. Mình cắt (instruction) kiwi (data), bạn còn lại sẽ cắt (instruction) cà rốt (data). Quick question, từ ví dụ trên, theo bạn SIMD được áp dụng ở đâu, cho cái gì (câu trả lời sẽ có ở cuối)? ![](https://i.imgur.com/4k8AcjO.png) ### 2.3) MISD Mỗi processor sẽ xử lý riêng một instruction, tuy nhiên tất cả các processors sẽ thực hiện thao tác trên cùng một data. Quay về bài toán đĩa salad, cụ thể ta sẽ có 2 instructions (cho 2 processors) là gọt vỏ và cắt, thao tác trên đối tượng là quả kiwi (data). Nghĩa là 1 người gọt vỏ kiwi, 1 người sẽ cắt kiwi, hai bọn mình làm đồng thời cùng một lúc. Nghe có vẻ không... make sence lắm nhỉ. Thực tế đúng là như vậy, MISD không phải là kiến trúc phổ biến. ![](https://i.imgur.com/MhMmt3w.png) ### 2.4) MIMD Cuối cùng là MIMD. Mỗi processor sẽ thực hiện những instruction riêng biệt trên data riêng biệt. Ví dụ rất đơn giản, mình sẽ gọt vỏ kiwi trong khi bạn còn lại cắt cà rốt. Kiến trúc MIMD rất phổ biển và xuất hiện trong gần như toàn bộ các máy tính hiện đại ngày này từ laptop, máy tính để bàn cho đến smart phone... ![](https://i.imgur.com/hQR3ia3.png) Đi sâu vào MIMD, chúng ta sẽ có hai cách xử lý bài toán trong kiến trúc MIMD đó là: - SPMD: Single program, multiple data. - MPMD: Multiple program, multiple data. #### 2.4.1) SPMD Với SPMD, các processors sẽ xử lý 1 bài toán song song với nhau (2 người cùng làm 1 đĩa salad), tuy nhiên mỗi processor sẽ thao tác với dữ liệu khác nhau. Nghe có vẻ giống SIMD nhưng thực tế không phải vậy, mặc dù cả 2 processors cùng xử lý 1 bài toán nhưng không cùng thực hiện 1 instruction. Ví dụ, cả 2 người cùng nhìn vào công thức làm món salad, sau đó sẽ chia thành các nhiệm vụ nhỏ và mỗi người làm một phần khác nhau. Mô hình này rất phổ biến và nó được áp dụng trong chính chiếc máy vi tính mà chúng ta đang sử dụng. ![](https://i.imgur.com/ytDXsIY.png) #### 2.4.2) MPMD Cuối cùng là MPMD. Thay vì làm 1 đĩa salad thì bọn mình làm 2 đĩa salad, mỗi một người sẽ làm một đĩa khác nhau dựa trên 2 công thức khác nhau và không liên quan đến công việc của người còn lại. MPMD sẽ phù hợp với các bài toán có thể chia nhỏ ra theo hướng chức năng. ![](https://i.imgur.com/hAasaNs.png) ### Reference: - https://hackmd.io/@datbv/r1uoC54Kd#Reference ### After credit Với ví dụ trên về SIMD, nó sẽ phù hợp với các bài toán xử lý với lượng dữ liệu lớn nhưng lại cùng một instruction, ví dụ như xử lý ảnh. Kiến trúc của GPU (Praphics processing unit - Card đồ họa) đa số sẽ là SIMD. © [Dat Bui](https://www.linkedin.com/in/datbv/)

    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