summaryrefslogtreecommitdiffstats
path: root/app/Parallel.hs
diff options
context:
space:
mode:
Diffstat (limited to 'app/Parallel.hs')
-rw-r--r--app/Parallel.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/app/Parallel.hs b/app/Parallel.hs
new file mode 100644
index 0000000..02252ff
--- /dev/null
+++ b/app/Parallel.hs
@@ -0,0 +1,22 @@
+{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
+
+module Parallel
+ ( streamsOf,
+ parMapM,
+ )
+where
+
+import Control.Concurrent.ParallelIO.Local (parallel, withPool)
+import GHC.Conc (numCapabilities)
+
+streamsOf :: Int -> [a] -> [[a]]
+streamsOf 1 xs = [xs]
+streamsOf n xs
+ | n > 0 = [everyN k xs | k <- [0 .. n - 1]]
+ | otherwise = []
+ where
+ everyN k xs = map snd $ filter ((== k) . (`mod` n) . fst) $ zip [0 ..] xs
+
+parMapM :: (a -> IO b) -> [a] -> IO [b]
+parMapM f xs = do
+ withPool numCapabilities $ \pool -> parallel pool (map f xs)