Why is there no mapM for turnip arrays?

Background

I use repa more as a management tool. I am passing reactive-banana AddHandlers to Array : Array D DIM2 (AddHandler Bool) .

I am currently using this kludge:

 mapMArray :: (Monad m, R.Source ra, R.Shape sh) => (a -> mb) -> Array r sh a -> m (Array D sh b) mapMArray fa = 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 ?

+5
source share
1 answer

In principle, for the same reason there is nothing like parMapM in parallel : mapM and mapM_ (or monadic actions in general) destroy parallelism. Here is a simple example:

 next :: State Int Int next = modify (+1) >> get 

Now, the hypothetical repaMapM should follow the sequence of all steps in the State monad if repaMapM (const next) . Since this is clearly not consistent with parallelism (and may also lead to poor performance), it is not part of repa. In the end, high performance and parallelism are right here in the description of repa (emphasis mine):

Turnip provides high-performance , regular, multi-dimensional, polymorphic parallel arrays.

+7
source

Source: https://habr.com/ru/post/1202763/


All Articles