Skip to content

Commit

Permalink
display available and selected backend at demo startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcdonell committed Mar 5, 2013
1 parent 8d8beef commit e72a6d4
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 115 deletions.
74 changes: 55 additions & 19 deletions examples/crystal/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
module Config (

Options, optBackend, optSize, optZoom, optScale, optDegree, optBench,
processArgs, run
parseArgs, run

) where

import Data.Char
import Data.List
import Data.Label
import System.Exit
import System.Console.GetOpt
import qualified Criterion.Main as Criterion
import qualified Criterion.Config as Criterion

import Data.Array.Accelerate ( Arrays, Acc )
import qualified Data.Array.Accelerate.Interpreter as Interp
#ifdef ACCELERATE_CUDA_BACKEND
Expand Down Expand Up @@ -59,14 +64,17 @@ run opts f = case _optBackend opts of
CUDA -> CUDA.run1 f
#endif


options :: [OptDescr (Options -> Options)]
options =
backends :: [OptDescr (Options -> Options)]
backends =
[ Option [] ["interpreter"] (NoArg (set optBackend Interpreter)) "reference implementation (sequential)"
#ifdef ACCELERATE_CUDA_BACKEND
, Option [] ["cuda"] (NoArg (set optBackend CUDA)) "implementation for NVIDIA GPUs (parallel)"
#endif
, Option [] ["size"] (ReqArg (set optSize . read) "INT") "visualisation size (200)"
]

options :: [OptDescr (Options -> Options)]
options = backends ++
[ Option [] ["size"] (ReqArg (set optSize . read) "INT") "visualisation size (200)"
, Option [] ["zoom"] (ReqArg (set optZoom . read) "INT") "pixel replication factor (3)"
, Option [] ["scale"] (ReqArg (set optScale . read) "FLOAT") "feature size of visualisation (30)"
, Option [] ["degree"] (ReqArg (set optDegree . read) "INT") "number of waves to sum for each point (5)"
Expand All @@ -75,19 +83,47 @@ options =
]


processArgs :: [String] -> IO (Options, [String])
processArgs argv =
case getOpt' Permute options argv of
(o,_,n,[]) -> case foldl (flip id) defaultOptions o of
opts | False <- get optHelp opts -> return (opts, n)
opts | True <- get optBench opts -> return (opts, "--help":n)
_ -> putStrLn (helpMsg []) >> exitSuccess
(_,_,_,err) -> error (helpMsg err)
basicHeader :: String
basicHeader = unlines
[ "accelerate-crystal (c) [2011..2013] The Accelerate Team"
, ""
, "Usage: accelerate-crystal [OPTIONS]"
]

