Try   HackMD

2016q3 Homework1 (raytracing)

contributed by <bobsonlin>

tags: bobsonlin

優化前

分析

未啟動編譯器最佳化下:(-O0)

$ make
$ ./raytracing

# Rendering scene
Done!
Execution time of raytracing() : 3.536172 sec

啟動編譯器最佳化:(-Ofast)

# Rendering scene
Done!
Execution time of raytracing() : 0.766980 sec

用gprof分析(編譯器未優化)

$ make PROFILE=1
$ ./raytracing

# Rendering scene
Done!
Execution time of raytracing() : 7.766907 sec

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 24.15      0.77     0.77 69646433     0.00     0.00  dot_product
 18.51      1.36     0.59 56956357     0.00     0.00  subtract_vector
 10.35      1.69     0.33 10598450     0.00     0.00  normalize
  9.88      2.01     0.32 13861875     0.00     0.00  rayRectangularIntersection
  7.68      2.25     0.25 31410180     0.00     0.00  multiply_vector
  6.74      2.47     0.22 13861875     0.00     0.00  raySphereIntersection
  5.49      2.64     0.18 17836094     0.00     0.00  add_vector

程式優化

Loop unrolling

math-toolkit.h中的dot_product函式中,我們嘗試將for loop拿掉

double dot_product(const double *v1, const double *v2) { double dp = 0.0; dp += v1[0] * v2[0] + v1[1] * v2[1]+ v1[2] * v2[2]; return dp; }
# Rendering scene
Done!
Execution time of raytracing() : 3.142566 sec

程式執行時間快了 0.393606 sec

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 23.78      0.58     0.58 56956357     0.00     0.00  subtract_vector
 12.71      0.89     0.31 31410180     0.00     0.00  multiply_vector
 11.28      1.17     0.28 69646433     0.00     0.00  dot_product
 11.07      1.44     0.27 10598450     0.00     0.00  normalize
  8.61      1.65     0.21 13861875     0.00     0.00  rayRectangularIntersection
  7.79      1.84     0.19 17821809     0.00     0.00  cross_product
  6.97      2.01     0.17 13861875     0.00     0.00  raySphereIntersection

dot_product的執行時間從 0.77 sec 降至 0.28 sec

Force Inline

# Rendering scene
Done!
Execution time of raytracing() : 2.881001 sec

程式執行時間快了 2.881001 sec

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 33.73      0.87     0.87 13861875     0.00     0.00  rayRectangularIntersection
 14.73      1.25     0.38 13861875     0.00     0.00  raySphereIntersection
 12.79      1.58     0.33  2110576     0.00     0.00  compute_specular_diffuse
 11.24      1.87     0.29  1048576     0.00     0.00  ray_color
  8.53      2.09     0.22  2110576     0.00     0.00  localColor
  6.59      2.26     0.17  1048576     0.00     0.00  rayConstruction
  4.65      2.38     0.12  4620625     0.00     0.00  ray_hit_object

在此列表中,已看不到math-toolkit.h中的所有function

心得

參考資料

  1. TempoJiJi大大的HackMD