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

后端 未结 3 725
不思量自难忘°
不思量自难忘° 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:13

    Not sure what your parFold function is supposed to do. If that is intended to be a parallel version of foldr or foldl, I think its definition is wrong.

    parFold :: (a -> a -> a) -> [a] -> a
    
    // fold right in haskell (takes 3 arguments)
    foldr :: (a -> b -> b) -> b -> [a] -> b
    

    Fold applies the same function to each element of the list and accumulates the result of each application. Coming up with a parallel version of it, i guess, would require that the function application to the elements are done in parallel - a bit like what parList does.

        par_foldr :: (NFData a, NFData b) => (a -> b -> b) -> b -> [a] -> b
        par_foldr f z [] = z
        par_foldr f z (x:xs) = res `using` \ _ -> rseq x' `par` rdeepseq res
                           where x' = par_foldr f z xs
                                 res = x `f` x'
    

提交回复
热议问题