fancyHeader :: Options -> String
fancyHeader opts = unlines (header : table)
where
helpMsg err = concat err ++ usageInfo header options
header = unlines
[ "accelerate-crystal (c) [2011..2012] The Accelerate Team"
, ""
, "Usage: accelerate-crystal [OPTIONS]"
]
active this = if this == map toLower (show $ get optBackend opts) then "*" else ""
(ss,bs,ds) = unzip3 $ map (\(b,d) -> (active b, b, d)) $ concatMap extract backends
table = zipWith3 paste (sameLen ss) (sameLen bs) ds
paste x y z = " " ++ x ++ " " ++ y ++ " " ++ z
sameLen xs = flushLeft ((maximum . map length) xs) xs
flushLeft n xs = [ take n (x ++ repeat ' ') | x <- xs ]
--
extract (Option _ los _ descr) =
let losFmt = intercalate ", " los
in case lines descr of
[] -> [(losFmt, "")]
(x:xs) -> (losFmt, x) : [ ("",x') | x' <- xs ]
--
header = intercalate "\n" [ basicHeader, "Available backends:" ]


parseArgs :: [String] -> IO (Options, Criterion.Config, [String])
parseArgs argv =
let
helpMsg err = concat err
++ usageInfo basicHeader options
++ usageInfo "\nGeneric criterion options:" Criterion.defaultOptions

in case getOpt' Permute options argv of
(o,_,n,[]) -> do

-- pass unrecognised options to criterion
(cconf, rest) <- Criterion.parseArgs Criterion.defaultConfig Criterion.defaultOptions n
case foldr id defaultOptions o of
opts | False <- get optHelp opts -> putStrLn (fancyHeader opts) >> return (opts, cconf, rest)
_ -> putStrLn (helpMsg []) >> exitSuccess

(_,_,_,err) -> error (helpMsg err)

6 changes: 3 additions & 3 deletions examples/crystal/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Data.Label
import Foreign.Ptr
import Control.Monad
import Control.Exception
import Criterion.Main ( defaultMain, bench, whnf )
import Criterion.Main ( defaultMainWith, bench, whnf )
import Foreign.ForeignPtr
import System.Environment
import System.IO.Unsafe
Expand Down Expand Up @@ -167,7 +167,7 @@ frame render size zoom time = G.scale zoom' zoom' pic
-- Main -----------------------------------------------------------------------
main :: IO ()
main
= do (config, nops) <- processArgs =<< getArgs
= do (config, crit, nops) <- parseArgs =<< getArgs
let size = get optSize config
zoom = get optZoom config
scale = get optScale config
Expand All @@ -178,7 +178,7 @@ main
void . evaluate $ render (A.fromList Z [0])

if get optBench config
then withArgs nops $ defaultMain
then withArgs nops $ defaultMainWith crit (return ())
[ bench "crystal" $ whnf (force . render) (A.fromList Z [1.0]) ]

#ifndef ACCELERATE_ENABLE_GUI
Expand Down
77 changes: 53 additions & 24 deletions examples/fluid/src/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ module Config (
simulationWidth, simulationHeight, initialDensity, initialVelocity,
displayScale, displayFramerate, optBench,

processArgs, run, run1
parseArgs, run, run1

) where

import Type

import Data.Char
import Data.List
import Data.IORef
import Data.Label
import Control.Monad
import System.Console.GetOpt
import System.Exit
import Prelude as P
import qualified Criterion.Main as Criterion
import qualified Criterion.Config as Criterion

import Data.Array.Accelerate as A
import Data.Array.Accelerate.IO as A
Expand Down Expand Up @@ -106,8 +110,8 @@ run1 opts f = case _optBackend opts of
#endif


processArgs :: [String] -> IO (Options, [String])
processArgs argv = do
parseArgs :: [String] -> IO (Options, Criterion.Config, [String])
parseArgs argv = do

-- Some additional options, which we will use to determine how to set up the
-- initial conditions of the simulator.
Expand All @@ -122,16 +126,18 @@ processArgs argv = do

-- Parse the command-line options
--
let options :: [OptDescr (Options -> IO Options)]
options =
-- Backend execution setup
let backends :: [OptDescr (Options -> IO Options)]
backends =
[ Option [] ["interpreter"] (NoArg (return . set optBackend Interpreter)) "reference implementation (sequential)"
#ifdef ACCELERATE_CUDA_BACKEND
, Option [] ["cuda"] (NoArg (return . set optBackend CUDA)) "implementation for NVIDIA GPUs (parallel)"
#endif
]

options :: [OptDescr (Options -> IO Options)]
options = backends ++
-- Simulation options
, Option [] ["viscosity"] (ReqArg (parse viscosity) "FLOAT") (describe viscosity "viscosity for velocity damping")
[ Option [] ["viscosity"] (ReqArg (parse viscosity) "FLOAT") (describe viscosity "viscosity for velocity damping")
, Option [] ["diffusion"] (ReqArg (parse diffusion) "FLOAT") (describe diffusion "diffusion rate for mass dispersion")
, Option [] ["delta"] (ReqArg (parse timestep) "FLOAT") (describe timestep "simulation time between each frame")
, Option [] ["density"] (ReqArg (parse inputDensity) "FLOAT") (describe inputDensity "magnitude of user input density")
Expand All @@ -154,32 +160,55 @@ processArgs argv = do

parse f x = return . set f (read x)
describe f msg = msg ++ " (" ++ show (get f defaultOptions) ++ ")"
helpMsg errs = concat errs ++ usageInfo header options ++ footer
header = unlines
[ "accelerate-fluid (c) 2011 Trevor L. McDonell"

basicHeader = unlines
[ "accelerate-fluid (c) [2011..2013] The Accelerate Team"
, ""
, "Usage: accelerate-fluid [OPTIONS]"
]

footer = unlines
basicFooter = unlines
[ ""
, "Runtime usage:"
, ""
, " click add density sources to the image"
, " shift-click add velocity sources"
, " r reset the image"
, " d toggle display of density field"
, " v toggle display of velocity field lines"
, " click add density sources to the image"
, " shift-click add velocity sources"
, " r reset the image"
, " d toggle display of density field"
, " v toggle display of velocity field lines"
]

(opts, rest)
helpMsg errs = concat errs
++ usageInfo basicHeader options
++ usageInfo "\nGeneric criterion options:" Criterion.defaultOptions

fancyHeader :: Options -> String
fancyHeader opts = unlines (header : table ++ lines basicFooter)
where
active this = if this == P.map toLower (show $ get optBackend opts) then "*" else ""
(ss,bs,ds) = P.unzip3 $ P.map (\(b,d) -> (active b, b, d)) $ concatMap extract backends
table = P.zipWith3 paste (sameLen ss) (sameLen bs) ds
paste x y z = " " ++ x ++ " " ++ y ++ " " ++ z
sameLen xs = flushLeft ((P.maximum . P.map P.length) xs) xs
flushLeft n xs = [ P.take n (x ++ repeat ' ') | x <- xs ]
--
extract (Option _ los _ descr) =
let losFmt = intercalate ", " los
in case lines descr of
[] -> [(losFmt, "")]
(x:xs) -> (losFmt, x) : [ ("",x') | x' <- xs ]
--
header = intercalate "\n" [ basicHeader, "Available backends:" ]

(opts, crit, rest)
<- case getOpt' RequireOrder options argv of
(actions,_,n,[]) -> do
o <- foldl (>>=) (return defaultOptions) actions
case get optHelp o of
False -> return (o,n)
_ | True <- get optBench o -> return (o,"--help":n)
_ -> putStrLn (helpMsg []) >> exitSuccess
opts <- foldl (>>=) (return defaultOptions) actions
(cconf, rest) <- Criterion.parseArgs Criterion.defaultConfig Criterion.defaultOptions n

case get optHelp opts of
False -> putStrLn (fancyHeader opts) >> return (opts, cconf, rest)
_ -> putStrLn (helpMsg []) >> exitSuccess

(_,_,_,errors) -> error (helpMsg errors)

-- Extract option values, and set up the initial conditions
Expand Down Expand Up @@ -278,5 +307,5 @@ processArgs argv = do
density <- mkInitialDensity
velocity <- mkInitialVelocity

return ( opts { _initialDensity = density, _initialVelocity = velocity }, rest )
return ( opts { _initialDensity = density, _initialVelocity = velocity }, crit, rest )

6 changes: 3 additions & 3 deletions examples/fluid/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import World
import Fluid
import Event
import Data.Label
import Criterion.Main
import Criterion.Main ( defaultMainWith, bench, whnf )
import Control.Exception
import System.Environment
import Graphics.Gloss.Interface.IO.Game
Expand All @@ -23,7 +23,7 @@ import Data.Array.Accelerate as A

main :: IO ()
main = do
(opt,noms) <- processArgs =<< getArgs
(opt,crit,noms) <- parseArgs =<< getArgs
let -- configuration parameters
--
width = get simulationWidth opt * get displayScale opt
Expand Down Expand Up @@ -71,7 +71,7 @@ main = do
if get optBench opt
#endif
-- benchmark
then withArgs noms $ defaultMain
then withArgs noms $ defaultMainWith crit (return ())
[ bench "fluid" $ whnf simulate initialWorld ]

-- simulate
Expand Down
Loading

0 comments on commit e72a6d4

Please sign in to comment.