在我最近的 工作与 Gibbs sampling
,我一直在大量使用的 RVar
,在我看来,提供了一个近乎理想的接口随机数生成。遗憾的是,由于无法在地图中使用单子动作,我无法使用 Repa。
虽然一元映射通常不能并行化,但在我看来,RVar
至少是一个单元映射可以安全并行化的例子(至少在原则上是这样; 我对 RVar
的内部工作原理并不十分熟悉)。也就是说,我想写下这样的东西,
drawClass :: Sample -> RVar Class
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
,其效果可以安全地并行化(可能至少由 RVar
居住) ?
它看起来像什么?还有哪些单子可能存在于这个类中?其他人是否考虑过这种方法在 Repa 的可能性?
最后,如果这种并行一元动作的概念不能被推广,那么在 RVar
的特定情况下(在这种情况下它会非常有用) ,有人认为有什么好的方法可以实现这种功能吗?为了并行性而放弃 RVar
是一个非常困难的权衡。