112 年 國立中山大學資訊工程學系碩士班乙組《計算機結構》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題20 分

  1. (20% total) Performance Calculations
    1.1 (5%) The miss latency is 10 cycles. If the direct-mapped cache has a two-cycle hit latency, what is the average memory latency with a 15% miss rate?
    1.2 (5%) The miss latency is 10 cycles. If the set-associative cache has a three-cycle hit latency, what the miss rate must the set-associative cache obtain to have a lower average latency than the direct-mapped cache with 15% miss rate?
    1.3 (5%) Consider a workload with 30% branches and a 66.66% branch prediction accuracy (33.33% misprediction rate). You may ignore memory operations. For a simple five-stage pipeline, what is the CPI of this workload assuming a three-cycle misprediction penalty and a single-cycle latency for all other instructions?
    1.4 (5%) Consider a workload with 30% branches and a 66.66% branch prediction accuracy (33.33% misprediction rate). You may ignore memory operations. Calculate under what condition a much deeper pipeline with double the core clock frequency will outperform the shallower pipeline?

登入後即可作答並保存紀錄。

這一題的完整詳解

1.1

根據平均記憶體存取時間(AMAT)公式:
Average Memory Latency=Hit Latency+Miss Rate×Miss Latency\text{Average Memory Latency} = \text{Hit Latency} + \text{Miss Rate} \times \text{Miss Latency}

代入已知數據:
Average Memory Latency=2+0.15×10=3.5 cycles\text{Average Memory Latency} = 2 + 0.15 \times 10 = 3.5 \text{ cycles}

【答案】3.5 cycles3.5 \text{ cycles}


1.2

設組相聯(Set-Associative)快取的失誤率為 msam_{sa}。
欲使組相聯快取的平均存取延遲低於直接對映(Direct-Mapped)快取的 3.5 cycles3.5 \text{ cycles},須滿足:
Hit Latency+msa×Miss Latency<3.5\text{Hit Latency} + m_{sa} \times \text{Miss Latency} < 3.5
3+msa×10<3.53 + m_{sa} \times 10 < 3.5
10⋅msa<0.5  ⟹  msa<0.05=5%10 \cdot m_{sa} < 0.5 \implies m_{sa} < 0.05 = 5\%

【答案】失誤率必須低於 5%5\%(即 msa<5%m_{sa} < 5\%)


1.3

每條指令的平均 CPI 計算如下:
CPI=Base CPI+Branch Stall Cycles\text{CPI} = \text{Base CPI} + \text{Branch Stall Cycles}

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題15 分

  1. (15% total) A memory system has four channels, and each channel has two ranks of DRAM chips. Each memory channel is controlled by a separate memory controller. Each rank of DRAM contains eight banks. A bank contains 32K rows. Each row in one bank is 8KB. The minimum retention time among all DRAM rows in the system is 64 ms. In order to ensure that no data is lost, every DRAM row is refreshed once per 64 ms. Every DRAM row refresh is initiated by a command from the memory controller which occupies the command bus on the associated memory channel for 5 ns and the associated bank for 40 ns. Let us consider a 1.024 second span of time. We define utilization (of a resource such as a bus or a memory bank) as the fraction of total time for which a resource is occupied by a refresh command. (Note: For each calculation in this question, you may leave your answer in simplified form in terms of powers of 2 and powers of 10.)

2.1 (5%) How many refreshes are performed by the memory controllers during the 1.024 second period in total across all four memory channels?
2.2 (5%) What command bus utilization, across all memory channels, is directly caused by DRAM refreshes?
2.3 (5%) What bank utilization (on average across all banks) is directly caused by DRAM refreshes?

登入後即可作答並保存紀錄。

這一題的完整詳解

2.1 總 Refresh 次數計算

