以下代码生成(可怕的)"嵌套并行性" 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
不同。
有关我为什么会收到此错误以及如何在进行并行计算时避免错误的任何想法?
答案 0 :(得分:1)
问题在于test-framework
是隐式多线程的(例如,请参阅this。)
适用于我的解决方案是使用选项defaultMainWithArgs
运行--threads=1
。