A parallel monad map in Haskell? Something like parMapM?

I'm looking for a way to run two computations in parallel in the ST-Monad. I am building a rather large array (using STUArray) and I would like to do it in parallel.

So far I've found this and this Q&A here on stackoverflow, however the first does not apply in my case, as it deals with pure code only and the second deals with the IO monad - but I am in a State Thread.

I've also found the monad-parallel package, but it requires me to have an instance of 'MonadParallel' for ST. Also the monad-par package does support only pure computations or the IO monad.

Is there a way to do a parallel monadic computation inside ST ?


First of all, by just two words out of your question: parallel and array - I must recommend you to take a look at repa . Also you should check out Data Parallel Haskell , as it promises to be a next huge milestone on the Haskell's road and there are some great people involved with this project.

Concerning your specific question, there are libraries able to do exactly what you ask for, just with an IO monad, the already named monad-parallel and async with mapConcurrently . Have you considered using stToIO to escape into IO ?

There's also a lifted-async library, which expands the standard version to work with MonadBaseControl , which has an instance of ST , so you can probably use its version of mapConcurrently or at least use it as an inspiration to implement your own.


I'm not sure if you can safely parallelize an ST monad or if that even would make sense, since usually, a computation in a state monad depends on the state, which is the result of previous computations.

What you could do however, would be to create the array from a list, and list creation is easily made parallel eg by something like parMap from the parallel package.

Giving us some more details about how you create the array data might help give you a better answer.

链接地址: http://www.djcxy.com/p/68030.html

上一篇: 在并行程序中保留“随机性”

下一篇: Haskell中的并行monad映射? 像parMapM?