觀念與推導:
全系統總 Refresh 次數等於全系統 DRAM row 總數乘以在 1.024 s1.024\text{ s} 時間間隔內單一 row 需進行 Refresh 的次數。

  1. 全系統 DRAM Row 總數:
    Total Rows=4 (channels)×2 (ranks)×8 (banks)×32K (rows)=22×21×23×215=221 rows\text{Total Rows} = 4\text{ (channels)} \times 2\text{ (ranks)} \times 8\text{ (banks)} \times 32\text{K (rows)} = 2^2 \times 2^1 \times 2^3 \times 2^{15} = 2^{21}\text{ rows}

  2. 1.024 s1.024\text{ s} 內單一 Row 的 Refresh 次數:
    Refreshes per Row=1.024 s64 ms=1024 ms64 ms=16=24 次\text{Refreshes per Row} = \frac{1.024\text{ s}}{64\text{ ms}} = \frac{1024\text{ ms}}{64\text{ ms}} = 16 = 2^4\text{ 次}

  3. 全系統總 Refresh 次數:
    Total Refreshes=221×24=225 次\text{Total Refreshes} = 2^{21} \times 2^4 = 2^{25}\text{ 次}

【答案】2252^{25} 次(或 33,554,43233,554,432 次)


2.2 Command Bus 使用率計算

觀念與推導:
每個 Channel 擁有獨立的 Memory Controller 與 Command Bus。單一 Channel 的 Command Bus 使用率即代表跨所有 Channel 的 Bus 平均使用率。

  1. 單一 Channel 的 Row 總數:
    Rows per Channel=2 (ranks)×8 (banks)×32K (rows)=21×23×215=219 rows\text{Rows per Channel} = 2\text{ (ranks)} \times 8\text{ (banks)} \times 32\text{K (rows)} = 2^1 \times 2^3 \times 2^{15} = 2^{19}\text{ rows}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題15 分

  1. (15% total) A benchmark searches for an entry in a linked list build from the following structure, which contains a key, a pointer to the next node in the linked list, and a pointer to the data entry.
    struct node {
    int key;
    struct node *next;
    struct data *ptr;
    }
    The following RISC-V code shows the core of the benchmark, which traverses the linked list and finds an entry with a particular key.
    loop: LW x3, 0 (x1)
    LW x4, 4 (x1)
    SEQ x3, x3, x2
    BNEZ x3, end
    ADD x1, x0, x4
    BNEZ x4, loop
    end:

load a key

load the next pointer key

set x3 if x3 == x2

find the entry

check the next code

We run this benchmark on a single-issue in-order processor. The processor can fetch and issue (dispatch) one instruction per cycle. If an instruction cannot be issued due to a data dependency, the processor stalls. Integer instructions take one cycle to execute and the result can be used in the next cycle. For example, if SEQ is executed in cycle 1, BNEZ can be executed in cycle 2. We also assume that the processor has a perfect branch predictor with no penalty for both taken and not-taken branches.

3.1 (5%) Assume that the system does not have a cache. Each memory operation directly accesses main memory and takes 50 CPU cycles. The load/store unit is fully pipelined, and non-blocking. After the processor issues a memory operation, it can continue executing instructions until it reaches an instruction that is dependent on an outstanding memory operation. How many cycles does it take to execute one iteration of the loop in steady state?
3.2 (5%) Now we add zero-overhead multithreading to the pipeline. A processor executes multiple threads, each of which performs an independent search. Hardware mechanisms schedule a thread to execute each cycle. In the first implementation, the processor switches to a different thread every cycle using fixed round robin scheduling. Each of the N threads executes one instruction every N cycle. What is the minimum number of threads that we need to fully utilize the processor, i.e., execute one instruction per cycle?
3.3 (5%) We change the processor to only switch to a different thread when an instruction cannot execute due to data dependency (data-dependent switching). What is the minimum number of threads to fully utilize the processor now? Note that the processor issues instructions in order in each thread.
Hint: consider how many instructions each thread can execute in steady state.

登入後即可作答並保存紀錄。

這一題的完整詳解

3.1 詳解

