OpenASIP  2.0
Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
FUFiniteStateAutomaton Class Reference

#include <FUFiniteStateAutomaton.hh>

Inheritance diagram for FUFiniteStateAutomaton:
Inheritance graph
Collaboration diagram for FUFiniteStateAutomaton:
Collaboration graph

Public Types

typedef FSAStateTransitionIndex OperationID
 
- Public Types inherited from FiniteStateAutomaton
typedef int FSAStateTransitionIndex
 Type used for indexing the transitions. More...
 
typedef int FSAStateIndex
 Type used for indexing the states. More...
 
typedef std::set< FSAStateIndexFSAStateIndexSet
 Type for a set of state indices. More...
 

Public Member Functions

 FUFiniteStateAutomaton (const TTAMachine::FunctionUnit &fu, bool lazyBuilding=true)
 
virtual ~FUFiniteStateAutomaton ()
 
FSAStateIndex joinState (FSAStateIndexSet sourceStates)
 
CollisionMatrixoperationCollisionMatrix (const std::string operationName)
 
virtual FSAStateIndex destinationState (FSAStateIndex source, FSAStateTransitionIndex transition)
 
virtual bool isLegalTransition (FSAStateIndex source, FSAStateTransitionIndex transition)
 
virtual std::string stateName (FSAStateIndex state) const
 
bool conflictsWith (OperationID operation) const
 Inline functions for fast access in the compiled simulator. More...
 
void issueOperation (OperationID operation)
 
void advanceCycle ()
 
void buildStateMachine ()
 
- Public Member Functions inherited from FiniteStateAutomaton
 FiniteStateAutomaton (FSAStateTransitionIndex defaultState=ILLEGAL_STATE, int transitionCount=0)
 
virtual ~FiniteStateAutomaton ()
 
virtual const std::string & transitionName (FSAStateTransitionIndex transition) const
 
virtual FSAStateTransitionIndex transitionIndex (const std::string &transitionName) const
 
virtual FSAStateIndex addState ()
 
virtual FSAStateTransitionIndex addTransitionType (const std::string &name)
 
virtual void setTransitionName (FSAStateTransitionIndex transition, const std::string &name)
 
virtual void setTransition (FSAStateIndex source, FSAStateIndex destination, FSAStateTransitionIndex transition)
 
virtual std::string toDotString () const
 
virtual FSAStateIndex startState () const
 

Private Types

typedef std::map< FSAStateIndex, CollisionMatrix * > StateCollisionMatrixIndex
 Index for collision matrices of states (key is the state index). More...
 
typedef std::map< CollisionMatrix, FSAStateIndexCollisionMatrixStateIndex
 Index for finding the state for a collision matrix. More...
 

Private Member Functions

void addCollisionMatrixForState (FSAStateIndex state, CollisionMatrix *matrix)
 
FiniteStateAutomaton::FSAStateIndex resolveState (FiniteStateAutomaton::FSAStateIndex source, FiniteStateAutomaton::FSAStateTransitionIndex transition)
 

Private Attributes

FUCollisionMatrixIndex operationCollisionMatrices_
 Collision matrices of operations are stored here. More...
 
StateCollisionMatrixIndex stateCollisionMatrices_
 The collision matrices of each state. More...
 
CollisionMatrixStateIndex collisionMatrixStates_
 An index for quickly finding the state of a collision matrix. More...
 
FSAStateTransitionIndex nopTransition_
 The number of the NOP transition. More...
 

Friends

class FSAFUResourceConflictDetector
 Friend due to highly optimized compiled simulation versions of the conflict detection functions. More...
 

Additional Inherited Members

- Static Public Attributes inherited from FiniteStateAutomaton
static const FSAStateIndex ILLEGAL_STATE = -1
 A state id which denotes an illegal state. More...
 
static const FSAStateIndex UNKNOWN_STATE = -2
 A state id which denotes an unknown (unresolved) state. Used for lazy construction of states. More...
 
- Protected Types inherited from FiniteStateAutomaton
typedef std::vector< FSAStateIndexTransitionVector
 Vector which stores the target states of for each transition type. More...
 
