113 年 國立臺灣大學資訊工程學系碩士班《計算機結構與作業系統》

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

第 1. 題5 分

[5 points] Power management is important to the design of computer systems. Which of the following statements is/are true?
(A) To achieve the maximum performance, the clock frequency should be increased, but the supply voltage should be reduced at the same time, so that the processor would not be overheated.
(B) Suppose the active power consumption is 10W when the supply voltage is 1.3V. When the supply voltage is reduced to 1.1V, the active power consumption is changed to 7.2W.
(C) When the processor is executing a memory-bound program, reducing the clock frequency should not impact the performance significantly, assuming the memory bus frequency remains the same.
(D) As technology continues to shrink, leakage power will become a dominant factor. Dynamic voltage and frequency scaling (DVFS) is often used to reduce the leakage power.
(E) The processor caches can be turned off immediately at any time to save the power consumption as much as possible because the data can always be found in the main memory.

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

這一題的完整詳解

核心觀念

本題考查計算機結構中系統功耗管理(Power Management)​的核心理論,涵蓋以下關鍵概念與公式:

  1. 動態功耗公式(Dynamic Power Formula):
    Pactive=12⋅C⋅V2⋅fP_{\text{active}} = \frac{1}{2} \cdot C \cdot V^2 \cdot f
    其中 CC 為負載電容(Capacitance),VV 為供給電壓(Supply Voltage),ff 為時脈頻率(Clock Frequency)。
  2. 邏輯閘延遲與電壓關係:晶體管的電路傳輸延遲與供給電壓成反比。降低電壓會增加電路延遲,從而限制最高可運行的時脈頻率 fmaxf_{\text{max}}。
  3. 記憶體受限(Memory-bound)程式的 DVFS 策略:當程式執行時間的瓶頸在於記憶體存取延遲時,適度調降 CPU 時脈頻率能顯著降低功耗且對整體效能影響極小。
  4. 功耗類型與控制技術對應:
    • 動態功耗(Dynamic Power):主要透過動態電壓與頻率調整(DVFS)進行控制。
    • 漏電功耗(Leakage / Static Power):主要透過電源閘控(Power Gating)進行控制。
  5. 快取快照與一致性(Cache Coherence & Consistency):Write-back 快取含有 Dirty Data,關閉快取前必須先執行寫回(Flush / Write-back),否則會導致資料遺失與記憶體不一致。

解題方法

  1. 硬體物理極限分析 (A):評估晶體管電路延遲、供給電壓與建立時間(Setup Time)之關係,判斷高頻低壓運行的可行性。
  2. 動態功耗計算 (B):利用 Pactive∝V2⋅fP_{\text{active}} \propto V^2 \cdot f 正比關係,代入電壓變化數值推導降壓後的功耗。
  3. 系統效能瓶頸分析 (C):比較 Compute-bound 與 Memory-bound 程式在 CPU 降頻時的效能變化差異。
  4. 功耗控制機制釐清 (D):區分動態功耗與靜態漏電功耗的產生原因及其對應的省電機制(DVFS vs. Power Gating)。
  5. 快取運作機制評估 (E):分析快取寫回(Write-back)策略與系統總能量消耗(Race-to-sleep 原則)。

選項分析

  • (A) 錯誤
    CMOS 電路的邏輯閘延遲與供給電壓 VV 成反比。若降低供給電壓 VV,晶體管導通速度變慢、訊號傳播延遲增加,會降低處理器所能承受的最大時脈頻率 fmaxf_{\text{max}}。若在降低電壓的同時強制提高時脈頻率,會引發建立時間違規(Setup Time Violation),導致資料採樣錯誤而使系統崩潰。

  • (B) 正確
    主動功耗(Active Power)即動態功耗,公式為 Pactive∝V2⋅fP_{\text{active}} \propto V^2 \cdot f。在題目未提及頻率改變的情況下,假設時脈頻率 ff 保持不變,功耗與電壓平方成正比:
    P2=P1×(V2V1)2P_2 = P_1 \times \left(\frac{V_2}{V_1}\right)^2
    代入已知數值 P1=10 WP_1 = 10\text{ W}、V1=1.3 VV_1 = 1.3\text{ V}、V2=1.1 VV_2 = 1.1\text{ V}:
    P2=10×(1.11.3)2=10×1.211.69≈7.1598 W≈7.2 WP_2 = 10 \times \left(\frac{1.1}{1.3}\right)^2 = 10 \times \frac{1.21}{1.69} \approx 7.1598\text{ W} \approx 7.2\text{ W}
    計算結果經四捨五入後精確符合 7.2 W7.2\text{ W}。

  • (C) 正確
    當程式屬於 Memory-bound 時,處理器大部分時間處於等待主記憶體(DRAM)回應的停頓狀態(Stall)。整體執行時間的瓶頸在於記憶體匯流排與 DRAM 的存取延遲。

🔒

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

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

免費註冊

第 2. 題5 分

[5 points] In this question, we examine how the pipeline affects the clock cycle time of the processor. Assume that individual stages of the datapath have the following latencies:
🖼️【此處有附圖,請對照原卷】
Also, assume that instructions executed by the processor are broken down as the following:

Instruction TypePercentage
ALU/Logic40%
Jump/Branch15%
Load25%
Store20%
Which of the following statements is/are true?
(A) On a non-pipeline datapath, the store instruction needs 2500 ps to complete.
(B) The total latency for a load instruction is 1.4X higher on a pipelined datapath.
(C) Assuming there are no stalls or hazards, the utilization of the data memory is below 50%.
(D) Assuming there are no stalls or hazards, for the given instruction mix, it is possible for a pipelined datapath to provide 3X throughput over a non-pipelined datapath.
(E) Pipelining does not only improve throughput, but it also helps save the power consumption for embedded processors.
🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 1 頁

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

這一題的完整詳解

核心觀念

單週期處理器中,時脈週期由最長指令路徑決定,所有指令都在一個週期內完成。管線處理器的時脈週期則由最慢的管線階段決定;理想情況下,管線填滿後每個週期可完成一條指令。

因此,單一指令的延遲要看它經過幾個週期,處理器的吞吐量則看穩定狀態下每個週期能完成幾條指令。

解題方法

原卷圖中的階段延遲為 IF=500 ps、ID=700 ps、EX=300 ps、MEM=600 ps、WB=400 ps;指令比例為 ALU/Logic 40%、Jump/Branch 15%、Load 25%、Store 20%。

單週期處理器的週期時間是五個階段延遲總和:

Tsingle=500+700+300+600+400=2500 psT_{\text{single}}=500+700+300+600+400=2500\text{ ps}

理想管線處理器的週期時間是最慢階段的延遲:

Tpipeline=max⁡(500,700,300,600,400)=700 psT_{\text{pipeline}}=\max(500,700,300,600,400)=700\text{ ps}

Load 會經過五個階段,因此管線處理器完成一條 Load 需要五個管線週期:

Lload,pipeline=5×700=3500 psL_{\text{load,pipeline}}=5\times700=3500\text{ ps}

選項分析