在穩定狀態(Steady State)下,分析單一迴圈迭代(共 6 道指令)的執行時序與資料相依性:

  • T+0T+0: 發射 LW x3, 0(x1),記憶體資料於 T+50T+50 到達。
  • T+1T+1: 發射 LW x4, 4(x1),記憶體資料於 T+51T+51 到達。
  • T+2∼T+49T+2 \sim T+49: SEQ x3, x3, x2 因相依於 x3 而停頓 48 個週期。
  • T+50T+50: 發射 SEQ x3, x3, x2,結果於 T+51T+51 可用。
  • T+51T+51: 發射 BNEZ x3, end。
  • T+52T+52: 發射 ADD x1, x0, x4(x4 已於 T+51T+51 到達,無 stall),結果於 T+53T+53 可用。
  • T+53T+53: 發射 BNEZ x4, loop。

下一迭代的 LW x3, 0(x1) 相依於 x1(結果於 T+53T+53 可用),故可於 T+54T+54 順利發射。

單一迭代總週期數 = 6 個執行週期 + 48 個停頓週期 = 54 週期。

【答案】 54 週期


3.2 詳解

在固定輪轉排程(Fine-grained Multithreading)下,NN 個執行緒輪流執行,同一執行緒相鄰兩道指令間隔 NN 個週期。為達 100% 處理器利用率,必須滿足同一執行緒內所有資料相依性的最長延遲需求。

分析迴圈內指令間的相依性距離與延遲:

  1. LW x3(第 1 道)與 SEQ x3(第 3 道):指令距離 d=2d = 2,記憶體延遲 50 週期:
    2N≥50  ⟹  N≥252N \ge 50 \implies N \ge 25
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題15 分

  1. (15% total) For a given compute kernel, we define a tensor's reuse interval (RI) as the number of different elements of that tensor that have been referenced between each re-reference of the same element. For example, consider the following:
    for m in [0, M)
    for n in [0, N)
    Z[m, n] = A[m] * B[n]
    Since A's element is used at every iteration of the inner loop, its RI is 1. Each element of B is re-referenced after N references, so its RI is N. Z has "infinite" reuse interval (i.e., no data reuse) since no element is re-referenced throughout the computation:
    RI of A = 1
    RI of B = N
    RI of Z = infinite / no reuse

4.1 (5%) Consider the following Matrix-Matrix multiply pseudocode, which multiplies two dense matrices A and B to produce Z:
;; Multiply two matrices A and B to produce Z
;; First matrix A is MxK
;; Second matrix B is K×N
;; Thus, resulting matrix Z is M×N
for m in [0, M)
for n in [0, N)
for k in [0, K)
Z[m, n] += A[m, k] * B[k, n]
What are the reuse intervals for the three matrices? Provide your answers in terms of M, N, and K.
4.2 (5%) Now consider the following where we re-order the loop nest:
for k in [0, K)
for m in [0, M)
for n in [0, N)
Z[m, n] += A[m, k] * B[k, n]
What are the reuse intervals for the three matrices? Provide your answers in terms of M, N, and K.

登入後即可作答並保存紀錄。

這一題的完整詳解

觀念說明

重用間隔(Reuse Interval, RI) 定義為同一陣列元素在連續兩次被存取(reference)之間,該陣列所被存取的不同元素數量。

  • 若最內層迴圈變數未出現在該陣列索引中,則在最內層迴圈的每次疊代皆存取相同元素,其 RI=1\text{RI} = 1。
  • 若要重新存取同一元素,需等待未出現在其索引中的最近一層外層迴圈遞增,其間該陣列所歷經的不同元素總數即為其 RI\text{RI}。

4.1 詳解

迴圈巢狀順序為 m→n→km \to n \to k:

  1. 陣列 Z[m,n]Z[m, n]:
    最內層迴圈為 kk,未包含於 ZZ 的索引 (m,n)(m, n) 中。在 kk 的每一次疊代中皆存取相同的 Z[m,n]Z[m, n],故存取間隔為 11 個元素。
    RI of Z=1\text{RI of } Z = 1

  2. 陣列 A[m,k]A[m, k]:
    索引未包含迴圈 nn(位於 mm 與 kk 之間)。欲重新存取相同的 A[m,k]A[m, k],需等迴圈 nn 遞增 11;在此期間,內層迴圈 kk 跑完一整圈(共 KK 次),存取 A[m,0…K−1]A[m, 0 \dots K-1]。故存取間隔為 KK 個不同元素。
    RI of A=K\text{RI of } A = K

  3. 陣列 B[k,n]B[k, n]:
    索引未包含最外層迴圈 mm。欲重新存取相同的 B[k,n]B[k, n],需等迴圈 mm 遞增 11;在此期間,內層的迴圈 nn(NN 次)與迴圈 kk(KK 次)完整執行,共存取 N×KN \times K 個不同元素。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4.3 題5 分

4.3 (5%) Now consider the following scenario:
M = 500, N = 1000, and K = 30
A, B, and Z are dense matrices.
The matrices are resident in DRAM at the beginning of the computation kernel.
Your wish to design a matrix-matrix multiply accelerator given the above assumptions. Your goal is to maximize compute intensity, defined as the average number of computations you perform per DRAM access. You are given the following hardware budget:
➤ A multiply-accumulate (MAC) unit with a flip-flop on each input and the output.
➤ An SRAM array large enough to store 32 elements.
Which order of loop nest in Questions 4.1 or 4.2, would you choose, and given the order how would you partition the SRAM budget among the different elements to maximize compute intensity? You can reorder the loop nests from Questions 4.1 and 4.2, but do not add more loops (e.g., no tiling).

登入後即可作答並保存紀錄。

這一題的完整詳解

題目模型與核心觀念

題幹未列出第 4.1、4.2 題的實際程式碼,以下依標準矩陣乘法模型說明:

A∈RM×K,B∈RK×N,Z∈RM×NA\in\mathbb{R}^{M\times K},\qquad B\in\mathbb{R}^{K\times N},\qquad Z\in\mathbb{R}^{M\times N} Zij=∑k=0K−1AikBkjZ_{ij}=\sum_{k=0}^{K-1}A_{ik}B_{kj}

本題考查兩個觀念:

  1. 計算強度(compute intensity)
Compute Intensity=計算次數DRAM 存取次數\text{Compute Intensity} = \frac{\text{計算次數}}{\text{DRAM 存取次數}}
  1. 迴圈順序與資料重用

kk 是 reduction dimension。若把 kk 放在最內層,同一個 ZijZ_{ij} 的累加值可以一直放在 MAC 的輸出 flip-flop 中,完成 KK 次 MAC 後才寫回 DRAM,能大幅減少 ZZ 的存取。


選擇迴圈順序

採用外層到內層為:

i→j→k\boxed{i\rightarrow j\rightarrow k}

其概念如下:

  • 固定 ii 時,先把整列 Ai,:A_{i,:} 放入 SRAM。
  • jj 逐一變化,重複使用這一列 AA。
  • 固定 (i,j)(i,j) 時,kk 在最內層,讓 ZijZ_{ij} 保存在輸出 flip-flop 中完成累加。

對應的運算流程為:

Zij←Zij+AikBkj,k=0,…,K−1Z_{ij}\leftarrow Z_{ij}+A_{ik}B_{kj}, \qquad k=0,\ldots,K-1

其中:

  • AikA_{ik} 從 SRAM 取出至 MAC 輸入 flip-flop。
  • BkjB_{kj} 以串流方式從 DRAM 讀取。
  • ZijZ_{ij} 保留在 MAC 輸出 flip-flop。

SRAM 分配

本題 K=30K=30,而 SRAM 可存放 3232 個元素,因此可將一整列 AA 放入 SRAM:

Ai,0,Ai,1,…,Ai,29A_{i,0},A_{i,1},\ldots,A_{i,29}

一個完整且實用的配置為:

資料SRAM 配置
AA3030 個元素
BB11 個串流緩衝元素
ZZ11 個交換/暫存元素
合計3232 個元素

真正帶來主要重用效果的是 AA 的 3030 個元素。BB 與 ZZ 各配置一格只作為串流緩衝;ZZ 的累加值主要由 MAC 輸出 flip-flop 保存,不需要在 SRAM 中保存整個輸出區塊。

