2017q1 Homework1(compute-pi)
contributed by < baimao8437
>
Reviewed by stanleytazi
- 信賴區間的公式目前看到的都是 standard error 下去算
,而非standard deviation
confidence interval
- 從比較圖來看好像都有看到OpenMP with 4 threads有比較大的抖動,有點好奇為什麼
- default 設定跑25次來算,可以再試試看跑多次一點,看不同做法的時間會不會差的比較明顯
Reviewed by hunng
- 利用多種方式計算並使用 AVX 實作
- 中間提到的不確定有沒有信賴區間,可以多次執行儲存結果後分析看看
- 而且有加上信賴區間比沒有加的抖動還要更大是不是出了什麼問題
花費時間 2/28 - 3/2
- 讀書:9 hr
- 文件:4 hr
- coding: 3 hr
看著發呆:3 hr
目標設定
- 習慣 linux 系統基本操作
- 熟練 git 基本版本控管操作
- 繪圖工具 LibreOffice、gnuplot
- SIMD 實作練習
- 複習微積分?
- Hackmd 文件 LaTeX 數學式寫法
- 減少發呆時間…
前置作業
這個作業一開始我整個霧煞煞 沒了老溼的精美影片教學 頓失依靠
只好先讀神人們的共筆了解一些基本名詞與時間分析
開發環境
SIMD指令集
- AVX (Advanced Vector Extensions) 是 Intel 一套用來作 Single Instruction Multiple Data 的指令集。
- 我的電腦 CPU 為 intel i5-4200U
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 →
- 使用時需
#include <immintrin.h>
,並在 Makefile 中的編譯選項加入-mavx
效能分析
測試原版程式效能:
可以看到 user(user mode CPU time) 跟 system (kernel mode CPU time) 時間
也可以看到佔用了多少 CPU OpenMP 2 threads 最多佔用 200% CPU
OpenMP 4 threads 最多就佔用 400% CPU
由於我還沒做好算數學的心理準備 就先來畫畫好了~
想要建立出作業範例以 LibreOffice 建立的圖表(如下圖):
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 →
先到 Makefile 的 gencsv 看到 seq 100 5000 25000
等於是 for(i=100; i<25000; i+=5000)
我調成 seq 1000 200 400000
跑跑看將近兩千筆資料
然後把出來的 csv 以 LibreOffice Calc 開啟 選擇以逗號分隔
再把全部的資料選取好後 按下圖表 再做一些設定:
- 圖表類型:設定為 線條圖 僅限線條 類型:平滑
- 資料範圍:勾選 已欄表示的資料序列 + 第一欄作為標籤
- 資料序列:把每條線名子改為對應 method
- 圖表元素:x 軸為 N, y 軸為 Time(sec)
這份報告之後可以拿來做研究所推甄、工作實習,甚至可作為論文的雛形,用與請精確 –jserv
Yes, Sir!baimao8437
畫出來後:
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 →
預料之中 跟原圖差距甚遠
連一點指數上升的感覺都沒有 無法理解
而且原圖是 Baseline > OpenMP_2 > AVX > OpenMP_4 > AVX_unrolling
我的結果是 Baseline > OpenMP_4 > OpenMP_2 > AVX > AVX_unrolling
好像差了有點多 不過參考了別人的圖發現有人跟我一樣結果 就安心多了
補上 gnuplot perfomance + error rate:
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 →
信賴區間
可看出圖表顯示有一些極端值造成線條的抖動
因此參考學長的筆記 試圖建立95信賴區間來排除極端值
機統沒學好 信賴區間忘光光: 簡單來說就這三句話
標準差長這樣:
然後 makefile 中加入 -lm
一樣讓他跑 2000 筆資料
結果:
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 →
OpenMP 4 threads 還是抖了一下到讓我懷疑到底有沒有信賴區間…
補上 gnuplot:
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 →
可以看到 一樣的演算法 但是用不同的優化結果
AVX_unrolling 的優化會造成再某些 N 計算結果誤差比較大
其他的算法 error rate 皆相同
Leibniz 公式證明
證明:
首先考慮
積分後
接著證明收斂
兩邊積分後等於
當時, 除積分項以外的項收斂到萊布尼茲級數, 同時積分項收斂至0
得證!
接著寫成 code:
第一次寫用了 pow(-1,i)去變換正負
但太花費時間 所以用 tmp 去變換正負
然後 Leibniz 優化挑了 AVX 來實作:
原本很畏懼 AVX 但有了上面的範例就好懂多了 也比較容易寫出來
- gnuplot 圖表

有點緊密 不過可以看到 AVX_unrolling 還是比 Leibniz_avx 快一點點

但是 error rate 卻明顯 Leibniz_avx 優於 AVX_unrolling
從輸出 csv 來看這邊 因為 Leibniz & Leibniz_avx 的 error rate 又全部跟其他重疊了 所以只有一條線
其他算 的方法 : Nilakantha method
寫成 code 也不難 大概長這樣
但是如果要用 AVX 優化的話 就要稍微細心一點了 改到頭昏腦脹
我相信應該有更簡潔的寫法 但是至少這個結果是對的
來看圖表

效能來看最好的三個是 :
(時間少) AVX > AVX_unrolling > Leibniz_avx (時間多)

這個圖表看起來跟上面的 error 圖看起來一樣
因為 Nilakantha 的兩個算法 錯誤率都非常接近 0
錯誤率低的嚇人 我把 N 調小 單獨看 Nilakantha
只要算到 50 幾項 就幾乎等於 了
參考資料: