Skip to content

Commit 0ceeac5

Browse files
committed
WIP: decision Tree
1 parent a970e85 commit 0ceeac5

File tree

4 files changed

+113
-19
lines changed

4 files changed

+113
-19
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module Bench where
2+
3+
import Basics
4+
import Gibbon.Vector
5+
import Gibbon.PList
6+
7+
type String = Vector Char
8+
9+
data PackedBool = B Int
10+
11+
data DecisionTree = Node PackedBool DecisionTree DecisionTree Inline | Leaf Inline
12+
13+
14+
15+
mkRandomDecisionTree :: Int -> DecisionTree
16+
mkRandomDecisionTree depth = if depth <= 0 then Leaf (Str (getRandomString 10))
17+
else
18+
let randBool = mod rand 2
19+
randString = getRandomString 10
20+
inline = Str randString
21+
leftSubtree = mkRandomDecisionTree (depth-1)
22+
rightSubtree = mkRandomDecisionTree (depth-1)
23+
in Node (B randBool) leftSubtree rightSubtree inline
24+
25+
26+
27+
fromInline :: Inline -> String
28+
fromInline inline = case inline of
29+
Str a -> a
30+
31+
fromBool :: PackedBool -> Int
32+
fromBool b = case b of
33+
B bb -> bb
34+
35+
merge_plist :: PList Inline -> PList Inline -> PList Inline
36+
merge_plist lst1 lst2 = case lst1 of
37+
Cons x rst -> Cons x (merge_plist rst lst2)
38+
Nil -> lst2
39+
40+
41+
append_plist :: PList Inline -> Inline -> PList Inline
42+
append_plist lst elem = case lst of
43+
Nil -> (Cons elem) Nil
44+
Cons x rst -> Cons x (append_plist rst elem)
45+
46+
singleton_plist :: Inline -> PList Inline
47+
singleton_plist elem = (Cons elem) Nil
48+
49+
50+
accumulateDecisions :: DecisionTree -> PList Inline
51+
accumulateDecisions tree = case tree of
52+
Node b left right str -> let bb = fromBool b
53+
in if bb == 1
54+
then
55+
let curr = append_plist Nil str
56+
vecLeft = accumulateDecisions left
57+
cc = singleton_plist (Str (getRandomString 10))
58+
vecRight = accumulateDecisions right
59+
in merge_plist (merge_plist curr vecLeft) vecRight
60+
else
61+
let vecLeft = accumulateDecisions left
62+
vecRight = accumulateDecisions right
63+
in merge_plist vecLeft vecRight
64+
65+
Leaf str -> (Cons str) Nil
66+
67+
68+
gibbon_main =
69+
let tree = mkRandomDecisionTree 2
70+
vec = accumulateDecisions tree
71+
in printPacked vec
72+
73+
--- filter
74+
75+
--- Map
76+
77+
--- Search
78+
79+
--- Length

gibbon-compiler/src/Gibbon/Passes/AccessPatternsAnalysis.hs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,34 @@ greedyOrderOfVertices ee = let edges' = P.map (\((a, b), c) -> ((P.fromInteg
9797
graph = buildG bounds edgesWithoutWeight
9898
weightMap = P.foldr (\(e, w) mm -> M.insert e w mm) M.empty edges'
9999
v'' = greedyOrderOfVerticesHelper graph (topSort graph) weightMap S.empty
100-
in v'' -- dbgTraceIt (sdoc (v'', (M.elems weightMap)))
100+
in v'' -- dbgTraceIt (sdoc ((topSort graph), (M.elems weightMap))) dbgTraceIt (sdoc (v'', (M.elems weightMap)))
101101

102102

