Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no mapM for repa arrays?

Tags:

haskell

frp

repa

Background

I am using repa more as a "management" tool. I pass around reactive-bananas 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?

like image 271
fho Avatar asked Sep 17 '14 12:09

fho


Video Answer


1 Answers

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.

like image 117
Zeta Avatar answered Oct 27 '22 03:10

Zeta