2017q1 Homework1 (compute-pi)
contributed by < twzjwang
>
作業說明: B03: compute-pi
github: twzjwang/compute-pi
Reviewed by hunng
- 實際實驗、比較各種擷取時間函數並比較
- 雖然只是更換 rand() 成 rand_r() 但卻能在效能上做出明顯改善
開發環境
-
Ubuntu Ubuntu 16.04.2 LTS
Linux 4.8.0-36-generic
-
cpu
version: Intel® Core™ i5-3337U CPU @ 1.80GHz
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
-
memory
size: 4GiB
開發前
數學相關式子可以試著使用 LaTeX 表示喔!
課程助教
- 時間處理
- Wall-clock time
- CPU time
- 程序在 CPU 上面運行消耗 (佔用) 的時間
- clock()
- multithreads - 每條 thread 的使用時間加總
- 時間函數
- clock()
- return number of clock ticks
- 將結果除以
CLOCKS_PER_SEC
得到時間
- 在 32-bit 系統下
CLOCKS_PER_SEC
= 1000000
/ 1000000 = 4295(s) = 72(min)
約72分鐘後會 wrap around
- 不能區分 kernel mode 、 user mode 占用的 CPU time
- clock_gettime()
- 根據參數
clockid_t
決定 clock 類型
- system-wide (for all processes)
- per-process
- 時間單位 nanoseconds : 1/ (s) , 10 億分之一秒
- time()
- return 1970-01-01 00:00:00 +0000 (UTC) 至現在的秒數
- 在 32-bit 系統下,time_t 是 signed int32
可記錄 (s) = 68(years),也就是到2038年會 wrap around
- 2038年問題
- gettimeofday()
- time() 和 gettimeofday() 不適合拿來作 benchmark
- 參考gettimeofday() should never be used to measure time
- Nobody sneaks in and changes your wristwatch when you’re not looking
- You’re not stupid, the computer is
- You are the limiting factor
- 生活中通常也是用現實時間的時間差計時
- 時間單位不夠精確
- 用 clock_gettime 代替
- It’s not stable across reboots
- goes up by one second for each second that passes in the physical world
- 步驟
- 隨機放置一點在 A(x,y) , 0<=x<=1, 0<=y<=1
- 重複 N 次,並紀錄 M
- C : 半徑為 1 ,原點為(0,0)的園
- N : 樣本數
- M : A 在 C 內的次數
- P :
- 使用 rand() 產生隨機數
方法 1: 實作 Monte Carlo method,分別使用 4 種時間函數計時
- 計時方法
- clock()
- clock_gettime()
- time()
- gettimeofday()
- 結果
方法 2: 比較不同樣本數得到結果的誤差
-
使用 clock_gettime()
-
結果
n |
|
|
|
|
|
|
|
|
|
|
time(s) |
0.000003 |
0.000002 |
0.000015 |
0.000128 |
0.000943 |
0.009436 |
0.040706 |
0.277493 |
2.723461 |
27.253682 |
pi |
4.000000 |
3.200000 |
3.120000 |
3.152000 |
3.167200 |
3.138880 |
3.141920 |
3.140899 |
3.141710 |
3.141604 |
error |
0.858407 |
0.058407 |
0.021593 |
0.010407 |
0.025607 |
0.002713 |
0.000327 |
0.000694 |
0.000117 |
0.000011 |

X label 可以參考美圖欣賞第三章 修改
課程助教
增加set xtics rotate by 45 right
方法 3: 用 OpenMP 平行化
方法 4: 用 OpenMP 平行化-改用 rand_r()
- 結果
- 以 N = 10000000 為例,比較平行化前後差異
- serial
- parallel
- 在 4 cores 環境下,執行時間前為平行前的 29%
- 以 N = 10000000 為例,在使用OpenMp平行化下,比較 rand() 與 rand_r()
- rand()
- rand_r()
- rand_r() 執行時間為 rand() 的 3%

將 Monte Carlo method 與其他方法合併
$ make plot
- 結果
- 暫不考慮誤差大小,僅考慮執行時間

參考資料