# 計算機結構_Fundamental
###### tags: `計算機結構`
### 計算機類別
#### - Personal Mobile Device (PMD)
- Critical : Cost & Real-time performance
- 例如 : 手機,平板電腦...
#### - Desktop Computing
- Critical : Price-performance(性價比)
- Applications and benchmarking do well
#### - Server
- Critical : Dependability & Scalability(可擴展性) & Throughput
- 最具代表的Server : Supercomputer
#### - Clusters/ Warehouse-Sacle Computers
- Critical : Price-performance & Power(功率)
- 因為SaaS(軟體即服務)需求上升而變得重要
- 最大的Clusters稱為Warehouse-scale computers (WSCs)
#### - Embedded Computers
- 特性 : Real-time constraint, Low power, Code density
- 以能不能運做第三方軟體作為與PMD的最大差異
- 例如 : 洗衣機, 烘碗機...
- 運作方是 : SoC(system on chip),由軟體及DSP(訊號處理器)組成,
\
### Parallelism分類
平行度是計算的重要方式,主要會因為耗能及成本被限制。並行方式主要分為
1. Data-Level parallelism (DLP)
2. Task-Level parallelism (TLP)
且硬體可以用以下四種方式來運用parallelism
1. Instruction-Level parallelism
3. Vector Architectures and Graphic Processor Units(GPUs) : 單條指令平行運用於一個數據集
5. Thread-Level Parallelism
6. Request-Level Parallelism
\
### Flynn's 計算機分類
除了計算機的分類以外,有些多處理器也可以這樣分
1. Single instruction stream, single data stream (SISD)
2. Single instruction stream, multiple data streams(SIMD)
3. Multiple instruction streams, single data stream(MISD)
4. Multiple instruction streams, multiple data streams(MIMD)
\
# 指令集架構(Instruction Set Architecture)
### 架構組成
1. 指令集類型 : RISC, CISC
2. Memory addressing
3. Addressing modes
4. Types and sizes of operands : ARM & MIPS大小為8 bits...
5. Operations : 數據傳輸指令(lw,sw), 算術邏輯運算(add,sub), 控制指令(j)
6. Control flow instructions : 無/有條件跳躍...
7. Encoding on ISA : 指令長度
\
# 技術趨勢
因應電腦技術變化迅速,現在的電腦一定要有以下四個應用技術
1. IC logic technology : 因為晶體管密度集晶片大小上升速度不同,需要的技術更新
2. DRAM technology : 因為DRAM容量上升會碰到瓶頸
3. Magnetic disk technology
4. Network technology
### 性能趨勢 : 通常用Bandwidth and Latency來評估
- Bandwidth or throughput : 再給定時間完成的總工作量
- Latency or response time : 完成一件工作所需要的時間
通常bandwidth的進步會比latency多好幾倍
### Computer設計目標
- High-performance design -> e.g. supercomputers
- Low cost performance -> e.g. embeded products
- Cost-performance design -> e.g. PC, workstations, and servers
\
# 可信度(Dependability)
### 定義介紹
- Service accomplishment = 系統依照SLA提供服務
- Service interruption = 系統執行與SLA規定不一致
- Failure = 從acc.轉為interrupt
- Restoration = 從interrupt轉為acc.
### 可靠度計算公式
- Module reliability = 可靠度(維持完整服務的時間)
1. Mean Time To Failure (MTTF) = 正常運作的時間長度的平均
2. Failures In Time (FIT) = $1/MTTF$ = 計算失誤率(通常用每十億小時為單位)
3. Mean Time To Repair (MTTR) =平均修復的時間
4. Mean Time Between Failures (MTBF) = $MTTF+MTTR$ = 平均故障間隔
- Module availability = 可用性(遇到故障的時間內可以運作的時間比例)
1. Module availability = $\dfrac{MTTF}{MTTF + MTTR}$
\
# 效能計算
### 效能計算名詞
- Response time(=Execution time): 完成一份工作的時間
- Throughput : 一段時間完成的工作量
- Elapsed time(=Wall-clock time) : 完成一項工作的延遲
- CPU time : 處理器執行時間
- System performance
一般來說X比Y快n倍的意思是 $\dfrac{Exec. Time Y}{Exec. Time X}$ $=n=$ $\dfrac{Perforamnce X}{performance Y}$
### 測試效能的程式選擇
- Real applications : 直接應用
- Modified (or scripted) applications
- Kernels : 程式關鍵部分
- Toy benchmarks : 簡單測試
- Synthetic benchmark : 合成程式
- **SPEC** : 針對CPU性能的測試集
### 時間運算公式
- Arithmetic mean = $\dfrac{1}{n}$ $\displaystyle\sum_{i=1}^{n}{Time_i}$
- Weighted Execution Time = $\displaystyle\sum_{i=1}^{n}{Time_i\times Weight_i}$
- Geometric means = 
- Exec. time ratio = $\dfrac{Exec.\ Time\ of\ Basic}{Exec.\ Time \ of\ A}$
- 優點:
1. 幾何平均後取ratio = ratio後取幾何平均
2. Basic計算機要選甚麼都沒關係(相對計算)
3. 比起算術平均更不容易出錯
- 缺點:
1. 不能預測執行時間(都用比例下去算)
- 範例
以下數據皆為執行時間