typedef std::vector< TransitionVectorTransitionMap
 Type for storing the transitions of each state. The vector stores for each state (indexed from 0) a vector with elements as many there are transitions. The value of the element is the target state to which the transition leads to, or ILLEGAL_STATE_INDEX if there is no transition for that transition type from that state. Using this structure it's possible to find the target state using two table (vector) lookups. More...
 
- Protected Attributes inherited from FiniteStateAutomaton
TransitionMap transitions_
 The state transitions. In protected to allow fast access from derived classes. More...
 

Detailed Description

Finite state automaton (FSA) used to model function unit pipeline resources.

Includes support for lazily initializing the states when needed.

Definition at line 55 of file FUFiniteStateAutomaton.hh.

Member Typedef Documentation

◆ CollisionMatrixStateIndex

Index for finding the state for a collision matrix.

Definition at line 104 of file FUFiniteStateAutomaton.hh.

◆ OperationID

Definition at line 61 of file FUFiniteStateAutomaton.hh.

◆ StateCollisionMatrixIndex

Index for collision matrices of states (key is the state index).

Definition at line 100 of file FUFiniteStateAutomaton.hh.

Constructor & Destructor Documentation

◆ FUFiniteStateAutomaton()

FUFiniteStateAutomaton::FUFiniteStateAutomaton ( const TTAMachine::FunctionUnit fu,
bool  lazyBuilding = true 
)

Initializes the FSA from the given FU.

This version is to be used in fast compiled simulation that needs to minimize all function call overheads. To allow figuring out the state ids statically, they are assigned according to the operation's order in the FU when fetched with FunctionUnit::operation().

The last state id (id FunctionUnit::operationCount()) is reserved for NOP. This way compiled simulator may access the state array with a constant index when issuing an operation or checking for a conflict without runtime overhead for figuring out the state id first for each operation.

Parameters
fuThe function unit to build the FSA from.
lazyBuildingTrue in case states should be built lazily the first time they are visited. Should improve startup time for larger FSAs.

Definition at line 61 of file FUFiniteStateAutomaton.cc.

62  :
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 }

References addCollisionMatrixForState(), FiniteStateAutomaton::addState(), FUCollisionMatrixIndex::at(), buildStateMachine(), TTAMachine::HWOperation::name(), nopTransition_, TTAMachine::FunctionUnit::operation(), operationCollisionMatrices_, TTAMachine::FunctionUnit::operationCount(), FiniteStateAutomaton::setTransitionName(), and StringTools::stringToUpper().

Here is the call graph for this function:

◆ ~FUFiniteStateAutomaton()

FUFiniteStateAutomaton::~FUFiniteStateAutomaton ( )
virtual

Destructor.

Definition at line 93 of file FUFiniteStateAutomaton.cc.

References AssocTools::deleteAllValues(), and stateCollisionMatrices_.

Here is the call graph for this function:

Member Function Documentation

◆ addCollisionMatrixForState()

void FUFiniteStateAutomaton::addCollisionMatrixForState ( FSAStateIndex  state,
CollisionMatrix matrix 
)
private

Connects a state and a collision matrix.

Parameters
stateThe state index.
matrixThe collision matrix (becomes owned by the index).

Definition at line 162 of file FUFiniteStateAutomaton.cc.

163  {
164 
165  stateCollisionMatrices_[state] = matrix;
166  collisionMatrixStates_[*matrix] = state;
167 }

References collisionMatrixStates_, and stateCollisionMatrices_.

Referenced by FUFiniteStateAutomaton(), and resolveState().

◆ advanceCycle()

void FUFiniteStateAutomaton::advanceCycle ( )

◆ buildStateMachine()

void FUFiniteStateAutomaton::buildStateMachine ( )

Builds all states and state transitions.

This might take very long time if there are lots of states to build. That is, if the FU pipeline resource usage patterns are long and complicated.

Definition at line 176 of file FUFiniteStateAutomaton.cc.

176  {
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 }

References FiniteStateAutomaton::ILLEGAL_STATE, operationCollisionMatrices_, resolveState(), and FUCollisionMatrixIndex::size().

Referenced by FUFiniteStateAutomaton(), and FSAFUResourceConflictDetector::initializeAllStates().

Here is the call graph for this function:

◆ conflictsWith()

