免費詳解 / 中正 / 硬體 / 103

103 中正資工所考古題解答|硬體逐題詳解

103 中正資工所硬體考古題的逐題詳解,共 9 題,免費公開。

逐題詳解

118%

(18 pt) Implement the inner product calculation of two 64-entry vectors (i.e. \(\sum_{i=0}^{63}x_i\times y_i\)).

(a) Assume \(x_i\) and \(y_i\) are signed integers (int). Write a C program to calculate their inner product. Use a loop to accumulate the products (i.e. with the loop body of "acc += x[i]*y[i];")

(b) Assume \(x_i\) is {1, −2, 3, −4, 5, −6, …, −62, 63, −64}, where odd numbers are positive and even numbers are negative. Find the binary representations (2's complement) of \(x_0\), \(x_1\), \(x_2\), and \(x_3\) respectively.

(c) The leading bits of the binary representations for small numbers (such as those in \(x_i\) in (b)) are all sign bits (i.e. 0's for positive and 1's for negative). Therefore, the inner product calculation of (b) will have significant bit transitions (due to the alternated signs) and thus consume large power. Someone unrolled the loop body in (a) as

acc += x[2*j]*y[2*j];
acc -= x[2*j+1]*y[2*j+1];

with all-positive \(x_i\) = {1, 2, 3, 4, 5, 6, …, 62, 63, 64} to reduce bit transitions (i.e. the leading sign bits are all zeroes now), but found it did not actually save power for some processors (also with a single hardware multiplier only). List two possible reasons.

(d) An alternative to implement the low-power inner product in (c) is to use two separate loops: one for "acc += x[2*j]*y[2*j];" and the other for "acc -= x[2*j+1]*y[2*j+1];". Can this approach help to save power? Briefly explain your answer.

(a)

int inner_product(const int x[64], const int y[64]) {
    int acc = 0;
    for (int i = 0; i < 64; i++)
        acc += x[i] * y[i];
    return acc;
}

(b) int 是 32 bit:

值2's complementhex
\(x_0\)10000 0000 0000 0000 0000 0000 0000 00010x00000001
\(x_1\)−21111 1111 1111 1111 1111 1111 1111 11100xFFFFFFFE
\(x_2\)30000 0000 0000 0000 0000 0000 0000 00110x00000003
\(x_3\)−41111 1111 1111 1111 1111 1111 1111 11000xFFFFFFFC

只看低 8 bit:0000 0001、1111 1110、0000 0011、1111 1100。正負交替,前面約 30 個 sign bit 每次都整排翻轉

(c) 兩個可能的原因:

① acc -= p 在硬體上是 acc + (~p + 1),adder 實際收到的是 product 的 2's complement,又是一整排 leading 1;+= 與 -= 交替,adder 輸入的高位元照樣每次整排翻轉,transition 只是從 multiplier 輸入搬到 adder 輸入

② 只把 \(x_i\) 改成全正,\(y_i\) 的正負沒變,product 的 sign 與高位元仍會交替;而且處理器的功耗大部分在 instruction fetch、register file、memory access 與 loop control,這些都沒變(交替的 add/sub 指令還會讓指令位元與 ALU 控制訊號翻轉),multiplier 輸入少掉的那點 transition 被蓋掉

(d) 可以。第一個 loop 全是 +=、第二個 loop 全是 -=:同一個 loop 內 adder 做的運算固定,送進 adder 的運算元高位元都是同一種 sign bit,前後兩次迭代之間不再整排翻轉,只有兩個 loop 交界翻一次

代價是多一個 loop 的 overhead,且 x、y 要各掃兩次;前提是 \(y_i\) 本身的正負不會交替,否則 product 的 sign 還是會翻

(b) 0x00000001、0xFFFFFFFE、0x00000003、0xFFFFFFFC (d) 可以(運算固定、不交替 add/sub)

27%

(7 pt) Both the following assembly codes implement the C statement "if((R1==R2)&&(R3==R4)) R5++;".

(a)  BNE R1, R2, Exit          (b)  CMP    R1, R2
     BNE R3, R4, Exit               CMPEQ  R3, R4
     ADD R5, R5, #1                 ADDEQ  R5, R5, #1
EXIT:

Calculate the execution times of each assembly code on a classical 5-stage pipelined processor (i.e. IF / ID / EX / MEM / WB), where branch instructions are resolved at EX.

假設沒有 branch prediction:branch 在 EX 才決定,後面要 stall 2 個 cycle 才知道下一條從哪裡 fetch。執行時間 =(執行的指令數 + stall 數)+ 4(最後一條指令走完 pipeline)

(a) 依條件分三種情況:

情況執行的指令指令數stallcycles
R1 ≠ R2BNE(taken)12\(1+2+4=7\)
R1 = R2,R3 ≠ R4BNE、BNE(taken)24\(2+4+4=10\)
R1 = R2,R3 = R4BNE、BNE、ADD34\(3+4+4=11\)

(b) conditional execution,沒有 branch:三條指令一定都流過 pipeline,條件不成立的指令只是不寫回;CMPEQ 用到 CMP 設定的 flag,靠 forwarding 不用 stall → 固定 \(3+4=7\) cycles

若 (a) 改用 predict-not-taken,只有 taken 時才 flush 2 個 cycle,三種情況是 7、8、7 cycles,仍不比 (b) 快;(b) 還省掉 branch 的 instruction 與預測錯誤的風險

(a) 7 / 10 / 11 cycles(依條件) (b) 固定 7 cycles

315%

(15 pt) 綜合簡答題:

(a) (3 pt) Explain the process of starvation and how aging can be used to prevent it. (b) (3 pt) What is the difference between deadlock prevention and deadlock avoidance? (c) (3 pt) Explain how copy-on-write operates. (d) (3 pt) Do all file systems suffer from internal fragmentation? Why? (e) (3 pt) What is the context switch time, associated with swapping, if a disk drive with a transfer rate of 2 MB/s is used to swap out part of a program that is 200 KB in size? Assume that no seeks are necessary and that the average latency is 15 ms. The time should reflect only the amount of time necessary to swap out the process.

(a) priority scheduling 下,低優先權的 process 一直被更高優先權的 process 插隊,無限期等不到 CPU,這就是 starvation(indefinite blocking)

aging:process 在 ready queue 等得越久,優先權就逐步提高(例如每等一段時間加 1),等夠久終究會變成最高優先權而被執行

(b) deadlock 四個必要條件:mutual exclusion、hold and wait、no preemption、circular wait

prevention:設計上讓其中至少一個條件永遠不成立(例如規定資源依序申請,破壞 circular wait);不用事先知道 process 的需求,但資源使用率低

avoidance:四個條件都允許,但 process 要事先宣告最大需求;每次申請時用 Banker's algorithm 檢查,核准後系統仍在 safe state 才給,否則讓它等

(c) fork() 之後 parent 與 child 先共用同一批 physical page,這些 page 標成唯讀的 copy-on-write;任一方要寫某一頁時觸發 page fault,OS 才複製那一頁給寫入的一方,page table 改指向新頁,再讓寫入繼續。沒被改到的 page 永遠不用複製,建立 child 快、也省 frame

(d) 是。file system 以 block 為單位配置空間,檔案大小通常不是 block size 的整數倍,最後一個 block 沒用完的部分就浪費掉;contiguous、linked、indexed 都是以 block 配置,一樣有

(e) transfer time \(=\dfrac{200\text{ KB}}{2\text{ MB/s}}=0.1\text{ s}=100\text{ ms}\),再加 average latency 15 ms(不需要 seek)

(e) \(100+15=115\) ms

44%

(4 pt) One technique for implementing lottery scheduling works by assigning processes lottery tickets, which are used for allocating CPU time. Whenever a scheduling decision has to be made, a lottery ticket is chosen at random, and the process holding that ticket gets the CPU. The BTV operating system implements lottery scheduling by holding a lottery 50 times each second, with each lottery winner getting 20 milliseconds of CPU time (20 milliseconds × 50 = 1 second). Describe how the BTV scheduler can ensure that higher-priority threads receive more attention from the CPU than lower-priority threads.

依優先權分配 ticket 數:優先權越高的 thread 拿到越多張 ticket

每張 ticket 中籤機率相同,一個 thread 長期拿到的 CPU 時間比例 \(\approx\dfrac{\text{它的 ticket 數}}{\text{全部 ticket 數}}\),所以高優先權 thread 得到較多 CPU;每秒 50 次抽籤,足夠讓長期比例逼近期望值

低優先權 thread 只要至少有一張 ticket,就不會 starvation(每次抽籤都有機會中)

56%

(6 pt) Consider the following page reference string: 7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 7, 1, 0, 5, 4, 6, 2, 3, 0, 1. Assuming demand paging with three frames, how many page faults would occur for the following replacement algorithms? LRU replacement、FIFO replacement、Optimal replacement

LRU(frames 由最久沒用到→最近用到)

ref7231253467
fault✓✓✓✓✓✓✓✓✓
frames772723231312125253534346467
ref7105462301
fault✓✓✓✓✓✓✓✓✓
frames467671710105054546462623230301

fault 共 18 次

FIFO(frames 由舊到新)

ref7231253467
fault✓✓✓✓✓✓✓✓
frames772723231231315315154546467
ref7105462301
fault✓✓✓✓✓✓✓✓✓
frames467671710105054546462623230301

fault 共 17 次

Optimal(換掉下一次用到最晚、或不再用到的 page;frames 以位置表示)

ref7231253467
fault✓✓✓✓✓✓✓✓
frames772723123123153153154156157
ref7105462301
fault✓✓✓✓✓
frames157157150150140160120130130130

fault 共 13 次

LRU 18 次 FIFO 17 次 Optimal 13 次

610%

(10 pt) If we want to design a carry-save adder to compute the addition of six 4-bit unsigned numbers: A, B, C, D, E, F with ONLY 1-bit full adders, and the delay time of the full adder is \(D_{FA}\). Please determine the minimum delay time for this carry-save adder.

六個數相加最大 \(6\times15=90\lt2^7\),結果 7 bit(\(s_6\ldots s_0\))

carry-save 做法:一個 full adder 把同一欄的 3 個 bit 壓成 1 個 sum(留在本欄)+1 個 carry(進下一欄),延遲都是 \(D_{FA}\);六個運算元 \(6\to4\to3\to2\) 要三層,最後兩個再用 full adder 串成 ripple 加起來

若三層都做完才開始 ripple,會多等很久;其實低位欄 bit 少、先壓完,ripple 的 carry 可以和上層的壓縮重疊。做法:每一欄把最早到的 3 個 bit 丟進一個 full adder,剩 2 個 bit 就用 full adder(第三個輸入接 0)收成 1 個。每欄的 bit 到達時間(單位 \(D_{FA}\)):

欄進入該欄的 bit 到達時間最後一個 sum 完成
00,0,0,0,0,02
10×6,另有 carry 1,1,23
20×6,carry 1,1,2,34
30×6,carry 1,1,2,3,45
4carry 1,1,2,3,56
5carry 3,67
6carry 77

\(s_0,\dots,s_6\) 依序在 \(2,3,4,5,6,7,7\ D_{FA}\) 完成

欄 0 有 6 個 bit,\(6\to2\to1\) 要 2 級;之後每往高位一欄,要等下一級送來的 carry,多 1 個 \(D_{FA}\),到 \(s_5\)、\(s_6\) 是 \(7D_{FA}\)

\(7\,D_{FA}\)

715%

(15 pt) The following techniques have been developed to reduce cache miss penalty or miss rate: "critical word first and early restart", "pipelined cache access", and "hardware prefetching of instructions". Please briefly explain these techniques and how they work.

critical word first and early restart(降 miss penalty) CPU 通常只急著要 block 裡的一個 word,不必等整個 block 載完

early restart:照 word 順序從 memory 讀 block,要的那個 word 一到就交給 CPU 繼續跑,剩下的 word 在背景補進 block

critical word first:先向 memory 要「導致 miss 的那個 word」,第一個送回,再補其他 word;CPU 馬上能繼續

pipelined cache access 把 cache 存取切成多個 pipeline stage,每個 cycle 都能開始新的一次存取

單次 hit 要的 cycle 數變多,但 cache bandwidth 提高、clock rate 可以拉高(代價是 branch misprediction penalty 變大、load 到使用的延遲變長)

hardware prefetching of instructions(降 miss rate/miss penalty) instruction miss 時除了抓需要的 block,也把下一個連續 block 抓進 stream buffer

之後若要的 block 在 stream buffer,就取消這次 miss,直接從 buffer 搬進 cache;利用指令循序執行的 spatial locality

815%

(15 pt) The critical-section problem is to design a protocol that the processes can use to cooperate. Please answer following questions.

(1) (5 pt) What are the requirements that a correct solution to the critical-section problem must satisfy? (2) (10 pt) Design a correct software solution to the critical-section problem for two processes. Present a justification of the algorithm's correctness.

(1) 三個條件:

mutual exclusion:同一時間最多一個 process 在 critical section 內

progress:沒有人在 critical section 時,只有想進去的 process 參與決定誰進,而且這個決定不能無限延後

bounded waiting:process 提出要求後,其他 process 搶先進入的次數有上限,不會 starvation

(2) Peterson's solution(\(P_i\),\(j=1-i\)):

int  turn;                     // 輪到誰
bool flag[2] = {false, false}; // 誰想進

do {
    flag[i] = true;
    turn = j;                          // 先讓給對方
    while (flag[j] && turn == j) ;     // busy wait
    /* critical section */
    flag[i] = false;
    /* remainder section */
} while (true);

mutual exclusion:兩個都在 critical section 內,代表兩人的 while 條件都不成立;但 flag 都是 true,只能靠 turn 同時不等於 \(j\)(\(P_i\) 通過)又不等於 \(i\)(\(P_j\) 通過),不可能(turn 只有一個值)

progress:\(P_j\) 不想進時 flag[j] 為 false,\(P_i\) 直接通過;兩個都想進時,turn 最後只會停在其中一個值,一定有一方通過

bounded waiting:\(P_j\) 出來後若想再進,會先把 turn = i,於是換 \(P_i\) 通過;\(P_i\) 最多等對方進去一次

兩行順序不能換:若先 turn = j 再 flag[i] = true,可能兩邊都通過;現代處理器上還要加 memory barrier,否則亂序執行會破壞假設

910%

(10 pt) Give an "efficient" solution to the producer-consumer problem. Present a justification of the algorithm's correctness.

用 semaphore 讓等待的 process 被 block,而不是 busy waiting;\(N\) 格 buffer 全部都用得到:

semaphore mutex = 1;   // 保護 buffer、in、out
semaphore empty = N;   // 空格數
semaphore full  = 0;   // 已放入的 item 數

void producer(void) {
    while (true) {
        item = produce();
        wait(empty);
        wait(mutex);
        buffer[in] = item;  in = (in + 1) % N;
        signal(mutex);
        signal(full);
    }
}

void consumer(void) {
    while (true) {
        wait(full);
        wait(mutex);
        item = buffer[out]; out = (out + 1) % N;
        signal(mutex);
        signal(empty);
        consume(item);
    }
}

正確性:

① mutex 保證同一時間只有一個 process 改 buffer、in、out

② empty 為 0(buffer 滿)時 producer 在 wait(empty) 被 block,不會蓋掉還沒取走的 item;full 為 0(buffer 空)時 consumer 被 block,不會讀到空格

③ 一定要先 wait(empty)/wait(full),再 wait(mutex):順序反過來,producer 拿著 mutex 在 buffer 滿時睡著,consumer 拿不到 mutex 也清不出空位,就 deadlock

④ 等待中的 process 都在 semaphore 的 queue 裡睡著,不佔 CPU,這是比 busy waiting 有效率的地方

109–115 的詳解也是這樣寫的。

國立中正大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 351 頁,逐題推導。

購買 · NT$ 850 看中正其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com