因此核心配置是:

SA=30\boxed{S_A=30}

DRAM 存取次數

將一次 MAC 視為一次 computation,總計算次數為:

C=MNK=500×1000×30=15,000,000C=MNK =500\times1000\times30 =15{,}000{,}000

採用 i→j→ki\rightarrow j\rightarrow k 後:

1. AA 的存取

每個 AA 元素在固定 ii 時載入 SRAM 一次,之後重複用於所有 jj:

DA=MK=500×30=15,000D_A=MK =500\times30 =15{,}000

2. BB 的存取

在此迴圈順序中,BkjB_{kj} 隨著 (j,k)(j,k) 變化,且沒有足夠 SRAM 保存所有 BB 的欄,因此每次 MAC 需讀取一個 BB 元素:

DB=MNK=15,000,000D_B=MNK =15{,}000{,}000

3. ZZ 的存取

每個 ZijZ_{ij}:

  • 開始累加時從 DRAM 讀取一次;
  • 完成 KK 次累加後寫回 DRAM 一次。

因此:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題15 分

  1. (15% total) Considering a system which applies the Sequential Consistency (SC). Suppose that three processes, P1, P2 and P3 are applied with different processors on a system (the values of RA, RB, RC were all zeros before the execution):
    P1
    P2
    P3
    P1.1: ST (A), 1
    P2.1: ST (B), 1
    P3.1: ST (C), 1
    P1.2: LD RC, (C) | P2.2: LD RA, (A) | P3.2: LD RB, (B)
    =
    After all processes are executed, it is possible for the system to have multiple machine states. For example, {RA, RB, RC} = {1,1,1} is possible if the execution sequence of instructions is P1.1-P2.1-P3.1-P1.2→P2.2→P3.2. Also, {RA, RB, RC}= {1,1,0} is possible if the sequence is P1.1→P1.2→P2.1 → P3.1 → P2.2 → P3.2. For each state of {RA, RB, RC}: {0,0,0} {0,1,0} {1,0,0} {0,0,1}, specify the execution sequence of instructions that results in the corresponding state. If the state is NOT possible with SC, just put X.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

順序一致性(Sequential Consistency, SC) 必須滿足以下兩個條件:

  1. 程式順序(Program Order, PO):單一進程內的指令執行順序不得改變:
    P1.1<P1.2,P2.1<P2.2,P3.1<P3.2P1.1 < P1.2, \quad P2.1 < P2.2, \quad P3.1 < P3.2
  2. 全域順序(Total Order):所有處理器的指令交錯成單一合法的全域執行順序。

各狀態推導與執行順序

1. 狀態 {RA,RB,RC}={0,0,0}\{RA, RB, RC\} = \{0, 0, 0\}

  • 數值要求:
    • RA=0  ⟹  P2.2<P1.1RA = 0 \implies P2.2 < P1.1
    • RB=0  ⟹  P3.2<P2.1RB = 0 \implies P3.2 < P2.1
    • RC=0  ⟹  P1.2<P3.1RC = 0 \implies P1.2 < P3.1
  • 推導:
    結合程式順序 PO 與數值要求,會形成環狀相依關係(Cycle):
    P1.1<P1.2<P3.1<P3.2<P2.1<P2.2<P1.1P1.1 < P1.2 < P3.1 < P3.2 < P2.1 < P2.2 < P1.1
    出現矛盾 P1.1<P1.1P1.1 < P1.1,故在 SC 模式下不可能實現。

2. 狀態 {RA,RB,RC}={0,1,0}\{RA, RB, RC\} = \{0, 1, 0\}

  • 數值要求:
    • RA=0  ⟹  P2.2<P1.1RA = 0 \implies P2.2 < P1.1
    • RB=1  ⟹  P2.1<P3.2RB = 1 \implies P2.1 < P3.2
    • RC=0  ⟹  P1.2<P3.1RC = 0 \implies P1.2 < P3.1
  • 合法執行順序:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題