111 年 國立中山大學資訊管理學系碩士班甲組《計算機結構》
第 1 題10 分
Problem 1: (10%)
- (2%) Please explain why we need virtual memory and explain its pros and cons.
- (2%) TLB is usually much smaller than the page table. Hence, fully associative is a good choice to design the TLB. Is it still a good choice to use fully associative if we enlarge the TLB size? Please explain your reasons.
- (2%) The multi-level cache is usually applied in modern systems. Please determine what kind of strategy is proper for cache consistency in writing data.
- (2%) Please explain the difference between the multi-word cache organization and set-associative organization and explain their pros and cons.
- (2%) Please explain why the nonblocking cache can improve the cache bandwidth.
登入後即可作答並保存紀錄。
第 1 題
1. 虛擬記憶體的必要性、優點與缺點
核心觀念
虛擬記憶體(Virtual Memory)將 CPU 產生的虛擬位址轉換成實體記憶體位址。若頁面大小為 bytes,虛擬位址可表示為:
頁表將虛擬頁號 對映至實體頁號 :
其中頁內位移 Page Offset 在轉換前後不變。TLB 則是頁表對映的高速快取。若頁表項顯示該頁不在主記憶體中,便產生 Page Fault,由作業系統從磁碟或 SSD 載入頁面。
為何需要虛擬記憶體
-
提供獨立且連續的位址空間
每個行程都可以視為擁有自己的大型連續記憶體,不必知道實體頁面實際分散在哪些位置。 -
實現保護與隔離
不同行程的虛擬位址可以對映到不同實體頁面,並透過頁表的權限位元限制讀取、寫入與執行權限,避免行程任意存取其他行程或核心資料。 -
支援需求分頁(Demand Paging)
程式不必一開始就全部載入主記憶體,只需載入目前使用的頁面,因此可以提高記憶體利用率。 -
支援大型位址空間與記憶體共享
行程的虛擬位址空間可以大於實體 RAM;共享函式庫、共享記憶體與 Copy-on-Write 也能透過不同虛擬位址對映到相同實體頁面實現。
優點
- 提供行程隔離與記憶體保護。
- 實體記憶體不必連續配置,配置彈性較高。
- 可用需求分頁降低程式啟動時的載入量。
- 支援共享頁面、虛擬記憶體映射檔案與 Copy-on-Write。
- 在工作集合(Working Set)適合的情況下,可同時執行多個大型程式。
缺點
- 每次記憶體存取都需要位址轉換;TLB Miss 時還要進行 Page Table Walk。
- Page Fault 必須存取磁碟或 SSD,延遲遠高於 RAM 存取。
- 頁表本身需要占用記憶體,多層頁表也會增加管理複雜度。
- 頁面配置會產生內部碎片。
- 若所有行程的工作集合超過可用 RAM,會發生 Thrashing,使系統大量換頁而效能急劇下降。
解題技巧
要區分三種事件:
- TLB Hit:快速完成位址轉換。
- TLB Miss:到頁表查詢,不一定發生 Page Fault。
- Page Fault:頁面不在 RAM,必須由作業系統載入,代價最高。
2. 擴大 TLB 後是否仍適合使用 Fully Associative
核心觀念
TLB 儲存的是近期使用的虛擬頁號到實體頁號的對映。Fully Associative TLB 中,每個虛擬頁號都可以放入任意 TLB entry,因此查詢時必須將輸入的 VPN 與所有 entry 平行比較。
若 TLB 有 個 entry:
- Fully Associative:每次存取比較 個 tag。
- -way Set Associative:先用 index 找到一個 set,再比較該 set 中的 個 tag。
解題方法與結論
TLB 通常比頁表小,因此 Fully Associative 的優點很明顯:
- 不需要 index bits。
- 任一虛擬頁都可放入任意位置。
- 沒有因固定索引造成的 Conflict Miss。
- 小型 TLB 可用平行比較快速完成查詢。
但當 TLB 擴大時,Fully Associative 的硬體成本會快速增加:
- 需要更多平行比較器。
- Tag 比對線路、結果選擇器與佈線負擔增加。
- 功耗與晶片面積增加。
- 平行比對結果的選擇與 replacement logic 變得更複雜。
- 存取時間可能受長距離佈線與高負載影響。
因此,TLB 大幅擴大後,通常不再適合維持 Fully Associative,而會採用較低路數的 Set Associative,甚至使用多層 TLB:
- 小型、速度要求高的 L1 TLB:可採 Fully Associative 或低路數配置。
- 容量較大的 L2 TLB:通常採 4-way、8-way 等 Set Associative。
解題技巧
Associativity 的取捨可記成:
- Fully Associative:命中率高,但硬體成本隨 entry 數量增加。
- Direct Mapped:硬體最簡單,但 Conflict Miss 較多。
- Set Associative:在成本與命中率之間取得折衷。
3. 多層快取的寫入策略
核心觀念
寫入策略主要分成兩部分:
-
Write-Through
寫入快取的同時,也立即寫入下一層快取或主記憶體。 -
Write-Back
先只更新目前層級的快取,並將該 cache line 標示為 Dirty;只有在該 line 被替換時,才寫回下一層。
此外,Write Miss 還要決定是否採用:
- Write-Allocate:先將資料所在的 cache line 載入,再在快取中修改。
- No-Write-Allocate:不載入 cache line,直接將資料寫入下一層。
解題方法與合理假設
題目未限定「一致性」是指各層資料立即同步,還是以整體效能為優先;依現代高效能多層快取的標準設計,採用 Write-Back 搭配 Write-Allocate。
寫入流程如下:
- CPU 寫入 L1 Cache 命中時,只更新 L1,並設定 Dirty Bit。
- L1 的 Dirty Cache Line 被替換時,整個 line 寫回 L2。
- 若 L2 也採 Write-Back,則 L2 將該 line 標示為 Dirty。
- 最後一層快取淘汰該 line 時,才寫回主記憶體。
此策略可避免每次 Store 都存取較低層快取或主記憶體,大幅減少寫入流量。Write-Allocate 則適合具有空間區域性的資料,因為同一 cache line 的其他資料很可能很快被使用。
Write-Through 的特性
Write-Through 的一致性管理較簡單,因為每次寫入都會立即傳遞到下一層;但缺點是:
- 寫入流量大。
- 主記憶體或低層快取承受較多請求。
- 高頻率 Store 會降低有效頻寬。
若課本將「一致性」嚴格定義為 L1 與 L2 的副本必須立即相同,則可使用 L1 Write-Through、較低層 Cache Write-Back 的混合策略。這能保持相鄰快取層的資料同步,並減少寫入主記憶體的次數。
在多核心系統中,Write-Back 本身不等於 Cache Coherence,還必須配合 MESI 等一致性協定,處理其他核心快取中的副本。
解題技巧
- 重視效能與寫入頻寬:Write-Back。
- 重視硬體簡單與立即同步:Write-Through。
- Write-Back 通常與 Write-Allocate 配對。
第 2 題30 分
Problem 2: (30%)
You have to run a program, which requires 4GB of memory space. Unfortunately, your computer system only allocates 2GB DRAM to this program. Hence, applying the virtual memory technique is necessary, and we assume that the total page size is 4KB. Besides, you also implement the write-back consistency policy and the LRU replacement policy in this virtual memory technique.
- (10%) How large is the involved page table at least?
- (10%) We assume that the CPU in your system accesses the data in this program randomly (i.e., no data locality). At the quasi-stationary, please show the approximated page fault rate.
- (10%) If we have a TLB in this system and the TLB miss rate is 75% at the quasi-stationary, how large is the involved TLB at least?
登入後即可作答並保存紀錄。
- 頁表大小
- 程式位址空間:
- 頁面大小:
- 頁數 頁
- 每個頁表項假設 4 B(存放框架號與控制位)
- 隨機存取下的近似缺頁率
- 實體記憶體可容納框架數 框
- 程式總頁數 頁
第 3 題25 分
Problem 3: (25%)
Now, you have a 32-bit processor, including a cache with 16KB data. We assume that the write-back consistency policy and the LRU replacement policy are both implemented in this cache as well. Please analyze the different architecture designs below.
- (5%) How many total bits are required if the direct-mapping strategy is involved, and the one-word block is considered?
- (5%) How many total bits are required if the direct-mapping strategy is involved, and the four-word block is considered?
- (5%) How many total bits are required if the two-way set-associative strategy is involved, and the four-word block is considered?
- (5%) TLB is usually adopted in modern systems. If we use the 32-bit address to access the TLB with 10 entries and each page size is 4KB, how large is the involved TLB?
- (5%) By following the previous question, how large is the involved page table?
登入後即可作答並保存紀錄。
核心觀念與計算假設
本題採用以下標準假設:
- 32-bit processor 的 word 為 bits,即 bytes。
- 記憶體採 byte-addressable。
- Cache 的 僅指資料區,不含 tag 與控制位元。
- 每個 cache line 具有 個 valid bit 與 個 dirty bit。
- 第 3 題的二路組相聯 cache,每組以 bit 記錄 LRU。
- 第 4、5 題未指定實體位址寬度、TLB 組織與 PTE 欄位;以下採 32-bit 虛擬位址、32-bit 實體位址、10-entry 全相聯 TLB,並計入 valid bit。
Cache 位址欄位公式為:
其中:
- Block offset:指出 block 內的 byte。
- Index:指出 cache line 或 set。
- Tag:儲存在 cache 中,用來比對位址。
- Write-back 需要 dirty bit,表示該 cache line 是否被修改。
- 每個 cache line 需要 valid bit,表示內容是否有效。
第 1 題:Direct Mapping,one-word block
解題方法
一個 word 為 bytes,因此:
Cache 資料容量為:
Cache line 數量:
因此:
- Block offset: bits
- Index: bits
- Tag:
每個 cache line 儲存:
Direct mapping 每個 index 只有一個候選位置,不需要額外儲存 LRU bit。
因此總位元數為:
換算為 bytes:
【答案】
第 2 題:Direct Mapping,four-word block
解題方法
四個 word 的 block 大小為:
Cache line 數量:
因此:
- Block offset: bits
- Index: bits
- Tag:
每個 block 包含四個 word,資料量為:
每個 cache line 儲存:
Direct mapping 不需 LRU bit,因此總位元數為:
【答案】
第 3 題:Two-way Set Associative,four-word block
解題方法
四-word block 仍為 bytes,因此 cache 總 line 數仍為:
二路組相聯表示每一組有兩個 cache lines,所以 set 數量為:
因此:
- Block offset: bits
- Set index: bits
- Tag:
第 4 題20 分
Problem 4: (20%)
The clock cycle time of a given processor is 1ns. We assume only one level cache is installed in this system and every data can be found in the main memory. Besides, we know the miss penalty is 20 clock cycles; the average miss rate is 5%; the cache access time is one clock cycle. Today, we have a program, and 40% of codes can be executed parallelly.
- (10%) Please find the average memory access time of this system.
- (10%) How many cores do you have to use to double the performance of this system? Please explain your answer.
登入後即可作答並保存紀錄。
1. 平均記憶體存取時間 (AMAT)
快取存取時間 (hit time) 為 1 個時脈=。
未命中懲罰 (miss penalty) 為 個時脈=,未命中率 。
2. 需使用多少核心才能將效能提升 2 倍?
程式中可平行化部分比例 ,其餘 為序列區段。
根據 Amdahl 定律:
第 5 題15 分
Problem 5: (15%)
Figure 1 shows a 5-stage pipelined MIPS CPU to support a simple code in Figure 2. We need to spend one clock cycle to complete all tasks in each pipeline stage.
Figure 1
[Image of a 5-stage MIPS pipeline: IF, ID, EX, MEM, WB]
Figure 2
sub 1, 12, 5
or 6, 14, 2
sw 2)
- (5%) Do we need to stall this architecture in Figure 1 to run the code in Figure 2? If yes, please determine how many cycles we should stall and explain your reasons. If not, please describe your reason.
- (5%) To support the code in Figure 2, please design a minimum viable product (MVP) of the ALU in Figure 1. You need to show the ALU architecture.
- (5%) Is it possible to move the Adder from the third pipeline stage to the second pipeline stage? If yes, please explain your re-design strategy. If not, please describe your reason.
登入後即可作答並保存紀錄。
核心觀念
本題考查三個重點:
- 資料相依與停滯:沒有資料前饋(forwarding)時,後續指令必須等前一指令將結果寫回暫存器,才能讀到正確資料。
- 共用加減法器:利用二補數關係 ,讓加法與減法共用同一個加法器,再以多工器選出算術或邏輯結果。
- 管線階段重配置:運算可以移到較早的階段,但資料、控制訊號與管線暫存器必須配合調整,並符合每階段一個時脈週期的限制。
解題方法
由原圖可讀出五階段為 IF、ID、EX、MEM、WB,未畫出將 EX/MEM 或 MEM/WB 結果送回 ALU 輸入端的前饋路徑。分支目標位址的加法器位於 EX,而 Branch 與 Zero 在 MEM 經 AND 閘產生 PCSrc,控制 IF 的 PC 選擇。
(1)是否需要停滯?需要幾個週期?
程式第一條指令:
sub $2, $1, $3
會產生新的暫存器 $2 值。其後四條指令都需要讀取 $2:
| 指令 | $2 的用途 |
|---|---|
and $12, $2, $5 | AND 的來源運算元 |
or $13, $6, $2 | OR 的來源運算元 |
add $14, $2, $2 | ADD 的兩個來源運算元 |
sw $15, 100($2) | 計算記憶體位址的基底暫存器 |
因此存在 RAW(先寫後讀)資料相依。
圖中沒有前饋路徑,and 必須等 sub 到達 WB,才能在 ID 讀到新值。圖未註明暫存器檔的讀寫時序,採用標準 MIPS 的「同一週期先寫入、後讀取」設計。
sub 的執行時序為:
and 原本在第 3 週期進入 ID、第 4 週期進入 EX;現在必須停留在 ID,等到第 5 週期讀取 $2,第 6 週期才能進入 EX。
完整時序如下:
| 指令 | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
sub | 1 | 2 | 3 | 4 | 5 |
and | 2 | 3~5 | 6 | 7 | 8 |
or | 3,停留至 5 | 6 | 7 | 8 | 9 |
add | 6 | 7 | 8 | 9 | 10 |
sw | 7 | 8 | 9 | 10 | 不寫回 |
停滯期間應凍結 PC 與 IF/ID 暫存器,並在 EX 插入兩個氣泡(bubble);前方的 sub 繼續前進。第 5 週期 sub 寫回後,and 讀到新值,其餘指令也都在更晚的週期讀取 $2,因此不需額外停滯。
(2)設計支援程式的最小 ALU 架構
這段程式需要以下功能:
| 指令 | ALU 所需運算 |
|---|---|
sub | |
and | |
or | |
add | |
sw | 基底位址加上符號延伸後的立即數 |
sw 的 ALU 輸入由圖中的 ALUSrc 多工器選擇,使:
ALU 執行加法,得到有效位址 。待儲存的資料 經另一條資料路徑送往資料記憶體,並非此加法的運算元。
共用加減法器
令 SUB 為減法控制訊號,對 的每一位執行 XOR:
再將 、 與進位輸入送入同一個 32 位元加法器:
SUB = 0:、進位輸入為 ,得到 。SUB = 1:、進位輸入為 ,得到 $A+\overline{B}+1=A-B