112 年 國立中山大學資訊工程學系碩士班乙組《計算機結構》
第 1 題20 分
- (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)公式:
代入已知數據:
【答案】
1.2
設組相聯(Set-Associative)快取的失誤率為 。
欲使組相聯快取的平均存取延遲低於直接對映(Direct-Mapped)快取的 ,須滿足:
【答案】失誤率必須低於 (即 )
1.3
每條指令的平均 CPI 計算如下:
代入已知數據:
【答案】
1.4
第 2 題15 分
- (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 總數乘以在 時間間隔內單一 row 需進行 Refresh 的次數。
-
全系統 DRAM Row 總數:
-
內單一 Row 的 Refresh 次數:
-
全系統總 Refresh 次數:
【答案】 次(或 次)
2.2 Command Bus 使用率計算
觀念與推導:
每個 Channel 擁有獨立的 Memory Controller 與 Command Bus。單一 Channel 的 Command Bus 使用率即代表跨所有 Channel 的 Bus 平均使用率。
-
單一 Channel 的 Row 總數:
-
單一 Channel 在 內的 Refresh 次數:
-
Command Bus 使用率:
第 3 題15 分
- (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) # load a key
LW x4, 4 (x1) # load the next pointer key
SEQ x3, x3, x2 # set x3 if x3 == x2
BNEZ x3, end # find the entry
ADD x1, x0, x4
BNEZ x4, loop # check the next code
end:
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 道指令)的執行時序與資料相依性:
- : 發射
LW x3, 0(x1),記憶體資料於 到達。 - : 發射
LW x4, 4(x1),記憶體資料於 到達。 - :
SEQ x3, x3, x2因相依於x3而停頓 48 個週期。 - : 發射
SEQ x3, x3, x2,結果於 可用。 - : 發射
BNEZ x3, end。 - : 發射
ADD x1, x0, x4(x4已於 到達,無 stall),結果於 可用。 - : 發射
BNEZ x4, loop。
下一迭代的 LW x3, 0(x1) 相依於 x1(結果於 可用),故可於 順利發射。
單一迭代總週期數 = 6 個執行週期 + 48 個停頓週期 = 54 週期。
【答案】 54 週期
3.2 詳解
在固定輪轉排程(Fine-grained Multithreading)下, 個執行緒輪流執行,同一執行緒相鄰兩道指令間隔 個週期。為達 100% 處理器利用率,必須滿足同一執行緒內所有資料相依性的最長延遲需求。
分析迴圈內指令間的相依性距離與延遲:
LW x3(第 1 道)與SEQ x3(第 3 道):指令距離 ,記憶體延遲 50 週期:
LW x4(第 2 道)與ADD x1(第 5 道):指令距離 ,記憶體延遲 50 週期:
ADD x1(第 5 道)與下一迭代LW x3(第 1 道):指令距離 ,整數延遲 1 週期:
最嚴格的瓶頸為 LW x3 到 SEQ x3 的相依性,故至少需要 25 個執行緒。
【答案】 25 個執行緒
第 4 題15 分
- (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 M×K
;; 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)之間,該陣列所被存取的不同元素數量。
- 若最內層迴圈變數未出現在該陣列索引中,則在最內層迴圈的每次疊代皆存取相同元素,其 。
- 若要重新存取同一元素,需等待未出現在其索引中的最近一層外層迴圈遞增,其間該陣列所歷經的不同元素總數即為其 。
4.1 詳解
迴圈巢狀順序為 :
-
陣列 :
最內層迴圈為 ,未包含於 的索引 中。在 的每一次疊代中皆存取相同的 ,故存取間隔為 個元素。
-
陣列 :
索引未包含迴圈 (位於 與 之間)。欲重新存取相同的 ,需等迴圈 遞增 ;在此期間,內層迴圈 跑完一整圈(共 次),存取 。故存取間隔為 個不同元素。
-
陣列 :
索引未包含最外層迴圈 。欲重新存取相同的 ,需等迴圈 遞增 ;在此期間,內層的迴圈 ( 次)與迴圈 ( 次)完整執行,共存取 個不同元素。
【答案】
- RI of A
- RI of B
- RI of Z
4.2 詳解
迴圈巢狀順序改為 :
第 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).
登入後即可作答並保存紀錄。
核心觀念
-
矩陣乘法運算維度
考慮密集矩陣乘法運算:其中矩陣維度分別為 、、,給定數值為:
總 MAC(Multiply-Accumulate)計算次數為:
-
計算強度(Compute Intensity)
計算強度定義為平均每次 DRAM 存取所能執行的計算次數:在總計算量固定的前提下,最大化計算強度等價於極小化 DRAM 存取次數,其關鍵在於透過晶片上的暫存結構最大化資料重用(Data Reuse)。
-
硬體資源之微架構角色
- MAC 單元(含輸入/輸出正反器):
- 輸出端正反器(Output Flip-Flop):可直接作為累加器(Accumulator)。只要將累加維度 置於最內層,每個輸出元素 即可在硬體正反器內就地累加 次,累加過程中完全無需讀寫 SRAM 或 DRAM。
- 輸入端正反器(Input Flip-Flops):負責暫存每個時脈週期送入乘法器的運算元,支援串流資料的打拍輸入。
- SRAM 陣列(容量 32 個元素):
題目限制不可增加額外迴圈(no tiling),因此無法進行微區塊(tiling)快取。SRAM 必須能完整裝下某一特定維度的完整切片(向量),方能避免重複載入。恰好 ,使得在 SRAM 中快取長度為 的一整列或一整欄成為可能;而 與 均遠大於 32,無法直接整列/整欄置入 SRAM。
- MAC 單元(含輸入/輸出正反器):
迴圈順序選擇分析
第 4.1 題與第 4.2 題給定的迴圈順序分別為:
- 第 4.1 題:
for m in [0, M) for n in [0, N) for k in [0, K) Z[m, n] += A[m, k] * B[k, n] - 第 4.2 題:
for k in [0, K) for m in [0, M) for n in [0, N) Z[m, n] += A[m, k] * B[k, n]
兩者效能差異如下:
-
為何選擇第 4.1 題():
- 累加維度 位於最內層。當外層 固定時,MAC 輸出正反器直接累加 的所有乘積項。在整個 步的計算過程中,無需對 DRAM 產生任何中間部分和(Partial Sum)的寫回與重讀。
- 每個 僅需在計算完成後寫回 DRAM 一次(若需讀入初始值則為讀一次寫一次),總存取量僅為 等級。
-
第 4.2 題()的致命缺陷:
- 累加維度 位於最外層。內層對固定 僅完成單一步 的部分和。
- 由於下一次對同一 累加必須等待整個 與 迴圈跑完(間隔 次運算),而 SRAM 容量僅有 32 個元素,根本無法暫存 的中間結果。
- 因此,每一次內層運算都必須將 從 DRAM 讀出、累加後寫回 DRAM,導致矩陣 的 DRAM 存取次數高達: 這將造成 DRAM 存取量急遽膨脹,計算強度嚴重退化。
-
重新排列順序之比較(如 ):
- 若將 同樣放在最內層,可考慮 :此時可將矩陣 的一欄(長度 )置於 SRAM,該欄隨外層 迴圈被重用 次,讀取 需 次 DRAM 存取。
- 相較之下,第 4.1 題的 將矩陣 的一列(長度 )置於 SRAM,該列隨中間 迴圈被重用 次,讀取 僅需 次 DRAM 存取。
- 因 ,保存 的一列能獲得更高的資料重用率(重用 1000 次 vs 500 次),DRAM 存取次數更少。
因此,最佳選擇確定為第 4.1 題的迴圈順序()。
SRAM 預算分配策略
硬體配置包含 32 元素的 SRAM 陣列以及 MAC 輸入/輸出正反器:
- 矩陣 分配 30 個元素:
在 順序中,外層每變換一個 ,就將矩陣 的第 列整列(,共 個元素)自 DRAM 載入 SRAM。在隨後的 次 迴圈中,所有 元素的存取皆直接命中 SRAM,完全不消耗 DRAM 頻寬。 - 矩陣 分配 0 個元素:
隨最內層 推進,自 DRAM 串流讀入 MAC 單元之輸入端正反器,利用硬體管線正反器進行計算,不需佔用 SRAM 空間。 - 矩陣 分配 0 個元素:
第 5 題15 分
- (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) 必須滿足以下兩個條件:
- 程式順序(Program Order, PO):單一進程內的指令執行順序不得改變:
- 全域順序(Total Order):所有處理器的指令交錯成單一合法的全域執行順序。
各狀態推導與執行順序
1. 狀態
- 數值要求:
- 推導:
結合程式順序 PO 與數值要求,會形成環狀相依關係(Cycle):
出現矛盾 ,故在 SC 模式下不可能實現。
2. 狀態
- 數值要求:
- 合法執行順序:
3. 狀態
- 數值要求:
- 合法執行順序: