本文介绍了并行mapM在Repa阵列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最近中使用 Gibbs sampling ,我一直在使用,在我看来,它为随机数生成提供了一个接近理想的界面。可悲的是,由于无法在地图中使用monadic动作,因此我一直无法使用Repa。



尽管明显的monadic地图一般无法并行化,在我看来, RVar 至少可以是monad中的一个例子,其中可以安全地并行化效果(至少在原则上;我并不熟悉内部工作原理 RVar )。也就是说,我想写下如下内容:

  drawClass :: Sample  - > RVar类
drawClass = ...

drawClasses :: Array U DIM1 Sample - > RVar(Array U DIM1 Class)
drawClasses samples = A.mapM drawClass samples

其中 A.mapM 看起来像是,

  mapM :: ParallelMonad m = > (a  - > m b) - > Array r sh a  - > m(Array r sh b)

显然这将如何工作取决于实现 RVar 及其基础 RandomSource ,原则上可以认为这将涉及为每个产生的线程绘制一个新的随机种子并继续像往常一样。



直觉上来说,这个想法似乎可能会推广到其他一些单子。



我的问题是:是否可以构造一个单元类 ParallelMonad monad,其中的效果可以安全地并行化(至少可以用 RVar )?



它看起来像什么?还有什么其他monad可以居住在这个班级?其他人是否考虑过这种方式在Repa中的可行性?

最后,如果这种并行monadic动作的概念不能一概而论,是否有人会看到任何好方法在 RVar (这里非常有用)的特定情况下做这项工作?为并行性放弃 RVar 是一个非常难以取舍的交易。

解决方案

这可能不是一个好主意,因为PRNG具有固有的连续性。相反,您可能需要如下转换代码:


  1. 声明一个IO函数( main
  2. 阅读尽可能多的随机数。

  3. 将(现在纯净的)数字传递给您修复功能。


In my recent work with Gibbs sampling, I've been making great use of the RVar which, in my view, provides a near ideal interface to random number generation. Sadly, I've been unable to make use of Repa due to the inability to use monadic actions in maps.

While clearly monadic maps can't be parallelized in general, it seems to me that RVar may be at least one example of a monad where effects can be safely parallelized (at least in principle; I'm not terribly familiar with the inner workings of RVar). Namely, I want to write something like the following,

drawClass :: Sample -> RVar Class
drawClass = ...

drawClasses :: Array U DIM1 Sample -> RVar (Array U DIM1 Class)
drawClasses samples = A.mapM drawClass samples

where A.mapM would look something like,

mapM :: ParallelMonad m => (a -> m b) -> Array r sh a -> m (Array r sh b)

While clearly how this would work depends crucially on the implementation of RVar and its underlying RandomSource, in principle one would think that this would involve drawing a new random seed for each thread spawned and proceeding as usual.

Intuitively, it seems that this same idea might generalize to some other monads.

So, my question is: Could one construct a class ParallelMonad of monads for which effects can be safely parallelized (presumably inhabited by, at the least, RVar)?

What might it look like? What other monads might inhabit this class? Have others considered the possibility of how this might work in Repa?

Finally, if this notion of parallel monadic actions can't be generalized, does anyone see any nice way to make this work in the specific case of RVar (where it would be very useful)? Giving up RVar for parallelism is a very difficult trade-off.

解决方案

It's probably not a good idea to do this due to inherently sequential nature of PRNGs. Instead, you might want to transition your code as follows:

  1. Declare an IO function (main, or what have you).
  2. Read as many random numbers as you need.
  3. Pass the (now pure) numbers onto your repa functions.

这篇关于并行mapM在Repa阵列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:11