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
|
2013-11-15 12:19:25 +00:00
|
|
|
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-19 12:20:06 +00:00
|
|
|
import Data.ByteString.Char8 (ByteString)
|
2013-11-14 12:28:05 +00:00
|
|
|
import Control.Monad.Par.Scheds.Trace
|
|
|
|
import qualified Data.Stream as S
|
|
|
|
import Data.Either (lefts, rights)
|
2013-11-15 12:19:25 +00:00
|
|
|
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)
|
2013-11-15 12:19:25 +00:00
|
|
|
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
|
2013-11-19 12:24:21 +00:00
|
|
|
-- depends on accelerate-cuda package in cabal, which needs the installed CUDA-stuff form
|
|
|
|
-- nVidia (nvcc, header-files, ...) and the propriatary driver
|
2013-11-15 12:19:25 +00:00
|
|
|
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-22 14:18:53 +00:00
|
|
|
type Attr = Matrix A.Int8
|
|
|
|
-- Graph consists of a Vector denoting which colums of the matrix represents wich originating
|
|
|
|
-- column in the global adjencency-matrix
|
|
|
|
type Graph = (A.Vector A.Int8, Matrix A.Int8)
|
|
|
|
-- Adjecency-Matrix
|
|
|
|
type Adj = Matrix A.Int8
|
|
|
|
-- Vector of the Adjecency-Matrix
|
|
|
|
type AdjV = A.Vector A.Int8
|
|
|
|
|
|
|
|
expand :: [Graph]-> Adj -> Attr ->[Graph]
|
|
|
|
expand g a att = undefined
|
|
|
|
|
|
|
|
-- constraint gets a Graph and an Attribute-Matrix and yields true, if the Graph still fulfills
|
|
|
|
-- all constraints defined via the Attribute-Matrix.
|
|
|
|
constraint :: Graph -> Attr -> Bool
|
|
|
|
constraint g a = undefined
|
|
|
|
|
|
|
|
|
|
|
|
-- addPoint gets a graph and a tuple of an adjecancy-Vector with an int wich column of the
|
|
|
|
-- Adjacency-Matrix the Vector should represent to generate further Graphs
|
|
|
|
addPoint :: Graph -> (Adj, Int) -> [Graph]
|
|
|
|
addPoint g (a, n) = undefined
|
|
|
|
|
|
|
|
|
|
|
|
-- addablePoints yields all valid addititonsto a Graph
|
|
|
|
addablePoints :: Adj -> Graph-> [(Adj, Int)]
|
|
|
|
addablePoints a g = undefined
|
|
|
|
|
2013-11-14 12:28:05 +00:00
|
|
|
|
2013-11-19 12:20:06 +00:00
|
|
|
-- TODO: Give createGraph a presized Array and no dynamic [Int].
|
2013-11-19 12:24:21 +00:00
|
|
|
-- should be createGraph :: T.Text -> Either (Vector Int) T.Text
|
2013-11-15 15:41:18 +00:00
|
|
|
createGraph :: T.Text -> Either [Int] T.Text
|
|
|
|
createGraph input = createGraph' input (Left [])
|
2013-11-14 14:27:42 +00:00
|
|
|
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
|
2013-11-14 14:27:42 +00:00
|
|
|
case next of
|
|
|
|
Left xs ->
|
2013-11-19 12:20:06 +00:00
|
|
|
case T.head (traceEvent "parsing" a) of
|
|
|
|
'0' -> Left $ traceEvent "parse-concat" 0:xs
|
|
|
|
'1' -> Left $ traceEvent "parse-concat" 1:xs
|
2013-11-15 15:41:18 +00:00
|
|
|
_ -> Right $ T.append (T.pack "cannot parse ") a
|
2013-11-14 14:27:42 +00:00
|
|
|
Right errstr ->
|
|
|
|
Right errstr
|
2013-11-14 12:28:05 +00:00
|
|
|
|
2013-11-15 15:41:18 +00:00
|
|
|
emptyLine :: T.Text -> Bool
|
|
|
|
emptyLine a
|
|
|
|
| T.null a = True
|
|
|
|
| otherwise = False
|
2013-11-14 14:27:42 +00:00
|
|
|
|
2013-11-15 15:41:18 +00:00
|
|
|
emptyLog :: [T.Text] -> Bool
|
2013-11-14 14:27:42 +00:00
|
|
|
emptyLog [] = True
|
2013-11-15 15:41:18 +00:00
|
|
|
emptyLog a = False --emptyLine $ foldl True (&&) (map emptyLine a)
|
2013-11-14 14:27:42 +00:00
|
|
|
|
|
|
|
-- TODO: implement calculation
|
2013-11-19 12:20:06 +00:00
|
|
|
--doCalculation :: Matrix Int -> B.ByteString
|
|
|
|
doCalculation a = B.pack $ (show a) ++ "\n"
|
2013-11-15 12:19:25 +00:00
|
|
|
|
2013-11-19 12:20:06 +00:00
|
|
|
infixl 1 +||
|
2013-11-15 12:19:25 +00:00
|
|
|
|
2013-11-19 12:20:06 +00:00
|
|
|
(+||) :: a -> Strategy a -> a
|
|
|
|
a +|| b = a `using` b
|
2013-11-14 14:27:42 +00:00
|
|
|
|
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
|
2013-11-19 12:20:06 +00:00
|
|
|
-- read file and clean
|
|
|
|
readFile <- return $ filter (not . emptyLine) (T.lines (decodeUtf8 input))
|
|
|
|
inputLines <- return $ length readFile
|
|
|
|
-- TODO: concat with foldl1' kills us later -> use presized/preallocated array so we
|
|
|
|
-- dont copy that much lateron. Best would be Matrix Int
|
2013-11-14 12:28:05 +00:00
|
|
|
-- unrefined_graph::[Either [Int] String] - [Int] is Adjacency-Line, String is parse-Error
|
2013-11-19 12:20:06 +00:00
|
|
|
unrefined_graph <- return $ (map (traceEvent "mapping" . createGraph) readFile)
|
|
|
|
+|| (parBuffer 100 rdeepseq) --run parallel, evaluate fully
|
2013-11-14 12:28:05 +00:00
|
|
|
--egraph <- return $ graphFolder unrefined_graph
|
2013-11-19 12:20:06 +00:00
|
|
|
|
|
|
|
(graph, log, lines) <- return $ ((foldl1' ((traceEvent "concatenating graph") . (++)) (lefts unrefined_graph), -- concatenated graph
|
|
|
|
traceEvent "concatenating log" T.intercalate (T.singleton '\n') (rights unrefined_graph), -- concat error-log
|
|
|
|
traceEvent "getting length" length unrefined_graph) -- number of elements in graph
|
2013-11-15 12:19:25 +00:00
|
|
|
-- in parallel
|
|
|
|
`using` parTuple3 rdeepseq rdeepseq rdeepseq)
|
|
|
|
|
|
|
|
-- validate graph
|
2013-11-19 12:20:06 +00:00
|
|
|
log <- return $ let l = traceEvent "first validation" length graph in
|
2013-11-15 12:19:25 +00:00
|
|
|
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"
|
2013-11-15 12:19:25 +00:00
|
|
|
else
|
|
|
|
log
|
2013-11-19 12:20:06 +00:00
|
|
|
output <- return $ case emptyLine (traceEvent "last validation" log) of
|
|
|
|
True -> doCalculation $ graph --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")
|
2013-11-14 14:27:42 +00:00
|
|
|
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
|