English 中文(简体)
如何在Haskell使用平行战略
原标题:How to use Parallel Strategies in Haskell

我有一份职能<代码>编号>。 下面是简单的测试案例:

import Control.Parallel.Strategies
import Control.DeepSeq
import System.Environment

frequencyBy :: (a -> b -> Bool) -> [a] -> [b] -> [(a,Int)]
frequencyBy f as bs = map 
    (a ->(a, foldr ( -> if f a b then (+) 1 else id) 0 bs)) as

main :: IO ()
main = do
  x:xs <- getArgs
  let result = frequencyBy (==) [1::Int .. 10000] [1 .. (read x)] `using` 
                 parList rdeepseq
  print $ product $ map snd $ result

我愿同时管理<条码>><>>>>>>>>>> >编号/代码>。 I m 试图通过parList rdeepseq做到这一点。 (main中的所有其他内容只是为了确保并非一切都得到最佳利用。) 然而,这只字塔工作,两条镜头的工作是两倍,一线照相工作。 我不理解我在这里做错的事情。

最佳回答

间接费用可能会放缓,这取决于主线(x)的大小;如果你在每一火焰中重新做的工作与每个公园(当然还有日程安排间接费用等)的铺设时间相当,那么你就会陷入问题。

You could try parListChunk, e.g. parListChunk 64 rdeepseq; you ll have to experiment to figure out which chunk size to use. While your current strategy is creating a spark for every element of the list, parListChunk creates a spark for each chunk of a certain size in the list, and uses the strategy you specify sequentially over each element of that chunk.

顺便提一下,中的<代码> /code> 可能会由于th太高而使事情放缓;例如,

frequencyBy :: (a -> b -> Bool) -> [a] -> [b] -> [(a,Int)]
frequencyBy f as bs = map (a -> (a, sum . map (const 1) . filter (f a) $ bs)) as

应当确定这一点。

当然,与以往一样,确保你重新编辑<代码>-O2,并使用<代码>+RTS-N。

问题回答

我认为,你的平行态度过于微薄。 <代码>parList试图同时评价每个要素,而且对于任何一项要素而言,确实没有什么可做的工作。

当我从<代码>parList改为parListChunk 500时,执行时间会增加近50%;因为Im是双记分机,与它一样。

作为参考,我正在用<代码>x=20000进行测试。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签