Why shouldn't I use F# asynchronous workflows for parallelism?

后端 未结 3 876
执念已碎
执念已碎 2021-01-31 19:41

I have been learning F# recently, being particularly interested in its ease of exploiting data parallelism. The data |> Array.map |> Async.Parallel |> Async.RunSy

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 20:33

    So why shouldn't I use async to execute parallel data processes?

    If you have a tiny number of completely independent non-async tasks and lots of cores then there is nothing wrong with using async to achieve parallelism. However, if your tasks are dependent in any way or you have more tasks than cores or you push the use of async too far into the code then you will be leaving a lot of performance on the table and could do a lot better by choosing a more appropriate foundation for parallel programming.

    Note that your example can be written even more elegantly using the TPL from F# though:

    Array.Parallel.map f xs
    

    What am I going to lose by writing parallel async code instead of using PLINQ or TPL?

    You lose the ability to write cache oblivious code and, consequently, will suffer from lots of cache misses and, therefore, all cores stalling waiting for shared memory which means poor scalability on a multicore.

    The TPL is built upon the idea that child tasks should execute on the same core as their parent with a high probability and, therefore, will benefit from reusing the same data because it will be hot in the local CPU cache. There is no such assurance with async.

提交回复
热议问题