2017q1 Homework1 (compute-pi) contributed by < ryanwang522
> Reviewed by Cayonliow
openmp_4 因爲不穩定所以圖會有波動, 可以透過將執行的次數調高然後取平均來消除, 可以看看 我的共筆 , 我是使用 gnuplot 制圖
開發環境
OS: ubuntu 16.04 LTS
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
Architecture:x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
Model name: Intel ® Core ™ i5-4200H CPU @ 2.80GHz
原始版本 進行實驗 Error Rate
loop = 250 , N = 100 : 5000 : 25000
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 →
loop = 250 , N = 100 : 1000 : 25000
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 unroll 的版本在 N 不夠大時會有較大的誤差以及不太穩定,其他方法的誤差隨著 N 的遞增都很快地趨近於 0。
目前對於 avx unroll 為何會波動較大還想不太到解釋。
Leibniz Method
參考 Leibniz formula for π
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 →
double compute_pi_Leibniz ( size_t N)
{
double pi = 0.0 ;
int factor = 1 ;
for ( size_t i = 0 ; i < N; i++ ) {
pi += ( double ) factor / ( 2.0 * ( double ) i + 1.0 ) ;
factor = - factor;
}
return ( 4 * pi) ;
}
這裡再實作一個 Openmp 的版本來進行比較
因為 Leibniz 需要正負相間的運算,所以利用 schedule(static, 1)
去明確指定每個 thread 所要負責的迴圈部份。
計時方法依然使用 clock_gettime()
。
double compute_pi_Leibniz_omp ( size_t N, int threads)
{
double pi = 0.0 ;
# pragma omp parallel for num_threads ( threads) \
reduction ( + : pi) schedule ( static , 1 )
for ( size_t i = 0 ; i < N; i++ ) {
if ( omp_get_thread_num ( ) % 2 == 0 )
pi += 1.0 / ( 2.0 * ( double ) i + 1.0 ) ;
else
pi += - 1.0 / ( 2.0 * ( double ) i + 1.0 ) ;
}
return ( 4 * pi) ;
}
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 →
Leibniz 的原始版本好像就比黎曼和快了不少!
Error Rate 比較,依然只有 AVX Unroll 較不穩定。
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 →
補上 gnuplot
寫 runtime.gp
一直遇到 x range is invalid
的錯誤訊息,後來發現是 gnuplot 預設用空白來切分數據。
加上 set datafile separator ","
就可以正常畫圖了!
這裡可以看到 OpenMP 在某些 N 值會有執行時間莫名增加的狀況,而其他方法都還算穩定,可以推斷是 thread 之間運作的 Overhead。
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 →
Monte Carlo Method Reference GNUPLOT 讀取逗號分隔的數據文件
Leibniz formula for π - wikipedia