(A) 正確。 單週期處理器的全域時脈週期由最長指令路徑決定,為 2500 ps。Store 雖然不需 WB,使用的階段延遲合計為 500+700+300+600=2100500+700+300+600=2100 ps,但仍在單一的 2500 ps 週期內完成。

(B) 正確。 單週期處理器的 Load 延遲為 2500 ps;管線處理器的 Load 延遲為 3500 ps。因此:

35002500=1.4\frac{3500}{2500}=1.4

也就是管線處理器的 Load 延遲是單週期處理器的 1.4 倍,亦即增加 40%。

🔒

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

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

免費註冊

第 3. 題5 分

[5 points] At the age of AI and big data, memory hierarchy has become more and more important. Which of the following statements is/are true?
(A) Programmers can ignore memory hierarchies in writing code because modern compilers can optimize the data accesses to reduce cache misses.
(B) The three Cs model is often used for understanding the behavior of memory hierarchies. In this model, all cache misses are classified into one of three categories: compulsory misses, capacity misses, and coherence misses.
(C) In a write-through cache, the modified block is written to the lower level of the hierarchy only when it is replaced.
(D) To support virtual memory, a cache can be virtually indexed and virtually tagged so that the cache can search for the data before the TLB translates the virtual address into a physical address.
(E) In a cache-coherent multiprocessor, the protocols to maintain coherence for multiple processors are called cache coherence protocols. With a write invalidate protocol, it invalidates copies in other caches on a write, so it is possible that a shared-memory multithreaded program generates more cache misses when it executes on a multiprocessor.

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

這一題的完整詳解

核心觀念

本題旨在考驗對記憶體階層(Memory Hierarchy)​相關機制的綜合理解,涉及電腦結構中的四大重點議題:

  1. 快取友善程式設計與區域性(Locality):程式員在撰寫高效能程式時,即使有現代編譯器輔助,仍需主動考慮時間區域性(Temporal Locality)與空間區域性(Spatial Locality)。
  2. 快取缺失分析模型(3C Model):由 Mark Hill 提出的經典 3C 模型,將單處理器系統中的快取缺失劃分為:
    • 強制性缺失(Compulsory miss):又稱冷啟動缺失(Cold-start miss),首次存取該記憶體區塊時發生的缺失。
    • 容量缺失(Capacity miss):因快取容量不足以容納程式執行所需的所有資料而發生的缺失。
    • 衝突缺失(Conflict miss):在直接對映(Direct-mapped)或組相聯(Set-associative)快取中,因多個區塊競爭同一個快取組(Set)而發生的缺失。
    • 註:在多處理器環境中引入的快取無效化缺失,則被稱為第四個 C——一致性缺失(Coherence miss)。
  3. 快取寫入策略(Write Strategy):
    • 直寫式(Write-through):每次寫入快取時,同步寫入下層記憶體。
    • 寫回式(Write-back):寫入時僅更新快取並標記為髒位元(Dirty bit),直到該區塊被替換(Replace/Evict)時才寫回下層記憶體。
  4. 虛擬記憶體與快取的定址架構:
    • VIVT(Virtually Indexed, Virtually Tagged):快取的索引與標籤皆使用虛擬位址,存取快取時完全不需要等待 TLB(Translation Lookaside Buffer)的位址轉譯。
  5. 多處理器快取一致性(Cache Coherence):
    • 在寫無效協定(Write Invalidate Protocol)下,當一個核心寫入共享區塊時,會發出 Invalid 訊號使其他核心快取中的副本失效,後續其他核心再次讀取時便會引發一致性缺失(Coherence miss),甚至產生偽共享(False sharing)​現象,導致快取缺失次數顯著增加。

解題方法

此題為多項選擇題,切入點為逐項對照電腦結構教科書(如 Patterson & Hennessy 的 Computer Organization and Design)中的標準定義與理論機制,比對各語句的觀念是否正確:

  1. 檢視編譯器優化的極限與程式員的責任。
  2. 比對 3C 模型(Three Cs Model)官方標準的三種分類名稱。
  3. 區分 Write-through 與 Write-back 的寫入行為差異。
  4. 驗證 VIVT 快取架構的運作原理與避開 TLB 延遲的優點。
  5. 分析 Write Invalidate 協定對多執行緒共享記憶體程式在多處理器上執行的影響。

選項分析

  • (A) 錯誤
    雖然現代編譯器具備一定程度的循環優化能力(例如 Loop Interchange、Loop Tiling/Blocking),但編譯器無法自動修正所有資料結構的記憶體佈局或演算法架構。在 AI 與巨量資料處理中,資料量遠大於快取容量,程式員仍必須瞭解階層式記憶體架構並撰寫快取友善(Cache-conscious)的程式碼(如連續記憶體存取、維護區域性),才能大幅降低 Cache Miss。

  • (B) 錯誤
    經典的 3C 模型(Three Cs Model)所定義的三種缺失類型為:Compulsory miss(強制性缺失)、Capacity miss(容量缺失) 與 Conflict miss(衝突缺失)。選項將 Conflict miss 誤寫為 Coherence miss。Coherence miss(一致性缺失)屬於多處理器環境下延伸擴充的「4C 模型」中的第四個 C,非經典 3C 模型的定義。

  • (C) 錯誤
    選項敘述的是寫回式快取(Write-back cache)​的行為。在直寫式快取(Write-through cache)​中,當寫入指令修改快取內的區塊時,該修改會立即同步寫入至下層記憶體(Lower level hierarchy),而非等到被替換時才寫入。

🔒

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

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

免費註冊

第 4. 題5 分

[5 points] All the results in the article, the use of low-precision calculation is critical to the transformer engine. The article mentions FP8, but there are two FP8 formats that are in use today. One is called E4M3, as it contains 4 exponent bits and 3 mantissa bits, and the other is called E5M2, which contains 5 exponent bits and 2 mantissa bits. Which of the following statements is/are true?
(A) The maximum value that can be represented by E4M3 is larger than E5M2.
(B) The minimum number of E4M3 is smaller than E5M2, in terms of absolute value.
(C) The maximum value that can be represented with E4M3 is larger than an unsigned 8-bit integer.
(D) The maximum value that can be represented with E5M2 is larger than an unsigned 16-bit integer.
(E) The entire transformer network can be calculated with FP8 to save the memory space and accelerate the execution.

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

這一題的完整詳解

核心觀念

本題考驗深度學習加速領域中 FP8(8-bit Floating Point)浮點數格式(包含 E4M3 與 E5M2)的規格、最大數值、最小絕對值推導,以及 Transformer Engine 採用 FP8 進行混合精度訓練(Mixed-Precision Training)的核心觀念。

浮點數表示法的基本公式為:
V=(−1)S×(1+M)×2E−BV = (-1)^S \times (1 + M) \times 2^{E - B}

