regular array computation in haskell on gpus
play

Regular Array Computation in Haskell on GPUs Geoffrey Mainland - PowerPoint PPT Presentation

Regular Array Computation in Haskell on GPUs Geoffrey Mainland Microsoft Research Ltd WG 2.8, November 2012 Regular Array Computation in Haskell on GPUs Geoffrey Mainland Microsoft Research Ltd WG 2.8, November 2012 Regular Array


  1. Regular Array Computation in Haskell on GPUs Geoffrey Mainland Microsoft Research Ltd WG 2.8, November 2012

  2. Regular Array Computation in Haskell on GPUs Geoffrey Mainland Microsoft Research Ltd WG 2.8, November 2012

  3. Regular Array Computation in Haskell (on CPUs) in Haskell. representation—easier reasoning about cost. Can we export this programming model to GPUs? ▶ Repa a fantastic library for writing regular array computations ▶ The type of an array tells the programmer about its ▶ Automatic parallelization ▶ Produces very efficient code.

  4. Regular Array Computation in Haskell (on CPUs) in Haskell. representation—easier reasoning about cost. Can we export this programming model to GPUs? ▶ Repa a fantastic library for writing regular array computations ▶ The type of an array tells the programmer about its ▶ Automatic parallelization ▶ Produces very efficient code.

  5. Computing the Mandelbrot Set bounded. point c escapes. a fixed limit, in which case we declare that the point is an ostensible member of the Mandelbrot set z 0 = 0 z i + 1 = z 2 i + c ▶ The point c is a member of the Mandelbrot set iff the z i ’s are ▶ If z i > 2 for some i , then the z i ’s are not bounded, i.e., the ▶ Iterate the computation of z i until it escapes or until we reach

  6. Representation . Shape . type R type Complex type StepPlane r r . . = Double = ( R , R ) type ComplexPlane r = Array r DIM2 Complex = Array . DIM2 ( Complex , Int )

  7. Shape . type R type Complex type StepPlane r r . . = Double = ( R , R ) type ComplexPlane r = Array r DIM2 Complex = Array . DIM2 ( Complex , Int ) Representation .

  8. type R type Complex type StepPlane r r . . = Double = ( R , R ) type ComplexPlane r = Array r DIM2 Complex = Array . DIM2 ( Complex , Int ) Representation . Shape .

  9. where Computing c and z 1 genPlane :: R → R → R → R → Int → Int → ComplexPlane D genPlane lowx lowy highx highy viewx viewy = fromFunction ( Z : . viewy : . viewx ) $ λ ( Z : . ( ! y ) : . ( ! x )) → ( lowx + ( fromIntegral x ∗ xsize ) / fromIntegral viewx , lowy + ( fromIntegral y ∗ ysize ) / fromIntegral viewy ) xsize , ysize :: R xsize = highx − lowx ysize = highy − lowy

  10. where Computing c and z 1 where {-# INLINE f #-} genPlane :: R → R → R → R → Int → Int → ComplexPlane D genPlane lowx lowy highx highy viewx viewy = fromFunction ( Z : . viewy : . viewx ) $ λ ( Z : . ( ! y ) : . ( ! x )) → ( lowx + ( fromIntegral x ∗ xsize ) / fromIntegral viewx , lowy + ( fromIntegral y ∗ ysize ) / fromIntegral viewy ) xsize , ysize :: R xsize = highx − lowx ysize = highy − lowy mkinit :: ComplexPlane U → StepPlane D mkinit cs = map f cs f :: Complex → ( Complex , Int ) f z = ( z , 0 )

  11. m Array r2 sh e {-# INLINE next #-} Array r1 sh e Load r1 sh e Target r2 e Source r2 e Monad m computeP where where {-# INLINE stepPoint #-} Computing z i + 1 = z 2 i + c step :: ComplexPlane U → StepPlane U → IO ( StepPlane U ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Int ) → ( Complex , Int ) stepPoint ! c ( ! z , ! i ) = if magnitude z ′ > 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next ! c ! z = c + ( z ∗ z )

  12. m Array r2 sh e where Array r1 sh e Load r1 sh e Target r2 e Source r2 e Monad m where computeP {-# INLINE stepPoint #-} {-# INLINE next #-} Computing z i + 1 = z 2 i + c step :: ComplexPlane U → StepPlane U → IO ( StepPlane U ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Int ) → ( Complex , Int ) stepPoint ! c ( ! z , ! i ) = if magnitude z ′ > 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next ! c ! z = c + ( z ∗ z ) zipWith :: ( Shape sh , Source r1 a , Source r2 b ) ⇒ ( a → b → c ) → Array r1 sh a → Array r2 sh b → Array D sh c

  13. {-# INLINE stepPoint #-} where where {-# INLINE next #-} Computing z i + 1 = z 2 i + c step :: ComplexPlane U → StepPlane U → IO ( StepPlane U ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Int ) → ( Complex , Int ) stepPoint ! c ( ! z , ! i ) = if magnitude z ′ > 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next ! c ! z = c + ( z ∗ z ) computeP :: ( Load r1 sh e , Target r2 e , Source r2 e , Monad m ) ⇒ Array r1 sh e → m ( Array r2 sh e )

  14. Putting it all together where mandelbrot :: R → R → R → R → Int → Int → Int → IO ( StepPlane U ) mandelbrot lowx lowy highx highy viewx viewy depth = do cs ← computeP $ genPlane lowx lowy highx highy viewx viewy zs1 ← computeP $ mkinit cs iterateM ( step cs ) depth zs1 iterateM :: Monad m ⇒ ( a → m a ) → Int → a → m a iterateM f = loop loop 0 x = return x loop n x = f x > = loop ( n − 1 ) >

  15. Nikola switcheroo import qualified Prelude as P Nikola import qualified Prelude as P import Prelude hiding map zipWith import Data Array Nikola Backend CUDA import Data Array Nikola Eval ▶ Repa import Prelude hiding ( map , zipWith ) import Data . Array . Repa

  16. Nikola switcheroo import qualified Prelude as P import qualified Prelude as P ▶ Repa import Prelude hiding ( map , zipWith ) import Data . Array . Repa ▶ Nikola import Prelude hiding ( map , zipWith ) import Data . Array . Nikola . Backend . CUDA import Data . Array . Nikola . Eval

  17. Nikola switcheroo type R type Complex type StepPlane r = Double = ( Exp R , Exp R ) type ComplexPlane r = Array r DIM2 Complex = Array r DIM2 ( Complex , Exp Int32 )

  18. . where New operator, New monad, P. where New representation, G. Computing z i + 1 = z 2 i + c in Nikola step :: ComplexPlane G → StepPlane G → P ( StepPlane G ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Exp Int32 ) → ( Complex , Exp Int32 ) stepPoint c ( z , i ) = if magnitude z ′ > ∗ 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next c z = c + ( z ∗ z )

  19. . where New operator, New monad, P. where Computing z i + 1 = z 2 i + c in Nikola step :: ComplexPlane G → StepPlane G → P ( StepPlane G ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Exp Int32 ) → ( Complex , Exp Int32 ) stepPoint c ( z , i ) = if magnitude z ′ > ∗ 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next c z = c + ( z ∗ z ) ▶ New representation, G.

  20. . where New operator, where Computing z i + 1 = z 2 i + c in Nikola step :: ComplexPlane G → StepPlane G → P ( StepPlane G ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Exp Int32 ) → ( Complex , Exp Int32 ) stepPoint c ( z , i ) = if magnitude z ′ > ∗ 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next c z = c + ( z ∗ z ) ▶ New representation, G. ▶ New monad, P.

  21. where where Computing z i + 1 = z 2 i + c in Nikola step :: ComplexPlane G → StepPlane G → P ( StepPlane G ) step cs zs = computeP $ zipWith stepPoint cs zs stepPoint :: Complex → ( Complex , Exp Int32 ) → ( Complex , Exp Int32 ) stepPoint c ( z , i ) = if magnitude z ′ > ∗ 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next c z = c + ( z ∗ z ) ▶ New representation, G. ▶ New monad, P. ▶ New operator, > ∗ .

  22. where Computing c and z 1 in Nikola genPlane :: Exp R → Exp R → Exp R → Exp R → Exp Int32 → Exp Int32 → P ( ComplexPlane G ) genPlane lowx lowy highx highy viewx viewy = computeP $ fromFunction ( Z : . viewy : . viewx ) $ λ ( Z : . y : . x ) → ( lowx + ( fromInt x ∗ xsize ) / fromInt viewx , lowy + ( fromInt y ∗ ysize ) / fromInt viewy ) xsize , ysize :: Exp R xsize = highx − lowx ysize = highy − lowy

  23. where where Computing c and z 1 in Nikola genPlane :: Exp R → Exp R → Exp R → Exp R → Exp Int32 → Exp Int32 → P ( ComplexPlane G ) genPlane lowx lowy highx highy viewx viewy = computeP $ fromFunction ( Z : . viewy : . viewx ) $ λ ( Z : . y : . x ) → ( lowx + ( fromInt x ∗ xsize ) / fromInt viewx , lowy + ( fromInt y ∗ ysize ) / fromInt viewy ) xsize , ysize :: Exp R xsize = highx − lowx ysize = highy − lowy mkinit :: ComplexPlane G → P ( StepPlane G ) mkinit cs = computeP $ map f cs f :: Complex → ( Complex , Exp Int32 ) f z = ( z , 0 )

  24. Calling Nikola functions import qualified Mandelbrot . NikolaV1 . Implementation as I step :: ComplexPlane CUF → StepPlane CUF → IO ( StepPlane CUF ) step = $ ( compile I . step ) genPlane :: R → R → R → R → Int32 → Int32 → IO ( ComplexPlane CUF ) genPlane = $ ( compile I . genPlane ) mkinit :: ComplexPlane CUF → IO ( StepPlane CUF ) mkinit = $ ( compile I . mkinit )

  25. In-place update where where step :: ComplexPlane G → MStepPlane G → P () step cs mzs = do zs ← unsafeFreezeMArray mzs loadP ( zipWith stepPoint cs zs ) mzs stepPoint :: Complex → ( Complex , Exp Int32 ) → ( Complex , Exp Int32 ) stepPoint c ( z , i ) = if magnitude z ′ > ∗ 4 . 0 then ( z , i ) else ( z ′ , i + 1 ) z ′ = next c z next :: Complex → Complex → Complex next c z = c + ( z ∗ z )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend