下面的代码使用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:ref1,ref2
发布于 2016-06-16 12:16:50
问题是test-framework是隐式多线程的(例如,请参阅这 )。
适合我的解决方案是使用选项defaultMainWithArgs运行--threads=1。
https://stackoverflow.com/questions/32981990
复制相似问题