OpenASIP  2.0
FUFiniteStateAutomaton.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file FUFiniteStateAutomaton.cc
26  *
27  * Definition of FUFiniteStateAutomaton class.
28  *
29  * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
34 #include "Application.hh"
35 #include "ResourceVectorSet.hh"
36 #include "CollisionMatrix.hh"
37 #include "StringTools.hh"
38 #include "AssocTools.hh"
39 #include "SequenceTools.hh"
40 #include "hash_set.hh"
41 #include "HWOperation.hh"
42 #include "FunctionUnit.hh"
43 
44 /**
45  * Initializes the FSA from the given FU.
46  *
47  * This version is to be used in fast compiled simulation that needs to
48  * minimize all function call overheads. To allow figuring out the state
49  * ids statically, they are assigned according to the operation's order
50  * in the FU when fetched with FunctionUnit::operation().
51  *
52  * The last state id (id FunctionUnit::operationCount()) is reserved for NOP.
53  * This way compiled simulator may access the state array with a constant
54  * index when issuing an operation or checking for a conflict without
55  * runtime overhead for figuring out the state id first for each operation.
56  *
57  * @param fu The function unit to build the FSA from.
58  * @param lazyBuilding True in case states should be built lazily the
59  * first time they are visited. Should improve startup time for larger FSAs.
60  */
62  const TTAMachine::FunctionUnit& fu, bool lazyBuilding) :
63  FiniteStateAutomaton(UNKNOWN_STATE, fu.operationCount() + 1),
64  operationCollisionMatrices_(fu) {
65 
66  // remove this loop from the benchmark
67  for (int i = 0; i < fu.operationCount(); ++i) {
68  const std::string opName =
70 
71  // each operation is a transition type in the state machine
72  setTransitionName(i, opName);
73  }
74  setTransitionName(fu.operationCount(), "[NOP]");
76 
77  // create S0
78  addState();
79 
80  // the initial state's collision matrix is an all zeros conflict matrix,
81  // copy the NOP matrix
83  0,
85 
86  if (!lazyBuilding)
88 }
89 
90 /**
91  * Destructor.
92  */
95 }
96 
97 /**
98  * Initializes the given state transition entry.
99  *
100  * The transition target state is resolved and built if necessary.
101  *
102  * @return The index of the target state.
103  */
108 
110  operationCollisionMatrices_.at(transition);
111 
112  const CollisionMatrix& stateMatrix = *stateCollisionMatrices_[source];
113  // Check whether the transition is legal, that is, the conflict
114  // matrix has a 0 in the column 1 (representing the next cycle) in
115  // the row representing the operation. The column 0 is for
116  // starting the operation at the same clock cycle, which cannot
117  // be done in case of our TTA FU. Thus, all states are cycle
118  // advancing.
119  if (transition == nopTransition_ ||
120  !stateMatrix.isCollision(transition, 1)) {
121 
122  // transition is legal, create the target state's collision
123  // matrix (all states considered cycle advancing) by shifting
124  // the source conflict matrix to left and ORing the operation's
125  // conflict matrix
126  CollisionMatrix* newMatrix = new CollisionMatrix(stateMatrix);
127  newMatrix->shiftLeft();
128  newMatrix->orWith(operationCollisionMatrix);
129 
130  // check if we already have a state with the created collision matrix
131  FSAStateIndex targetState = ILLEGAL_STATE;
132  CollisionMatrixStateIndex::const_iterator foundState =
133  collisionMatrixStates_.find(*newMatrix);
134  if (foundState != collisionMatrixStates_.end()) {
135 
136  // found an old state with the wanted collision matrix,
137  // set the transition target to the old state
138  targetState = (*foundState).second;
139 
140  delete newMatrix;
141  newMatrix = NULL;
142  } else {
143  // no state with the wanted collision matrix exists,
144  targetState = addState();
145  addCollisionMatrixForState(targetState, newMatrix);
146  }
147  setTransition(source, targetState, transition);
148  return targetState;
149  } else {
150  setTransition(source, ILLEGAL_STATE, transition);
151  return ILLEGAL_STATE;
152  }
153 }
154 
155 /**
156  * Connects a state and a collision matrix.
157  *
158  * @param state The state index.
159  * @param matrix The collision matrix (becomes owned by the index).
160  */
161 void
163  FSAStateIndex state, CollisionMatrix* matrix) {
164 
165  stateCollisionMatrices_[state] = matrix;
166  collisionMatrixStates_[*matrix] = state;
167 }
168 
169 /**
170  * Builds all states and state transitions.
171  *
172  * This might take very long time if there are lots of states to build. That
173  * is, if the FU pipeline resource usage patterns are long and complicated.
174  */
175 void
177 
178  hash_set<FSAStateIndex> handledStates;
179  hash_set<FSAStateIndex> unfinishedStatesList;
180  unfinishedStatesList.insert(0); // start from the start state
181 
182  while (!unfinishedStatesList.empty()) {
183  const FSAStateIndex state = *unfinishedStatesList.begin();
184  unfinishedStatesList.erase(state);
185  // go through all operations (transitions), the NOP is included
186  // and resolve their target state from the current state
187  for (int i = 0; i < operationCollisionMatrices_.size(); ++i) {
188  FSAStateIndex resolvedState = resolveState(state, i);
189  if (resolvedState != ILLEGAL_STATE &&
190  handledStates.find(resolvedState) == handledStates.end() &&
191  resolvedState != state)
192  unfinishedStatesList.insert(resolvedState);
193  }
194  handledStates.insert(state);
195  }
196 }
197 
198 /**
199  * Returns the collision matrix for the given operation.
200  *
201  * @param operationName Name of the operation.
202  * @return The collision matrix.
203  */
206  const std::string operationName) {
209 }
210 
211 /**
212  * Returns the index of the state resulting from the given transition from
213  * the given source state.
214  *
215  * This method is called often, thus should be as fast as possible.
216  * Therefore, all range checking etc. is disabled. This implementation
217  * supports lazy initialization, i.e., it is capable of building missing
218  * states when requested.
219  *
220  * @param source The source state.
221  * @param transition The transition.
222  * @return The index of the destination state.
223  */
226  FSAStateIndex source, FSAStateTransitionIndex transition) {
227  if (transitions_[source][transition] == UNKNOWN_STATE)
228  return resolveState(source, transition);
229  return transitions_[source][transition];
230 }
231 
232 /**
233  * Returns true in case the given transition is legal (accepted by the
234  * state machine) from the given state.
235  *
236  * This method is called often, thus should be as fast as possible.
237  * Therefore, all range checking etc. is disabled. This implementation
238  * supports lazy initialization, i.e., it is capable of building missing
239  * states when requested.
240  *
241  * @param source The source state.
242  * @param transition The transition.
243  * @return True in case the transition is legal.
244  */
245 bool
247  FSAStateIndex source,
248  FSAStateTransitionIndex transition) {
249  return destinationState(source, transition) != ILLEGAL_STATE;
250 }
251 
252 /**
253  * Returns a join state which is created by ORin the conflict vectors of the
254  * given source states.
255  *
256  * If the state already exists, returns it, creates a new state otherwise.
257  *
258  * @param sourceStates The set of source states which are joined.
259  * @return Index to the join state.
260  */
263  abortWithError("Not implemented.");
264  return -1;
265 }
266 
267 /**
268  * Returns the textual description of the state at the given index.
269  *
270  * @param state The index of the state of which name to return.
271  * @return The name of the state.
272  */
273 std::string
275  StateCollisionMatrixIndex::const_iterator i =
276  stateCollisionMatrices_.find(state);
277 
278  if (i == stateCollisionMatrices_.end())
279  throw OutOfRange(
280  __FILE__, __LINE__, __func__, "No such state.");
281 
282  return (*i).second->toDotString();
283 }
FiniteStateAutomaton::ILLEGAL_STATE
static const FSAStateIndex ILLEGAL_STATE
A state id which denotes an illegal state.
Definition: FiniteStateAutomaton.hh:61
FiniteStateAutomaton::FSAStateIndex
int FSAStateIndex
Type used for indexing the states.
Definition: FiniteStateAutomaton.hh:57
FUFiniteStateAutomaton::isLegalTransition
virtual bool isLegalTransition(FSAStateIndex source, FSAStateTransitionIndex transition)
Definition: FUFiniteStateAutomaton.cc:246
BitMatrix::orWith
void orWith(const BitMatrix &another)
FiniteStateAutomaton::transitions_
TransitionMap transitions_
The state transitions. In protected to allow fast access from derived classes.
Definition: FiniteStateAutomaton.hh:120
FUFiniteStateAutomaton::FUFiniteStateAutomaton
FUFiniteStateAutomaton(const TTAMachine::FunctionUnit &fu, bool lazyBuilding=true)
Definition: FUFiniteStateAutomaton.cc:61
FUFiniteStateAutomaton::addCollisionMatrixForState
void addCollisionMatrixForState(FSAStateIndex state, CollisionMatrix *matrix)
Definition: FUFiniteStateAutomaton.cc:162
OutOfRange
Definition: Exception.hh:320
SequenceTools.hh
FUFiniteStateAutomaton::operationCollisionMatrix
CollisionMatrix & operationCollisionMatrix(const std::string operationName)
Definition: FUFiniteStateAutomaton.cc:205
ResourceVectorSet.hh
FUFiniteStateAutomaton::~FUFiniteStateAutomaton
virtual ~FUFiniteStateAutomaton()
Definition: FUFiniteStateAutomaton.cc:93
FUFiniteStateAutomaton::buildStateMachine
void buildStateMachine()
Definition: FUFiniteStateAutomaton.cc:176
FUFiniteStateAutomaton::stateName
virtual std::string stateName(FSAStateIndex state) const
Definition: FUFiniteStateAutomaton.cc:274
FUFiniteStateAutomaton::stateCollisionMatrices_
StateCollisionMatrixIndex stateCollisionMatrices_
The collision matrices of each state.
Definition: FUFiniteStateAutomaton.hh:109
hash_set.hh
FUCollisionMatrixIndex::size
int size() const
Definition: FUCollisionMatrixIndex.cc:99
FUFiniteStateAutomaton::resolveState
FiniteStateAutomaton::FSAStateIndex resolveState(FiniteStateAutomaton::FSAStateIndex source, FiniteStateAutomaton::FSAStateTransitionIndex transition)
Definition: FUFiniteStateAutomaton.cc:105
FiniteStateAutomaton::UNKNOWN_STATE
static const FSAStateIndex UNKNOWN_STATE
A state id which denotes an unknown (unresolved) state. Used for lazy construction of states.
Definition: FiniteStateAutomaton.hh:64
StringTools::stringToUpper
static std::string stringToUpper(const std::string &source)
Definition: StringTools.cc:143
FiniteStateAutomaton::setTransition
virtual void setTransition(FSAStateIndex source, FSAStateIndex destination, FSAStateTransitionIndex transition)
Definition: FiniteStateAutomaton.cc:219
StringTools.hh
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
FUFiniteStateAutomaton::joinState
FSAStateIndex joinState(FSAStateIndexSet sourceStates)
Definition: FUFiniteStateAutomaton.cc:262
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
HWOperation.hh
TTAMachine::HWOperation::name
const std::string & name() const
Definition: HWOperation.cc:141
Application.hh
__func__
#define __func__
Definition: Application.hh:67
BitMatrix::shiftLeft
void shiftLeft()
FUCollisionMatrixIndex::at
CollisionMatrix & at(int index)
Definition: FUCollisionMatrixIndex.cc:89
TTAMachine::FunctionUnit::operationCount
virtual int operationCount() const
Definition: FunctionUnit.cc:419
FUFiniteStateAutomaton::destinationState
virtual FSAStateIndex destinationState(FSAStateIndex source, FSAStateTransitionIndex transition)
Definition: FUFiniteStateAutomaton.cc:225
FUFiniteStateAutomaton::nopTransition_
FSAStateTransitionIndex nopTransition_
The number of the NOP transition.
Definition: FUFiniteStateAutomaton.hh:113
FUFiniteStateAutomaton::operationCollisionMatrices_
FUCollisionMatrixIndex operationCollisionMatrices_
Collision matrices of operations are stored here.
Definition: FUFiniteStateAutomaton.hh:107
FiniteStateAutomaton
Definition: FiniteStateAutomaton.hh:49
CollisionMatrix::isCollision
virtual bool isCollision(std::size_t row, std::size_t column) const
Definition: CollisionMatrix.cc:106
FiniteStateAutomaton::transitionIndex
virtual FSAStateTransitionIndex transitionIndex(const std::string &transitionName) const
Definition: FiniteStateAutomaton.cc:120
CollisionMatrix.hh
FiniteStateAutomaton::addState
virtual FSAStateIndex addState()
Definition: FiniteStateAutomaton.cc:178
FiniteStateAutomaton::FSAStateTransitionIndex
int FSAStateTransitionIndex
Type used for indexing the transitions.
Definition: FiniteStateAutomaton.hh:55
FiniteStateAutomaton::setTransitionName
virtual void setTransitionName(FSAStateTransitionIndex transition, const std::string &name)
Definition: FiniteStateAutomaton.cc:93
AssocTools.hh
FUFiniteStateAutomaton::collisionMatrixStates_
CollisionMatrixStateIndex collisionMatrixStates_
An index for quickly finding the state of a collision matrix.
Definition: FUFiniteStateAutomaton.hh:111
FUFiniteStateAutomaton.hh
TTAMachine::FunctionUnit::operation
virtual HWOperation * operation(const std::string &name) const
Definition: FunctionUnit.cc:363
FiniteStateAutomaton::FSAStateIndexSet
std::set< FSAStateIndex > FSAStateIndexSet
Type for a set of state indices.
Definition: FiniteStateAutomaton.hh:59
AssocTools::deleteAllValues
static void deleteAllValues(ContainerType &aMap)
CollisionMatrix
Definition: CollisionMatrix.hh:49
FunctionUnit.hh