Permalink
Browse files

add Par monad sudoku examples

  • Loading branch information...
simonmar committed Sep 12, 2011
1 parent df3b6a9 commit 90a596201a52bb55487db8916d8e7cc648f3e163
Showing with 79 additions and 0 deletions.
  1. +10 −0 code/sudoku-par1.hs
  2. +21 −0 code/sudoku-par2.hs
  3. +11 −0 code/sudoku-par3.hs
  4. +37 −0 code/sudoku-par4.hs
View
@@ -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
@@ -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
@@ -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
@@ -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)

0 comments on commit 90a5962

Please sign in to comment.