其中:

  • SS 為符號位元(Sign bit)。
  • EE 為偏置指數(Biased Exponent),若指數位元數為 kk,偏置量(Bias)值為 B=2k−1−1B = 2^{k-1} - 1。
  • MM 為尾數(Mantissa / Fraction)。
  • 非規格化數(Subnormal number)時,E=0E = 0,公式調整為 V=(−1)S×(0+M)×21−BV = (-1)^S \times (0 + M) \times 2^{1 - B}。

解題方法

1. E4M3 與 E5M2 規格分析

  • E4M3 格式(1 位元符號 + 4 位元指數 + 3 位元尾數):

    • 指數偏置量 B=24−1−1=7B = 2^{4-1} - 1 = 7。
    • 最大有限數值(Max Value):在 OCP / NVIDIA FP8 標準中,E4M3 為了提高動態範圍不保留 ±∞\pm\infty,僅將 E=11112E=\text{1111}_2 且 M=1112M=\text{111}_2 留作 NaN。其最大數值發生於 E=11112(15)E=\text{1111}_2 (15) 且 M=1102M=\text{110}_2:
      Max(E4M3)=(1+68)×215−7=1.75×28=448\text{Max}(E4M3) = \left(1 + \frac{6}{8}\right) \times 2^{15 - 7} = 1.75 \times 2^8 = 448
      (若依傳統 IEEE 754 規範保留 E=15E=15 給 NaN/∞\infty,最大指數取 1414 且 M=1112M=\text{111}_2,最大值為 (1+7/8)×214−7=1.875×128=240(1 + 7/8) \times 2^{14-7} = 1.875 \times 128 = 240)。
    • 最小正絕對值(Min Magnitude):發生於最小非規格化數(E=0E = 0 且 M=0012M = \text{001}_2):
      Min>0(E4M3)=(0+18)×21−7=2−3×2−6=2−9=1512≈0.001953\text{Min}_{>0}(E4M3) = \left(0 + \frac{1}{8}\right) \times 2^{1 - 7} = 2^{-3} \times 2^{-6} = 2^{-9} = \frac{1}{512} \approx 0.001953
  • E5M2 格式(1 位元符號 + 5 位元指數 + 2 位元尾數):

    • 指數偏置量 B=25−1−1=15B = 2^{5-1} - 1 = 15。
    • 最大有限數值(Max Value):遵守傳統 IEEE 754 規範,E=111112(31)E=\text{11111}_2 (31) 保留給 ∞\infty 與 NaN。最大有限數發生於 E=111102(30)E=\text{11110}_2 (30) 且 M=112M=\text{11}_2:
      Max(E5M2)=(1+34)×230−15=1.75×215=57344\text{Max}(E5M2) = \left(1 + \frac{3}{4}\right) \times 2^{30 - 15} = 1.75 \times 2^{15} = 57344
    • 最小正絕對值(Min Magnitude):發生於最小非規格化數(E=0E = 0 且 M=012M = \text{01}_2):
      Min>0(E5M2)=(0+14)×21−15=2−2×2−14=2−16=165536≈0.00001526\text{Min}_{>0}(E5M2) = \left(0 + \frac{1}{4}\right) \times 2^{1 - 15} = 2^{-2} \times 2^{-14} = 2^{-16} = \frac{1}{65536} \approx 0.00001526

選項分析

  • (A) 錯誤

    • E4M3 的最大值為 448448。
    • E5M2 的最大值為 5734457344。
    • 因此 E4M3 可表示的最大值小於 E5M2。
  • (B) 錯誤

    • E4M3 最小非零絕對值為 2−9=15122^{-9} = \frac{1}{512}。
    • E5M2 最小非零絕對值為 2−16=1655362^{-16} = \frac{1}{65536}。
    • 2−9>2−162^{-9} > 2^{-16},故 E4M3 的絕對值最小值大於 E5M2。
  • (C) 正確

    • 無符號 8 位元整數(unsigned 8-bit integer, uint8)的最大值為 28−1=2552^8 - 1 = 255。
    • E4M3 的最大值為 448448。
🔒

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

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

免費註冊

第 5. 題5 分

[5 points] Training GPT-3 model was no easy task, as a complete training of the full 175-billion parameter network with an entire training dataset could take weeks and cost millions of dollars. According to the article, which of the following statements is/are true?
(A) All the parameters of GPT-3 can be placed on the memory of H100 GPU when most of the parameters are represented with FP8.
(B) One can use the LLM benchmark results to estimate the full training time and decide which solution would train GPT-3 faster.
(C) The LLM benchmark likelihood tests are a variation of linked lists, where the links are embedded in the item structure that's being linked.
(D) Based on the reported results, parallel computing is critical to speed up the training time.
(E) Based on the reported results, the efficiency of training is improved when the number of accelerators is increased from 256 to 512.

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

這一題的完整詳解

參考書等級:大學部教材/期刊綜述

  • 175175 B 參數若以 FP8 (1 Byte) 表示,所需記憶體約為 175175 GB,超過單顆 H100(80 GB),故 (A) 錯。
  • LLM 基準測試提供每顆加速卡的吞吐量 (tokens / sec)。以此乘以加速卡數目與總訓練步數,可估算完整訓練時間,進而比較不同配置的效率,故 (B) 為真。
  • 基準的似然測試是對模型輸出機率的評估,與鏈結串列結構無關,故 (C) 錯。
🔒

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

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

免費註冊

第 6. 題5 分

[5 points] The MLPerf benchmark suite contain 8 benchmarks related to machine learning, and the article presents a figure to show the benchmark results. Note that the x-axis in the figure represents the execution time. The GPT-3 LLM benchmark results show that, on a per-chip basis, H100 systems were much faster than Gaudi2, but the mixed precision capability has not yet been enabled on Gaudi2. Recently, Intel submitted new MLPerf results in November 2023 and Gaudi2 demonstrated a significant 2x performance leap, with the implementation of the FP8 data type on the GPT-3 training benchmark. According to article and the recently result, which of the following statements is/are true?
(A) GPU is always faster than CPU and the other accelerators for running the MLPerf benchmarks.
(B) Based on the recent result, with mixed precision capability, Gaudi2 is faster than H100 on per-chip basis.
(C) Most of the results are benchmarked on GPUs.
(D) Many of the results are running on chips manufactured by Taiwan Semiconductors Manufacturing Co.'s 5-nanometer process.
(E) Dataset is important to machine learning, and it can be hard to build a representative benchmark because of the risk in violating data privacy.

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

這一題的完整詳解

核心觀念

本題考查機器學習基準測試套件(MLPerf Benchmark Suite)的設計原則、AI 硬體加速器(GPU、TPU、專用加速器等)的性能表現評估、半導體先進製程發展,以及機器學習資料集建立過程中所面臨的資料隱私(Data Privacy)挑戰。

  1. MLPerf 基準測試套件:由 MLCommons 推出的機器學習標準基準測試,用於衡量 ML 訓練(Training)與推論(Inference)在各式硬體平台上的執行時間(Execution Time)與效能。
  2. 混合精度(Mixed Precision)與 FP8 Data Type:在大型語言模型(LLM,如 GPT-3)訓練中,啟用低精度資料型態(如 FP8、BF16)可顯著減少記憶體頻寬需求與運算延遲,進而提升運算效能。
  3. AI 晶片與半導體製程:當前主流 MLPerf 測試硬體(如 NVIDIA H100 等)高度依賴台積電(TSMC)的 5 奈米家族(如 TSMC 4N)先進製程。
  4. 資料集與隱私權規範:建立具代表性且符合真實場景的 ML Benchmark 需要廣泛的真實資料,但常受限於個人資料保護法規(如 GDPR)與商業隱私風險。

