修复中的嵌套并行性

时间:2015-10-07 00:44:34

标签: haskell repa

以下代码生成(可怕的)"嵌套并行性" repa-3.4.0.1错误:

import Control.Monad.Identity (runIdentity, liftM)
import Data.Array.Repa              as R
import Data.Array.Repa.Repr.Unboxed
import Data.Vector.Unboxed
import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.QuickCheck hiding (generate,output)

main :: IO ()
main = defaultMainWithArgs [prop,prop] ["--maximum-generated-tests=1"]

prop = testProperty "Test" $ property prop_fmap

prop_fmap :: Arr Int -> Bool
prop_fmap x = fmapT id x == x




newtype Arr r = Arr (Array U DIM1 r) deriving (Eq, Show)

instance (Arbitrary r, Unbox r) => Arbitrary (Arr r) where
    arbitrary = replM arbitrary
    shrink = shrinkNothing

replM :: (Unbox r, Monad mon) => mon r -> mon (Arr r)
replM = let n = 6
        in liftM (Arr . fromUnboxed (Z:.n)) . replicateM n

fmapT :: (Unbox a, Unbox b) => (a -> b) -> Arr a -> Arr b
fmapT f (Arr v) = Arr $ force' $ R.map f $ v

force' :: (Shape sh, Unbox r) => Array D sh r -> Array U sh r
force' = runIdentity . computeP

确切的错误是:

Performing nested parallel computation sequentially.   You've probably
called the 'compute' or 'copy' function while another   instance was
already running. This can happen if the second version   was suspended
due to lazy evaluation. Use 'deepSeqArray' to ensure   that each array
is fully evaluated before you 'compute' the next one. 

我正在使用ghc Main -threaded进行编译并使用Main +RTS -N2运行。我在deepSeqArray的定义中尝试使用fmapT,但它没有帮助。由于测试是独立的(除了对随机性进行排序)之外,在这个例子中,不清楚嵌套并行性是如何可行的。

有趣的是,如果我将main更改为quickCheck prop_fmap,则会成功完成100次测试。因此,似乎有一些与测试框架相关的事情(可能是monad测序的一般问题)与QuickCheck不同。

有关我为什么会收到此错误以及如何在进行并行计算时避免错误的任何想法?

注意:使用Identity monad:ref1ref2

1 个答案:

答案 0 :(得分:1)

问题在于test-framework是隐式多线程的(例如,请参阅this。)

适用于我的解决方案是使用选项defaultMainWithArgs运行--threads=1