haskell代码上的任务并行性

时间:2014-04-01 21:34:52

标签: haskell parallel-processing par

我一直在努力理解haskell上并行性的某些方面。我必须对一段代码应用并行性,但我尝试过的尝试都无法正常工作。

功能就是这个:

fft :: [Complex Float] -> [Complex Float]
fft [a] = [a]
fft as = interleave ls rs
  where
    (cs,ds) = bflyS as
    ls = fft cs
    rs = fft ds

interleave [] bs = bs
interleave (a:as) bs = a : interleave bs as

halve as = splitAt n' as
   where
    n' = div (length as + 1) 2

-- twiddle factors
tw :: Int -> Int -> Complex Float
tw n k = cis (-2 * pi * fromIntegral k / fromIntegral n)

bflyS :: [Complex Float] -> ([Complex Float], [Complex Float])
bflyS as = (los,rts)
  where
    (ls,rs) = halve as
    los = zipWith (+) ls rs
    ros = zipWith (-) ls rs
    rts = zipWith (*) ros [tw n i | i <- [0..n - 1]]
    n = length a

我尝试将此功能任务并行的尝试如下:

bflySTask :: [Complex Float] -> ([Complex Float], [Complex Float])
bflySTask as = (los,rts) `using` if n>1000 then  parTuple2 (parListChunk 500 rseq)    (parListChunk 500 rseq) else r0
  where
    (ls,rs) = halve as
    los = zipWith (+) ls rs 
    ros = zipWith (-) ls rs
    n = length as
    fx =  (map (tw (n)) [0..n-1])
    rts =  zipWith (*) ros fx

和Par Monad一起

bflySTask :: [Complex Float] -> ([Complex Float], [Complex Float])
bflySTask as = f as
  where
    (ls,rs) = halve as
    los = zipWith (+) ls rs
    ros = zipWith (-) ls rs
    n = length as
    fx = (map (tw (n)) [0..n-1]) 

    f as = if n>10000 then
                runPar $ do
                v1<-new
            v2<-new
            fork $ put v1 (zipWith (*) ros fx)
            fork $ put v2 (zipWith (+) ls rs)
            a <- get v1
            b <- get v2
            return (a, b)
        else
            (zipWith (+) ls rs, zipWith (*) ros fx)

这两种方法都没有像并行GC工作平衡1.30这样的并行性,而且一些火花已经过GC并且失败了。有没有人知道在不改变数据结构的情况下对这个函数应用并行性有什么不同呢?

0 个答案:

没有答案