SLAM Engineer

๐ŸŒˆ Row-major, Column-major, ๊ทธ๋ฆฌ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ ์ตœ์ ํ™”


Compiler ๊ฐ€ ํ•ด์ฃผ๋Š” ๊ฒƒ๊ณผ ์•ˆํ•ด์ฃผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

  • ์ปดํŒŒ์ผ๋Ÿฌ (์™€ ChatGPT)๋Š” ๋‚˜์˜ ๋™๋ฃŒ๋‹ค.

๊ฐœ์š”

  • ์ผ๋ฐ˜์ ์ธ ์ฝ”๋”ฉ ํŒ ๊ฐ™์€ ์ด์•ผ๊ธฐ๋Š” ์ธํ„ฐ๋„ท์— ์ด๋ฏธ ๋งŽ์œผ๋ฏ€๋กœ ์›๋ž˜ ์ž˜ ์•ˆ์“ฐ๋ ค๊ณ  ํ•˜์ง€๋งŒ โ€ฆ
    • ๋‚˜๋ฆ„๋Œ€๋กœ ์‹คํ—˜ํ•ด๋ณธ ๋ฐ”๊ฐ€ ์žˆ์–ด ๊ธฐ๋ก ์ฐจ์›์—์„œ ์ •๋ฆฌํ•ด๋‘๋ ค๊ณ  ํ•œ๋‹ค.

๋ฌธ์ œ ์ƒํ™ฉ

  • Matrix ๊ฐ€ ์žˆ์„ ๋•Œ ์ต์ˆ™ํžˆ ์•Œ๋ ค์ง„ ๊ฟ€ํŒ์ด
    • Matrix ์ „์ฒด elmenet ๋“ค์„ ์ˆœํšŒํ•˜๋ ค๋ฉด for loop ๋‘๋ฒˆ์„ ๋Œ์•„์•ผ ํ•˜๋Š”๋ฐ,
    • ์ด ๋•Œ row-major ๊ฐ€ column-major ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๋ฐฐ๊ฒฝ ์ง€์‹

์‹ค์Šต

Hands-on Experience

  • ๋Š˜ ๊ฐ•์กฐํ•˜๋Š” ๊ฒƒ: ๊ทธ๋ƒฅ ์ฝ์–ด๋ณด๊ณ  ๊ทธ๋ฆผ๋งŒ ๋ณด๋Š” ๊ฒƒ๊ณผ ์ง์ ‘ from-scratch๋กœ ๊ตฌํ˜„ํ•ด์„œ ์ž๊ธฐ ๋ฐ์ดํ„ฐ์—์„œ ์‹คํ—˜ํ•ด์„œ ์ง์ ‘ run script ์ณ์„œ ์ง์ ‘ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋Š” ๊ฒƒ์€ ์‹ค์Šต์ž ์ž…์žฅ์—์„œ ์ฒœ์ง€ ์ฐจ์ด ์ด๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ง์ ‘ํ•ด๋ณด์ž.

๊ตฌํ˜„

  • ์ฝ”๋“œ
  • ์„ค๋ช…
    • ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๊ฐ„์„ ์ธก์ •ํ•ด์ฃผ๋Š” ํŒฉํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ 

    • ํŒฉํ† ๋ฆฌํด๋ž˜์Šค์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„œ๋กœ๋‹ค๋ฅธ 3์ข…์˜ ๋žŒ๋‹คํ•จ์ˆ˜๋จธ์‹ ๋“ค์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