103103
greedyOrderOfVerticesHelper :: Graph -> [Int] -> M.Map (Int, Int) Int -> S.Set Int -> [Int]
104104
greedyOrderOfVerticesHelper graph vertices' weightMap visited = case vertices' of
105105
[] -> []
106106
x:xs -> if S.member x visited
107107
then greedyOrderOfVerticesHelper graph xs weightMap visited
108-
else let successors = reachable graph x
108+
else let successors = succGraph x (G.edges graph) visited
109109
removeCurr = S.toList $ S.delete x (S.fromList successors)
110110
orderedSucc = orderedSuccsByWeight removeCurr x weightMap visited
111-
visited' = P.foldr S.insert S.empty orderedSucc
111+
visited' = P.foldr S.insert visited orderedSucc
112112
v'' = greedyOrderOfVerticesHelper graph xs weightMap visited'
113-
in if successors == [x]
114-
then orderedSucc ++ v'' --dbgTraceIt (sdoc (v'', orderedSucc))
115-
else [x] ++ orderedSucc ++ v''
113+
in [x] ++ orderedSucc ++ v''
114+
-- dbgTraceIt (sdoc (x, successors, removeCurr, orderedSucc, v'', S.toList visited' , S.toList visited ))
115+
--then dbgTraceIt (sdoc (x, successors, removeCurr, orderedSucc, v'', S.toList visited')) orderedSucc ++ v'' --dbgTraceIt (sdoc (v'', orderedSucc))
116+
--else dbgTraceIt (sdoc (x, successors, removeCurr, orderedSucc, v'', S.toList visited')) [x] ++ orderedSucc ++ v''
117+
118+
119+
succGraph :: Int -> [(Int, Int)] -> S.Set Int -> [Int]
120+
succGraph node edges visited = case edges of
121+
[] -> []
122+
(a, b):xs -> if S.member b visited || S.member a visited
123+
then succGraph node xs visited
124+
else
125+
if node == a then [b] ++ succGraph node xs visited
126+
else succGraph node xs visited
127+
116128

117129
orderedSuccsByWeight :: [Int] -> Int -> M.Map (Int, Int) Int -> S.Set Int -> [Int]
118130
orderedSuccsByWeight s i weightMap visited = case s of
@@ -271,7 +283,7 @@ removeDuplicates list =
271283
-- | a.) Multiple datacon fields read in the same expression.
272284
-- | Since this will be run after flatten, it is safe to assume that only possibly a maximum of two variables can be read in one let binding.
273285
-- | Except function calls! where more than two fields can be passed as arguments.
274-
evaluateExpressionFieldGraph ::
286+
evaluateExpressionFieldGraph :: (Out l, Out d, Out (e l d)) =>
275287
Maybe (DataCon, Integer) ->
276288
(G.Vertex -> (((PreExp e l d), Integer), Integer, [Integer])) ->
277289
(Integer -> Maybe G.Vertex) ->
@@ -416,19 +428,22 @@ evaluateExpressionFieldGraph currField nodeFromVertex vertexFromNode graph xs ma
416428
{- list of tuples, where each tuple == ([(dcon, id), ... ], likelihood) -}
417429
succDataCon' =
418430
P.zipWith (\x y -> (x, y)) succDataCon succprob
419-
newEdges =
431+
-- FIXME: TODO: This might be needed for the other cases in this function as well.
432+
-- This is to make sure we recurse on all possible successors.
433+
newEdges' = constructFieldGraph (Just (dcon, pred)) nodeFromVertex vertexFromNode graph succVertices map datacon
434+
newEdges = newEdges' ++ (
420435
P.concat $
421436
P.map
422437
( \x ->
423438
case x of
424439
(varsl, prob) ->
425440
P.map (\y -> ((pred, snd y), prob)) varsl
426441
)
427-
succDataCon'
442+
succDataCon' )
428443
in case newEdges of
429444
[] ->
430445
case successors of
431-
[] ->
446+
[] -> --dbgTraceIt (sdoc (currField, succVertices, newEdges, newEdges'))
432447
[]
433448
++ constructFieldGraph
434449
Nothing
@@ -438,7 +453,7 @@ evaluateExpressionFieldGraph currField nodeFromVertex vertexFromNode graph xs ma
438453
xs
439454
map
440455
datacon
441-
_ ->
456+
_ -> --dbgTraceIt (sdoc (currField, succVertices, newEdges, newEdges'))
442457
newEdges
443458
++ constructFieldGraph
444459
(Just (dcon, pred))
@@ -448,7 +463,7 @@ evaluateExpressionFieldGraph currField nodeFromVertex vertexFromNode graph xs ma
448463
xs
449464
map
450465
datacon
451-
_ ->
466+
_ -> --dbgTraceIt (sdoc (currField, succVertices, newEdges, newEdges'))
452467
newEdges
453468
++ constructFieldGraph
454469
Nothing
@@ -511,7 +526,7 @@ evaluateExpressionFieldGraph currField nodeFromVertex vertexFromNode graph xs ma
511526
error
512527
"evaluateExpressionFieldGraph: More than one variable from DataCon in a let binding not modelled into Field dependence graph yet!"
513528

514-
constructFieldGraph ::
529+
constructFieldGraph :: (Out l, Out d, Out (e l d)) =>
515530
Maybe (DataCon, Integer) ->
516531
(G.Vertex -> (((PreExp e l d), Integer), Integer, [Integer])) ->
517532
(Integer -> Maybe G.Vertex) ->
@@ -694,7 +709,7 @@ constructFieldGraph currField nodeFromVertex vertexFromNode graph progress map d
694709
_ -> error "not expected"
695710

696711
-- | From an expression provided, Recursively find all the variables that come from a DataCon expression, that is, are fields in a DataConE.
697-
findFieldInDataConFromVariableInExpression ::
712+
findFieldInDataConFromVariableInExpression :: (Out l, Out d, Out (e l d)) =>
698713
(PreExp e l d) ->
699714
[(((PreExp e l d), Integer), Integer, [Integer])] ->
700715
VariableMap ->
@@ -722,7 +737,7 @@ findFieldInDataConFromVariableInExpression exp graph map datacon =
722737
let freeVars = freeVarsInOrder rhs
723738
fromDataCon = P.map (\v -> M.findWithDefault Nothing v map) freeVars
724739
removeMaybe = Mb.catMaybes fromDataCon
725-
newDatacons =
740+
newDatacons = --dbgTraceIt (sdoc (v, freeVars))
726741
[ if dcon == datacon
727742
then Just (dcon, id')
728743
else Nothing

gibbon-compiler/src/Gibbon/Passes/ControlFlowGraph.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Data.Set as S
1818

1919
-- Haskell Imports
2020
import Prelude as P
21-
21+
import Text.PrettyPrint.GenericPretty
2222

2323
-- | CFGfunctionMap ex, a map storing a function, represented by Var (function name) to its control flow graph
2424
-- | Edge == (ex, Integer) the IR expression and its corresponding probability
@@ -58,11 +58,11 @@ type CFGfunctionMap ex
5858

5959
-- | Generate a CFG out of a Function definition.
6060
-- | Returns a map mapping a function to its corresponding CFG
61-
getFunctionCFG :: FunDef (PreExp e l d) -> CFGfunctionMap (PreExp e l d)
61+
getFunctionCFG :: (Out l, Out d, Out (e l d)) => FunDef (PreExp e l d) -> CFGfunctionMap (PreExp e l d)
6262
getFunctionCFG f@FunDef {funName, funBody, funTy, funArgs} =
6363
let (edgeList, _, _) = generateCFGExp 0 100 funBody
6464
(graph, nodeFromVertex, vertexFromKey) = G.graphFromEdges edgeList
65-
in M.insert funName (graph, nodeFromVertex, vertexFromKey) (M.empty)
65+
in M.insert funName (graph, nodeFromVertex, vertexFromKey) (M.empty) --dbgTraceIt (sdoc edgeList)
6666

6767

6868
-- | generate the Edges from the IR expression.

gibbon-compiler/src/Gibbon/Passes/OptimizeADTLayout.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ getVariableAndProducer funName pMap venv@Env2{vEnv, fEnv} ddefs dconName exp =
746746
justVariables = Maybe.catMaybes potentialVarsOfTy
747747
in if P.null justVariables
748748
-- dbgTraceIt (sdoc (funName, dconName, args, venv))
749-
then error "getVariableAndProducer: no variables of Ty to optimize found!"
749+
then Nothing-- dbgTraceIt (sdoc (funName, dconName, args, venv)) error "getVariableAndProducer: no variables of Ty to optimize found!"
750750
else
751751
if P.length justVariables > 1
752752
then error "getVariableAndProducer: More than one variable of the type being optimized is passed to function call. Not implemented yet!"

0 commit comments

Comments
 (0)