05.03.2016 Views

Programming in Scala”

fpiscompanion

fpiscompanion

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter notes 20<br />

Notes on chapter 7: Purely functional parallelism<br />

FP has a long history of us<strong>in</strong>g comb<strong>in</strong>ator libraries for express<strong>in</strong>g parallelism, and there are a lot<br />

of variations of the general idea. The ma<strong>in</strong> design choices <strong>in</strong> this sort of library are around how<br />

explicit to make the fork<strong>in</strong>g and jo<strong>in</strong><strong>in</strong>g of parallel computations. That is, should the API force the<br />

programmer to be fully explicit about when parallel tasks are be<strong>in</strong>g forked off <strong>in</strong>to a separate logical<br />

thread, or should this be done automatically? And similarly, when wait<strong>in</strong>g for the results of multiple<br />

logical threads (say, for the implementation of map2), should the order of these jo<strong>in</strong>s be someth<strong>in</strong>g<br />

the programmer explicitly specifies or chosen by the framework?<br />

The library we developed <strong>in</strong> this chapter sits somewhere <strong>in</strong> the middle–it is explicit about where<br />

tasks are forked, but not when tasks are jo<strong>in</strong>ed (notice that map2 picks the order it waits for the two<br />

tasks whose results it is comb<strong>in</strong><strong>in</strong>g). The jo<strong>in</strong> order can be made more explicit. Simon Marlow, one<br />

of the GHC Haskell⁸⁶ developers, discusses this <strong>in</strong> Parallel programm<strong>in</strong>g <strong>in</strong> Haskell with explicit<br />

futures⁸⁷. Also see the full paper, Seq no more: Better Strategies for Parallel Haskell⁸⁸, which does a<br />

nice job of expla<strong>in</strong><strong>in</strong>g some of the history of approaches for parallelism <strong>in</strong> Haskell.<br />

Note that because Scala is a strict-by-default language, be<strong>in</strong>g more explicit about the jo<strong>in</strong> order isn’t<br />

necessarily as beneficial as <strong>in</strong> Haskell. That is, we can get away with reason<strong>in</strong>g about jo<strong>in</strong> order<br />

much like we th<strong>in</strong>k about evaluation <strong>in</strong> normal strict function application.<br />

This style of library isn’t particularly good at express<strong>in</strong>g pipel<strong>in</strong>e parallelism that’s possible when<br />

transform<strong>in</strong>g streams of data. For <strong>in</strong>stance, if we have a Stream[Int] of 10000 items, and we wish<br />

to square each value, then compute a runn<strong>in</strong>g sum of the squared values, there is a potential for<br />

parallelism–as we are squar<strong>in</strong>g values, we can be pass<strong>in</strong>g the squared values off to another consumer<br />

that is emitt<strong>in</strong>g the runn<strong>in</strong>g sum of the values it receives. We’ll be discuss<strong>in</strong>g stream process<strong>in</strong>g and<br />

pipel<strong>in</strong>e parallelism more <strong>in</strong> part 4.<br />

Notes about map fusion<br />

We noted <strong>in</strong> this chapter that one of our laws for Par, sometimes called map fusion, can be used as<br />

an optimization:<br />

map(map(y)(g))(f) == map(y)(f compose g)<br />

That is, rather than spawn<strong>in</strong>g a separate parallel computation to compute the second mapp<strong>in</strong>g, we<br />

can fold it <strong>in</strong>to the first mapp<strong>in</strong>g. We mentioned that our representation of Par doesn’t allow for<br />

this, as it’s too ‘opaque’. If we make Par a proper data type and give it constructors that we can<br />

pattern match on, then it’s easy to implement map fusion:<br />

⁸⁶http://www.haskell.org/ghc/<br />

⁸⁷http://ghcmutter<strong>in</strong>gs.wordpress.com/2010/08/20/parallel-programm<strong>in</strong>g-<strong>in</strong>-haskell-with-explicit-futures/<br />

⁸⁸http://www.haskell.org/~simonmar/papers/strategies.pdf

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!