Try โ€‚โ€‰HackMD

2016q3 Homework (raytracing)

contributed by <kaizsv>

prof

# 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

loop unrolling

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

ๅฟซไบ†1็ง’ๅทฆๅณใ€‚

OpenMP

# 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ใ€‚

ๅคšๆ ธๅฟƒ้ซ˜ๆ•ˆ่ƒฝ็จ‹ๅผ้–‹็™ผ

tags: assigment_2 raytracing