Skip to content

Latest commit

ย 

History

History
107 lines (81 loc) ยท 5.31 KB

Swift_Accelerate.md

File metadata and controls

107 lines (81 loc) ยท 5.31 KB

Accelerate ํ”„๋ ˆ์ž„์›Œํฌ๋ž€?

  • CPU์—์„œ ์‹คํ–‰๋˜๋Š” ์ด๋ฏธ์ง€ ๋ฐ ์‹ ํ˜ธ ์ฒ˜๋ฆฌ, ๋ฒกํ„ฐ ์‚ฐ์ˆ , ์„ ํ˜• ๋Œ€์ˆ˜ ๋ฐ ๊ธฐ๊ณ„ ํ•™์Šต์„ ์ง€์›ํ•˜๋Š” ์ˆ˜์ฒœ ๊ฐœ์˜ ์ €์ˆ˜์ค€ ์ˆ˜ํ•™์  ์˜ˆ์ธก์น˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ๋›ฐ์–ด๋‚œ ์„ฑ๋Šฅ์„ ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด Swift API๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

  • vDSP : ํฐ ๋ฒกํ„ฐ์— ๋Œ€ํ•œ ์‚ฐ์ˆ , ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜, 2 ์ฐจ ํ•„ํ„ฐ๋ง ๋ฐ ๊ฐ•๋ ฅํ•œ ์œ ํ˜• ๋ณ€ํ™˜์„ ํฌํ•จํ•œ ๋””์ง€ํ„ธ ์‹ ํ˜ธ ์ฒ˜๋ฆฌ ๋ฃจํ‹ด์„ ์ œ๊ณต
  • vForce. ์‚ผ๊ฐ๋ฒ•๊ณผ ๋กœ๊ทธ ๋ฃจํ‹ด์„ ํฌํ•จํ•˜์—ฌ ์‚ฐ์ˆ  ๋ฐ ์ดˆ์›” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” vForce.
  • Quadrature, ํ•จ์ˆ˜์˜ ์ˆ˜์น˜ ์ ๋ถ„
  • vImage๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ ํ•ต์‹ฌ ๊ทธ๋ž˜ํ”ฝ ๋ฐ ํ•ต์‹ฌ ๋น„๋””์˜ค์™€ ์‰ฝ๊ฒŒ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€์†์€ ๋ฒกํ„ฐํ™”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ ์ด์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.

Accelerate๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ ๋‹จ์ผ ๋ช…๋ น ๋‹ค์ค‘ ๋ฐ์ดํ„ฐ ๋˜๋Š” simD ๋ ˆ์ง€์Šคํ„ฐ์—์„œ ๊ณ„์‚ฐ์ด ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ ˆ์ง€์Šคํ„ฐ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•ญ๋ชฉ์„ ๋‹จ์ผ ๋ ˆ์ง€์Šคํ„ฐ๋กœ ํŒจํ‚นํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ๋™์ผํ•œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹จ์ผ 128 ๋น„ํŠธ ๋ ˆ์ง€์Šคํ„ฐ๋Š” ์‹ค์ œ๋กœ 4 ๊ฐœ์˜ 32 ๋น„ํŠธ ๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฒกํ„ฐํ™” ๋œ ๊ณฑ์…ˆ ์—ฐ์‚ฐ์€ ํ•œ ๋ฒˆ์— 4 ์Œ์˜ ์š”์†Œ๋ฅผ ๋™์‹œ์— ๊ณฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ž‘์—…์ด ๋” ๋นจ๋ผ์งˆ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—๋„ˆ์ง€ ํšจ์œจ๋„ ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค.

