Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why_should_I_learn_OS_and_CA.pdf

William-Mou
September 04, 2022
200

 Why_should_I_learn_OS_and_CA.pdf

William-Mou

September 04, 2022
Tweet

Transcript

  1. 計算機 結構 SITCON 2022 William Mou 作業 系統 為什麼 要學

    教 練 我 想 打 Code 綜 觀 ⼤ 部 分 ⼤ 學 的 資 訊 ⼯ 程 學 系 除 了 了 ⼤ ⼀ 的 程 式 設 計 外 更 多 的 時 間 在 學 習 計 算 機 相 關 的 知 識 對 於 還 是 新 ⼿ 的 學 ⽣ 全 然 不 知 為 何 要 學 習 這 些 內 容 對 於 ⼀ 個 憧 憬 撰 寫 程 式 碼 以 開 發 軟 體 為 ⼰ 願 的 學 ⽣ 計 算 機 結 構 和 作 業 系 統 到 底 具 有 什 麼 樣 的 意 義 真 的 只 是 求 學 歷 程 上 的 背 科 ⽤ 來 換 取 GPA 嗎
  2. 嚴謹的實驗先講環境 Ubuntu 20.04 AMD EPYC 7742 64-Core Processor * 2

    L1d cache size: 2MiB ( 32 KiB / core) L1i cache size: 2MiB ( 32 KiB / core) L2 cache size: 32MiB (512 KiB / core) L3 cache size: 256MiB ( 4 MiB / core) capabilities: sse sse2 ht ssse3 fma cx16 sse4_1 sse4_2 avx avx2 DIMM DDR4 Synchronous Registered (Buffered) 
 3200 MHz (0.3 ns) g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
  3. 你的時間不是我的時間 real 0m0.184s user 0m0.120s sys 0m0.064s Wall Time ⼈的觀點


    程式開始時按下
 程式結束時放開 CPU 時間 程式的觀點
 程式在 CPU 內 跑了多少時間 說到計算 C++ 程式的時間,你想到哪些⽅式? • Linux 的 time 指令 • 使⽤ <time.h> 的 time() • 使⽤ <time.h> 的 clock() • 使⽤ <chrono> 的 high_resolution_clock()
  4. 考完試 還記得什麼? Computer Organization and Architecture is the study of

    internal working, structuring, and implementation of a computer system. Architecture in the computer system, same as anywhere else, refers to the externally visual attributes of the system. Organization of a computer system is the way of practical implementation that results in the realization of architectural specifications of a computer system. More Information 2022 Fahule Company
  5. Study with Three Case Branch Prediction Cache Locality Vector Processing

    浪費時間排序陣列, 程式執⾏卻變快了? 迴圈展開!先數 i 還 j 居然差這麼多? SIMD ⽤好 CPU 的全 部價值!
  6. Case 1 Branch Prediction 排序後的陣列 排序前的陣列 In computer architecture, a

    branch predictor is a digital circuit that tries to guess which way a branch (e.g., an if–then–else structure) will go before this is known definitively. 浪費時間排序陣列, 程式執⾏卻變快了?
  7. 找出陣列內⼤於 128 的數字
 並將其加總。 1. 先建立⼀個陣列,並填入亂數 2. 利⽤ std::sort 來排序這個陣列

    3. 利⽤ if 判斷是否符合條件,若是則加總 其中,第⼆步驟的「排序」與程式邏輯並無關 聯,我們嘗試看看移除與否對性能造成的影響
  8. 事先排序的影響 無事先排序 有事先排序 0 5.5 11 16.5 22 快 2.5

    倍! 單位(秒) 無事先排序 有事先排序 迴圈部分耗時 20.779 8.089 整⽀程式耗時 20.783 8.096
  9. Time 8 MOV EAX … F D X M W

    16 CMP EAX 127 F D X M W 24 JLE for F D NOP NOP NOP 32 MOV … F NOP NOP NOP NOP 40 MOV … F D X M W Order 來看 Code
  10. CPU ⾯臨的是⋯ • 如果 CPU 猜對了,就可以不 間斷地執⾏程式。
 • 如果 CPU

    猜錯了,那就要花 更多的時間,Rolling Back 回 頭重來。 CPU bet… 現代⼿握著操縱杆。當程式有 if 語句時,就會被迫 ⾯臨⼆選⼀的運氣⼤挑戰。
 cmp: 先比較誰⼤誰⼩,
 jle: 左邊 ≤ 127 則跳過 25 ⾏。
  11. Case1-without-sort.out 9839M branches 1615M branch-misses time: 20.83 second Case1.out 9836M

    branches 0.53M branch-misses time: 8.13 second 性能分析 $ perf stat Perf is a profiler tool for Linux 2.6+ based systems that abstracts away CPU hardware differences in Linux performance measurements and presents a simple commandline interface. Perf is based on the perf_events interface exported by recent versions of the Linux kernel.
  12. Case 1 Branch Prediction 為了避免分⽀預測帶來的額外開銷,在 for 迴圈 前加上⼀⾏排序,確實能⼤幅提升程式效能。 Case1 讓我們看到了計算機結構的知識,確實

    會對⾼階程式語⾔的性能,帶來相當顯著的影 響。簡單的⼀⾏程式碼,也能像是演算法⼀ 樣,改善你的程式效能。
  13. Case 2 Cache Locality I 在外⾯ J 在外⾯ Frequently used

    cases need to be faster: Programs often invest most of the time in a few core functions and these functions in return have most to do with the loops. So, these loops should be designed in a way that they possess a good locality. 迴圈展開! 先數 i 還 j 居然差這麼多?
  14. Data Commentary memory speed is becoming a bottleneck of the

    overall performance of a computer. 2022 SITCON 處理器(CPU)和內存(DRAM)之間的性能 差距越來越⼤。 這對今天的計算機來說是⼀個⼤問題:內存速 度正在成為計算機整體性能的瓶頸。 妥善地使⽤記憶體,逐漸成為更重要的⽬標。
  15. Locality Temporal locality 時間局部性表明相同的數據對像在程序執⾏期間很可能被 CPU 多次重⽤。⼀旦數據對像在第⼀次未命中時被寫入緩存,則可以 預期該對像上的許多後續命中。由於⾼速緩存比主存儲器等下⼀ 個較低級別的存儲更快,因此這些後續命中可以比原始未命中更 快地提供服務。 Spatial

    locality 它表明如果⼀個數據對像被引⽤⼀次,那麼它的相鄰數據對像很 可能在不久的將來也會被引⽤。內存塊通常包含多個數據對象。 由於空間局部性,我們可以預期在未命中後復制塊的成本將通過 對該塊內其他對象的後續引⽤來分攤。
  16. Locality Information • L1 Data cache = 32 KB, 64

    B/line, 8- WAY. write-back, ECC. Two 256-bit loads and one 256-bit store per cycle. • L1 Instruction cache = 32 KB, 64 B/ line, 8-WAY. 32 bytes fetch / cycle. Latency • L1 Cache Latency = 4 cycles • L2 Cache Latency = 12 cycles • L3 Cache Latency = 38 cycles • RAM Latency = 38 cycles + 66 ns
  17. i 在外⾯ miss rate = 4/16 a[i][j] j=0 j=1 j=2

    j=3 i=0 w[0] (miss) w[1] (hit) w[2] (hit) w[3] (hit) i=1 w[4] (miss) w[5] (hit) w[6] (hit) w[7] (hit) i=2 w[8] (miss) w[9] (hit) w[10] (hit) w[11] (hit) i=3 w[12] (miss) w[13] (hit) w[14] (hit) w[15] (hit)
  18. j 在外⾯ miss rate = 16/16 a[i][j] j=0 j=1 j=2

    j=3 i=0 w[0] (miss) w[4] (miss) w[8] (miss) w[12] (miss) i=1 w[1] (miss) w[5] (miss) w[9] (miss) w[13] (miss) i=2 w[2] (miss) w[6] (miss) w[10] (miss) w[14] (miss) i=3 w[3] (miss) w[7] (miss) w[11] (miss) w[15] (miss)
  19. 64 bits a[0,0] 64 bits a[0,1] 64 bits a[0,2] 64

    bits a[0,3] a[1,0] a[1,1] a[1,2] a[1,3] a[2,0] a[2,1] … Memory 陣列是 1 維? a[i][j] j=0 j=1 j=2 j=3 i=0 w[0] (miss) w[1] (hit) w[2] (hit) w[3] (hit) i=1 w[4] (miss) w[5] (hit) w[6] (hit) w[7] (hit) i=2 w[8] (miss) w[9] (hit) w[10] (hit) w[11] (hit) i=3 w[12] (miss) w[13] (hit) w[14] (hit) w[15] (hit) L1d cache size: 2MiB ( 32 KiB / core) 64 bits = 8 Bytes 4 numbers * 8 Bytes = 32 KB
  20. 你不會互換 i, j? 64 bits a[0,0] 64 bits a[0,1] 64

    bits a[0,2] 64 bits a[0,3] a[1,0] a[1,1] a[1,2] a[1,3] a[2,0] a[2,1] … 64 bits a[0,0] 64 bits a[0,1] 64 bits a[0,2] 64 bits a[0,3] a[1,0] a[1,1] a[1,2] a[1,3] a[2,0] a[2,1] … 64 bits a[0,0] 64 bits a[0,1] 64 bits a[0,2] 64 bits a[0,3] a[1,0] a[1,1] a[1,2] a[1,3] a[2,0] a[2,1] …
  21. Case 3 Vector Processing with SIMD without SIMD Single instruction,

    multiple data (SIMD) is a type of parallel processing in Flynn's taxonomy. SIMD can be internal (part of the hardware design) and it can be directly accessible through an instruction set architecture (ISA), but it should not be confused with an ISA. SIMD ⽤好 CPU 的全部價值!
  22. 計算 6.4M 個數字的平⽅ 1.先建立⼀個陣列,並填入數字 2.利⽤ _mm_sqrt_ps() ⼀次對 4 個 float

    平⽅ 3.利⽤ _mm_store_ps() ⼀次存放 4 個 float ⼀個 float 佔⽤ 4 bytes = 32 bits ⼀個 128 bits 的 register 可存 4 個
  23. 使⽤ SSE 指令集的影響 單位(秒) 使⽤ SSE 未使⽤ SSE Function 耗時

    0.0448 0.1903 ⼀個 128 bits 的 register 可操作 4 個 float 正好加速 4 倍有餘 未使⽤ SSE 使⽤ SSE 0 0.05 0.1 0.15 0.2 快 4 倍!
  24. 更長的 Register 64 位 CPU 的 register 有 64 bits。

    但還有更長的⋯⋯ 如何查看? (gdb) 
 info all-registers
  25. Case1-without-sort.out 9839M branches 1615M branch-misses time: 20.83 second Case1.out 9836M

    branches 0.53M branch-misses time: 8.13 second 性能分析 $ perf stat Perf is a profiler tool for Linux 2.6+ based systems that abstracts away CPU hardware differences in Linux performance measurements and presents a simple command line interface. Perf is based on the perf_events interface exported by recent versions of the Linux kernel.
  26. 回顧 CPU g++ -S -O3 -msse2 sse2-none.cpp Normal 也可以幫你使⽤到部分的 xmm

    但性能不會像是⼿動寫這麼好 問題回顧 單位(秒) 使⽤ SSE 未使⽤ SSE ⾃動 -O3 Function 耗時 0.0448 0.1903 0.1041 使⽤編譯器加速
  27. ⽤ Struct 把 Array 包起來, 還是⽤ Array 把 Struct 包起來?

    SOA: Structure of Arrays AOS: Array of Structures AOSOA: Array of structures of arrays 當⼀個對 SIMD 友善的⼯程師
  28. More Resource https://hackmd.io/@owlfox/csapptw/ CMU 兩位教授 Randal E. Bryant 和 David

    R. O’Hallaron 巧妙地 把程式設計及效能最佳化、數位電路基礎、指令集架構、組合語 ⾔、儲存裝置、連結器與載入器、⾏程、虛擬記憶體等等⾃各不同 的學科的核⼼知識點攪和在⼀起,並以程式開發者的視⾓呈現,所 以這本書的書名叫 “A Programmer’s Perspective”。