Try   HackMD

2017q1 Homework4 (phonebook-concurrent)

contributed by < etc276 >

tags: 嵌入式

開發環境

  • Ubuntu 16.10 ( 64 bit )
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz
Stepping:              4
CPU MHz:               2400.878
CPU max MHz:           3000.0000
CPU min MHz:           500.0000
BogoMIPS:              4788.90
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              4096K
NUMA node0 CPU(s):     0-3

作業要求

  • 在 GitHub 上 fork phonebook-concurrent,然後適度修改 phonebook_opt.c 和相關的檔案
  • 閱讀 concurrency 、驗證程式正確性、分析並說明如何改善效能(需有圖表)
  • 延續 B01: phonebook 的開發方向,本作業著重於透過 POSIX Thread 來縮減 alloc() 的時間成本
    • 詳細閱讀實驗,指出缺失並提出改進縮減 append() 時間的可行提案
    • 需要一併實作出刪除 (remove) 特定資料的功能
  • 延續 B05: introspect,嘗試重構 (refactor) 給定的程式碼,使得程式更容易閱讀和維護

開發紀錄

閱讀程式架構

  • 一樣,先看 makefile,可以發現多了 text_align
phonebook_opt: $(SRCS_common) phonebook_opt.c phonebook_opt.h text_align.c
	$(CC) $(CFLAGS_common) $(CFLAGS_opt) \
		-DIMPL="\"$@.h\"" -o $@ \
		$(SRCS_common) $@.c text_align.c
  • 閱讀 text_align.c 和實驗之後,有了以下了解

    • 為了避免 I/O Bound,所以修改原本讀取資料的方法
    • 方法是不逐一"讀取"資料,而是將資料對齊對映到虛擬記憶體上
    • 這樣的好處是,當有多條 thread 的情況就可以真的分別"拿到"資料
  • 目前對這次作業的理解 (並行)

    • 改善 input 方式以實現 CPU Bound
    • 避免 system call 等無法有效利用 thread 的原因 (wait)
    • 將所有任務分配給多條 thread (從 init 到 append 結束)

重構 (Refactor)

  • 重構的用意是:在不改變軟體外部行為的前提下,改變其內部結構,使其更容易理解且易於修改
  • 目標是讓程式碼的可讀性提升,以及易於維護
  • 先來看本次的 main.c
    • 搜尋 ifndef 有 5 個結果
    • 搜尋 if defined 有 3 個結果
    • 搜尋 clock_gettime 有 7 個結果
    • 整個 main.c 多達 201 行,大部分都是"分別"實作 orig 和 opt
  • 目前的想法利用 API 包裝兩者的不同,使得 main.c 更為簡潔

並行 (Concurrency)

  • Concurrency 對軟體設計的影響

    • 想要充分使用到 CPU 的資源
    • 程式越來越有機會造成 CPU-bound。
    • 軟體效能優化將會越來越重要
    • 程式語言必須好好處理 concurrency
  • 要把工作真的拆開,且盡量避免互相卡住 (lock)

  • 並行是一種程式架構,平行是一種執行方式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 生活化的例子如下

    • 並行:邊看書,邊滑臉書 (不同時)
    • 平行:邊走路,邊呼吸
  • 平行迷思 2 * 3GHz < 6GHz

    • 總有東西要等
    • 例如 thread 互相連接的方式 (接 n 次還是 log n 次)
    • 非 CPU Bound (例如在等 IO 就沒辦法分工)
    • 哪個 thread 先執行 (類似 i = i++ )
    • 應該只有效能上的提升,結果不該改變