解題方法

依據題幹所述背景資訊、MLPerf 官方公佈的效能數據(特別是 2023 年 11 月 Intel Gaudi2 啟用 FP8 精度後的 GPT-3 Benchmark 結果)與電腦結構中關於基準測試設計與晶片製造的常識進行邏輯推導:

  1. 比較加速器性能:分析絕對語氣選項(如「GPU 永遠最快」或「Gaudi2 啟用 FP8 後超越 H100」)的真實性。
  2. 分析 MLPerf 提交結構與製造技術:評估現有提交結果的硬體架構分布(GPU 為主)與晶片製程技術(台積電 5 奈米製程)。
  3. 評估 Benchmark 建立的現實限制:探討機器學習資料集收集時隱私權對建立標準化 Benchmark 的影響。

選項分析

  • (A) 錯誤

    • 說明:GPU 並非在所有 MLPerf 測試項目中都「絕對(always)」優於 CPU 或其他專用加速器(如 Google TPU、AWS Trainium 等 ASIC)。特定專用加速器(ASIC/TPU)或針對特定延遲敏感型與小批次推論優化的硬體,在部分測試中能展現出優於通用 GPU 的效能或能源效率。「always」一詞過於絕對,不符合電腦結構與效能評估的實務結果。
  • (B) 錯誤

    • 說明:雖然 Intel 在 2023 年 11 月向 MLPerf 提交的最新結果中,Gaudi2 藉由導入 FP8 資料型態實現了約 2×2\times 的顯著效能飛躍,但 NVIDIA H100 在單晶片(per-chip basis)基準下,同樣啟用 FP8 與 Transformer Engine 時的執行速度依然快於 Gaudi2。Gaudi2 雖縮小了差距並展現優異的性價比,但在單晶片絕對效能上仍未超越 H100。
🔒

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

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

免費註冊

第 7. 題10 分

