module Control.Parallel.Stochastic
( purifyRandomST
, ParallelSeeds
, parMapST
, splitParMapST
)
where
import Control.Monad.ST
import Control.Parallel.Strategies
import System.Random.MWC
import Data.Splittable
purifyRandomST :: (forall s.GenST s -> ST s a) -> Seed -> (a, Seed)
purifyRandomST f seed = runST $ do
g <- restore seed
r <- f g
g' <- save g
return (r, g')
type RandomFunction source result = (forall s.GenST s -> source -> ST s result)
parMapST :: RandomFunction a b -> [(a, Seed)] -> [(b, Seed)]
parMapST f = parMap rpar (\(p, seed) -> purifyRandomST (`f` p) seed)
splitParMapST :: (Split source, Combine result) =>
RandomFunction source result
-> source
-> ParallelSeeds
-> (result, ParallelSeeds)
splitParMapST f wholeSource oldSeeds =
let
sources = (splitIn (length oldSeeds) wholeSource)
(results, newSeeds) = unzip $ parMapST f $ zip sources oldSeeds
in
(combine results, newSeeds)
type ParallelSeeds = [Seed]