Yen-Kuan Wu
    • 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
    # W1-QA ###### tags: `c14006078` `W1-QA` `sysprog21` ## W1 課程提問 * [21 世紀的系統軟體](https://hackmd.io/s/r1srX7Za) * 看不懂 No pain, No gain...的電路 * GPU 將會是相對,concurrency的問題,但是我好奇目前有沒有任何工具可以探測這個程式或是function的可平行度有多少,簡單來說就是一個程是來判斷這份工作要給CPU或者GPU,目前我們寫的程式都是寫我要用CPU或者GPU,一般程式vs CUDA,是否有這樣的機制呢? * ## 隨堂測驗 W1 解法 [link](https://hackmd.io/s/r13u0Af6) ## Q1: 以下程式的作用為何? ```C int p(int i, int N) { return (i < N && printf("%d\n", i) && !p(i + 1, N)) \ || printf("%d\n", i); } ``` ## 解法 這題我再一開始並沒有馬上反映出來,先帶個數字進去才了解的,後來我領悟到這種 recursive 版本,最主要要先找中止條件,所以很明顯,即是 `i < N`,因為 printf()的 return 值是印出多少個字元,所以永遠為 true,再來是運算式是`left to right`,再來是`&&`和`||`,這個即是`and`和`or`,而`and`當前面出現`false`時後面就不用算了,而C的設計也是一模一樣,反之`or`,分析完這幾個細節後,再來就是找細部的中止條件,我們發現遞迴還會出現再這 `!p( i+1, N)`,所以他的中止條件依舊是`i < N`,所以會先 print i~N-1,會再 i == N,時,會傳回 true,因為 || printf(),再來因為 !p(i +1, N),所以都會前面都會是 false,所以 or 又會繼續印完,印出 `i~N~i`。 >> 根據老師的提示,我有兩個部份沒考慮清楚 >> 1.input的範圍沒考慮 2. stack depth 沒考慮 >> >> 第1點我要補充的是當 i >=間 N 時,會輸出一個 i。 >> 第2點經過我測試,真的會有問題,我用 `i = -2147483647 N = 2147483647`,但我目前沒有一個很好的算法來測量我的 stack 到底多大,目前我只能 print 大約這個數字 260984 ~ 261800,我好奇點是他居然會浮動= =,所以目前我沒有一個很好的算法。 * `&&` 前後的敘述對調,是否影響輸出? * 從 `i < N && printf("%d\n", i)` 變更為 `printf("%d\n", i) && (i < N)` 的話,程式輸出為何? * 解法 * 就上面的分析,會不一樣的地方出在 i == N 時,這樣會 print出兩次 N。 ## Q2: 考慮以下 swap 程式: ```C void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; } ``` 能否避免使用區域變數 t?又,可否改用 bit-wise operator 來改寫? --> 算術: ``` *a = *a - *b; *b = *a + *b; *a = *b - *a; ``` ## 解法 ==這題我完全沒有想到該怎麼做,我已經被區域變數的解法給綁死了= =,忘了swap的真正意義,就是把值對調就好了,而我卻死腦經卡在以前的作法,這題的概念很簡單,即是算出差距,先把b 變到 a的高度,由於我們來留著 差距,再把 a的高度弄到b之前的高度。== 邏輯運算: ``` *a = *a XOR *b *b = *a XOR *b *a = *a XOR *b ``` ## 解法 ==我們要先知道一件事情,if a ^ b = c,then a ^ c = b and c ^ b = a,所以這也是變相找到差距的意思,先記算出a ^ b,再來就是保留他,並且去 XOR 另一個數字,跟上面的想法是一樣的。== **IMPORTANT:邏輯運算的好處是,可避免前述算術會遇到的 integer overflow,而且不限定位元數。** ## Q3: 用 C 語言實做 `char *reverse(char *s)` 來反轉 NULL 結尾的字串,限定 in-place 與遞迴 參考實作 (都還可以大幅改進,只是列出來給大家看) * [Day2](https://embedded2015.hackpad.com/GGI4IhFdLUW) **C-style string reverse (遞迴 + in-place)** * 先想想這個版本可用嗎? (以及對應的問題) ```C void reverse (char *s) { if (*s == ’\0’) return; reverse (s+1); printf ("%c", *s); } ``` ## 解法 ==首先上面的實作,我認為是可行的,但效能上應該是非常差的,還有他沒有做到return reverse string的功能,只有 print,而且他有非常非常多次的 function call的 overhead,我也稍微看過老師給的參考實作,首先很漂亮的是這個== ``` void swap(char *c1, char *c2){ *c1 ^= *c2 ^= *c1 ^= *c2; } ``` ==再來是 iterative的版本,等於找 N/2次,但我相信 strlen()必定也有很高的 overhead,我目前的想法有跳兩格找到\`\0\`後,再往前跳,這個想法可以類推到 3 4 5 ...,但重點是如何決定這個數字,所以目前我想到最好的應該就是`log N`,的strlen()== ==目前我的想法應該跟他一樣會使用 iterative的方式==

    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