root/hiersort/sim/Proc.hs

Revision 1, 5.4 kB (checked in by thesz, 2 years ago)

Initial commit

Line 
1 -- | Proc.hs
2 -- A processor circuit.
3 --
4 -- Copytight (C) 2007, 2008 Serguey Zefirov
5 --
6 -- This program is free software; you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as published by
8 -- the Free Software Foundation; either version 3, or (at your option)
9 -- any later version.
10 -- See file COPYING or visit http://www.gnu.org/licenses for details.
11
12 -- It executes pair programs delivered from matcher circuit.
13 -- Main role of that circuit is to resolve processor resource
14 -- allocation conflicts and perform a little scheduling of threads.
15 --
16
17 module Proc where
18
19 import InterfaceTypes
20
21 import Info
22
23 import S
24
25 import Data.Maybe
26 import qualified Data.Set as Set
27 import qualified Data.Map as Map
28 import Data.Bits
29
30 -------------------------------------------------------------------------------
31 -- Execution devices set configuration.
32
33 devSet chaninfos = Map.fromList $ map chan chaninfos++otherDevices
34         where
35                 chan (ch,enabled,width)
36                         | enabled   = (MessageChannel ch,width)
37                         | otherwise = (MessageChannel ch,0)
38                 otherDevices = map (\(r,w) -> (OtherResource r,w)) [
39                                  (IAdder,6)
40                                 ,(ILogic,6)
41                                 ,(IMultiplier,1)
42                                 ,(FAdder,1)
43                                 ,(FMultiplier,1)
44                                 ,(FDivider,1)
45                         ]
46
47 -------------------------------------------------------------------------------
48 -- Processor state change function.
49
50 processorF execWindow maxProgs shedPolicy
51         sendChanWidth
52         progTrans
53         mask ourindex
54         progStates (pair,matcherCTS) =
55         (nextProgStates,((tokenMatch,rtr),debInfo))
56         where
57                 -- Can we receive another pair?
58                 rtr = max 0 (maxProgs - length progStates)
59                 -- Split programs into working and idle set.
60                 (workSet,idleSet) = splitAt execWindow progStates
61                 -- Perform update in work set.
62                 resourceSet =
63                         devSet [
64                                         (MsgChannelMatch,matcherCTS,sendChanWidth)
65                                 ]
66                 (workSetDelayed,workSetSheduled,tokenMatch) =
67                         workCycle [] [] [] workSet resourceSet
68                 -- expand arguments for progTrans
69                 expandProgTrans (pair,order) = progTrans pair order
70                 -- Pair program.
71                 pairProgram = map expandProgTrans pair
72                 -- Next program states.
73                 nextProgStates = case shedPolicy of
74                         SheduleAllWorkBack ->
75                                    idleSet
76                                 ++ workSetDelayed
77                                 ++ workSetSheduled
78                                 ++ pairProgram
79                         SheduleSheduledBack ->
80                                    workSetDelayed
81                                 ++ idleSet
82                                 ++ workSetSheduled
83                                 ++ pairProgram
84                         SheduleWindow ->
85                                    workSetSheduled
86                                 ++ workSetDelayed
87                                 ++ idleSet
88                                 ++ pairProgram
89                 tripDistMatch = fmap (\msg -> distance mask ourindex (msgModuleAddr msg)) tokenMatch
90
91                 isProcMessage (ProcMessage _) = True
92                 isProcMessage _               = False
93                 isHeadProcMessage [] = False
94                 isHeadProcMessage (h:_) = isProcMessage h
95                 anyProcMessage = or $ map isHeadProcMessage workSet
96                 anySent = (length tokenMatch) > 0
97
98                 procBlocked
99                         | anyProcMessage && not anySent = "processor is blocked."
100                         | otherwise = "processor isn't blocked."
101                 procIdle
102                         | length progStates < 1 = "processor is idle."
103                         | otherwise = "processor isn't idle."
104
105                 noInfo = ""
106                 debInfo = "proc info"
107                         !#      procBlocked
108                         ?#      procIdle
109                         ?#      ("matcherCTS",matcherCTS)
110 --                      ##      ("pair",pair)
111 --                      ##      ("pairProgram",pairProgram)
112 --                      ##      ("resourceSet",resourceSet)
113 --                      ##      ("sendChanWidth",sendChanWidth)
114 --                      ##      ("progStates",progStates)
115 --                      ##      ("workSet",workSet)
116 --                      ##      ("idleSet",idleSet)
117 --                      ##      ("nextProgStates",nextProgStates)
118                         ##      ("tokenMatch",tokenMatch)
119 --                      ##      ("length tokenMatch",length tokenMatch)
120 --                      ##      ("mask",mask)
121 --                      ##      ("ourindex",ourindex)
122 --                      ##      ("map upPred tokenMatch",map upPred tokenMatch)
123 --                      ##      ("workSetLen",length workSet)
124 --                      ##      ("execWindow",execWindow)
125 --                      ##      ("tripDistMatch",tripDistMatch)
126                         #!      ("procRTR",rtr)
127
128 distance mask src dest = case dest of
129         HostTok -> ("to host",highest mask)
130         ModTok x -> ("to module",2*highest (mask .&. (xor src x)))
131         where
132                 highest 0 = 0
133                 highest n = 1+highest (shiftR n 1)
134
135 hasHeadMsg [] = False
136 hasHeadMsg (p:ps) = headMsg p || hasHeadMsg ps
137 headMsg (c:cs) = case c of
138         ProcMessage _ -> True
139         _             -> False
140 headMsg _ = False
141
142 -- No more programs to update.
143 workCycle sheduled delayed messageMatch [] _ = (
144                  reverse delayed
145                 ,reverse sheduled
146                 ,messageMatch
147                 )
148 -- Has at least one program to update.
149 workCycle sheduled delayed messageMatch (p:ps) resSet
150         | notBlocked = workCycle (advanced++sheduled) delayed newMatch ps remainingResources
151         | otherwise = workCycle sheduled (p:delayed) messageMatch ps resSet
152         where
153                 (pcommand,advanced) = case p of
154                         []     -> (ProcNop,[])
155                         [c]    -> (c,[])
156                         (c:cs) -> (c,[cs])
157                 allocResource r = case Map.lookup r resSet of
158                         Just 0 -> (False,resSet)
159                         Just c -> (True,Map.adjust (\x -> x - 1) r resSet)
160                         Nothing -> error $ unwords ["Can't allocate resource out of the set:",show r,", resources",show resSet]
161                 (notBlocked,remainingResources) = case pcommand of
162                         ProcMessage msg -> allocResource $ MessageChannel MsgChannelMatch
163                         ProcResource r  -> allocResource $ OtherResource r
164                         ProcNop         -> (True,resSet)
165                 newMatch = case pcommand of
166                         ProcMessage msg -> checkMsg messageMatch msg
167                         _ -> messageMatch
168                 checkMsg old new = old++[new]
169
170 -------------------------------------------------------------------------------
171 -- Processor circuit
172
173 processor execWindow maxProg shedPolicy sendChanWidth progTrans mask ourindex pair matcherCTS =
174         (matcherTokens,rtr,info)
175         where
176                 procF = processorF execWindow maxProg shedPolicy sendChanWidth progTrans mask ourindex
177                 (bus,info) = unzipS $ loopS [] procF $ zipS pair matcherCTS
178                 (matcherTokens,rtr) = unzipS bus
Note: See TracBrowser for help on using the browser.