contributed by <kaizsv
>
# Rendering scene
Done!
Execution time of raytracing() : 6.473611 sec
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
24.22 0.53 0.53 56956357 0.00 0.00 subtract_vector
23.30 1.04 0.51 69646433 0.00 0.00 dot_product
13.94 1.35 0.31 31410180 0.00 0.00 multiply_vector
6.85 1.50 0.15 17836094 0.00 0.00 add_vector
5.48 1.62 0.12 10598450 0.00 0.00 normalize
4.57 1.72 0.10 17821809 0.00 0.00 cross_product
4.57 1.82 0.10 13861875 0.00 0.00 rayRectangularIntersection
4.57 1.92 0.10 13861875 0.00 0.00 raySphereIntersection
ไธ้ขๆฏๆ็ prof ็ตๆ๏ผๅ
ๅฐๅธธ่ขซๅผๅซ็subtract_vector
, dot_product
, multiply_vector
, add_vector
๏ผๅ loop unrolling
# Rendering scene
Done!
Execution time of raytracing() : 5.899568 sec
ๅฟซไบ1็งๅทฆๅณใ
# Rendering scene
Done!
Execution time of raytracing() : 0.983607 sec
Verified OK
OpenMP ๅฏไปฅๅจshared-memory machine
ไธๅท่กๅนณ่ก็จๅผ๏ผๅจ็ทจ็นนๆๅ ไธ-fopenmp
ใ
#include <omp.h>
#pragma omp parallel for num_threads(16) \
schedule(guided, 4) \
private(d) \
private(stk) \
firstprivate(object_color)
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
double r = 0, g = 0, b = 0;
/* MSAA */
for (int s = 0; s < SAMPLES; s++) {
idx_stack_init(&stk);
rayConstruction(d, u, v, w,
i * factor + s / factor,
j * factor + s % factor,
view,
width * factor, height * factor);
if (ray_color(view->vrp, 0.0, d, &stk, rectangulars, spheres,
lights, object_color,
MAX_REFLECTION_BOUNCES)) {
r += object_color[0];
g += object_color[1];
b += object_color[2];
} else {
r += background_color[0];
g += background_color[1];
b += background_color[2];
}
pixels[((i + (j * width)) * 3) + 0] = r * 255 / SAMPLES;
pixels[((i + (j * width)) * 3) + 1] = g * 255 / SAMPLES;
pixels[((i + (j * width)) * 3) + 2] = b * 255 / SAMPLES;
}
}
}
#pragma omp parallel for
้ๆฏ OpenMP ็็ทจ็นนๅจๆไปค๏ผ่กจ็คบ่ฆๅฐ้ๅ for ่ฟดๅๅนณ่กๅใ
num_threads(16)
่ฆ็จๅนพๅ threads ไพๅท่ก๏ผไนๅฏไปฅ็จ omp_get_max_threads()
่ฎๆๅค็ threads ๅท่กใ
schedule(dynamic, 4)
็ทจ็นนๅจ่ฆๅฆไฝๅ้
ๅทฅไฝ๏ผๅฐฑ็จschedule
ๆไปคใ
static
ๆฏๅ threads ๆไพๅบๅท่ก่ขซๅๅฒ็ๅทฅไฝ๏ผ่ schedule(static, 4) ็ๆๆๅฆไธไพๅญใ
โโโโโโโโโโ #pragma omp parallel for num_threads[4] schedule(static)
โโโโโโโโโโ for (int i = 0; i < 1000; i++) {}
โโโโโโโโโโ
โโโโโโโโโโ thread 1: i = 0 ~ 249
โโโโโโโโโโ thread 2: i = 250 ~ 499
โโโโโโโโโโ thread 3: i = 500 ~ 749
โโโโโโโโโโ thread 4: i = 750 ~ 999
โโโโโโโโโโ #pragma omp parallel for num_threads[4] schedule(static, 4)
โโโโโโโโโโ for (int i = 0; i < 1000; i++) {}
โโโโโโโโโโ
โโโโโโโโโโ thread 1: i = 0, 1, 2, 3, 16, 17...
โโโโโโโโโโ thread 2: i = 4, 5, 6, 7, 20, 21...
โโโโโโโโโโ thread 3: i = 8, 9, 10, 11, 24, 25...
โโโโโโโโโโ thread 4: i = 12, 13, 14, 15, 28, 29...
dynamic
็ถ threads ๅฎๆๆๅๅๅกๅพ๏ผๆๆๅๆ ๅ้ ๅฆไธๅๅๅกๅปๅท่กใ
guided
่ dynamic ้กไผผ๏ผไฝๅๅกๅคงๅฐๆๆๆธ้ๆธใ
auto
็ทจ็นนๅจ่ช่กๆฑบๅฎใ
runtime
ไฝฟ็จ่ ็จ็ฐๅข่ฎๆธ OMP_SCHEDULE ๆฑบๅฎใ
private and shared
private ๅฐฑๆฏๆฏๅ thread ๆ่ชๅทฑ็ไธไปฝ่ฎๆธ๏ผๅ็ shared ๅฐฑๆฏๆๆ threads ๅ ฑ็จใ
firstprivate
ๅฎๅฐฑๆฏ private ่ฎๆธ๏ผไฝๅฆๆ่ฉฒ่ฎๆธๅจ้ฒๅ ฅ่ฟดๅๅๅฐฑๆๅๅงๅผ๏ผๅ firstprivate ๆไฟ็๏ผๅฆๆๆฏ private ็่ฉฑๆๆฏๆช็ฅ๏ผๅ็้ๆ lastprivate ๆฏๅท่ก็ท็ตๆๅพ private ่ฎๆธๆฏๅฆๆๆดๆฐใ
ๅจraytracing
่ฟดๅๅ
ง๏ผstk
ๆฏๆฌก่ฟดๅ้ฝๆๆธ
็ฉบ๏ผd
ๆๅ
ๅจrayConstruction
่ขซๆญฃ่ฆๅ๏ผๅ่จ็ฎ้ก่ฒ๏ผ่object_color
ๅจ้ฒๅ
ฅ่ฟดๅๅๆๅๅงๅผไบ๏ผๅฐฑ่จญๆfirstprivate
ใ
ๅคๆ ธๅฟ้ซๆ่ฝ็จๅผ้็ผ
assigment_2
raytracing