never executed always true always false
1 {-# LANGUAGE GADTs #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3
4 {-# OPTIONS -fno-warn-orphans #-}
5
6 {- |
7 Module : NITTA.Synthesis.Steps.Dataflow
8 Description :
9 Copyright : (c) Aleksandr Penskoi, 2021
10 License : BSD3
11 Maintainer : aleksandr.penskoi@gmail.com
12 Stability : experimental
13 -}
14 module NITTA.Synthesis.Steps.Dataflow (
15 DataflowMetrics (..),
16 ) where
17
18 import Data.Aeson (ToJSON)
19 import Data.Set qualified as S
20 import GHC.Generics (Generic)
21 import NITTA.Intermediate.Analysis (ProcessWave (..))
22 import NITTA.Intermediate.Types (
23 Function (inputs),
24 Variables (variables),
25 WithFunctions (functions),
26 )
27 import NITTA.Model.Networks.Bus (BusNetwork)
28 import NITTA.Model.Problems.Dataflow (
29 DataflowProblem (dataflowDecision),
30 DataflowSt (..),
31 dataflowOption2decision,
32 )
33 import NITTA.Model.Problems.Endpoint (EndpointSt (epAt))
34 import NITTA.Model.ProcessorUnits.Types (UnitTag)
35 import NITTA.Model.TargetSystem (TargetSystem (mUnit))
36 import NITTA.Model.Time (TimeConstraint (..), VarValTime)
37 import NITTA.Synthesis.Types (
38 SynthesisDecisionCls (..),
39 SynthesisState (
40 SynthesisState,
41 numberOfDataflowOptions,
42 processWaves,
43 sTarget,
44 transferableVars
45 ),
46 (<?>),
47 )
48 import NITTA.Utils.Base (unionsMap)
49 import Numeric.Interval.NonEmpty (Interval, inf, sup)
50
51 data DataflowMetrics = DataflowMetrics
52 { pWaitTime :: Float
53 , pRestrictedTime :: Bool
54 , pNotTransferableInputs :: [Float]
55 -- ^ number of variables, which is not transferable for affected
56 -- functions.
57 , pFirstWaveOfTargetUse :: Float
58 -- ^ number of the first wave in which one of the target variables is used
59 }
60 deriving (Generic)
61
62 instance ToJSON DataflowMetrics
63
64 instance
65 (UnitTag tag, VarValTime v x t) =>
66 SynthesisDecisionCls
67 (SynthesisState (TargetSystem (BusNetwork tag v x t) tag v x t) tag v x t)
68 (TargetSystem (BusNetwork tag v x t) tag v x t)
69 (DataflowSt tag v (TimeConstraint t))
70 (DataflowSt tag v (Interval t))
71 DataflowMetrics
72 where
73 decisions SynthesisState{sTarget} o = let d = dataflowOption2decision o in [(d, dataflowDecision sTarget d)]
74
75 parameters SynthesisState{transferableVars, sTarget, processWaves} DataflowSt{dfSource, dfTargets} _ =
76 let TimeConstraint{tcAvailable, tcDuration} = epAt $ snd dfSource
77 vs = unionsMap (variables . snd) dfTargets
78 lvs = length vs
79 waveNum =
80 length
81 . takeWhile (\ProcessWave{pwFs} -> lvs == length (vs `S.difference` unionsMap inputs pwFs))
82 $ processWaves
83 in DataflowMetrics
84 { pWaitTime = fromIntegral (inf tcAvailable)
85 , pRestrictedTime = fromEnum (sup tcDuration) /= maxBound
86 , pNotTransferableInputs =
87 let fs = functions $ mUnit sTarget
88 affectedFunctions = filter (\f -> not $ null (inputs f `S.intersection` vs)) fs
89 notTransferableVars = map (\f -> inputs f S.\\ transferableVars) affectedFunctions
90 in map (fromIntegral . length) notTransferableVars
91 , pFirstWaveOfTargetUse = fromIntegral waveNum :: Float
92 }
93
94 estimate
95 SynthesisState{numberOfDataflowOptions}
96 _o
97 _d
98 DataflowMetrics
99 { pWaitTime
100 , pNotTransferableInputs
101 , pRestrictedTime
102 , pFirstWaveOfTargetUse
103 } =
104 sum
105 [ 2000
106 , (numberOfDataflowOptions >= threshold) <?> 1000
107 , pRestrictedTime <?> 200
108 , sum pNotTransferableInputs * (-5)
109 , -pWaitTime
110 , -pFirstWaveOfTargetUse
111 ]
112
113 threshold = 20