# 計算機組織 Chapter5 ###### tags: `計算機組織` ### Temporal locality 最近被存取過的資料可能很快又會被存取 舉例 : loop ### Spatial locality 鄰近於最近被存取過的的資料,可能很快就會被存取 舉例 : array ### Miss penalty cache miss 之後去 memory 抓資料的時間 ### Critical word first 直接把需要的word上傳,不需要等到整個block被填滿 ### Early restart word依序填滿,當missing word出現之後把它上傳 ### Write through - 定義 : 寫入過程中會更新cache和memory中的data - 缺點 : 因為要更新memory所以會蠻花時間的 - 解決方法 : write buffer ->等buffer滿才做寫入 ### Write back - 定義 : 寫入過程中只會更新cache , 直到已更新的cache block(dirty block)要被覆蓋了才會上傳 ### Write Allocation - 定義 : 寫入時如果資料不在cache中是否要將memory block搬到cache - 要搬 : fetch the block(miss) - 不搬(Write around) : 不放到cache寫,直接到memory寫 舉例 : 電腦初始化時 ### Replacement Plicy - 方法一 : 隨機 - 方法二 : Least-recently used(LRU) ### Multilevel cache - Primary cache : focus on minimal hit time - L-2 cache : focus on low miss rate to avoid main memory access , hit time has less overall impact ### Virtual Memory - Programs share main memory - CPU and OS translate virtual addresses to physical addresses > VM 'block' is called page > VM translation 'miss' is called page fault ### Address translation 處理器所執行的指令位置和資料位置都是virtual address,存取之前須經由address translation過程確認指令和資料的physical address才能存取,如果無法轉換的話即為page fault ### Page fault penalty - 定義 : once page fault , 會出發exception並且page must be fetched from disk , handled by OS - minimize page fault rate : fully associative placement(use page table) > 只要page是空的就可以放 > page table就像block裡面的index ### Repalce and Write - To reduce page fault rate , OS週期性的將每個page的use bit設為0,之後如果page被使用會被設為1,如果要找被取代的就找use bit是0的 - 使用write back ### Translation look-aside buffer(TLB) - 紀錄virtual page number所對應的physical page number,加快轉換速度(in CPU) ### Sources of misses 1. Compulsory misses : 第一次使用,cache裡面一定沒有,所以一定產生miss 2. Capacity misses : cache比較小,原本放置的block可能被replace之後又miss又被放進去 3. Conflict misses : 如果cache都已經放資料了,之後的memory block沒有地方放,只能犧牲某個block,但這個block之後可能又會被需要 ### Cache Coherence problem suppose two cpu cores share a physical address space they can have two different values for the same location ![](https://i.imgur.com/Zwo5MdM.png) --- 計算機組織Ch6 === ### Fine-grain multithreading - 定義 : switch threads after each cycle ### Coarse-grain multithreading - 定義 : only switch on long stall(e.g. cache miss) , simplifies hardware , but doesn't hide short stalls(e.g. data hazards)