Unnecessary concurrency and terminal silliness.
This commit is contained in:
@@ -5,11 +5,16 @@ import Add(safeAddOps,unsafeAddOps,safeSignedAddOps,unsafeSignedAddOps)
|
|||||||
import Base(base)
|
import Base(base)
|
||||||
import BinaryOps(binaryOps)
|
import BinaryOps(binaryOps)
|
||||||
import Compare(comparisons, signedComparisons)
|
import Compare(comparisons, signedComparisons)
|
||||||
|
import Control.Concurrent(forkFinally)
|
||||||
|
import Control.Concurrent.MVar(MVar, newEmptyMVar, newMVar, putMVar, takeMVar)
|
||||||
|
import Control.Monad(replicateM, void)
|
||||||
import Conversions(conversions, signedConversions)
|
import Conversions(conversions, signedConversions)
|
||||||
import CryptoNum(cryptoNum)
|
import CryptoNum(cryptoNum)
|
||||||
import Control.Monad(forM_,unless)
|
import Control.Monad(forM_,unless)
|
||||||
|
import Data.Text.Lazy(Text, pack)
|
||||||
import Division(divisionOps)
|
import Division(divisionOps)
|
||||||
import File(File,Task(..),generateTasks)
|
import File(File,Task(..),generateTasks)
|
||||||
|
import GHC.Conc(getNumCapabilities)
|
||||||
import ModInv(generateModInvOps)
|
import ModInv(generateModInvOps)
|
||||||
import ModOps(modulusOps)
|
import ModOps(modulusOps)
|
||||||
import Multiply(safeMultiplyOps, unsafeMultiplyOps)
|
import Multiply(safeMultiplyOps, unsafeMultiplyOps)
|
||||||
@@ -22,6 +27,7 @@ import System.Environment(getArgs)
|
|||||||
import System.Exit(die)
|
import System.Exit(die)
|
||||||
import System.FilePath(takeDirectory,(</>))
|
import System.FilePath(takeDirectory,(</>))
|
||||||
import System.IO(IOMode(..),withFile)
|
import System.IO(IOMode(..),withFile)
|
||||||
|
import System.ProgressBar(Label(..), Progress(..), ProgressBar, Timing, defStyle, newProgressBar, stylePrefix, updateProgress)
|
||||||
import System.Random(getStdGen)
|
import System.Random(getStdGen)
|
||||||
|
|
||||||
lowestBitsize :: Word
|
lowestBitsize :: Word
|
||||||
@@ -69,17 +75,47 @@ signedFiles = [
|
|||||||
allFiles :: [File]
|
allFiles :: [File]
|
||||||
allFiles = unsignedFiles ++ signedFiles
|
allFiles = unsignedFiles ++ signedFiles
|
||||||
|
|
||||||
|
printLast :: Progress String -> Timing -> Text
|
||||||
|
printLast prog _ = pack (progressCustom prog)
|
||||||
|
|
||||||
|
runThread :: ProgressBar String -> FilePath -> MVar [Task] -> IO (MVar ())
|
||||||
|
runThread pb outputPath mtaskls =
|
||||||
|
do res <- newEmptyMVar
|
||||||
|
void $ forkFinally step (threadDie res)
|
||||||
|
return res
|
||||||
|
where
|
||||||
|
step =
|
||||||
|
do tasks <- takeMVar mtaskls
|
||||||
|
case tasks of
|
||||||
|
[] ->
|
||||||
|
putMVar mtaskls []
|
||||||
|
task : rest ->
|
||||||
|
do putMVar mtaskls rest
|
||||||
|
let target = outputPath </> outputFile task
|
||||||
|
createDirectoryIfMissing True (takeDirectory target)
|
||||||
|
withFile target WriteMode $ \ targetHandle ->
|
||||||
|
writer task targetHandle
|
||||||
|
updateProgress pb (\ p -> p{ progressCustom = outputFile task,
|
||||||
|
progressDone = progressDone p + 1 })
|
||||||
|
step
|
||||||
|
threadDie resmv thrRes =
|
||||||
|
do case thrRes of
|
||||||
|
Left se -> putStrLn ("Thread died: " ++ show se)
|
||||||
|
Right () -> return ()
|
||||||
|
putMVar resmv ()
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main =
|
main =
|
||||||
do args <- getArgs
|
do args <- getArgs
|
||||||
unless (length args == 1) $
|
unless (length args == 1) $
|
||||||
die ("generation takes exactly one argument, the target directory")
|
die ("generation takes exactly one argument, the target directory")
|
||||||
g <- getStdGen
|
g <- getStdGen
|
||||||
let allTasks = generateTasks g allFiles bitsizes
|
let style = defStyle{ stylePrefix = Label printLast }
|
||||||
|
allTasks = generateTasks g allFiles bitsizes
|
||||||
|
progress = Progress 0 total "starting"
|
||||||
total = length allTasks
|
total = length allTasks
|
||||||
forM_ (zip [(1::Word)..] allTasks) $ \ (i, task) ->
|
pb <- newProgressBar style 60 progress
|
||||||
do putStrLn ("[" ++ show i ++ "/" ++ show total ++ "] " ++ outputFile task)
|
chan <- newMVar allTasks
|
||||||
let target = head args </> outputFile task
|
count <- getNumCapabilities
|
||||||
createDirectoryIfMissing True (takeDirectory target)
|
threads <- replicateM count (runThread pb (head args) chan)
|
||||||
withFile target WriteMode $ \ targetHandle ->
|
forM_ threads (\ m -> takeMVar m)
|
||||||
writer task targetHandle
|
|
||||||
@@ -52,8 +52,8 @@ library
|
|||||||
executable generation
|
executable generation
|
||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall -threaded -with-rtsopts=-N
|
||||||
build-depends: base, directory, filepath, generation, random
|
build-depends: base, directory, filepath, generation, random, terminal-progress-bar, text
|
||||||
|
|
||||||
test-suite test-generation
|
test-suite test-generation
|
||||||
type: exitcode-stdio-1.0
|
type: exitcode-stdio-1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user