vDSP

  • ํ‘ธ๋ฆฌ์— ๋ณ€ํ™˜,
  • 2 ์ฐจ ํ•„ํ„ฐ๋ง,
  • Convolution ๋ฐ ์ƒ๊ด€ ๊ด€๊ณ„ ๋ถ„์„
  • ๋ฒกํ„ฐํ™” ๋œ ๋””์ง€ํ„ธ ์‹ ํ˜ธ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ
  • ์š”์†Œ ๋ณ„ ์‚ฐ์ˆ  ๋ฐ ํ˜•์‹ ๋ณ€ํ™˜

๋”ฐ๋ผ์„œ ์˜ˆ๋ฅผ ๋“ค์–ด ๋‘ ์‹ ํ˜ธ์˜ ์ผ๊ด€์„ฑ์„ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€์—†๋Š” ๊ฒฝ์šฐ์—๋„ vDSP์˜ ์ผ๋ฐ˜ ๊ณ„์‚ฐ ๋ฃจํ‹ด์€ ์•ฑ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ์†”๋ฃจ์…˜์„ ์ œ๊ณต ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ : ๋ชจ๋“  ๋ฐฐ์—ด์— ๋Œ€ํ•ด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ + ํ‰๊ท 

result[i] = (a[i] + b[i]) * (c[i] - d[i])
  • for ๊ตฌ๋ฌธ์œผ๋กœ ๋ณ€ํ™˜
  • vDSP ํด๋ž˜์‹ API๋ฅผ ์‚ฌ์šฉ
var result = [Float](repeating: 0, count: n)
vDSP_vasbm(a, 1, b, 1, c, 1, d, 1, &result, 1, vDSP_Length(result.count))
  • vDSP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 4 ๊ฐœ์˜ ๋ฃจํ”„๋ณด๋‹ค ์•ฝ 3 ๋ฐฐ ๋น ๋ฆ…๋‹ˆ๋‹ค.
  • ์ƒˆ๋กœ์šด vDSP ์šฉ Swift API๋ฅผ ์‚ฌ์šฉ
var result = [Float](repeating: 0, count: n)
vDSP_multiply(addition: (a, b), subtraction: (c, d), result: &result)
  • ์นด์šดํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ „๋‹ฌํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋‹ˆ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ณ  ๊ฐ„๊ฒฐํ•จ.

vDSP for Type Conversion

Double ๋ฐฐ์—ด์„ 16๋น„ํŠธ ์ธ์ˆ˜๊ฐ’์œผ๋กœ ๋ฐ˜์˜ฌ๋ฆผํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด

let result = source.map {
  return UInt16($0.rounded(.towardZero))
}

vDSP : ์ด์ „๋ณด๋‹ค 4๋ฐฐ ๋น ๋ฆ„

Let result = vDSP.floatingPointToInteger (source, integerType: UInt16.self,
Rounding: .towardZero)

vDSP for Discrete Fourier transform

  • ๋ฆฌ์†Œ์Šค ํ•ด์ œ๋ฅผ ๊ฑฑ์ •ํ•  ํ•„์š” ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.
  • ๊ณ ์œ ์˜ transform ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•จ

vDSP for Diquadratic Filtering

  • ์˜ค๋””์˜ค ์ž‘์—…์„ ํ•˜๋ฉด 2์ฐจ ํ•„ํ„ฐ๋ง ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋•Œ biquard๊ธฐ๋Šฅ์„ ์จ์„œ ์ €์ฃผํŒŒ์ˆ˜/๊ณ ์ฃผํŒŒ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค.

์ƒˆ๋กœ์šด API

var biquad = vDSP.Biquad(coefficients: [b0, b1, b2, a1, a2, b0, b1, b2, a1, a2], channelCount: channelCount,
sectionCount: sections,
ofType: Float.self
)
  • ๊ณ„์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๊ณ  ์ฑ„๋„๊ณผ ์„น์…˜ ์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ์ž๋™ ํ•„ํ„ฐ๋ง ํ•ด์ฅผ

