为什么没有mapM来修复数组? [英] Why is there no mapM for repa arrays?
问题描述
背景
我更多地使用 repa
作为管理工具。我在 Array
中传递 reactive-banana
s AddHandlers
: Array D DIM2(AddHandler Bool)
。
目前我正在使用这个kludge:
mapMArray ::(Monad m,R.Source ra,R.Shape sh)=> (a - > m b) - > Array r sh a - > m(阵列D sh b)
mapMArray f a = do
l < - mapM f。 R.toList $ a
return $ R.fromFunction sh(\ i - > l !! R.toIndex sh i)
where sh = R.extent a
$ c
$ b 所以我可以这样做:
makeNetworkDesc :: Frameworks t => Array D DIM2(AddHandler Bool) - >时刻t()
makeNetworkDesc事件=做
- 输入
aes< - 来自AddHandler事件的mapMArray
- 输出
_ < - mapMArray(reactimate。(print< $>)aes
问题
有没有理由不包含在 repa
?
解决方案基本上出于同样的原因,没有任何东西像 parMapM
并行: mapM
和 mapM _
(或一般的单点动作) destroy 并行。下面是一个简单的例子:
next :: State Int Int
next = modify(+1)>>得到
现在假设 repaMapM
需要对 State
monad中的所有步骤进行排序,如果使用 repaMapM(const next)
。由于这明显违背了并行性(并且还可能导致性能低下),因此它不是修复的一部分。毕竟,高性能和并行性就在这里,在repa的描述中(重点是我的):
Repa提供高性能,常规,多维,形态多态 parallel 数组。
Background
I am using repa
more as a "management" tool. I pass around reactive-banana
s AddHandlers
in an Array
: Array D DIM2 (AddHandler Bool)
.
Currently I am using this kludge:
mapMArray :: (Monad m, R.Source r a, R.Shape sh) => (a -> m b) -> Array r sh a -> m (Array D sh b)
mapMArray f a = do
l <- mapM f . R.toList $ a
return $ R.fromFunction sh (\i -> l !! R.toIndex sh i)
where sh = R.extent a
So I can do something like this:
makeNetworkDesc :: Frameworks t => Array D DIM2 (AddHandler Bool) -> Moment t ()
makeNetworkDesc events = do
-- inputs
aes <- mapMArray fromAddHandler events
-- outputs
_ <- mapMArray (reactimate . (print <$>)) aes
Question
Is there a reason why this is not included in repa
?
解决方案 Basically for the same reason there's nothing like parMapM
in parallel: mapM
and mapM_
(or monadic actions in general) destroy parallelism. Here's a simple example:
next :: State Int Int
next = modify (+1) >> get
Now, a hypothetical repaMapM
needs to sequence all steps in the State
monad, if one would use repaMapM (const next)
. Since this clearly defies parallelism (and can also lead to low performance), it isn't part of repa. After all, high performance and parallelism is right there in repa's description (emphasis mine):
Repa provides high performance, regular, multi-dimensional, shape polymorphic parallel arrays.
这篇关于为什么没有mapM来修复数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!