parallel func onal programming lecture 3
play

Parallel Func+onal Programming Lecture 3 Mary Sheeran - PowerPoint PPT Presentation

Parallel Func+onal Programming Lecture 3 Mary Sheeran with thanks to Simon Marlow for use of slides and to Koen Claessen for the guest appearance


  1. Parallel ¡Func+onal ¡Programming ¡ Lecture ¡3 ¡ Mary ¡Sheeran ¡ with ¡thanks ¡to ¡Simon ¡Marlow ¡for ¡use ¡of ¡slides ¡ and ¡to ¡Koen ¡Claessen ¡for ¡the ¡guest ¡appearance ¡ ¡ h?p://www.cse.chalmers.se/edu/course/pfp ¡ ¡ ¡ ¡

  2. par ¡and ¡pseq ¡ MUST ¡ Pass ¡an ¡unevaluated ¡computa+on ¡to ¡par ¡ ¡It ¡must ¡be ¡somewhat ¡expensive ¡ Make ¡sure ¡the ¡result ¡is ¡not ¡needed ¡for ¡a ¡bit ¡ Make ¡sure ¡the ¡result ¡is ¡shared ¡by ¡the ¡rest ¡of ¡the ¡ program ¡ ¡ ¡ ¡ ¡

  3. par ¡and ¡pseq ¡ MUST ¡ Pass ¡an ¡unevaluated ¡computa+on ¡to ¡par ¡ ¡It ¡must ¡be ¡somewhat ¡expensive ¡ Make ¡sure ¡the ¡result ¡is ¡not ¡needed ¡for ¡a ¡bit ¡ Make ¡sure ¡the ¡result ¡is ¡shared ¡by ¡the ¡rest ¡of ¡the ¡ program ¡ ¡ Demands ¡an ¡opera+onal ¡understanding ¡of ¡program ¡execu+on ¡ ¡ ¡ ¡

  4. Eval ¡monad ¡plus ¡Strategies ¡ Eval ¡monad ¡enables ¡expressing ¡ordering ¡between ¡ instances ¡of ¡par ¡and ¡pseq ¡ ¡ Strategies ¡separate ¡algorithm ¡from ¡parallelisa+on ¡ Provide ¡useful ¡higher ¡level ¡abstrac+ons ¡ But ¡s+ll ¡demand ¡an ¡understanding ¡of ¡laziness ¡

  5. Haskell’11 ¡ 89 ¡

  6. Builds ¡on ¡Koen’s ¡paper ¡ JFP’99 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Call ¡this ¡PMC ¡

  7. the ¡Par ¡Monad ¡ Our ¡goal ¡with ¡this ¡work ¡is ¡to ¡find ¡a ¡parallel ¡programming ¡model ¡ that ¡is ¡expressive ¡enough ¡to ¡subsume ¡Strategies, ¡robust ¡enough ¡to ¡ reliably ¡express ¡parallelism, ¡and ¡accessible ¡enough ¡that ¡non-­‑expert ¡ programmers ¡can ¡achieve ¡parallelism ¡with ¡li?le ¡effort. ¡

  8. Slide ¡by ¡Simon ¡Marlow ¡

  9. IVar ¡ ¡a ¡write-­‑once ¡mutable ¡reference ¡cell ¡ ¡ supports ¡two ¡opera+ons: ¡put ¡and ¡get ¡ ¡ ¡ put ¡assigns ¡a ¡value ¡to ¡the ¡IVar, ¡and ¡may ¡only ¡be ¡executed ¡ once ¡per ¡Ivar ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Subsequent ¡puts ¡are ¡an ¡error ¡ ¡ get ¡waits ¡un+l ¡the ¡IVar ¡has ¡been ¡assigned ¡a ¡value, ¡and ¡then ¡ returns ¡the ¡value ¡

  10. the ¡Par ¡Monad ¡ Implemented ¡as ¡a ¡Haskell ¡library ¡ ¡surprisingly ¡li?le ¡code! ¡ ¡includes ¡a ¡work ¡stealing ¡scheduler ¡ ¡You ¡get ¡to ¡roll ¡your ¡own ¡schedulers! ¡ Programmer ¡has ¡more ¡control ¡than ¡with ¡Strategies ¡ ¡=> ¡less ¡error ¡prone? ¡ Good ¡performance ¡(comparable ¡to ¡Strategies) ¡ ¡par+cularly ¡if ¡granularity ¡is ¡not ¡too ¡small ¡ ¡ ¡

  11. Slide ¡by ¡Simon ¡Marlow ¡

  12. spawn :: NFData a => Par a -> Par (IVar a) spawn p = do i <- new fork (do x <- p; put i x) return i

  13. parMapM :: NFData b => (a -> Par b) -> [a] -> Par [b] parMapM f as = do ibs <- mapM (spawn . f) as mapM get ibs mapM :: (Monad m, Traversable t) => (a -> m b) -> t a -> m (t b) Slide ¡by ¡Simon ¡Marlow ¡

  14. parfib :: Int -> Int -> Par Int parfib n t | n <= 2 = return 1 | n <= t = return $ sfib n | otherwise = do x <- spawn $ parfib (n-1) t y <- spawn $ parfib (n-2) t x' <- get x y' <- get y return (x' + y’) Slide ¡by ¡Simon ¡Marlow ¡

  15. parMapM :: NFData b => (a -> Par b) -> [a] -> Par [b] parMapM f as = do ibs <- mapM (spawn . f) as mapM get ibs

  16. parMapM :: NFData b => (a -> Par b) -> [a] -> Par [b] parMapM f as = do ibs <- mapM (spawn . f) as mapM get ibs Slide ¡by ¡Simon ¡Marlow ¡

  17. parMapM :: NFData b => (a -> Par b) -> [a] -> Par [b] parMapM f as = do ibs <- mapM (spawn . f) as mapM get ibs Slide ¡by ¡Simon ¡Marlow ¡

  18. Slide ¡by ¡Simon ¡Marlow ¡

  19. Slide ¡by ¡Simon ¡Marlow ¡

  20. Slide ¡by ¡Simon ¡Marlow ¡

  21. Slide ¡by ¡Simon ¡Marlow ¡

  22. Create ¡nodes ¡and ¡edges ¡and ¡let ¡the ¡scheduler ¡do ¡the ¡work ¡ ¡ No ¡dependency ¡analysis ¡required! ¡ ¡ Maximum ¡parallelism ¡for ¡li?le ¡programmer ¡effort ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ Dynamic ¡parallelism ¡ ¡ Very ¡nice ¡ J ¡ ¡

  23. Slide ¡by ¡Simon ¡Marlow ¡

  24. Slide ¡by ¡Simon ¡Marlow ¡

  25. Slide ¡by ¡Simon ¡Marlow ¡

  26. Slide ¡by ¡Simon ¡Marlow ¡

  27. Slide ¡by ¡Simon ¡Marlow ¡

  28. Slide ¡by ¡Simon ¡Marlow ¡

  29. Slide ¡by ¡Simon ¡Marlow ¡

  30. Slide ¡by ¡Simon ¡Marlow ¡

  31. Slide ¡by ¡Simon ¡Marlow ¡

  32. Slide ¡by ¡Simon ¡Marlow ¡

  33. Slide ¡by ¡Simon ¡Marlow ¡

  34. Slide ¡by ¡Simon ¡Marlow ¡

  35. Slide ¡by ¡Simon ¡Marlow ¡

  36. Slide ¡by ¡Simon ¡Marlow ¡

  37. Slide ¡by ¡Simon ¡Marlow ¡

  38. Slide ¡by ¡Simon ¡Marlow ¡

  39. Slide ¡by ¡Simon ¡Marlow ¡

  40. Slide ¡by ¡Simon ¡Marlow ¡

  41. Par ¡monad ¡compared ¡to ¡Strategies ¡ Separa+on ¡of ¡func+on ¡and ¡parallelisa+on ¡done ¡ differently ¡ Eval ¡monad ¡and ¡Strategies ¡are ¡advisory ¡ Par ¡monad ¡does ¡not ¡support ¡specula+ve ¡parallelism ¡ as ¡Stategies ¡do ¡ Par ¡monad ¡supports ¡stream ¡processing ¡pipelines ¡ well ¡ ¡ Note: ¡Par ¡monad ¡and ¡Strategies ¡can ¡be ¡combined… ¡

  42. Par ¡Monad ¡easier ¡to ¡use ¡than ¡par? ¡ fork ¡creates ¡one ¡parallel ¡task ¡ Dependencies ¡between ¡tasks ¡represented ¡by ¡Ivars ¡ No ¡need ¡to ¡reason ¡about ¡laziness ¡ ¡put ¡is ¡hyperstrict ¡by ¡default ¡ ¡ ¡ Final ¡sugges+on ¡in ¡Par ¡Monad ¡paper ¡is ¡that ¡maybe ¡par ¡ is ¡suitable ¡for ¡automa+c ¡parallelisa+on ¡ ¡ ¡ ¡

  43. Next ¡ Con+nue ¡working ¡on ¡Lab ¡A ¡(due ¡11.59 ¡April ¡18) ¡ ¡ Friday ¡15.15 ¡EC ¡ ¡ ¡ ¡ ¡Nikita ¡on ¡GHC ¡Heap ¡Internals, ¡ garbage ¡collec+on ¡etc. ¡ ¡ Erlang ¡starts ¡next ¡week ¡(mon ¡and ¡fri) ¡ ¡ Don’t ¡miss ¡ ¡David ¡Duke ¡next ¡Thursday ¡on ¡ Skeletons ¡for ¡Parallel ¡Scien+fic ¡Compu+ng ¡ ¡(very ¡cool) ¡ ¡

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