# 2016q3 Homework 1 (compute-pi)
###### tags: `sysprog21` `JongSyuGithub`
contributed by <`JonSyuGithub`>
github: https://gtihub.com/JonSyuGithub/compute-pi
## Environment
* OS: Ubuntu 16.04 LTS
* CPU: [i5-457](http://ark.intel.com/products/75043/Intel-Core-i5-4570-Processor-6M-Cache-up-to-3_60-GHz)
* Memory 12GB ($ cat /proc/meminfo)
* Update & Install
```
$ sudo apt-get update
$ sudo apt-get install build-essential
$ sudo apt-get install linux-tools-common linux-tools-generic
$ sudo apt-get install astyle colordiff gnuplot
```
## Objective
* 學習透過離散微積分求圓周率
* Leibniz formula for π
* 積分計算圓周率π Function
* 著手透過 SIMD 指令作效能最佳化
## make plot
* [資料重新導流](http://dywang.csie.cyut.edu.tw/dywang/linuxProgram/node20.html) : 將在terminal要輸出的資料輸出到檔案的方法,分為stdout(一般輸出)、stdeer(錯誤輸出)
* [1] [/usr/bin/time](http://www.thegeekstuff.com/2013/10/time-command-format/) [2] [/usr/bin/time](http://www.cyberciti.biz/faq/unix-linux-time-command-examples-usage-syntax/) [3] [/usr/bin/time](http://www.1987.name/655.html)︰產生程式執行時與時間相關參數資訊
* real (%E): 命令開始執行到結束的時間。這個時間包括其他process所佔用的時間片段,以及本身process被阻塞時所花費的時間。
* 'Real' is calculated from a start and end time gathered from the gettimeofday (2) call.
* Note that this is across all CPUs, so if the process has **multiple threads** (and this process is running on a computer with more than one processor) it could **potentially exceed the wall clock time** reported by Real (which usually occurs).
* user (%U): process花費在user mode中的CPU時間,這是唯一真正用於執行process所花費的時間,其他process和本身process花費阻塞狀態中的時間沒有計算在內。
* sys (%S): 花費在kernel mode中的CPU時間,代表在kernel中執行系統調用所花費的時間,這也是真正由process使用CPU的時間。
* User+Sys will tell you how much actual CPU time your process used.
```
# In this example, show just the user, system, and total time using format option:
$ { /usr/bin/time -f 'baseline %E %U %S' ./time_test_baseline ; } 2>time.txt
```
* [gnuplot](http://people.duke.edu/~hpgavin/gnuplot.html):畫圖指令寫法
* [using](http://gnuplot.sourceforge.net/docs_4.2/node133.html):設定存取input txt file的行數與對應的長條圖標示
* [xtic](http://gnuplot.sourceforge.net/docs_4.2/node295.html):設定使用行
* [折線圖](http://blog.csdn.net/baigoocn/article/details/38417505)
* Makefile的for loop教學
* seq後面接著的數字表示 (1) i的起始數字 (2) i在每次迴圈之後要加多少 (3) i結束的數字。
* [Automatic Variables](https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html)
```
for i in `seq 100 5000 25000`; do \
printf "%d," $$i;\
./benchmark_clock_gettime $$i; \
done > result_clock_gettime.csv
```
* Github: [Git教學](http://gogojimmy.net/2012/01/17/how-to-use-git-1-git-basic/)
```
# setting parameter
$ git config --global user.email "your e-mail"
$ git config --global user.name "your name"
$ git config --global push.default matching
$ git remote set-url origin https://github.com/JonSyuGithub/compute-pi.git
# update
$ git add runtime.gp
$ git commit -m "Add runtime.pg for plot command"
$ git add runtime2.gp
$ git commit -m "Add runtime2.pg for plot2 command"
$ git add Makefile
$ git commit -m "make plot & plot2 commands in the makefile"
$ git push
```
## Running
```
$ make
$ make plot
```

```
# runtime2.gp
plot 'result_clock_gettime.csv' using 1:2 with lines title 'baseline',\
'result_clock_gettime.csv' using 1:3 with lines title 'OpenMP_2',\
'result_clock_gettime.csv' using 1:4 with lines title 'OpenMP_4',\
'result_clock_gettime.csv' using 1:5 with lines title 'AVX_SIMD',\
'result_clock_gettime.csv' using 1:6 with lines title 'AVX SIMD + Loop unrolling'
```
```
$ make
$ make plot2
```

```
# runtime2.pg
plot 'result_clock_gettime.csv' using 1:2 smooth bezier with lines title 'baseline',\
'result_clock_gettime.csv' using 1:3 smooth bezier with lines title 'OpenMP_2',\
'result_clock_gettime.csv' using 1:4 smooth bezier with lines title 'OpenMP_4',\
'result_clock_gettime.csv' using 1:5 smooth bezier with lines title 'AVX_SIMD',\
'result_clock_gettime.csv' using 1:6 smooth bezier with lines title 'AVX SIMD + Loop unrolling'
```
```
$ make
$ make plot2
```

## References
* [A03: compute-pi](https://hackmd.io/s/rJARexQT)
* [進階電腦系統理論與實作 (Fall 2016)A03: compute-pi](https://hackmd.io/MYUwHAJgnALADMAtAJhAIzYmA2OBmRKARgFYCpswMYQiBDbSoA==?view)
>> 參考此未同學在Makefile自行新增plot指令繪製圖 [name=JonSyuGithub]