curlyw
    • 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
    • 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 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
    # 2022q1 Homework3 (fibdrv) contributed by <`curlyw819` > ###### tags: `linux2022` ## **大數運算** :::danger 中英文間用一個半行空白字元或標點符號區隔。 :notes: jserv ::: 由於 Fib(92) 開始的計算會有 overflow 的問題,因此需要使用到大數運算的方法。原則上是直接使用字元運算並以陣列的形式來儲存,藉此來解決 overflow 的問題。 ### **資料結構** 使用結構來儲存字元與操作所需的資訊,定義資料結構如下 ```c typedef struct { char decimal[500]; int length; } bignum; ``` * `decimal[size - 1]` = `msb`,`number[0]` = `lsb` * `length`用來儲存數字位數 #### **大數加法** * 運算的過程使用ASCII來使數字以字元的方式直接做相加,所以兩數相加後要再減去多加的48 * 從個位數開始相加並且考慮進位,也就是說MSB為個位數 ```c if (x->length < y->length) return bn_add(y, x, dest); ``` * 以上程式碼為了計算方便,使`x->length` >= `y->length` ```c void bn_add(bignum *x, bignum *y, bignum *dest) { if (x->length < y->length) return bn_add(y, x, dest); int carry = 0; for (int i = 0; i < y->length; i++) { dest->decimal[i] = y->decimal[i] + x->decimal[i] + carry - OFFSET; if (dest->decimal[i] >= 10 + OFFSET) { // OFFSET = 48 carry = 1; dest->decimal[i] -= 10; } else carry = 0; } for (int i = y->length; i < x->length; i++) { dest->decimal[i] = x->decimal[i] + carry; if (dest->decimal[i] >= 10 + OFFSET) { carry = 1; dest->decimal[i] -= 10; } else carry = 0; } dest->length = x->length; dest->decimal[dest->length] = carry + OFFSET; if (carry) dest->length++; dest->decimal[dest->length] = '\0'; } ``` 第一個 `for` 是做字元的相加並考慮進位,結果放入 `dest`,第二個for是將較長的數字 `x` 剩餘的部分直接放入 `dest` #### **新增數字** ```c #define bn_tmp(x) bn_new(&(bignum) { .length = 0 }, x) bignum *bn_new(bignum *bn, char decimal[]) { bn->length = strlen(decimal); memcpy(bn->decimal, decimal, bn->length); return bn; } ``` #### **fib_without_fast_doubling** 這邊的`fib`是沒有使用`fast_doubling`加速過的,且使用不會用到陣列的計算方式 ```c bignum *fib_sequence_org(int k) { int i; bignum *f0 = bn_tmp("0"); bignum *f1 = bn_tmp("1"); bignum *ans = bn_tmp("0"); if(k == 0) return f0; if(k == 1) return f1; for(i = 2; i <= k; i++){ bn_add(f0, f1, ans); f0 = f1; f1 = ans; } return ans; } ``` ## **效能量測** 第一階段我主要想測量在沒有`fast_doubling`的原始fib 量測結果尚未排除作業系統排程的干擾 ### **量測user space 和 kernel space 間傳輸使用copy_to_user的傳遞時間** * 使`fibdrv`中的`fib_read`回傳`fib_sequence_org`在`kernel space`運算費式數列的時間 * 量測`client`中`user space`呼叫`read`前後之時間差 * 將`kernel space`與`user space`量到的時間做相減,可得到`copy_to_user`的傳遞時間 #### **kernel space read** * 在`kernel space`使用`ktime` ```c static ssize_t fib_read(struct file *file, char *buf, size_t size, loff_t *offset) { ktime_t k1, k2; k1 = ktime_get(); char *rev_fib = fib_sequence_org(*offset)->decimal; int length = strlen(rev_fib); char result[MAX_DIGIT]; for (int i = 0; i < length; i++) result[i] = rev_fib[length - 1 - i]; result[length] = '\0'; copy_to_user(buf, result, length + 1); k2 = ktime_sub(ktime_get(), k1); return (long long)ktime_to_ns(k2); } ``` #### **user space read** * 在`user space`使用`clock_gettime` ```c long elapse(struct timespec start, struct timespec end) { return ((long) 1.0e+9 * end.tv_sec + end.tv_nsec) - ((long) 1.0e+9 * start.tv_sec + start.tv_nsec); } ``` 由於量測結果很小,所以先將其放大並增加精確度再相減 ```c for (int i = 0; i <= offset; i++) { lseek(fd, i, SEEK_SET); clock_gettime(CLOCK_REALTIME, &t1); sz = read(fd, buf, 500); clock_gettime(CLOCK_REALTIME, &t2); printf("%d ",i); printf("%lld ",elapse(t1,t2) - sz); printf("%ld ", elapse(t1, t2)); printf("%lld \n",sz); } ``` #### gnuplot ![](https://i.imgur.com/lACyf5F.png) 將量測出的時間用gnuplot建成圖表 * 隨著`size`增加,傳輸的資料量也會增加,但是耗費的時間卻幾乎相同

    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