Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add Par monad sudoku examples

  • Loading branch information...
commit 90a596201a52bb55487db8916d8e7cc648f3e163 1 parent df3b6a9
@simonmar authored
View
10 code/sudoku-par1.hs
@@ -0,0 +1,10 @@
+import Sudoku
+import Control.Exception
+import System.Environment
+import Data.Maybe
+
+main :: IO ()
+main = do
+ [f] <- getArgs
+ grids <- fmap lines $ readFile f
+ print (length (filter isJust (map solve grids)))
View
21 code/sudoku-par2.hs
@@ -0,0 +1,21 @@
+import Sudoku
+import Control.Exception
+import System.Environment
+import Data.Maybe
+import Control.Monad.Par
+
+main :: IO ()
+main = do
+ [f] <- getArgs
+ grids <- fmap lines $ readFile f
+
+ let (as,bs) = splitAt (length grids `div` 2) grids
+
+ print $ length $ filter isJust $ runPar $ do
+ i1 <- new
+ i2 <- new
+ fork $ put i1 (map solve as)
+ fork $ put i2 (map solve bs)
+ as' <- get i1
+ bs' <- get i2
+ return (as' ++ bs')
View
11 code/sudoku-par3.hs
@@ -0,0 +1,11 @@
+import Sudoku
+import Control.Exception
+import System.Environment
+import Data.Maybe
+import Control.Monad.Par
+
+main :: IO ()
+main = do
+ [f] <- getArgs
+ grids <- fmap lines $ readFile f
+ print (length (filter isJust (runPar $ parMap solve grids)))
View
37 code/sudoku-par4.hs
@@ -0,0 +1,37 @@
+import Sudoku
+import Control.Exception
+import System.Environment
+import Data.Maybe
+import Control.Monad.Par
+import Control.DeepSeq
+
+main :: IO ()
+main = do
+ [f,n] <- getArgs
+ grids <- fmap lines $ readFile f
+ print (length (filter isJust (runPar $ parMapChunk (read n) solve grids)))
+
+parMapChunk :: NFData b => Int -> (a -> b) -> [a] -> Par [b]
+parMapChunk n f xs = fmap concat $ parMap (map f) (chunk n xs)
+
+chunk :: Int -> [a] -> [[a]]
+chunk _ [] = []
+chunk n xs = as : chunk n bs where (as,bs) = splitAt n xs
+
+-- No chunks (sudoku-par3):
+-- Total time 43.71s ( 43.73s elapsed)
+-- chunk 100:
+-- Total time 44.43s ( 44.44s elapsed)
+--
+-- No chunks, -N8:
+-- Total time 67.73s ( 8.38s elapsed)
+-- (5.21x)
+-- chunk 10, -N8:
+-- Total time 61.62s ( 7.74s elapsed)
+-- (5.64x)
+-- chunk 100, -N8:
+-- Total time 60.81s ( 7.73s elapsed)
+-- (5.65x)
+-- chunk 1000, -N8:
+-- Total time 61.74s ( 7.88s elapsed)
+-- (5.54x)
Please sign in to comment.
Something went wrong with that request. Please try again.