vForce

  • Arithmetic functions: floor, ceil, abs, remainder โ€ฆ

  • Exponential and logarithmic functions: exp, log, โ€ฆ

  • Trigonometric functions: sin, cos, tan, โ€ฆ

  • Hyperbolic functions: sing, asking, โ€ฆ

    • ์ œ๊ณฑ๊ทผ(sqrt)์„ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง์€ ๊ธฐ์กด map ์‚ฌ์šฉ ๋กœ์ง๋ณด๋‹ค ์ตœ๋Œ€ 10๋ฐฐ ๋น ๋ฅธ ๋ฒกํ„ฐํ™”๋œ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
let result = vForce.sqrt(a)

Quadrature (๊ตฌ์ ๋ฒ•)

์œ ํ•œ ๋˜๋Š” ๋ฌดํ•œ ๊ฐ„๊ฒฉ์— ๋Œ€ํ•œ ๋ช…ํ™•ํ•œ ์ ๋ถ„ ํ•จ์ˆ˜์˜ ๊ทผ์‚ฌ๊ฐ’์„ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜

  • ์ƒˆ๋กœ์šด API๋Š” ๊ธฐ์กด ์ฝ”๋“œ์— ๋น„ํ•ด ์ƒ๋‹นํžˆ ๋‹จ์ˆœํ•ด์ง€๊ณ  ๊ทธ ์–‘๋„ ์ค„์—ˆ์Œ
  • ํ†ตํ•ฉ ํ•จ์ˆ˜๋ฅผ C ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

vImage

Core Graphic, Core Video, Alpha blending, Format conversions, Histogram operation, Geometry, Morphology ๋ฅผ ์œ„ํ•œ api ์ œ๊ณต

  • Throwable initializer๋ฅผ ์‚ฌ์šฉํ•จ.
  • Any to any : Core Video์™€ Core Graphic ๊ฐ„ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก API๋ฅผ ์ œ๊ณต
  • ๋น„๋””์˜ค ํ”ฝ์…€ ๋ฒ„ํผ๋ฅผ ์ด๋ฏธ์ง€๋กœ ์†์‰ฝ๊ฒŒ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” API ์ œ๊ณต

summary

Accelerate ํ”„๋ ˆ์ž„ ์›Œํฌ๊ฐ€ ๋™์ผํ•œ ํ”Œ๋žซํผ์—์„œ Eigen๋ณด๋‹ค ๊ฑฐ์˜ 2.5 ๋ฐฐ ๋น ๋ฆ…๋‹ˆ๋‹ค.

Accelerate ํ”„๋ ˆ์ž„ ์›Œํฌ๊ฐ€ ํ”Œ๋žซํผ์— ๋งž๊ฒŒ ์กฐ์ •๋˜์–ด ํ”Œ๋žซํผ์ด ์ œ๊ณต ํ•  ์ˆ˜์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์™„์ „ํžˆ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์•ฑ์—์„œ Accelerate๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ด ์„ฑ๋Šฅ์€ ์—๋„ˆ์ง€ ์†Œ๋น„๋ฅผ ์ค„์ž„์œผ๋กœ์จ ๋ฐฐํ„ฐ๋ฆฌ ์ˆ˜๋ช…์ด ํ–ฅ์ƒ๋˜๊ณ  ์‚ฌ์šฉ์ž์—๊ฒŒ ๋” ๋‚˜์€ ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์š”์•ฝํ•˜์ž๋ฉด Accelerate๋Š” ๋น ๋ฅด๊ณ  ์—๋„ˆ์ง€ ํšจ์œจ์ ์ธ ๋Œ€๊ทœ๋ชจ ์ˆ˜ํ•™ ๊ณ„์‚ฐ ๋ฐ ์ด๋ฏธ์ง€ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ œ Accelerate์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งค์šฐ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” Swift ์นœํ™”์  ์ธ API๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ํ•ด๋‹น ์„ฑ๋Šฅ ๋ฐ ์—๋„ˆ์ง€ ํšจ์œจ์„ฑ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋กํ•ฉ๋‹ˆ๋‹ค.