How do I write a parallel reduction using strategies in Haskell?

后端 未结 3 751
不思量自难忘°
不思量自难忘° 2021-01-02 23:46

In high-performance computing, sums, products, etc are often calculated using a \"parallel reduction\" that takes n elements and completes in O(log n) time

3条回答
  •  梦谈多话
    2021-01-03 00:20

    This seems like a good start:

    parFold :: (a -> a -> a) -> [a] -> a
    parFold f = go
      where
      strategy = parList rseq
    
      go [x] = x
      go xs = go (reduce xs `using` strategy)
    
      reduce (x:y:xs) = f x y : reduce xs
      reduce list     = list   -- empty or singleton list
    

    It works, but parallelism is not so great. Replacing parList with something like parListChunks 1000 helps a bit, but speedup is still limited to under 1.5x on an 8-core machine.

提交回复
热议问题