bool FUFiniteStateAutomaton::conflictsWith ( OperationID  operation) const

Inline functions for fast access in the compiled simulator.

◆ destinationState()

FUFiniteStateAutomaton::FSAStateIndex FUFiniteStateAutomaton::destinationState ( FSAStateIndex  source,
FSAStateTransitionIndex  transition 
)
virtual

Returns the index of the state resulting from the given transition from the given source state.

This method is called often, thus should be as fast as possible. Therefore, all range checking etc. is disabled. This implementation supports lazy initialization, i.e., it is capable of building missing states when requested.

Parameters
sourceThe source state.
transitionThe transition.
Returns
The index of the destination state.

Reimplemented from FiniteStateAutomaton.

Definition at line 225 of file FUFiniteStateAutomaton.cc.

226  {
227  if (transitions_[source][transition] == UNKNOWN_STATE)
228  return resolveState(source, transition);
229  return transitions_[source][transition];
230 }

References resolveState(), FiniteStateAutomaton::transitions_, and FiniteStateAutomaton::UNKNOWN_STATE.

Referenced by isLegalTransition().

Here is the call graph for this function:

◆ isLegalTransition()

bool FUFiniteStateAutomaton::isLegalTransition ( FSAStateIndex  source,
FSAStateTransitionIndex  transition 
)
virtual

Returns true in case the given transition is legal (accepted by the state machine) from the given state.

This method is called often, thus should be as fast as possible. Therefore, all range checking etc. is disabled. This implementation supports lazy initialization, i.e., it is capable of building missing states when requested.

Parameters
sourceThe source state.
transitionThe transition.
Returns
True in case the transition is legal.

Reimplemented from FiniteStateAutomaton.

Definition at line 246 of file FUFiniteStateAutomaton.cc.

248  {
249  return destinationState(source, transition) != ILLEGAL_STATE;
250 }

References destinationState(), and FiniteStateAutomaton::ILLEGAL_STATE.

Here is the call graph for this function:

◆ issueOperation()

void FUFiniteStateAutomaton::issueOperation ( OperationID  operation)

◆ joinState()

FiniteStateAutomaton::FSAStateIndex FUFiniteStateAutomaton::joinState ( FSAStateIndexSet  sourceStates)

Returns a join state which is created by ORin the conflict vectors of the given source states.

If the state already exists, returns it, creates a new state otherwise.

Parameters
sourceStatesThe set of source states which are joined.
Returns
Index to the join state.

Definition at line 262 of file FUFiniteStateAutomaton.cc.

262  {
263  abortWithError("Not implemented.");
264  return -1;
265 }

References abortWithError.

◆ operationCollisionMatrix()

CollisionMatrix & FUFiniteStateAutomaton::operationCollisionMatrix ( const std::string  operationName)

Returns the collision matrix for the given operation.

Parameters
operationNameName of the operation.
Returns
The collision matrix.

Definition at line 205 of file FUFiniteStateAutomaton.cc.

206  {
209 }

References FUCollisionMatrixIndex::at(), operationCollisionMatrices_, StringTools::stringToUpper(), and FiniteStateAutomaton::transitionIndex().

Referenced by resolveState().

Here is the call graph for this function:

◆ resolveState()

FiniteStateAutomaton::FSAStateIndex FUFiniteStateAutomaton::resolveState ( FiniteStateAutomaton::FSAStateIndex  source,
FiniteStateAutomaton::FSAStateTransitionIndex  transition 
)
private

Initializes the given state transition entry.

The transition target state is resolved and built if necessary.

Returns
The index of the target state.

Definition at line 105 of file FUFiniteStateAutomaton.cc.

107  {
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 }

References addCollisionMatrixForState(), FiniteStateAutomaton::addState(), FUCollisionMatrixIndex::at(), collisionMatrixStates_, FiniteStateAutomaton::ILLEGAL_STATE, CollisionMatrix::isCollision(), nopTransition_, operationCollisionMatrices_, operationCollisionMatrix(), BitMatrix::orWith(), FiniteStateAutomaton::setTransition(), BitMatrix::shiftLeft(), and stateCollisionMatrices_.

Referenced by FSAFUResourceConflictDetector::advanceCycleLazyInline(), buildStateMachine(), destinationState(), and FSAFUResourceConflictDetector::issueOperationLazyInline().