\
# Quantitative Principles of Computer Design
以下可以去尋找最佳的優化方式/位置
- Make the common case fast
- Amdahl’s Law
- $Amdahl's\ law=\dfrac{1}{(1-Enhanced\ Fraction)+ \dfrac{Enhanced\ Fraction}{Speedup}}$
- $Speedup= \dfrac{Performance\ of\ new\ task}{Performace\ of\ old\ task}
=\dfrac{Exec.\ time\ of\ old\ task}{Exec.\ time\ of\ new\ task}$
- \begin{aligned}
Exec.\ time\ of\ new\ task =Exec.\ time\ of\ old\ task+((1-Enhanced\ Fraction)+ \dfrac{Enhanced\ Fraction}{Speedup})
\end{aligned}
- The CPU performance equation
- $CPI=\dfrac{\#\ of \ clock\ cycle}{\#\ of\ Instuction}$
- \begin{aligned}
CPU\ time &=\#\ of\ Clock\ cycles\times Clock\ cycle\ time=\dfrac{\#\ of\ Clock\ cycles}{Clock\ rate}\\
&=\#\ of\ Instruction\times Clock\ cycle\ time\times Cycles\ per\ instuction
\end{aligned}
- $\dfrac{Instuction}{Program}\times \dfrac{\#\ of\ Clock\ cycle}{Instruction} \times \dfrac{Second}{\#\ of\ Clock\ cycle}= \dfrac{Second}{Program}=CPU\ time$
- $\#\ of\ clock\ cycle=\sum_{i}{}IC\times CPI_i$
- $CPU\ time=\#\ of\ clock\ cycle \times Clock\ cycle\ time=(\sum_{i}{}IC\times CPI_i) \times Clock\ cycle\ time$
- Principle of locality : 優化程式碼最常執行的部分或是最常使用的資料區塊
- Temporal locality
- Spatial locality
- Take advantage of parallelism
- 通常是最有效的方法
- 最常用pipelining
- Use in digital design -> e.g. set-associative caches
- Use in ALUs carry-lookahead
# 謬論及錯誤觀念
### 謬論
- Multiprocessors are a silver bullet(萬能)
-> 多核只有提高平行處理能力, 還是要看程式平行化的處理
- Hardware enhancements that increase performance improve energy efficiency or are at worst energy neutral(硬體效能的提升一定會提高耗能效率<至少不會降低)
- Benchmarks remain valid indefinitely
-> 基準有可能會隨著時間變化
-> 工程師可能會只針對此基準下去做優化
-> 系統自動做出有利解讀(尤其在花費大量時間做少部分程式碼的kernel或program)
- The rated mean time to failure of disks is 1,200,000 hours or almost 140 years, so disks practically never fail(磁盤平均無故障時間約140年,表示有生之年決不會故障)
- Peak performance tracks observed performance
->峰值變化範圍大,且會受到benchmark影響,沒有甚麼觀察效果,除非program非常小
- Synthetic benchmarks predict performance for real programs
- MIPS is an accurate measure for comparing performance among computers(MIPS是拿來比較性能的指標之一)
-> $MIPS=\dfrac{\#\ of\ Instruction}{MIPS \times\ 10^6}$
-> 不同指令架構不能比較
-> 在同一台電腦上的MIPS不一定相同
-> MIPS通常表現得與效能相反
### 錯誤觀念
- Falling prey to Amdahl’s heartbreaking law
-> 要記得在優化前最好先進行測量
- A single point of failure
->透過Amdaul去確保有容錯空間/機制
- Fault detection can lower availability(故障檢測會降低可用性)