Multicore programming in Haskell - Control.Parallel

后端 未结 4 895
误落风尘
误落风尘 2021-02-08 04:25

I\'m trying to learn how to use the Control.Parallel module, but I think I didn\'t get it right.

I\'m trying to run the following code (fibs.hs).

4条回答
  •  庸人自扰
    2021-02-08 04:36

    You're creating an exponential number of sparks (think of how many recursive calls you're creating here). To actually get good parallelism you need to create less parallel work in this case, since your hardware can't handle that many threads (and so GHC doesn't make them).

    The solution is to use a cutoff strategy, as described in this talk: http://donsbot.wordpress.com/2009/09/05/defun-2009-multicore-programming-in-haskell-now/

    Basically, switch over to the straight line version once you reach a certain depth, and use +RTS -sstderr to see how many sparks are being converted, so you can determine if you're wasting work or not.

提交回复
热议问题