์‹คํ–‰

  • ์‹ค์Šตํ™˜๊ฒฝ
    • cpp web compiler ๋ผ๊ณ  ์น˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์‹ค์Šตํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ณณ๋“ค์ด ๋งŽ์ด ๋‚˜์˜จ๋‹ค.
      • ๋‚˜๋Š” onlinegdb ์— ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ด์„œ ์‹คํ–‰ํ•ด๋ณด์•˜๋‹ค.
    • Tip
      • O3 ๋กœ build ํ•˜๋ ค๋ฉด,
        • ์ง์ ‘ ๋ฆฌ๋ˆ…์Šค ๋จธ์‹ ์—์„œ ์‹คํ—˜ํ•œ๋‹ค๋ฉด g++ -std=c++17 -O3 main.cpp -o main ์™€ ๊ฐ™์ด ๋นŒ๋“œํ•ด์ฃผ๋ฉด ๋˜๊ณ ,
        • ์œ„์˜ onlinegdb ์‚ฌ์ดํŠธ์—์„œ ์ ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ•œ ๋‹ค์Œ OK๋ˆ„๋ฅด๊ณ  ์‹คํ–‰ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ฒฐ๊ณผ

  • matrix dimension SIZE๋ฅผ ๋‹ฌ๋ฆฌํ•ด๊ฐ€๋ฉฐ ์‹คํ—˜ํ•˜์˜€๋‹ค.
  • ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค (๊ฐ SIZE์— ๋Œ€ํ•ด 1ํšŒ์”ฉ๋งŒ ์ธก์ •ํ•˜์˜€๋‹ค).
    • ์—ฌ๊ธฐ์„œ ๊ฐ€๋กœ์ถ•์˜ Size ๋Š” ํ•œ ์ถ•์˜ dimension์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, SIZE=10000 ์ด๋ฉด, 10000 by 10000 matrix ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ์œ„ ablation study์˜ ์˜๋„
    • Question 1: row-major ์™€ column major ์˜ from-scratch ๊ตฌํ˜„์ฒด ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•˜์˜€๋‹ค. (see ํŒŒ๋ž‘์‹ค์„  <=> ๋นจ๊ฐ•์‹ค์„ )
    • Question 2: row-major์˜ from-scratch ๊ตฌํ˜„์ฒด์™€ row-major ์˜ std::algorithmic(i.e., functional)ํ•œ ๊ตฌํ˜„์ฒด ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•˜์˜€๋‹ค. (see ํŒŒ๋ž‘์‹ค์„  <=> ์ดˆ๋ก์‹ค์„ )
    • Question 3: compiler optimization option (-O3) ์„ ์•ˆ๋„ฃ๊ณ  ๋„ฃ๊ณ  ์ฐจ์ด๋ฅผ ๋น„๊ตํ•˜์˜€๋‹ค (see ์‹ค์„  <=> ์ ์„ )

๊ฒฐ๊ณผ ํ•ด์„

  • Answer of Question 1:
    • ๋‹น์—ฐํžˆ ์ตํžˆ ์•Œ๊ณ  ์žˆ๋Š”๋Œ€๋กœ row-major ๊ฐ€ ํ›จ์”ฌ ๋น ๋ฅด๋‹ค (see ํŒŒ๋ž‘์‹ค์„  <=> ๋นจ๊ฐ•์‹ค์„ ).
  • Answer of Question 2:
    • std::algorithm์„ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜๋ฉด functional ํ•œ ๋””์ž์ธ์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ฐ•์ œ๋จ์œผ๋กœ์จ ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ์ข‹์•„์ง„๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์ตœ๋Œ€ํ•œ std::algorithm์„ ํ™œ์šฉํ•˜๊ณ  ์‹ถ๋‹ค.
    • ํ•˜์ง€๋งŒ no optimization ํ™˜๊ฒฝ์—์„œ๋Š” raw ํ•œ double nested loops ๋ณด๋‹ค ๋Š๋ฆฐ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ๋‹ค (see ํŒŒ๋ž‘์‹ค์„  <=> ์ดˆ๋ก์‹ค์„ ).
      • ์‚ฌ์‹ค n์ด ์ž‘์„ ๋•Œ (e.g., ~ SIZE=10000 => 10000*10000 ํšŒ์˜ ์ˆœํšŒ) ๋Š” ์ฐจ์ด๊ฐ€ ํฌ์ง€ ์•Š๋‹ค.
  • Answer of Question 3:
    • -O3 ์˜ต์…˜์„ ๋„ฃ์–ด์ฃผ๋‹ˆ std::algorith์„ ํ™œ์šฉํ•œ functionalํ•œ code ๊ฐ€ (์ ์–ด๋„ ์ด ์˜ˆ์ œ์˜ ์ด ์‹คํ—˜์—์„œ๋Š”) raw ํ•œ nested for loop ๋ณด๋‹ค (์กฐ๊ธˆ์ด๋‚˜๋งˆ) ๋” ๋นจ๋ž๋‹ค.

