contributed by < twzjwang
>
作業說明: B02: raytracing
github: twzjwang/raytracing
Sean1127
Modify xxx.c
因為 GitHub 會自動追蹤被修改過的檔案Billy4195
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
jserv
提到 Ubuntu 14.04 版本太舊
main()
裡使用 #include
的寫法$ make
$ ./raytracing
# Rendering scene
Done!
Execution time of raytracing() : 3.388795 sec
$ make clean
$ make PROFILE=1
$ ./raytracing
$ gprof ./raytracing | less
# Rendering scene
Done!
Execution time of raytracing() : 7.183814 sec
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
21.41 0.54 0.54 69646433 0.00 0.00 dot_product
17.61 0.98 0.44 56956357 0.00 0.00 subtract_vector
9.61 1.22 0.24 13861875 0.00 0.00 rayRectangularIntersection
8.81 1.44 0.22 13861875 0.00 0.00 raySphereIntersection
8.41 1.65 0.21 10598450 0.00 0.00 normalize
...
math-toolkit.h
中所有 function 的 loop unroll
dot_product
:static inline
double dot_product(const double *v1, const double *v2)
{
double dp = 0.0;
for (int i = 0; i < 3; i++)
dp += v1[i] * v2[i];
return dp;
}
static inline
double dot_product(const double *v1, const double *v2)
{
double dp = 0.0;
dp += v1[0] * v2[0];
dp += v1[1] * v2[1];
dp += v1[2] * v2[2];
return dp;
}
dot_product
# Rendering scene
Done!
Execution time of raytracing() : 6.414793 sec
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
22.94 0.42 0.42 69646433 0.00 0.00 dot_product
11.89 0.63 0.22 56956357 0.00 0.00 subtract_vector
11.61 0.84 0.21 13861875 0.00 0.00 rayRectangularIntersection
11.06 1.04 0.20 10598450 0.00 0.00 normalize
8.85 1.20 0.16 17821809 0.00 0.00 cross_product
...
多處理機與平行程式設計
課程曾使用過以下 API
多處理機與平行程式設計
課程投影片ch3)延續實驗一
選擇平行化的部分(function)
選擇平行化 raytracing()
內的最外圍迴圈( height
)
先用4個 threads
傳遞參數部分寫法有點忘了
編譯失敗
-pthread
過程中不小心下到 git reset
到上一次commit
一開始輸出的圖如下
height
width
myrank
threat_count
以外的參數傳遞方法從 call by value => call by address
make check
: Verified OK
$ ./raytracing
# Rendering scene
arg myrank 1
arg myrank 0
arg myrank 2
arg myrank 3
Done!
Execution time of raytracing() : 3.450752 sec
h_start
~h_end
) h_end
設錯,每個 thread 都執行到 height
int h_size = arg->height / arg->thread_count;
int h_start = h_size * arg->myrank;
int h_end = (arg->myrank == arg->thread_count - 1) ?
h_size * (arg->myrank + 1) : arg->height;
for (int j = h_start; j < h_end; j++) {
...
int h_size = arg->height / arg->thread_count;
int h_start = h_size * arg->myrank;
int h_end = (arg->myrank < arg->thread_count - 1) ?
h_size * (arg->myrank + 1) : arg->height;
for (int j = h_start; j < h_end; j++) {
...
可以強調一下修改了哪裡,閱讀速度會比較快Billy SU
$ ./raytracing
# Rendering scene
arg myrank 1
arg myrank 0
arg myrank 3
arg myrank 2
Done!
Execution time of raytracing() : 1.216715 sec
若改變thread數量?
num of thread | 1 | 2 | 4 | 8 | 16 |
---|---|---|---|---|---|
Execution time | 2.51 | 1.38 | 1.22 | 1.11 | 1.10 |
Speedup | 1.00 | 1.82 | 2.06 | 2.26 | 2.28 |
Efficency | 1.00 | 0.91 | 0.52 | 0.28 | 0.14 |
for (int j = arg->myrank; j < arg->height; j += arg->thread_count)
$ ./raytracing
# Rendering scene
arg myrank 0
arg myrank 1
arg myrank 2
arg myrank 3
Done!
Execution time of raytracing() : 1.086578 sec
twzjwang