From 75ace0b26691848b9a16a9a0ec1880110e8262b8 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Thu, 11 Jan 2024 03:27:17 +0100 Subject: feat: add `check` cmd --- app/Parallel.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/Parallel.hs (limited to 'app/Parallel.hs') 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) -- cgit v1.2.3