Here is the call graph for this function:

◆ stateName()

std::string FUFiniteStateAutomaton::stateName ( FSAStateIndex  state) const
virtual

Returns the textual description of the state at the given index.

Parameters
stateThe index of the state of which name to return.
Returns
The name of the state.

Reimplemented from FiniteStateAutomaton.

Definition at line 274 of file FUFiniteStateAutomaton.cc.

274  {
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 }

References __func__, and stateCollisionMatrices_.

Friends And Related Function Documentation

◆ FSAFUResourceConflictDetector

friend class FSAFUResourceConflictDetector
friend

Friend due to highly optimized compiled simulation versions of the conflict detection functions.

Definition at line 59 of file FUFiniteStateAutomaton.hh.

Member Data Documentation

◆ collisionMatrixStates_

CollisionMatrixStateIndex FUFiniteStateAutomaton::collisionMatrixStates_
private

An index for quickly finding the state of a collision matrix.

Definition at line 111 of file FUFiniteStateAutomaton.hh.

Referenced by addCollisionMatrixForState(), and resolveState().

◆ nopTransition_

FSAStateTransitionIndex FUFiniteStateAutomaton::nopTransition_
private

The number of the NOP transition.

Definition at line 113 of file FUFiniteStateAutomaton.hh.

Referenced by FUFiniteStateAutomaton(), and resolveState().

◆ operationCollisionMatrices_

FUCollisionMatrixIndex FUFiniteStateAutomaton::operationCollisionMatrices_
private

Collision matrices of operations are stored here.

Definition at line 107 of file FUFiniteStateAutomaton.hh.

Referenced by buildStateMachine(), FUFiniteStateAutomaton(), operationCollisionMatrix(), and resolveState().

◆ stateCollisionMatrices_

StateCollisionMatrixIndex FUFiniteStateAutomaton::stateCollisionMatrices_
private

The collision matrices of each state.

Definition at line 109 of file FUFiniteStateAutomaton.hh.

Referenced by addCollisionMatrixForState(), resolveState(), stateName(), and ~FUFiniteStateAutomaton().


The documentation for this class was generated from the following files:
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
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::addCollisionMatrixForState
void addCollisionMatrixForState(FSAStateIndex state, CollisionMatrix *matrix)
Definition: FUFiniteStateAutomaton.cc:162
OutOfRange
Definition: Exception.hh:320
FUFiniteStateAutomaton::operationCollisionMatrix
CollisionMatrix & operationCollisionMatrix(const std::string operationName)
Definition: FUFiniteStateAutomaton.cc:205
FiniteStateAutomaton::FiniteStateAutomaton
FiniteStateAutomaton(FSAStateTransitionIndex defaultState=ILLEGAL_STATE, int transitionCount=0)
Definition: FiniteStateAutomaton.cc:55
FUFiniteStateAutomaton::buildStateMachine
void buildStateMachine()
Definition: FUFiniteStateAutomaton.cc:176
FUFiniteStateAutomaton::stateCollisionMatrices_
StateCollisionMatrixIndex stateCollisionMatrices_
The collision matrices of each state.
Definition: FUFiniteStateAutomaton.hh:109
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
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
TTAMachine::HWOperation::name
const std::string & name() const
Definition: HWOperation.cc:141
__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
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
FiniteStateAutomaton::addState
virtual FSAStateIndex addState()
Definition: FiniteStateAutomaton.cc:178
FiniteStateAutomaton::setTransitionName
virtual void setTransitionName(FSAStateTransitionIndex transition, const std::string &name)
Definition: FiniteStateAutomaton.cc:93
FUFiniteStateAutomaton::collisionMatrixStates_
CollisionMatrixStateIndex collisionMatrixStates_
An index for quickly finding the state of a collision matrix.
Definition: FUFiniteStateAutomaton.hh:111
TTAMachine::FunctionUnit::operation
virtual HWOperation * operation(const std::string &name) const
Definition: FunctionUnit.cc:363
AssocTools::deleteAllValues
static void deleteAllValues(ContainerType &aMap)
CollisionMatrix
Definition: CollisionMatrix.hh:49