hgraph/src/Main.hs

147 lines
5.6 KiB
Haskell
Raw Normal View History

2013-11-14 12:28:05 +00:00
{-# LANGUAGE CPP, TemplateHaskell #-}
-----------------------------------------------------------------------------
--
-- Module : Main
-- Copyright :
-- License : AllRightsReserved
--
-- Maintainer :
-- Stability :
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------
module Main (
main
) where
import Control.Monad (unless)
import Control.Parallel.Strategies
import Control.DeepSeq
2013-11-14 12:28:05 +00:00
import Data.List
import System.Exit (exitFailure)
import System.Environment
import Test.QuickCheck.All (quickCheckAll)
2013-11-15 15:41:18 +00:00
import qualified Data.ByteString.Char8 as B
2013-11-14 12:28:05 +00:00
import Data.ByteString.Lazy.Char8 (ByteString)
import Control.Monad.Par.Scheds.Trace
import qualified Data.Stream as S
import Data.Either (lefts, rights)
import Debug.Trace
2013-11-15 15:41:18 +00:00
import qualified Data.Text as T
import Data.Text.Encoding
2013-11-15 17:15:32 +00:00
import Stream hiding (map)
import qualified Data.Array.Accelerate as A
-- change to Data.Array.Accelerate.CUDA as I and link accelerate-cuda to use GPU instead of CPU
import Data.Array.Accelerate.Interpreter as I
2013-11-15 17:15:32 +00:00
type Matrix e = A.Array A.DIM2 e
2013-11-14 12:28:05 +00:00
2013-11-15 15:41:18 +00:00
createGraph :: T.Text -> Either [Int] T.Text
createGraph input = createGraph' input (Left [])
where
2013-11-15 15:41:18 +00:00
createGraph' :: T.Text -> Either [Int] T.Text -> Either [Int] T.Text
createGraph' a r
| T.null a = r
| otherwise =
let next = (createGraph' (T.tail a) r) in
case next of
Left xs ->
2013-11-15 15:41:18 +00:00
case T.head a of
'0' -> Left $ 0:xs
'1' -> Left $ 1:xs
2013-11-15 15:41:18 +00:00
_ -> Right $ T.append (T.pack "cannot parse ") a
Right errstr ->
Right errstr
--createGraph input = Right $ "Parsing-error in line: " ++ input
2013-11-14 12:28:05 +00:00
-- TODO: not needed anymore. remove? Later use?
2013-11-14 12:28:05 +00:00
graphFolder :: [Either [Int] String] -> (Either [[Int]] String)
graphFolder [] = Right "empty Graph"
graphFolder l = graphFolder' l (Left [[]])
where
graphFolder' :: [Either [Int] String] -> (Either [[Int]] String) -> (Either [[Int]] String)
graphFolder' [] r = r
graphFolder' (a:as) r =
case a of
-- we have an intact [Int]
Left b ->
case graphFolder' as r of
-- append if rest is ok.
Left xs -> Left (b:xs)
-- ooops. Error-String -> Discard result
Right s -> Right s
-- we have an Error-String -> ignore results, append errors if possible
Right s ->
case graphFolder' as r of
Left x -> Right s
Right ss -> Right (ss ++ "\n" ++ s)
concatWith :: String -> String -> String -> String
concatWith d a b = a ++ d ++ b
2013-11-15 15:41:18 +00:00
emptyLine :: T.Text -> Bool
emptyLine a
| T.null a = True
| otherwise = False
2013-11-15 15:41:18 +00:00
emptyLog :: [T.Text] -> Bool
emptyLog [] = True
2013-11-15 15:41:18 +00:00
emptyLog a = False --emptyLine $ foldl True (&&) (map emptyLine a)
-- TODO: implement calculation
2013-11-15 15:41:18 +00:00
doCalculation :: Matrix Int -> B.ByteString
doCalculation a = B.pack $ "" --(show a) ++ "\n"
2013-11-14 12:28:05 +00:00
exeMain = do
args <- getArgs
input <- case args of
["-"] -> B.getContents
[] -> error "Error: No filename or stdinput (-) given."
[file] -> B.readFile file
-- unrefined_graph::[Either [Int] String] - [Int] is Adjacency-Line, String is parse-Error
unrefined_graph <- return $ parMap (rparWith rdeepseq) --run parallel, evaluate fully
-- and filter empty lines
(createGraph) (filter (not . emptyLine)
-- split at \n, convert to String
2013-11-15 15:41:18 +00:00
(T.lines (decodeUtf8 input)))
2013-11-14 12:28:05 +00:00
--egraph <- return $ graphFolder unrefined_graph
(graph, log, lines) <- return $ ((foldl1 (++) (lefts unrefined_graph), -- concatenated graph
2013-11-15 15:41:18 +00:00
T.intercalate (T.singleton '\n') (rights unrefined_graph), -- concat error-log
length unrefined_graph) -- number of elements in graph
-- in parallel
`using` parTuple3 rdeepseq rdeepseq rdeepseq)
-- validate graph
log <- return $ let l = length graph in
if l /= lines*lines then
2013-11-15 15:41:18 +00:00
T.append log $ T.pack $ "Lines dont match up. Read " ++ (show l) ++
" chars. Expected " ++ (show (lines*lines)) ++
" chars.\n"
else
log
output <- return $ case emptyLine log of
True -> doCalculation $ A.fromList (A.Z A.:. lines A.:. lines) graph
2013-11-15 15:41:18 +00:00
_ -> encodeUtf8 $ T.append (T.append (T.pack "Error detected:\n") log) (T.pack "\n\n")
B.putStr output
2013-11-14 12:28:05 +00:00
-- Entry point for unit tests.
testMain = do
allPass <- $quickCheckAll -- Run QuickCheck on all prop_ functions
unless allPass exitFailure
-- This is a clunky, but portable, way to use the same Main module file
-- for both an application and for unit tests.
-- MAIN_FUNCTION is preprocessor macro set to exeMain or testMain.
-- That way we can use the same file for both an application and for tests.
#ifndef MAIN_FUNCTION
#define MAIN_FUNCTION exeMain
#endif
main = MAIN_FUNCTION