[10 points] A group of researchers at Massachusetts Institute of Technology developed a LLM called TinyChat to run on weak, power-constrained edge devices (https://hanlab.mit.edu/blog/tinychat). First, they profiled the execution time of TinyChat in the target application scenario, to understand the workload. The LLM workload was broken down into two stages, Context stage and Generation Stage, and the profile revealed that the Generation Stage dominates the execution time, as Generation occupies 310ms of the total execution time (340ms). Furthermore, they used arithmetic intensity and roofline model to characterize the workload, and the figure below reported the arithmetic intensity for the two stages.
🖼️【此處有附圖,請對照原卷】
Suppose we are designing a processor chip and fine-tune the LLM for a similar application, which of the following statements is/are true?
(A) We should include more arithmetic units in the chip design as it will increase the peak throughput and effectively accelerate the LLM.
(B) Because the performance of the LLM is mostly memory-bound, increasing the memory bandwidth should improve the performance.
(C) Increasing the clock rate for the processor should improve the power efficiency in terms of throughput per watt.
(D) Improving the branch predictor should greatly increase the performance because better branch prediction can reduce the control hazards.
(E) Suppose the original LLM uses FP16 to store the parameters, compressing the model to use FP8 parameters should accelerate the LLM by approximately 2 times.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁原卷第 6 頁

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

這一題的完整詳解

核心觀念

本題考查大型語言模型(LLM)推論工作負載特徵、Roofline 模型(Roofline Model)、運算強度(Arithmetic Intensity)、記憶體頻寬限制(Memory-Bound)​與處理器功耗效率。

  1. LLM 推論的兩階段特徵:
    • Context Stage(Prefill 階段):同時平行處理輸入 prompt 的所有 token,屬於矩陣-矩陣乘法(GEMM),具有高運算強度,通常落在 Roofline 模型的運算受限區(Compute-Bound)。
    • Generation Stage(Decoding / Autoregressive 階段):每次只生成一個 token,需要將整份模型權重從記憶體搬移至處理器中進行矩陣-向量乘法(GEMV)。在 Batch Size 很小(如邊緣裝置 TinyChat)時,運算強度極低(約 1∼4 FLOPs/Byte1\sim 4\text{ FLOPs/Byte}),深深落在 Roofline 模型的記憶體頻寬受限區(Memory-Bound)。
  2. Roofline 模型公式:
    Attainable Performance=min⁡(Peak Performance,Arithmetic Intensity×Memory Bandwidth)\text{Attainable Performance} = \min\left(\text{Peak Performance}, \text{Arithmetic Intensity} \times \text{Memory Bandwidth}\right)
    當系統處於記憶體受限斜率段時,效能與「記憶體頻寬」以及「運算強度(FLOPsBytes\frac{\text{FLOPs}}{\text{Bytes}})」成正比;增加運算單元(提高 Peak Performance 天花板)無法提升效能。
  3. 功耗效率(Power Efficiency):
    Throughput per Watt=ThroughputPower\text{Throughput per Watt} = \frac{\text{Throughput}}{\text{Power}}
    CMOS 電路動態功耗 P∝CV2fP \propto C V^2 f。提高時脈頻率 ff 往往需要同步提高工作電壓 VV,使功耗以二次方甚至三次方急遽上升;且在記憶體受限的架構下,提升運算核心時脈並無法等比例提升吞吐量,導致功耗效率下降。

解題方法

從題目與原卷附圖讀取之關鍵圖表資訊:

  • 圓餅圖(Execution Time Breakdown):總執行時間為 340 ms340\text{ ms},其中 Generation 階段佔據 310 ms310\text{ ms}(高達約 91.2%91.2\%),Context 階段僅佔約 30 ms30\text{ ms}(處理 200 tokens 耗時極短,附圖顯示為 10∼30 ms10\sim 30\text{ ms})。工作負載絕對由 Generation 階段主導。
  • Roofline 模型圖:
    • 橫軸為 Arithmetic Intensity(單位:FLOPs/Byte,刻度標記 0, 75, 150, 225, 300)。
    • 縱軸為 Peak Throughput(單位:TFLOPS,刻度標記 0, 36, 72, 108, 144, 180)。
    • 傾斜邊界代表記憶體頻寬上限,平坦天花板約在 155 TFLOPS155\text{ TFLOPS} 處。
    • Generation Stage 的運算強度:W16A16 精度下 Arithmetic Intensity=1 FLOP/Byte\text{Arithmetic Intensity} = 1\text{ FLOP/Byte}(吞吐量僅 1 TFLOPS1\text{ TFLOPS});W4A16 精度下 Arithmetic Intensity=4 FLOPs/Byte\text{Arithmetic Intensity} = 4\text{ FLOPs/Byte}(吞吐量為 4 TFLOPS4\text{ TFLOPS}),兩者均遠低於轉折點(Ridge Point 約 165 FLOPs/Byte165\text{ FLOPs/Byte}),處於純記憶體頻寬受限區。
    • Context Stage 的運算強度:Arithmetic Intensity≥165 FLOPs/Byte\text{Arithmetic Intensity} \ge 165\text{ FLOPs/Byte},已抵達平坦的算力天花板區。

依據此負載瓶頸切入,逐一檢驗各選項在硬體改進與量化壓縮下的效能與功耗表現。


選項分析

  • (A) 錯誤:
    Generation 階段佔總執行時間的 91.2%91.2\%,且運算強度僅為 1 FLOP/Byte1\text{ FLOP/Byte},落於 Roofline 模型的斜坡區(Memory-Bound)。增加晶片上的運算單元(Arithmetic Units)僅能拉高水平方向的峰值算力天花板(Peak Compute Throughput),對處於斜坡區的 Generation 階段完全無加速效果;即使能微幅加速僅佔不到 10%10\% 時間的 Context 階段,根據阿姆達爾定律(Amdahl's Law),對整體 LLM 的效能提升亦微乎其微。

  • (B) 正確:
    在 Roofline 模型的記憶體受限區域內,運算效能由公式 Performance=Arithmetic Intensity×Bandwidth\text{Performance} = \text{Arithmetic Intensity} \times \text{Bandwidth} 決定。由於 LLM 的執行時間絕大部分(310 ms/340 ms310\text{ ms} / 340\text{ ms})受限於記憶體搬移,提高記憶體頻寬(Memory Bandwidth)會使 Roofline 模型的斜率變陡,直接等比例提升 Generation 階段的吞吐量,顯著改善 LLM 整體效能。

  • (C) 錯誤:
    功耗效率定義為每瓦吞吐量(Throughput per Watt\text{Throughput per Watt}),即每運算單位所需能量的倒數。

🔒

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

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

免費註冊

第 8. 題10 分

[10 points] Suppose we are designing a computing system that aims to train LLMs with multi-trillion of parameters. For that, let us review the latest system architecture design. NVIDIA recently announced DGX GH200 AI Supercomputer. According to its specifications (shown below), it is capable of 128 petaFLOPS of FP8 AI performance with 32 GH200 superchips and 19.5 TB of shared memory.
🖼️【此處有附圖,請對照原卷】
For simplicity, assume that our target LLM uses an FP8 to represent each parameter, most of the calculations are matrix-vector multiplications performed in FP8, and the arithmetic intensity is 1 FLOPs/Byte for FP8. Suppose we use a DGX GH200 to train the LLM, which of the following statements is/are true?
(A) If we only use the GPU memory to store the input data and parameters, the total GPU memory capacity, 144GB x 32 = 4.6TB, should be enough to run a LLM with 1 trillion parameters, but the performance is limited to 32 x 4900 GB/s * 1 FLOPS = 5 petaFLOPS, which is only 40% of the peak performance of the system.
(B) It is possible to accelerate the training process for a 1-trillion parameter LLM with 2 sets of DGX GH200, but the scalability should be poor, according to the IEEE Spectrum article shown in a previous question.
(C) Because the CPU memory is 3~4 times larger than the GPU memory, if we modify our software to utilize the unified memory feature on each GH200 node, we can train a much larger LLM at the same speed.
(D) If we cannot afford to buy a DGX GH200, we can still try to train a 1-trillion parameter LLM with a high-performance storage on one GH200 chip as a poor-man's solution. We can use a RAID-class disk array to take advantage of the high-speed I/O bandwidth (256GB/s in each direction is not far from the 500GB/s CPU memory bandwidth) and optimize the software to minimize the performance degradation.
(E) Speaking of software optimization, cache caching is important to accelerate large-scale matrix-vector multiplications, and the same concept can be used to here. The principle behind cache blocking is to increase the cache block size to reduce the number of cache misses.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 6 頁原卷第 7 頁原卷第 8 頁

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

這一題的完整詳解

核心觀念

本題考「Roofline model」與大模型訓練的記憶體容量、頻寬和分散式擴充性。Roofline 模型用算術強度估計記憶體頻寬所能支撐的運算吞吐量:

可達吞吐量≤記憶體頻寬×算術強度\text{可達吞吐量} \leq \text{記憶體頻寬} \times \text{算術強度}

算術強度是每搬移一 Byte 資料所執行的 FLOPs 數。當工作負載受記憶體頻寬限制時,增加運算單元不一定能提升效能。

解題方法

原卷規格表列出 DGX GH200 有 32 顆 GH200、128 PFLOPS 的 FP8 AI 效能及 19.5 TB 共用記憶體;架構圖則列出每顆 GH200 有 144 GB GPU 記憶體、最高 4.9 TB/s 頻寬,以及 480 GB CPU 記憶體。題目另給 FP8 算術強度為 1 FLOP/Byte。這些數字可直接用來檢查選項的容量與吞吐量計算。

每個參數以 FP8 儲存時約占 1 Byte,因此 1 兆個參數約需 1 TB 儲存空間。若只計參數,32 顆 GPU 的容量為:

144 GB×32=4608 GB≈4.6 TB144\ \text{GB} \times 32 = 4608\ \text{GB} \approx 4.6\ \text{TB}

但頻寬所能支撐的吞吐量為:

4.9 TB/s×32×1 FLOP/Byte=156.8 TFLOPS=0.1568 PFLOPS4.9\ \text{TB/s} \times 32 \times 1\ \text{FLOP/Byte} = 156.8\ \text{TFLOPS} = 0.1568\ \text{PFLOPS}

這只有系統標示峰值 128 PFLOPS 的約 0.12%0.12\%,不是選項所稱的 5 PFLOPS 或 40%。

選項分析

(A) 錯誤。 4.6 TB 的 GPU 記憶體容量足以容納 1 兆個 FP8 參數,這部分的估算成立;但依題目給的算術強度計算,頻寬受限吞吐量是 0.1568 PFLOPS,不是 5 PFLOPS,因此後續的 40% 也不成立。此外,實際訓練還需考慮輸入資料、梯度、啟動值與訓練狀態等記憶體需求。

(B) 正確。 兩套 DGX GH200 可透過分散式訓練分攤模型與運算,增加可用資源並加速訓練;但跨系統的資料交換與同步會帶來通訊成本,所以效能不會隨系統數量等比例增加。IEEE Spectrum 的 GPT-3 基準測試報導也指出,這類訓練至少需要多個處理器,且實際測試使用了大量加速器,反映出平行化的必要性與系統規模需求。

🔒

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

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

免費註冊

第 9. 題20 分

[20 points] For each of the following statements, answer Yes if it is TRUE or brief why it is wrong.
A. (2 points) The less frames, the more page faults.
B. (2 points) The less frames, the more page fault rates.
C. (2 points) The smaller time quantum for scheduling, the longer average turnaround time.
D. (2 points) Firmware can be executed faster in ROM than in RAM.
E. (2 points) Working set model has to be supported by MMU.
F. (2 points) A safe state will not go to a deadlocked state.
G. (2 points) Context switch is usually supported in hardware instruction for performance.
H. (2 points) Section Semantics says the file is closed, the changes are visible only to new sessions.
I. (2 points) A real-time scheduler schedules tasks according to their real-time priorities.
J. (2 points) The two-phase locking protocol guarantees serializability and prevents deadlock.

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

這一題的完整詳解

核心觀念

本題涵蓋作業系統的虛擬記憶體、CPU 排程、死結、檔案一致性語意與即時系統,以及資料庫的並行控制。判斷是非時,關鍵在於區分:

  • 「通常如此」與「必然如此」。
  • 特定演算法的性質與所有演算法的性質。
  • 模型本身的定義與作業系統實作時可能使用的硬體輔助。
  • 協定保證的性質與協定未保證的性質。

解題方法

每一敘述先找出其隱含的「絕對化」條件。例如「frames 越少,page faults 越多」只有在具備堆疊性質的頁面置換演算法中才成立;若題目未指定演算法,便不能視為普遍真理。

死結與排程題則要區分「系統目前狀態」及「未來資源配置政策」。安全狀態代表存在一條可完成所有工作的安全序列,但不代表系統若任意分配資源,未來永遠不會進入死結。

選項分析

A. The less frames, the more page faults.

錯誤。

頁框數減少時,缺頁次數不一定增加。對具有堆疊性質的演算法,例如 Optimal 與 LRU,較多頁框包含較少頁框時的所有頁面,因此頁框增加不會增加缺頁次數。

但 FIFO 可能發生 Belady’s anomaly:頁框增加後,缺頁次數反而增加。因此,若未指定使用 LRU、Optimal 等堆疊演算法,不能斷言頁框越少就必然有越多缺頁。

B. The less frames, the more page fault rates.

錯誤。

缺頁率定義為:

page fault rate=page faultsmemory references\text{page fault rate} = \frac{\text{page faults}}{\text{memory references}}

若比較的是同一段記憶體參考字串,分母固定,缺頁率的變化方向與缺頁次數相同。由於 FIFO 等演算法可能出現 Belady’s anomaly,頁框較少時也不保證缺頁率必然較高。

C. The smaller time quantum for scheduling, the longer average turnaround time.

錯誤。

Round-Robin 的時間片 qq 變小,會增加 context switch 的頻率;若 context switch 有成本,確實常使平均 turnaround time 上升。但這不是必然結果。

例如系統只有一個行程時,不論 qq 多大,皆不需在行程間切換,turnaround time 不變。不同工作負載下,較小的時間片也可能使短工作較早完成,降低部分行程的 turnaround time。因此不能將此敘述視為普遍定律。

D. Firmware can be executed faster in ROM than in RAM.

錯誤。

ROM 的主要用途是保存開機程式、韌體與不應輕易修改的內容,不是提供較快的執行速度。一般而言,RAM 的讀寫與執行速度通常快於 ROM。

實務上,韌體常先從 ROM 或 Flash 載入 RAM,再由 RAM 執行,以提升效能。

E. Working set model has to be supported by MMU.

錯誤。

Working set model 用來描述某行程在時間窗 Δ\Delta 內實際使用的頁面集合:

W(t,Δ)={在 (t−Δ,t) 期間被參考的頁面}W(t,\Delta) = \{\text{在 }(t-\Delta,t)\text{ 期間被參考的頁面}\}

其核心概念是由作業系統依據頁面參考歷史估計行程所需頁面數量,以避免 thrashing。MMU 可提供頁表、reference bit、dirty bit 等硬體資訊,使作業系統較容易近似 working set;但 working set model 本身不是必須由 MMU 直接實作或支援的機制。

F. A safe state will not go to a deadlocked state.

錯誤。

安全狀態表示系統存在一個安全序列,使所有行程都能依序取得所需資源並完成。例如行程序列:

P2→P1→P3P_2 \rightarrow P_1 \rightarrow P_3

若能使所有行程依此序完成,系統目前就是安全狀態。

然而,安全狀態不表示未來任意資源配置後都不會死結。只有 Banker’s algorithm 在每次資源請求時拒絕會使系統進入不安全狀態的配置,才能保證系統持續避免死結。

G. Context switch is usually supported in hardware instruction for performance.

錯誤。

Context switch 通常由作業系統核心執行。核心必須儲存目前行程的暫存器、程式計數器、堆疊指標與行程控制區塊資訊,再載入下一個行程的執行狀態。

硬體會協助處理中斷、trap、特權模式切換及部分暫存器保存,但完整的 context switch 通常不是單一硬體指令即可完成,因為還涉及排程決策、行程狀態更新與位址空間切換。

H. Session Semantics says the file is closed, the changes are visible only to new sessions.

正確。

🔒

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

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

免費註冊

第 10. 題5 分

  1. (5 points) When a mouse device is moved, a monitor reflects by redrawing the mouse icon in windows manager. What does the mouse driver (not ISR) do in this event? How is the driver called? How does the windows manager know to start redrawing?

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

這一題的完整詳解

核心觀念

本題考查作業系統中輸入輸出子系統(I/O Subsystem)、兩階段中斷處理架構(Two-Phase Interrupt Handling / Top Half & Bottom Half)、事件驅動架構(Event-Driven Architecture)​與圖形介面視窗管理員(Window Manager / Compositor)繪圖管線的協同運作機制。

整體資料傳遞與控制流程如下:

滑鼠硬體產生位移⟶硬體中斷 (IRQ)⟶ISR (Top Half)⟶排定延後處理 (Bottom Half / DPC)\text{滑鼠硬體產生位移} \longrightarrow \text{硬體中斷 (IRQ)} \longrightarrow \text{ISR (Top Half)} \longrightarrow \text{排定延後處理 (Bottom Half / DPC)} ⟶驅動程式解析封包並產生事件⟶核心系統輸入佇列⟶視窗管理員 (Window Manager)⟶更新座標並標記重繪\longrightarrow \text{驅動程式解析封包並產生事件} \longrightarrow \text{核心系統輸入佇列} \longrightarrow \text{視窗管理員 (Window Manager)} \longrightarrow \text{更新座標並標記重繪}

各關鍵元件的職責分工如下:

  1. ISR(Interrupt Service Routine / 上半部 Top Half):執行於硬體中斷環境(關閉或遮罩中斷),追求極致簡短。主要工作為確認/清除硬體中斷信號、讀取控制器資料暫存器(或確認 DMA 傳輸)至核心暫存緩衝區,並排定延後處理機制,隨即退出中斷環境。
  2. 裝置驅動程式(Device Driver / 下半部 Bottom Half / DPC):執行於軟體中斷或核心執行緒環境(中斷已重新開啟)。負責解析協定封包、計算座標與按鍵狀態、封裝為標準系統輸入事件(如 Linux 的 struct input_event 或 Windows 的 MOUSE_INPUT_DATA),並掛入系統輸入佇列。
  3. 視窗管理員(Window Manager / 顯示伺服器 Compositor):處於使用者空間或圖形子系統核心執行緒,維護事件迴圈(Event Loop)。監聽輸入佇列、取出事件後計算游標新座標,並將畫面中受影響的區域標記為「無效/髒污區域(Dirty Region)」,驅動畫面重繪。

問題深度剖析與推導

題目針對滑鼠移動後螢幕重繪游標的完整過程,提出三個核心問題:

1. 滑鼠驅動程式(非 ISR)在此事件中的工作(What does the mouse driver do?)

滑鼠移動時,硬體控制器會發出中斷,ISR 先行讀取原始位元組後,後續的實質驅動程式工作包含:

  • 協定解碼與資料解析(Packet Parsing):滑鼠傳送的原始資料通常為封裝好的資料封包(例如 PS/2 協定的 3 或 4 位元組封包,或 USB HID Report)。驅動程式負責依據通訊協定解析各個位元組,提取出相對位移量(Δx,Δy\Delta x, \Delta y)、滾輪滾動量以及各按鍵之點擊/釋放狀態。
  • 數據標準化與事件轉換(Event Normalization):將硬體特定的原始計數值轉換為作業系統標準化的輸入資料結構,並可在此階段套用驅動層級之滑鼠靈敏度、加速度(Mouse Acceleration/Ballistics)與座標軸映射。
  • 事件佇列發送與通知(Event Enqueuing & Notification):將建立好的標準輸入事件寫入作業系統核心的系統輸入佇列(System Input Queue),並透過信號、事件物件或條件變數喚醒(Wake Up)等待輸入訊息的視窗子系統或視窗管理員。

2. 驅動程式是如何被呼叫的?(How is the driver called?)

驅動程式絕不可能由滑鼠硬體直接呼叫,而是由作業系統的延後中斷處理機制(Split Interrupt / Deferred Execution)​所調度呼叫:

  1. 硬體中斷與 ISR 觸發:滑鼠移動產生電氣訊號,觸發 CPU 中斷線路。CPU 透過中斷向量表(IDT)跳轉執行滑鼠裝置的 ISR(Top Half)。
  2. ISR 排定延後程序:ISR 為了避免長時間遮罩中斷而影響系統即時性,僅執行必要之硬體暫存器存取與確認,接著呼叫核心 API 將後續處理常式加入延後排程:
    • 在 Windows 系統中:ISR 呼叫 KeInsertQueueDpc(),將驅動程式的 DPC(Deferred Procedure Call) 物件放入處理器的 DPC 佇列。
    • 在 Linux 系統中:ISR 標記並排定 Bottom Half 機制,例如發出軟體中斷(raise_softirq)、排定 tasklet,或是將處理函式掛入工作佇列(workqueue)或喚醒執行緒化中斷常式(Threaded IRQ Handler)。
  3. 核心排程器執行驅動程式:當硬體中斷處理完畢且 CPU 中斷重新啟用後,作業系統在返回使用者空間前或在適當的排程時機,於較低的優先權等級(例如 Windows 的 DISPATCH_LEVEL 或 Linux 的軟中斷/核心執行緒環境)調度並執行該佇列中的常式,從而呼叫滑鼠驅動程式的延後處理函式進行封包處理。
    (註:若為現代 USB 滑鼠,則由 USB 主控制器驅動程式於 USB 中斷傳輸完成時,透過非同步 I/O 完成回呼函式(Completion Callback)呼叫 HID 滑鼠驅動程式。)

3. 視窗管理員如何知道要開始重新繪製?(How does the windows manager know to start redrawing?)

🔒

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

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

免費註冊

第 11. 題10 分

(10 points) Intrusive linked lists are a variation of linked lists, where the links are embedded in the item structure that's being linked as follows. It is used in Linux kernel for better performance than traditional linked lists. Why? Hint: Consider memory related advantages.

typedef struct list {
  struct list *next;
} list;

typedef struct item {
  int val;  // data
  list links;
} item;

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

這一題的完整詳解

核心觀念解析

在作業系統(特別是 Linux 核心)與高效能系統軟體中,侵入式鏈結串列(Intrusive Linked List) 是一種將鏈結節點直接嵌入至資料結構內部的設計模式。相較於傳統鏈結串列(Traditional / Non-intrusive Linked List),其在計算機記憶體階層架構與動態記憶體管理上具有顯著的效能優勢。

1. 資料結構與記憶體配置對比

  • 傳統鏈結串列(Non-intrusive List):
    串列節點與資料主體分離,通常透過包裝節點內含指向資料的指標:

    struct traditional_node {
        void *data;                      // 指向實際資料物件的指標
        struct traditional_node *next;  // 指向下一個節點的指標
    };
    

    在將資料插入串列時,必須為包裝節點單獨進行一次動態記憶體配置。走訪時需要進行兩次指標解引用(先解引用節點指標,再解引用 data 指標)。

  • 侵入式鏈結串列(Intrusive List):
    鏈結節點直接作為成員內嵌於資料結構內部:

    typedef struct list {
        struct list *next;
    } list;
    
    typedef struct item {
        int val;      // data
        list links;   // 內嵌鏈結結構體
    } item;
    

    在建立資料物件 item 時,鏈結節點便已包含在同一個記憶體區塊中;插入串列僅需修改指標,完全不需要為節點額外配置記憶體。


記憶體相關的效能優勢(Memory-Related Advantages)

題目提示從記憶體維度切入,其關鍵優勢可歸納為以下四大方面:

1. 顯著提升快取空間區域性並降低 Cache Miss 與 TLB Miss

  • 空間區域性(Spatial Locality)最佳化:
    在侵入式鏈結串列中,鏈結欄位(links)與資料成員(val)同屬一個 item 實例,在記憶體位址上是物理連續的,通常座落於同一個 CPU 快取行(Cache Line,現代架構多為 64 Bytes)之內。當 CPU 走訪串列讀取 links.next 時,硬體的快取行填入與快取預取(Hardware Prefetching)會同時將相鄰的資料成員一併載入 L1/L2 快取中。
  • 消除二次指標解引用(Pointer Indirection):
    傳統串列存取資料必須先載入 traditional_node,再透過 data 指標存取遠端資料。由於節點與資料區塊是各自獨立配置的,二者在記憶體中通常相距甚遠,每次存取資料極易引發兩次獨立的快取缺失(Cache Miss)與 TLB Miss。侵入式串列直接在同區塊內運算,大幅縮短記憶體存取延遲與匯流排頻寬消耗。

2. 消除額外動態記憶體配置與堆積中介資料(Metadata)開銷

  • 零額外節點配置開銷(Zero Extra Allocation Overhead):
    傳統串列每將一個物件加入串列,就必須呼叫核心配置器(如 kmalloc、Slab Allocator)配置一次節點記憶體,造成顯著的執行時間開銷;而侵入式串列將欄位直接做在物件內,插入與移除操作純粹為指標賦值,完全不需呼叫記憶體配置器。
  • 減少記憶體浪費與碎裂化(Memory Fragmentation):
    每次動態記憶體配置都會附帶記憶體管理器的標頭中介資料(Metadata Header)以及位址對齊(Alignment Padding)。
🔒

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

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

免費註冊

第 12. 題15 分

[15 points] When a Round Robin scheduler implemented so that each task is preempted accurately every 1 millisecond and resume after indefinitely? (5 points) In the same RR scheduling, please write (pseudo) codes for a daemon (looping) task T with comments as succinct as possible so that T runs for 1 second, then blocked for another 1 second. (10 points) Hint: Asynchronous I/O might be needed.

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

這一題的完整詳解

核心觀念

本題考驗作業系統(Operating System)中輪轉調度法(Round Robin, RR Scheduling)​的搶佔機制、任務狀態轉移(Task State Transition),以及非同步 I/O(Asynchronous I/O)​如何配合實現特定時間間隔的執行與阻塞行為。

  1. 輪轉調度法(Round Robin Scheduling):

    • 屬搶佔式(Preemptive)調度演算法,系統定義一個固定的時間斷片(Time Quantum, 記為 QQ)。本題中 Q=1 msQ = 1\text{ ms}。
    • 搶佔條件:若任務屬於完全的 CPU 密集型(CPU-bound),即在 1 ms1\text{ ms} 時間斷片內未主動發起阻塞式系統呼叫(Blocking System Call)或讓出 CPU(Yield),則會在執行滿 1 ms1\text{ ms} 時精確地被計時中斷(Timer Interrupt)搶佔,並置於就緒佇列(Ready Queue)末端。
    • 恢復執行(Resume)週期:若就緒佇列中共有 NN 個 CPU-bound 任務,某任務被搶佔後,需等待其餘 N−1N-1 個任務各執行 1 ms1\text{ ms}(即等待時間為 (N−1)×1 ms(N-1) \times 1\text{ ms}),隨後回到佇列頭部並恢復執行。只要任務為無限迴圈且佇列中任務數 NN 不變,此「執行 1 ms→1\text{ ms} \rightarrow 被搶佔 →\rightarrow 等待 (N−1) ms→(N-1)\text{ ms} \rightarrow 恢復執行」的過程將無限期持續重複(Resume indefinitely)。
  2. 任務狀態轉移與非同步 I/O:

    • 任務在三態模型中切換:RUNNING(運行)、READY(就緒)、BLOCKED(阻塞)。
    • 傳統同步 I/O(Synchronous I/O):發起呼叫時會立刻讓任務轉入 BLOCKED 狀態,無法在 I/O 進行的同時進行 CPU 計算。
    • 非同步 I/O(Asynchronous I/O):發起呼叫(如 POSIX aio_read)後系統立即返回控制權,任務保持在 RUNNING/READY 狀態繼續執行 1 秒的 CPU 運算;待 1 秒運算結束後,任務再透過阻塞等待指令(如 aio_suspend)進入 BLOCKED 狀態 1 秒。

解題方法

第一部分:分析搶佔與無限期恢復執行的條件(5 points)

要使輪轉調度器精確地每 1 ms1\text{ ms} 搶佔任務,且任務能在搶佔後無限期恢復執行,必須滿足以下三個條件:

  1. 時間斷片設定:作業系統調度器的時間斷片設定為 Q=1 msQ = 1\text{ ms}。
  2. 任務行為屬性:任務必須為無限迴圈的 CPU 密集型任務(CPU-bound Task)。任務在 1 ms1\text{ ms} 的執行期間內絕不發起任何阻塞式 I/O、主動讓出(Yield)或終止,確保每次均由計時中斷觸發搶佔。
  3. 就緒佇列動態:就緒佇列(Ready Queue)中保持有固定數量 NN 個此類 CPU-bound 任務。當任務執行滿 1 ms1\text{ ms} 被搶佔移至佇列尾端後,經歷 (N−1)×1 ms(N-1) \times 1\text{ ms} 的等待時間即可再次被調度執行(Resume),且此循環因任務不終止而無限期重複。

第二部分:設計 Daemon Task T 的虛擬碼(10 points)

任務 TT 的目標為:執行 1 秒 →\rightarrow 阻塞 1 秒 →\rightarrow 無限循環。

  • 執行 1 秒階段:發起非同步 I/O 請求(不阻塞控制權),並透過時間檢查進行 1 秒的 CPU 密集型運算。在 RR 調度器作用下,這 1 秒內任務 TT 會被搶佔與調度數次(累積執行時間或經過時間達 1 秒)。
  • 阻塞 1 秒階段:1 秒計算完成後,呼叫阻塞指令(如 aio_suspend 或定時器阻塞)等待非同步 I/O / 定時器完成,使作業系統將任務 TT 的狀態轉為 BLOCKED,持續 1 秒不佔用 CPU 資源。

細節與選項分析

  1. 第一小題細節分析:

    • 為何必須強調 CPU-bound? 若任務在 1 ms1\text{ ms} 內發起了同步 I/O,任務會在未滿 1 ms1\text{ ms} 時即主動釋放 CPU 進入 BLOCKED 狀態,破壞「精確每 1 ms1\text{ ms} 被搶佔」的條件。
    • Resume 的精確時間推導:設就緒佇列中有 NN 個任務,忽略上下文切換(Context Switch)時間 SS。當前任務被搶佔後,前面有 N−1N-1 個任務排隊,每個任務消耗 1 ms1\text{ ms},因此恢復執行的等待時間為:
      Twait=(N−1)×1 msT_{\text{wait}} = (N-1) \times 1\text{ ms}
      若考慮上下文切換開銷 SS,則等待時間為 Twait=(N−1)×(1 ms+S)T_{\text{wait}} = (N-1) \times (1\text{ ms} + S)。
  2. 第二小題細節分析:

    • 提示 Asynchronous I/O 的作用:若使用傳統同步 I/O(read() / sleep()),任務呼叫的瞬間就會進入 BLOCKED 狀態,無法達成「先持續執行 1 秒,再阻塞 1 秒」的要求。
    • 使用非同步 I/O(例如 POSIX aio_read 與 aio_suspend)可以精確分離「非同步背景觸發」與「主動等待阻塞」兩個階段。

解題技巧

  1. 狀態轉移圖驗算:
    • 0 ~ 1 秒(Running/Ready 階段):TT 處於 RUNNING ↔\leftrightarrow READY 輪轉。在 Q=1 msQ = 1\text{ ms} 下,1 秒內被搶佔約 1000 次(若為單一任務)。
    • 1 ~ 2 秒(Blocked 階段):TT 呼叫 aio_suspend(),狀態轉為 BLOCKED,移出 Ready Queue,不參與 CPU 輪轉調度。
  2. 非同步 I/O 標準 Call 模式:
    • aio_read() / aio_write():非阻塞,立即返回 →\rightarrow 任務維持 RUNNING。
    • aio_suspend():阻塞直到 I/O 或指定時間完成 →\rightarrow 任務轉為 BLOCKED。

關鍵程式碼與複雜度分析

虛擬碼(Pseudocode)

🔒

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

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

免費註冊

其他考古題