on atom_typeclass_simple help sign in
93 lines 3.5 kB history raw
1{-# LANGUAGE FlexibleInstances,OverloadedStrings #-}
2import ProjectM36.Base
3import ProjectM36.Relation
4import ProjectM36.Relation.Show.CSV
5import ProjectM36.Relation.Show.HTML
6import TutorialD.Interpreter.Base (displayOpResult)
7import TutorialD.Interpreter.DatabaseExpr (interpretDatabaseExpr)
8import TutorialD.Interpreter.RODatabaseContextOperator (interpretRODatabaseContextOp)
9import ProjectM36.RelationalExpression
10import qualified Data.HashSet as HS
11import qualified Data.ByteString.Lazy.Char8 as BS
12import qualified Data.IntMap as IM
13import qualified Data.Hashable as Hash
14import qualified Data.Vector as V
15import Options.Applicative
16import qualified Data.Map as M
17import qualified Data.Text.IO as TIO
18import System.IO
19import Control.Monad.State
21dumpcsv :: Relation -> IO ()
22dumpcsv rel = case relationAsCSV rel of
23 Left err -> hPutStrLn stderr (show err)
24 Right bsData -> BS.putStrLn bsData
26data BigrelArgs = BigrelArgs Int Int String
28parseAttributeCount :: Parser Int
29parseAttributeCount = option auto (short 'a' <> long "attribute-count")
31parseTupleCount :: Parser Int
32parseTupleCount = option auto (short 't' <> long "tuple-count")
34parseTutD :: Parser String
35parseTutD = strOption (short 'd' <> long "tutoriald")
37parseArgs :: Parser BigrelArgs
38parseArgs = BigrelArgs <$> parseAttributeCount <*> parseTupleCount <*> parseTutD
40main :: IO ()
41main = do
42 bigrelArgs <- execParser $ info (helper <*> parseArgs) fullDesc
43 --matrixRestrictRun
44 matrixRun bigrelArgs
45 --vectorMatrixRun
46 --intmapMatrixRun
48matrixRun :: BigrelArgs -> IO ()
49matrixRun (BigrelArgs attributeCount tupleCount tutd) = do
50 case matrixRelation attributeCount tupleCount of
51 Left err -> putStrLn (show err)
52 Right rel -> if tutd == "" then
53 putStrLn "Done."
54 else do
55 let setx = Assign "x" (ExistingRelation rel)
56 context = execState (evalContextExpr setx) dateExamples
57 interpreted = interpretDatabaseExpr context tutd
58 plan = interpretRODatabaseContextOp context $ ":showplan " ++ tutd
59 displayOpResult plan
60 case interpreted of
61 Right context' -> TIO.putStrLn $ relationAsHTML ((relationVariables context') M.! "x")
62 Left err -> hPutStrLn stderr (show err)
65intmapMatrixRun :: IO ()
66intmapMatrixRun = do
67 let matrix = intmapMatrixRelation 100 100000
68 putStrLn (show matrix)
70--compare IntMap speed and size
71--this is about 3 times faster (9 minutes) for 10x100000 and uses 800 MB
72intmapMatrixRelation :: Int -> Int -> HS.HashSet (IM.IntMap Atom)
73intmapMatrixRelation attributeCount tupleCount = HS.fromList $ map mapper [0..tupleCount]
74 where
75 mapper tupCount = IM.fromList $ map (\c-> (c, Atom tupCount)) [0..attributeCount]
77instance Hash.Hashable (IM.IntMap Atom) where
78 hashWithSalt salt tupMap = Hash.hashWithSalt salt (show tupMap)
80vectorMatrixRun :: IO ()
81vectorMatrixRun = do
82 let matrix = vectorMatrixRelation 100 100000
83 putStrLn (show matrix)
85-- 20 s 90 MBs- a clear win- ideal size is 10 * 100000 * 8 bytes = 80 MB! without IntAtom wrapper
86--with IntAtom wrapper: 1m12s 90 MB
87vectorMatrixRelation :: Int -> Int -> HS.HashSet (V.Vector Atom)
88vectorMatrixRelation attributeCount tupleCount = HS.fromList $ map mapper [0..tupleCount]
89 where
90 mapper tupCount = V.replicate attributeCount (Atom tupCount)
92instance Hash.Hashable (V.Vector Atom) where
93 hashWithSalt salt vec = Hash.hashWithSalt salt (show vec)