Eddie Wang
    • 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
    # Using Ripes simultor to test the impact of the memory accessing pattern <`contributed by eddie9712`> [Question Reference](https://cs61c.org/su20/labs/lab07/) ### environment: ``` operating system:ubuntu 20.04 simulator:Ripes-v2.1.0-linux-x86_64.AppImage virtual machine:virtualbox 6.1 ``` CPU info: ``` vendor_id : GenuineIntel cpu family : 6 model : 126 model name : Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz stepping : 5 cpu MHz : 1497.602 cache size : 8192 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 22 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor ssse3 cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single fsgsbase avx2 invpcid rdseed clflushopt md_clear flush_l1d arch_capabilities bugs : spectre_v1 spectre_v2 spec_store_bypass swapgs itlb_multihit bogomips : 2995.20 clflush size : 64 cache_alignment : 64 address sizes : 39 bits physical, 48 bits virtual power management: ``` Clone the source code from here: `https://github.com/61c-teach/su20-lab-starter` ## Exercise 1 - A Couple of Memory Access Scenarios At the first exercise we need to adjust the program parameters and run the piece of code on Ripes simulator to see how the cache works and discuss the questions in lab07.**(There are some difference between the Venus and Ripes, so I will adjust some cache parameters in lab07)** `cache.s` is the piece of code that we should run with different cache configurations (assembly code) [here](https://github.com/eddie9712/Computer_Architecture/tree/main/riscv/Lab4) PSEUDOCODE of `cache.s`: ```clike= int array[]; //Assume sizeof(int) == 4 for (k = 0; k < repcount; k++) { // repeat repcount times /* Step through the selected array segment with the given step size. */ for (index = 0; index < arraysize; index += stepsize) { if(option==0) array[index] = 0; // Option 0: One cache access - write else array[index] = array[index] + 1; // Option 1: Two cache accesses - read AND write } } ``` ### Scenario 1: ``` Program Parameters: Array Size (a0): 128 (bytes) Step Size (a1): 8 Rep Count (a2): 4 Option (a3): 0 Cache Parameters: Cache Levels: 1 Block Size: 8 Number of Blocks: 4 Placement Policy: Direct Mapped Associativity: 1 Block Replacement Policy: LRU ``` According to the cache parameter, we can get the configuration of cache : ![](https://i.imgur.com/tgyTuk0.png) Index: 2 bits (4 slots) offset: 3 bits (8 bytes each block(entry)) Tag: 32-2-3=27 bits * task 1:try to figure out the hit rate before I run the code: In `cache.s` , with the program parameters above, we assume that the memory addresses of the array is started at ` 0x10000000` so the accessed address will be will be `0x1000000`, `0x10000020`,`0x10000040`, `0x10000060` , and then I break them down to the **TIO** format(direct mapped) , they all have the same index(0) ,so I think the miss pattern is => `0x1000000` : miss `0x10000032` : miss `0x10000064` : miss `0x10000096`: miss and even though the program repeats for 4 times ,the hit rate is still the same that is 0; * task 2: What combination of parameters is producing the hit rate you observe? I think the combination of the **step size** and **block size** cause the hit rate 0, because the block size just 8 bytes so the step size is too big to access the data which is btought to cache previous time, and the step size=8 also cause the **ping pong effect**(with mapping to index 0) * task 3: What is our hit rate if we increase Rep Count arbitrarily? Why? There isn't any influences if we increase Rep Count arbitrarily , because the accessing patterns are the same at each iteration(Rep Count) * task 4:How could we modify one program parameter to increase our hit rate? I think the answer is **decreaseing the step size**, because when the step_size=eight the next accessing of the address will not include in the block that we bring to cache(and the next accessing will evict the previous block) , so we need to **decrease the step_size to 1(hit rate:0.5)** ### Scenario 2: ```clike= Program Parameters: (set these by initializing the a registers in the code) Array Size (a0): 256 (bytes) Step Size (a1): 2 Rep Count (a2): 1 Option (a3): 1 Cache Parameters: (set these in the Cache tab) Cache Levels: 1 Block Size: 16 Number of Blocks: 16 Placement Policy: N-Way Set Associative Associativity: 4 Block Replacement Policy: LRU ``` And here is the cache configuration: ![](https://i.imgur.com/cbIRCdj.png) Index: 2 bits(4 sets) Offset: 4 bits(16 bytes/block) Tag: 32-2-4=26 bits 1. How many memory accesses are there per iteration of the inner loop? (not the one involving repcount). It’s not 1. Ans: 2 times(lw and sw) 2. What is the repeating hit/miss pattern? WHY? (Hint: it repeats every 4 accesses). Ans: access pattern: array[0] : miss array[0] : hit array[2] : hit array[2] : hit After that, every accessing to the array[4n] will get a miss 3. This should follow very straightforwardly from the above question: Explain the hit rate in terms of the hit/miss pattern. Ans: 3/4=>0.75 4. Keeping everything else the same, what happens to our hit rate as Rep Count goes to infinity? Why? Try it out by changing the appropriate program parameter and letting the code run! You should have noticed that our hit rate was pretty high for this scenario, and your answer to the previous question should give you a good understanding of why. IF YOU ARE NOT SURE WHY, consider the size of the array and compare it to the size of the cache. Now, consider the following: Ans: If I set the program parameter to 1000(***when the value of repcount increasing the value of hit rate also increasing***) then I get the hit rate 0.998. It is very close to 1, I think the reason why we get this result is that the cache can contain the all data accessed by us.However, it still need to consider the compulsory miss when the block first time brought to cache * Extension question: >Suppose we have a program that iterates through a very large array (AKA way bigger than the size of the cache) repcount times. During each Rep, we map a different function to the elements of our array (e.g. if Rep Count = 1024, we map 1024 different functions onto each of the array elements, one per Rep). (For reference, in this scenario, we just had one function (incrementation) and one Rep). >QUESTION: Given the program described above, how can we restructure its array accesses to achieve a hit rate like that achieved in this scenario? Assume that each array element is to be modified independently of the others AKA it doesn’t matter if Rep k is applied to element arr[i] before Rep k is applied to element arr[i+1], etc. Ans:To achieve the hit rate like that achieved in this scenario, I think I can access the array with size of **"block"**. At first, we need to access the whole array(assume it is an integer array) each `repcount`, so we will access all of elements in the array(`array[0],array[1],array[2].....`) since we would like to take adventage of the spatial locality we will access the array sequentially then we get: array[0]:miss array[1]:hit array[2]:hit array[3]:hit Because we can't avoid the compulsory miss, so we will get the hit rate `0.75`, thereby we can take advantage of the temporal locality with the next `repcount` with the reversing order accessing, so the hit rate will increase when the repcount increase. **Explanation:** Assume the array contains 10 blocks that map to the first set(4-way set) When the cache full: first set: slot 1: block 1 ==>LRU slot 2: block 2 slot 3: block 3 slot 4: block 4 at the next time when the cache full: slot 1: block 5 ==>LRU slot 2: block 6 slot 3: block 7 slot 4: block 8 there still 2 blocks to access, so: slot 1: block 9 slot 2: block 10 slot 3: block 7 ==>LRU slot 4: block 8 Then it goes on the next repcount(reversing order) slot 1: block 9 slot 2: block 10 slot 3: block 7 ==>LRU slot 4: block 8 so the block 10 ,9 , 7, 8 will all hit in the cache Then we can observe the miss/hit patterns: rep 1: 1:m 2:m 3:m 4:m 5:m 6:m 7:m 8:m 9:m 10:m rep 2: 10:h 9:h 8:h 7:h 6:m 5:m 4:m 3:m 2:m 1:m And then We can directive the concept to our experiment=> Each block accessing will get 1 misses and 3 hits.Howerver ,the next repcount will get 4 hits(each block) for the fisrt four blocks. Experiment:(Get the source code [here]()) I modify the given code `cache.s`, this program accesses the whole array(set each element to zero).When the repcount is an odd number ,it accesses the program with increasing index, contrastly , it accesses the array with decreasing index. * Result: ![](https://i.imgur.com/m8a6K76.png) ### Scenario 3: No multilevel cache in Ripes. ## Exercise 2 - Loop Ordering and Matrix Multiplication * task 1 : Think about what the strides are for the nested loops in the other five implementations. * multMat2: * moves through A with stride: 0 * moves through B with stride: n * moves through C with stride: n * multMat3: * moves through A with stride: n * moves through B with stride: 1 * moves through C with stride: 0 * multMat4: * moves through A with stride: 1 * moves through B with stride: 0 * moves through C with stride: 1 * multMat5: * moves through A with stride: 0 * moves through B with stride: n * moves through C with stride: n * multMat6: * moves through A with stride: 1 * moves through B with stride: 0 * moves through C with stride: 1 * task 2: Afetr executing `make ex2` we get the results of the matrix multiplcation in different ways: (Gflop/s=>Giga-floating-point-operations per second) ijk: n = 1000, 1.486 Gflop/s ikj: n = 1000, 1.275 Gflop/s jik: n = 1000, 1.573 Gflop/s jki: n = 1000, 10.009 Gflop/s kij: n = 1000, 1.361 Gflop/s kji: n = 1000, 9.560 Gflop/s * task 3,4,5:Which ordering(s) perform best for these 1000-by-1000 matrices? Why? As you can see, since the higher Gflop/s represents the higher performance so the order `jki` gets the best performance on matrix mulitiplcation. In my opinion, we can look at the answers for the first question, the order `jki` has the strides in the innermost loop: ``` * multMat4: * moves through A with stride: 1 * moves through B with stride: 0 * moves through C with stride: 1 ``` So I go to check the block size of my cache: `vi /proc/cpuinfo`: and hen I get: `cache_alignment : 64`(each block has 64 bytes) Ok, so it is reasonable that the stride of accessing the array will impact the performance , if the stride of accessing is bigger than the block size , it will cause a cache miss, take the the order `ikj` for example(which has the worst performance): In the innermost loop, the accessing patterns wiill be: i=0,k=0 j=0 C[0] A[0] B[0] C[1000] A[0] B[1000] C[2000] A[0] B[2000] C[3000] A[0] B[3000] ...... C[1000000] A[0] B[1000000] Contrastly, the accessing pattern of order `ijk`: j=0,k=0 i=0 C[0] A[0] B[0] C[1] A[0] B[1] C[2] A[0] B[2] C[3] A[0] B[3] ...... C[1000] A[0] B[1000] We can observe that In the `jki` case, it will cause the cache miss again and again(poor locality).However ,the `ijk` will take adventage of the locality and then it will cause the lower miss rate. However, we can find the order `kji` has the same strides to `ijk` in the innermost loop: ``` * multMat6: * moves through A with stride: 1 * moves through B with stride: 0 * moves through C with stride: 1 ``` (It has the 2th performance on matrix multiplication) But why it has the worse performance than `ijk`? When we check the strides of every loop we can find that they are all composed of three types of strides: e.g.(stride for C,A,B) order `kji`: The first loop: 0,n,1 The second loop: n,0,n The third loop(innermost): 1,1,0 order `ijk`: The first loop: n,0,n The second loop: 0,n,1 The third loop(innermost): 1,1,0 At the third loop they both have the strides `1,1,0` to access the array , but at the second loop order `kji` has the strides `n,0,n` which will cause more cache miss than `0,n,1`. Because the frequency of the different loops => if we have strides n in second loop which means after execuing the third loop completely the next second loop will access the array with strides n. **Explanation:** **(order `ijk`):** The third loop access the array C with stride 0 C[0],C[0],C[0]....C[0] After executing completely,the second loop will access with strides 1(at second loop k++),so it start accesses C[1] in the third loop(**no cache miss**) **(order `kji`):** The third loop access the array C with stride 0 C[0],C[0],C[0]....C[0] After executing completely,the second loop will access with strides n(at second loop k++),so it start accesses C[1000] in the third loop(**cache miss**) From the result above, since the **stride n** will cause cache miss.Furthermore, if the stride n is taken for the more inner loop the more times it will be taken.I think that is the reason why the `ijk` has the best performance(innermost loop strides : 1,1,0),and the `kji` has the worst performace(innermost loop strides :n,0,n) ## Exercise 3 - Cache Blocking and Matrix Transposition At first, I implement the `transpose_blocking()` function : ```clike= void transpose_blocking(int n, int blocksize, int *dst, int *src) { for (int x = 0; x < n; x+=blocksize) { for (int y = 0; y < n ; y+=blocksize) { for(int i=x ; i < x+blocksize ; i++){ //transpose each block for(int j=y ; j < y+blocksize ; j++){ if( i<n && j<n){ //to avoid accessing out of the array dst[j + i * n] = src[i + j * n]; } } } } } } ``` ### Part 1 - Changing Array Sizes Fix the blocksize to be 20, and run your code with n equal to 100, 1000, 2000, 5000, and 10000. * The experiment results: ``` n=100 Testing naive transpose: 0.004 milliseconds Testing transpose with blocking: 0.008 milliseconds n=1000 Testing naive transpose: 1.043 milliseconds Testing transpose with blocking: 1.162 milliseconds n=2000 Testing naive transpose: 16.898 milliseconds Testing transpose with blocking: 9.398 milliseconds n=5000 Testing naive transpose: 339.297 milliseconds Testing transpose with blocking: 67.806 milliseconds n=10000 Testing naive transpose: 1473.8 milliseconds Testing transpose with blocking: 232.271 milliseconds ``` * When the array size became n=2000, transpose with blocking become faster * I guess that the reason why the cache blocking cache is outperforming the non-blocking cache in certain size is because of **cache size**. Let's take my cpu for example, my cpu has a 8192KB cache, so if the the array size that we accessing is smaller than our cache, the two ways will have the similar performance(compulsory miss). * Result with pyplot: ![](https://i.imgur.com/4hD0Zjk.png) ### Part 2 - Changing Blocksize Fix n to be 10000, and run your code with blocksize equal to 50, 100, 500, 1000, 5000. * The experiment result: ``` Testing naive transpose: 1119.9 milliseconds Testing transpose with blocking: 194.637 milliseconds Testing naive transpose: 1100.25 milliseconds Testing transpose with blocking: 165.453 milliseconds Testing naive transpose: 1108.46 milliseconds Testing transpose with blocking: 147.401 milliseconds Testing naive transpose: 1085.85 milliseconds Testing transpose with blocking: 178.388 milliseconds Testing naive transpose: 1100.17 milliseconds Testing transpose with blocking: 851.952 milliseconds ``` * Acorrding to the experiment result, when the blocksize achieved 5000 , the performance of the blocking-cache is similar to the non-blocking cache , * Results with pyplot: ![](https://i.imgur.com/krVw9gJ.png) # Modified Code: Here I moidified the makefile and the `transpose.c` to test the results with different argument, and I also write a python script for plotting the results: run `make ex3_part1` for different arraysize,run `make ex3_part2` for different blocksize [source code](https://github.com/eddie9712/Computer_Architecture/tree/main/riscv/Lab4)

    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