๊ฒฐ๋ก 

Lessons

  • ์œ„์˜ ๊ฒฐ๋ก ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ ˆ์Šจ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
    1. ๊ดœํ•œ ์„ฑ๋Šฅ๊ณ ๋ฏผ์•„์ง‘ํ•˜์ง€ ๋ง๊ณ  std::algorithm์˜ functional ํ•œ interface๋ฅผ ์ตœ๋Œ€ํ•œ ํ™œ์šฉํ•˜์ž.
      • ์ฝ”๋“œ์˜ readability, maintainability, and testability ๋ฅผ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ๋”์šฑ ์ค‘์š”ํ•˜๋‹ค.
    2. ๊ทธ๋Ÿฐ ๋‹ค์Œ, ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด์„œ๋Š” O3 ๋“ฑ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ๋ฏฟ์ž (์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋‚˜์˜ ๋™๋ฃŒ! ๋ฒ„๊ทธ๋„ ์ฐพ์•„์ฃผ๊ณ  ์„ฑ๋Šฅ๋„ ์˜ฌ๋ ค์ค€๋‹ค).
      • ํœด๋จผ๋ฆฌ๋”๋ธ” ์ฝ”๋“œ์˜ ๊ตฌํ˜„์ด ์–ด๋–ป๊ณ  ์ €๋–ป๊ณ ์— ๋Œ€ํ•œ ํ† ์˜๋ฅผ ์•„๋ฌด๋ฆฌ ํ•ด๋„ ์‹ค์ œ ๊ตฌ๋™๊ณผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์ฝ”๋“œ์˜ ์„ฑ๋Šฅ์— ๊ด€ํ•ด ์ด์•ผ๊ธฐ ํ•˜๋ ค๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ ์ตœ์ ํ™” ๊นŒ์ง€ ์ ์šฉ๋œ ๊ฒƒ์— ๋Œ€ํ•ด์„œ์—๋งŒ ๋…ผ์˜ํ•ด์•ผ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.
    3. ๊ทธ๋Ÿฐ๋ฐ ์• ์ดˆ์— ์„ค๊ณ„ ์ž์ฒด๊ฐ€ ์ž˜๋ชป๋˜(==column-major)๋ฉด ์•„๋ฌด๋ฆฌ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ๋ฏฟ์–ด๋„ ์•ˆ ๋˜๋Š” ๊ฒƒ์€ ์•ˆ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.์•ˆ๋ผ ๋Œ์•„๊ฐ€
      • ์˜ˆ๋ฅผ ๋“ค์–ด, ์œ„์˜ ๊ฒฐ๊ณผ ๊ทธ๋ฆผ์—์„œ, O3 optimization์„ ์ ์šฉํ•œ ๋นจ๊ฐ•์ ์„ ์€ opt ๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์€ ์ดˆ๋ก์‹ค์„  ๊ฒฐ๊ณผ๋ณด๋‹ค ์—ฌ์ „ํžˆ ๋งค์šฐ ๋Š๋ ธ๋‹ค.
    4. ๋”ฐ๋ผ์„œ HW๋ฅผ ์ดํ•ดํ•˜๊ณ  ๊ทผ์›์ ์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ๊ฐœ๋…์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์€ ์—ฌ์ „ํžˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ๋ชซ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
      • HW์— ๋Œ€ํ•œ ์ดํ•ด๋Š” ๋งŽ์€ ๊ฒƒ๋“ค์ด ์ถ”์ƒํ™” ๋˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์ž…๋ฌธํ•˜๊ธฐ ์ ์  ๋” ์‰ฌ์›Œ์ง€๋Š” ์š”์ฆ˜์—๋„ ์—ฌ์ „ํžˆ ์ค‘์š”ํ•˜๋‹ค.
    5. ๊ทธ๋ฆฌ๊ณ  ์ธก์ •ํ•˜์ž!
      • ์ธก์ •ํ•œ ๊ฒƒ์œผ๋กœ ๋…ผ์˜ํ•ด์•ผ ํ•œ๋‹ค.
  • ps.
    • ๋ฒค์น˜๋งˆํฌ๋Š” ์—ฌ๋Ÿฌ๋ฒˆ ๋Œ๋ ค๋ด์•ผ ํ•˜๋Š”๋ฐ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” ํŽธ์˜์ƒ 1๋ฒˆ๋งŒ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์†Œ์š”์‹œ๊ฐ„๋„ ๋จธ์‹ ๋งˆ๋‹ค๋„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฝํ–ฅ๋งŒ ์ฐธ๊ณ ๋ฅผ ํ•˜์ž. ๊ฒŒ๋‹ค๊ฐ€ ์œ„์—์„œ๋Š” onlinegdb๋ผ๋Š” ์‚ฌ์ดํŠธ์—์„œ ์‹คํ–‰ํ•˜์˜€๋Š”๋ฐ, ์ž์›์˜ ๊ฒฝ์Ÿ์ด ์žˆ๋Š”์ง€..? ๋‹ค๋ฅธ ์‹œ๊ฐ์— ๊ฐ™์€ ์ฝ”๋“œ๋„ ์‹คํ–‰ํ•˜๋‹ˆ ์—„์ฒญ ๋” ๋Š๋ฆด ๋•Œ๋„ ์žˆ์—ˆ๋‹ค.
    • ์œ„์˜ matrix์˜ˆ์ œ ๊ฐ™์€ ๋ฌธ์ œ์—์„œ๋Š” ์• ์ดˆ์— ์Œฉ์œผ๋กœ ๋Œ ์ƒ๊ฐ๋ณด๋‹ค๋Š” vectorization and parallel algorithms์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์ด๋‹ค.
    • ๊ทธ๋‚˜์ €๋‚˜ ChatGPT(4) ์—๊ฒŒ ์œ„์˜ ์ฝ”๋“œ ์ฃผ์„์— ์žˆ๋Š” ๊ฒฐ๊ณผ ์„œ๋จธ๋ฆฌ ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ฃผ๊ณ  ํŒŒ์ด์ฌ์œผ๋กœ ๋น„๊ต ํ”Œ๋กฏ ๊ทธ๋ ค์ค˜ ๋ผ๊ณ  ํ•˜๊ณ  ์ƒ‰๊น” ์ •๋„๋งŒ ์ง€์ •ํ•ด์คฌ๋”๋‹ˆ ์œ„์˜ ๊ฒฐ๊ณผ plot์„ ๊ทธ๋ฆฌ๋Š” ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค!! ๋ถˆ๊ณผ ๋ช‡ ๊ฐœ์›”๋งŒ์— ์ด์ œ ์‹ ๊ธฐํ•˜์ง€์•Š๊ณ  ์ด์ •๋„๋Š” ์ด์ œ ๋‹น์—ฐํžˆ ํ•ด์ฃผ๊ฒ ์ง€? ๋ผ๋Š” ๋งˆ์Œ์œผ๋กœ ์ ์‘ํ•ด๊ฐ€๊ณ  ์žˆ๋Š” ๋“ฏํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด์ค€๋‹ค!