yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SubCircuit::SolverWorker Class Reference
+ Collaboration diagram for SubCircuit::SolverWorker:

Data Structures

struct  DiBit
 
struct  DiCache
 
struct  DiEdge
 
struct  DiNode
 
struct  GraphData
 
struct  NodeSet
 

Protected Member Functions

 SolverWorker (Solver *userSolver)
 
void setVerbose ()
 
void addGraph (std::string graphId, const Graph &graph)
 
void addCompatibleTypes (std::string needleTypeId, std::string haystackTypeId)
 
void addCompatibleConstants (int needleConstant, int haystackConstant)
 
void addSwappablePorts (std::string needleTypeId, const std::set< std::string > &ports)
 
void addSwappablePortsPermutation (std::string needleTypeId, const std::map< std::string, std::string > &portMapping)
 
void solve (std::vector< Solver::Result > &results, std::string needleGraphId, std::string haystackGraphId, const std::map< std::string, std::set< std::string >> &initialMappings, bool allowOverlap, int maxSolutions)
 
void mine (std::vector< Solver::MineResult > &results, int minNodes, int maxNodes, int minMatches, int limitMatchesPerGraph)
 
void clearOverlapHistory ()
 
void clearConfig ()
 

Private Types

typedef std::vector< std::map
< int, int > > 
adjMatrix_t
 

Private Member Functions

bool matchNodePorts (const Graph &needle, int needleNodeIdx, const Graph &haystack, int haystackNodeIdx, const std::map< std::string, std::string > &swaps) const
 
bool matchNodes (const GraphData &needle, int needleNodeIdx, const GraphData &haystack, int haystackNodeIdx) const
 
void generateEnumerationMatrix (std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, const std::map< std::string, std::set< std::string >> &initialMappings) const
 
bool checkEnumerationMatrix (std::vector< std::set< int >> &enumerationMatrix, int i, int j, const GraphData &needle, const GraphData &haystack)
 
bool pruneEnumerationMatrix (std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int &nextRow, bool allowOverlap)
 
void printEnumerationMatrix (const std::vector< std::set< int >> &enumerationMatrix, int maxHaystackNodeIdx=-1) const
 
bool checkPortmapCandidate (const std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int idx, const std::map< std::string, std::string > &currentCandidate)
 
void generatePortmapCandidates (std::set< std::map< std::string, std::string >> &portmapCandidates, const std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int idx)
 
bool prunePortmapCandidates (std::vector< std::set< std::map< std::string, std::string >>> &portmapCandidates, std::vector< std::set< int >> enumerationMatrix, const GraphData &needle, const GraphData &haystack)
 
void ullmannRecursion (std::vector< Solver::Result > &results, std::vector< std::set< int >> &enumerationMatrix, int iter, const GraphData &needle, GraphData &haystack, bool allowOverlap, int limitResults)
 
void solveForMining (std::vector< Solver::Result > &results, const GraphData &needle)
 
int testForMining (std::vector< Solver::MineResult > &results, std::set< NodeSet > &usedSets, std::set< NodeSet > &nextPool, NodeSet &testSet, const std::string &graphId, const Graph &graph, int minNodes, int minMatches, int limitMatchesPerGraph)
 
void findNodePairs (std::vector< Solver::MineResult > &results, std::set< NodeSet > &nodePairs, int minNodes, int minMatches, int limitMatchesPerGraph)
 
void findNextPool (std::vector< Solver::MineResult > &results, std::set< NodeSet > &pool, int oldSetSize, int increment, int minNodes, int minMatches, int limitMatchesPerGraph)
 

Static Private Member Functions

static void printAdjMatrix (const adjMatrix_t &matrix)
 
static int numberOfPermutations (const std::vector< std::string > &list)
 
static void permutateVectorToMap (std::map< std::string, std::string > &map, const std::vector< std::string > &list, int idx)
 
static int numberOfPermutationsArray (const std::vector< std::vector< std::string >> &list)
 
static void permutateVectorToMapArray (std::map< std::string, std::string > &map, const std::vector< std::vector< std::string >> &list, int idx)
 
static void applyPermutation (std::map< std::string, std::string > &map, const std::map< std::string, std::string > &permutation)
 

Private Attributes

SolveruserSolver
 
std::map< std::string, GraphDatagraphData
 
std::map< std::string,
std::set< std::string > > 
compatibleTypes
 
std::map< int, std::set< int > > compatibleConstants
 
std::map< std::string,
std::set< std::set
< std::string > > > 
swapPorts
 
std::map< std::string,
std::set< std::map
< std::string, std::string > > > 
swapPermutations
 
DiCache diCache
 
bool verbose
 

Static Private Attributes

static const int maxPermutationsLimit = 1000000
 

Friends

class Solver
 

Detailed Description

Definition at line 287 of file subcircuit.cc.

Member Typedef Documentation

typedef std::vector<std::map<int, int> > SubCircuit::SolverWorker::adjMatrix_t
private

Definition at line 291 of file subcircuit.cc.

Constructor & Destructor Documentation

SubCircuit::SolverWorker::SolverWorker ( Solver userSolver)
inlineprotected

Definition at line 1474 of file subcircuit.cc.

1474  : userSolver(userSolver), verbose(false)
1475  {
1476  }

Member Function Documentation

void SubCircuit::SolverWorker::addCompatibleConstants ( int  needleConstant,
int  haystackConstant 
)
inlineprotected

Definition at line 1498 of file subcircuit.cc.

1499  {
1500  compatibleConstants[needleConstant].insert(haystackConstant);
1501  }
std::map< int, std::set< int > > compatibleConstants
Definition: subcircuit.cc:717
void SubCircuit::SolverWorker::addCompatibleTypes ( std::string  needleTypeId,
std::string  haystackTypeId 
)
inlineprotected

Definition at line 1493 of file subcircuit.cc.

1494  {
1495  compatibleTypes[needleTypeId].insert(haystackTypeId);
1496  }
std::map< std::string, std::set< std::string > > compatibleTypes
Definition: subcircuit.cc:716
void SubCircuit::SolverWorker::addGraph ( std::string  graphId,
const Graph graph 
)
inlineprotected

Definition at line 1483 of file subcircuit.cc.

1484  {
1485  assert(graphData.count(graphId) == 0);
1486 
1487  GraphData &gd = graphData[graphId];
1488  gd.graphId = graphId;
1489  gd.graph = graph;
1490  diCache.add(gd.graph, gd.adjMatrix, graphId, userSolver);
1491  }
void add(const Graph &graph, adjMatrix_t &adjMatrix, const std::string &graphId, Solver *userSolver)
Definition: subcircuit.cc:662
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715

+ Here is the call graph for this function:

void SubCircuit::SolverWorker::addSwappablePorts ( std::string  needleTypeId,
const std::set< std::string > &  ports 
)
inlineprotected

Definition at line 1503 of file subcircuit.cc.

1504  {
1505  swapPorts[needleTypeId].insert(ports);
1506  diCache.compareCache.clear();
1507  }
std::map< std::pair< int, int >, bool > compareCache
Definition: subcircuit.cc:660
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718
void SubCircuit::SolverWorker::addSwappablePortsPermutation ( std::string  needleTypeId,
const std::map< std::string, std::string > &  portMapping 
)
inlineprotected

Definition at line 1509 of file subcircuit.cc.

1510  {
1511  swapPermutations[needleTypeId].insert(portMapping);
1512  diCache.compareCache.clear();
1513  }
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
std::map< std::pair< int, int >, bool > compareCache
Definition: subcircuit.cc:660
static void SubCircuit::SolverWorker::applyPermutation ( std::map< std::string, std::string > &  map,
const std::map< std::string, std::string > &  permutation 
)
inlinestaticprivate

Definition at line 380 of file subcircuit.cc.

381  {
382  std::vector<std::pair<std::string, std::string>> changeLog;
383  for (const auto &it : permutation)
384  if (map.count(it.second))
385  changeLog.push_back(std::pair<std::string, std::string>(it.first, map.at(it.second)));
386  else
387  changeLog.push_back(std::pair<std::string, std::string>(it.first, it.second));
388  for (const auto &it : changeLog)
389  map[it.first] = it.second;
390  }

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::checkEnumerationMatrix ( std::vector< std::set< int >> &  enumerationMatrix,
int  i,
int  j,
const GraphData needle,
const GraphData haystack 
)
inlineprivate

Definition at line 885 of file subcircuit.cc.

886  {
887  for (const auto &it_needle : needle.adjMatrix.at(i))
888  {
889  int needleNeighbour = it_needle.first;
890  int needleEdgeType = it_needle.second;
891 
892  for (int haystackNeighbour : enumerationMatrix[needleNeighbour])
893  if (haystack.adjMatrix.at(j).count(haystackNeighbour) > 0) {
894  int haystackEdgeType = haystack.adjMatrix.at(j).at(haystackNeighbour);
895  if (diCache.compare(needleEdgeType, haystackEdgeType, swapPorts, swapPermutations)) {
896  const Graph::Node &needleFromNode = needle.graph.nodes[i];
897  const Graph::Node &needleToNode = needle.graph.nodes[needleNeighbour];
898  const Graph::Node &haystackFromNode = haystack.graph.nodes[j];
899  const Graph::Node &haystackToNode = haystack.graph.nodes[haystackNeighbour];
900  if (userSolver->userCompareEdge(needle.graphId, needleFromNode.nodeId, needleFromNode.userData, needleToNode.nodeId, needleToNode.userData,
901  haystack.graphId, haystackFromNode.nodeId, haystackFromNode.userData, haystackToNode.nodeId, haystackToNode.userData))
902  goto found_match;
903  }
904  }
905 
906  return false;
907  found_match:;
908  }
909 
910  return true;
911  }
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
bool compare(int needleEdge, int haystackEdge, const std::map< std::string, std::set< std::set< std::string >>> &swapPorts, const std::map< std::string, std::set< std::map< std::string, std::string >>> &swapPermutations)
Definition: subcircuit.cc:685
virtual bool userCompareEdge(const std::string &needleGraphId, const std::string &needleFromNodeId, void *needleFromUserData, const std::string &needleToNodeId, void *needleToUserData, const std::string &haystackGraphId, const std::string &haystackFromNodeId, void *haystackFromUserData, const std::string &haystackToNodeId, void *haystackToUserData)
Definition: subcircuit.cc:1607
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::checkPortmapCandidate ( const std::vector< std::set< int >> &  enumerationMatrix,
const GraphData needle,
const GraphData haystack,
int  idx,
const std::map< std::string, std::string > &  currentCandidate 
)
inlineprivate

Definition at line 965 of file subcircuit.cc.

966  {
967  assert(enumerationMatrix[idx].size() == 1);
968  int idxHaystack = *enumerationMatrix[idx].begin();
969 
970  const Graph::Node &nn = needle.graph.nodes[idx];
971  const Graph::Node &hn = haystack.graph.nodes[idxHaystack];
972 
973  if (!matchNodePorts(needle.graph, idx, haystack.graph, idxHaystack, currentCandidate) ||
974  !userSolver->userCompareNodes(needle.graphId, nn.nodeId, nn.userData, haystack.graphId, hn.nodeId, hn.userData, currentCandidate))
975  return false;
976 
977  for (const auto &it_needle : needle.adjMatrix.at(idx))
978  {
979  int needleNeighbour = it_needle.first;
980  int needleEdgeType = it_needle.second;
981 
982  assert(enumerationMatrix[needleNeighbour].size() == 1);
983  int haystackNeighbour = *enumerationMatrix[needleNeighbour].begin();
984 
985  assert(haystack.adjMatrix.at(idxHaystack).count(haystackNeighbour) > 0);
986  int haystackEdgeType = haystack.adjMatrix.at(idxHaystack).at(haystackNeighbour);
987  if (!diCache.compare(needleEdgeType, haystackEdgeType, currentCandidate, swapPorts, swapPermutations))
988  return false;
989  }
990 
991  return true;
992  }
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
virtual bool userCompareNodes(const std::string &needleGraphId, const std::string &needleNodeId, void *needleUserData, const std::string &haystackGraphId, const std::string &haystackNodeId, void *haystackUserData, const std::map< std::string, std::string > &portMapping)
Definition: subcircuit.cc:1597
static std::string idx(std::string str)
Definition: test_autotb.cc:57
bool matchNodePorts(const Graph &needle, int needleNodeIdx, const Graph &haystack, int haystackNodeIdx, const std::map< std::string, std::string > &swaps) const
Definition: subcircuit.cc:725
bool compare(int needleEdge, int haystackEdge, const std::map< std::string, std::set< std::set< std::string >>> &swapPorts, const std::map< std::string, std::set< std::map< std::string, std::string >>> &swapPermutations)
Definition: subcircuit.cc:685
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::clearConfig ( )
inlineprotected

Definition at line 1585 of file subcircuit.cc.

1586  {
1587  compatibleTypes.clear();
1588  compatibleConstants.clear();
1589  swapPorts.clear();
1590  swapPermutations.clear();
1591  diCache.compareCache.clear();
1592  }
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
std::map< std::pair< int, int >, bool > compareCache
Definition: subcircuit.cc:660
std::map< std::string, std::set< std::string > > compatibleTypes
Definition: subcircuit.cc:716
std::map< int, std::set< int > > compatibleConstants
Definition: subcircuit.cc:717
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718
void SubCircuit::SolverWorker::clearOverlapHistory ( )
inlineprotected

Definition at line 1579 of file subcircuit.cc.

1580  {
1581  for (auto &it : graphData)
1582  it.second.usedNodes.clear();
1583  }
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715
void SubCircuit::SolverWorker::findNextPool ( std::vector< Solver::MineResult > &  results,
std::set< NodeSet > &  pool,
int  oldSetSize,
int  increment,
int  minNodes,
int  minMatches,
int  limitMatchesPerGraph 
)
inlineprivate

Definition at line 1397 of file subcircuit.cc.

1399  {
1400  int groupCounter = 0;
1401  std::map<std::string, std::vector<const NodeSet*>> poolPerGraph;
1402  std::set<NodeSet> nextPool;
1403 
1404  for (auto &it : pool)
1405  poolPerGraph[it.graphId].push_back(&it);
1406 
1407  if (verbose)
1408  my_printf("\nMining for frequent subcircuits of size %d using increment %d:\n", oldSetSize+increment, increment);
1409 
1410  int count = 0;
1411  for (auto &it : poolPerGraph)
1412  {
1413  std::map<int, std::set<int>> node2sets;
1414  std::set<NodeSet> usedSets;
1415 
1416  for (int idx = 0; idx < int(it.second.size()); idx++) {
1417  for (int node : it.second[idx]->nodes)
1418  node2sets[node].insert(idx);
1419  }
1420 
1421  for (int idx1 = 0; idx1 < int(it.second.size()); idx1++, count++)
1422  {
1423  std::set<int> idx2set;
1424 
1425  for (int node : it.second[idx1]->nodes)
1426  for (int idx2 : node2sets[node])
1427  if (idx2 > idx1)
1428  idx2set.insert(idx2);
1429 
1430  for (int idx2 : idx2set)
1431  {
1432  if (it.second[idx1]->extendCandidate(*it.second[idx2]) != increment)
1433  continue;
1434 
1435  NodeSet mergedSet = *it.second[idx1];
1436  mergedSet.extend(*it.second[idx2]);
1437 
1438  if (usedSets.count(mergedSet) > 0)
1439  continue;
1440 
1441  const std::string &graphId = it.first;
1442  const auto &graph = graphData[it.first].graph;
1443 
1444  if (verbose) {
1445  my_printf("<%d%%/%d> Found %s[", int(100*count/pool.size()), oldSetSize+increment, graphId.c_str());
1446  bool first = true;
1447  for (int nodeIdx : mergedSet.nodes) {
1448  my_printf("%s%s", first ? "" : ",", graph.nodes[nodeIdx].nodeId.c_str());
1449  first = false;
1450  }
1451  my_printf("] ->");
1452  }
1453 
1454  int matches = testForMining(results, usedSets, nextPool, mergedSet, graphId, graph, minNodes, minMatches, limitMatchesPerGraph);
1455 
1456  if (verbose)
1457  my_printf(" %d%s\n", matches, matches < minMatches ? " *purge*" : "");
1458 
1459  if (minMatches <= matches)
1460  groupCounter++;
1461  }
1462  }
1463  }
1464 
1465  pool.swap(nextPool);
1466 
1467  if (verbose)
1468  my_printf("Found a total of %d subgraphs in %d groups.\n", int(pool.size()), groupCounter);
1469  }
static std::string idx(std::string str)
Definition: test_autotb.cc:57
int testForMining(std::vector< Solver::MineResult > &results, std::set< NodeSet > &usedSets, std::set< NodeSet > &nextPool, NodeSet &testSet, const std::string &graphId, const Graph &graph, int minNodes, int minMatches, int limitMatchesPerGraph)
Definition: subcircuit.cc:1283
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715
#define my_printf
Definition: subcircuit.cc:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::findNodePairs ( std::vector< Solver::MineResult > &  results,
std::set< NodeSet > &  nodePairs,
int  minNodes,
int  minMatches,
int  limitMatchesPerGraph 
)
inlineprivate

Definition at line 1358 of file subcircuit.cc.

1359  {
1360  int groupCounter = 0;
1361  std::set<NodeSet> usedPairs;
1362  nodePairs.clear();
1363 
1364  if (verbose)
1365  my_printf("\nMining for frequent node pairs:\n");
1366 
1367  for (auto &graph_it : graphData)
1368  for (int node1 = 0; node1 < int(graph_it.second.graph.nodes.size()); node1++)
1369  for (auto &adj_it : graph_it.second.adjMatrix.at(node1))
1370  {
1371  const std::string &graphId = graph_it.first;
1372  const auto &graph = graph_it.second.graph;
1373  int node2 = adj_it.first;
1374 
1375  if (node1 == node2)
1376  continue;
1377 
1378  NodeSet pair(graphId, node1, node2);
1379 
1380  if (usedPairs.count(pair) > 0)
1381  continue;
1382 
1383  int matches = testForMining(results, usedPairs, nodePairs, pair, graphId, graph, minNodes, minMatches, limitMatchesPerGraph);
1384 
1385  if (verbose)
1386  my_printf("Pair %s[%s,%s] -> %d%s\n", graphId.c_str(), graph.nodes[node1].nodeId.c_str(),
1387  graph.nodes[node2].nodeId.c_str(), matches, matches < minMatches ? " *purge*" : "");
1388 
1389  if (minMatches <= matches)
1390  groupCounter++;
1391  }
1392 
1393  if (verbose)
1394  my_printf("Found a total of %d subgraphs in %d groups.\n", int(nodePairs.size()), groupCounter);
1395  }
int testForMining(std::vector< Solver::MineResult > &results, std::set< NodeSet > &usedSets, std::set< NodeSet > &nextPool, NodeSet &testSet, const std::string &graphId, const Graph &graph, int minNodes, int minMatches, int limitMatchesPerGraph)
Definition: subcircuit.cc:1283
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715
#define my_printf
Definition: subcircuit.cc:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::generateEnumerationMatrix ( std::vector< std::set< int >> &  enumerationMatrix,
const GraphData needle,
const GraphData haystack,
const std::map< std::string, std::set< std::string >> &  initialMappings 
) const
inlineprivate

Definition at line 851 of file subcircuit.cc.

852  {
853  std::map<std::string, std::set<int>> haystackNodesByTypeId;
854  for (int i = 0; i < int(haystack.graph.nodes.size()); i++)
855  haystackNodesByTypeId[haystack.graph.nodes[i].typeId].insert(i);
856 
857  enumerationMatrix.clear();
858  enumerationMatrix.resize(needle.graph.nodes.size());
859  for (int i = 0; i < int(needle.graph.nodes.size()); i++)
860  {
861  const Graph::Node &nn = needle.graph.nodes[i];
862 
863  for (int j : haystackNodesByTypeId[nn.typeId]) {
864  const Graph::Node &hn = haystack.graph.nodes[j];
865  if (initialMappings.count(nn.nodeId) > 0 && initialMappings.at(nn.nodeId).count(hn.nodeId) == 0)
866  continue;
867  if (!matchNodes(needle, i, haystack, j))
868  continue;
869  enumerationMatrix[i].insert(j);
870  }
871 
872  if (compatibleTypes.count(nn.typeId) > 0)
873  for (const std::string &compatibleTypeId : compatibleTypes.at(nn.typeId))
874  for (int j : haystackNodesByTypeId[compatibleTypeId]) {
875  const Graph::Node &hn = haystack.graph.nodes[j];
876  if (initialMappings.count(nn.nodeId) > 0 && initialMappings.at(nn.nodeId).count(hn.nodeId) == 0)
877  continue;
878  if (!matchNodes(needle, i, haystack, j))
879  continue;
880  enumerationMatrix[i].insert(j);
881  }
882  }
883  }
std::map< std::string, std::set< std::string > > compatibleTypes
Definition: subcircuit.cc:716
bool matchNodes(const GraphData &needle, int needleNodeIdx, const GraphData &haystack, int haystackNodeIdx) const
Definition: subcircuit.cc:773

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::generatePortmapCandidates ( std::set< std::map< std::string, std::string >> &  portmapCandidates,
const std::vector< std::set< int >> &  enumerationMatrix,
const GraphData needle,
const GraphData haystack,
int  idx 
)
inlineprivate

Definition at line 994 of file subcircuit.cc.

996  {
997  std::map<std::string, std::string> currentCandidate;
998 
999  for (const auto &port : needle.graph.nodes[idx].ports)
1000  currentCandidate[port.portId] = port.portId;
1001 
1002  if (swapPorts.count(needle.graph.nodes[idx].typeId) == 0)
1003  {
1004  if (checkPortmapCandidate(enumerationMatrix, needle, haystack, idx, currentCandidate))
1005  portmapCandidates.insert(currentCandidate);
1006 
1007  if (swapPermutations.count(needle.graph.nodes[idx].typeId) > 0)
1008  for (const auto &permutation : swapPermutations.at(needle.graph.nodes[idx].typeId)) {
1009  std::map<std::string, std::string> currentSubCandidate = currentCandidate;
1010  applyPermutation(currentSubCandidate, permutation);
1011  if (checkPortmapCandidate(enumerationMatrix, needle, haystack, idx, currentSubCandidate))
1012  portmapCandidates.insert(currentSubCandidate);
1013  }
1014  }
1015  else
1016  {
1017  std::vector<std::vector<std::string>> thisSwapPorts;
1018  for (const auto &ports : swapPorts.at(needle.graph.nodes[idx].typeId)) {
1019  std::vector<std::string> portsVector;
1020  for (const auto &port : ports)
1021  portsVector.push_back(port);
1022  thisSwapPorts.push_back(portsVector);
1023  }
1024 
1025  int thisPermutations = numberOfPermutationsArray(thisSwapPorts);
1026  for (int i = 0; i < thisPermutations; i++)
1027  {
1028  permutateVectorToMapArray(currentCandidate, thisSwapPorts, i);
1029 
1030  if (checkPortmapCandidate(enumerationMatrix, needle, haystack, idx, currentCandidate))
1031  portmapCandidates.insert(currentCandidate);
1032 
1033  if (swapPermutations.count(needle.graph.nodes[idx].typeId) > 0)
1034  for (const auto &permutation : swapPermutations.at(needle.graph.nodes[idx].typeId)) {
1035  std::map<std::string, std::string> currentSubCandidate = currentCandidate;
1036  applyPermutation(currentSubCandidate, permutation);
1037  if (checkPortmapCandidate(enumerationMatrix, needle, haystack, idx, currentSubCandidate))
1038  portmapCandidates.insert(currentSubCandidate);
1039  }
1040  }
1041  }
1042  }
bool checkPortmapCandidate(const std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int idx, const std::map< std::string, std::string > &currentCandidate)
Definition: subcircuit.cc:965
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
static void permutateVectorToMapArray(std::map< std::string, std::string > &map, const std::vector< std::vector< std::string >> &list, int idx)
Definition: subcircuit.cc:370
static std::string idx(std::string str)
Definition: test_autotb.cc:57
static void applyPermutation(std::map< std::string, std::string > &map, const std::map< std::string, std::string > &permutation)
Definition: subcircuit.cc:380
static int numberOfPermutationsArray(const std::vector< std::vector< std::string >> &list)
Definition: subcircuit.cc:359
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::matchNodePorts ( const Graph needle,
int  needleNodeIdx,
const Graph haystack,
int  haystackNodeIdx,
const std::map< std::string, std::string > &  swaps 
) const
inlineprivate

Definition at line 725 of file subcircuit.cc.

726  {
727  const Graph::Node &nn = needle.nodes[needleNodeIdx];
728  const Graph::Node &hn = haystack.nodes[haystackNodeIdx];
729  assert(nn.ports.size() == hn.ports.size());
730 
731  for (int i = 0; i < int(nn.ports.size()); i++)
732  {
733  std::string hnPortId = nn.ports[i].portId;
734  if (swaps.count(hnPortId) > 0)
735  hnPortId = swaps.at(hnPortId);
736 
737  if (hn.portMap.count(hnPortId) == 0)
738  return false;
739 
740  const Graph::Port &np = nn.ports[i];
741  const Graph::Port &hp = hn.ports[hn.portMap.at(hnPortId)];
742 
743  if (int(hp.bits.size()) < np.minWidth || hp.bits.size() > np.bits.size())
744  return false;
745 
746  for (int j = 0; j < int(hp.bits.size()); j++)
747  {
748  const Graph::Edge &ne = needle.edges[np.bits[j].edgeIdx];
749  const Graph::Edge &he = haystack.edges[hp.bits[j].edgeIdx];
750 
751  if (ne.constValue || he.constValue) {
752  if (ne.constValue != he.constValue)
753  if (compatibleConstants.count(ne.constValue) == 0 || compatibleConstants.at(ne.constValue).count(he.constValue) == 0)
754  return false;
755  continue;
756  }
757 
758  if (ne.isExtern || needle.allExtern) {
759  if (he.portBits.size() < ne.portBits.size())
760  return false;
761  } else {
762  if (he.portBits.size() != ne.portBits.size())
763  return false;
764  if (he.isExtern || haystack.allExtern)
765  return false;
766  }
767  }
768  }
769 
770  return true;
771  }
std::vector< Node > nodes
Definition: subcircuit.h:72
std::map< std::string, int > portMap
Definition: subcircuit.h:63
std::vector< Edge > edges
Definition: subcircuit.h:73
std::set< BitRef > portBits
Definition: subcircuit.h:43
std::map< int, std::set< int > > compatibleConstants
Definition: subcircuit.cc:717
std::vector< PortBit > bits
Definition: subcircuit.h:57
std::vector< Port > ports
Definition: subcircuit.h:64

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::matchNodes ( const GraphData needle,
int  needleNodeIdx,
const GraphData haystack,
int  haystackNodeIdx 
) const
inlineprivate

Definition at line 773 of file subcircuit.cc.

774  {
775  // Rules for matching nodes:
776  //
777  // 1. their typeId must be identical or compatible
778  // (this is checked before calling this function)
779  //
780  // 2. they must have the same ports and the haystack port
781  // widths must match the needle port width range
782  //
783  // 3. All edges from the needle must match the haystack:
784  // a) if the needle edge is extern:
785  // - the haystack edge must have at least as many components as the needle edge
786  // b) if the needle edge is not extern:
787  // - the haystack edge must have the same number of components as the needle edge
788  // - the haystack edge must not be extern
789 
790  const Graph::Node &nn = needle.graph.nodes[needleNodeIdx];
791  const Graph::Node &hn = haystack.graph.nodes[haystackNodeIdx];
792 
793  assert(nn.typeId == hn.typeId || (compatibleTypes.count(nn.typeId) > 0 && compatibleTypes.at(nn.typeId).count(hn.typeId) > 0));
794 
795  if (nn.ports.size() != hn.ports.size())
796  return false;
797 
798  std::map<std::string, std::string> currentCandidate;
799 
800  for (const auto &port : needle.graph.nodes[needleNodeIdx].ports)
801  currentCandidate[port.portId] = port.portId;
802 
803  if (swapPorts.count(needle.graph.nodes[needleNodeIdx].typeId) == 0)
804  {
805  if (matchNodePorts(needle.graph, needleNodeIdx, haystack.graph, haystackNodeIdx, currentCandidate) &&
806  userSolver->userCompareNodes(needle.graphId, nn.nodeId, nn.userData, haystack.graphId, hn.nodeId, hn.userData, currentCandidate))
807  return true;
808 
809  if (swapPermutations.count(needle.graph.nodes[needleNodeIdx].typeId) > 0)
810  for (const auto &permutation : swapPermutations.at(needle.graph.nodes[needleNodeIdx].typeId)) {
811  std::map<std::string, std::string> currentSubCandidate = currentCandidate;
812  applyPermutation(currentSubCandidate, permutation);
813  if (matchNodePorts(needle.graph, needleNodeIdx, haystack.graph, haystackNodeIdx, currentCandidate) &&
814  userSolver->userCompareNodes(needle.graphId, nn.nodeId, nn.userData, haystack.graphId, hn.nodeId, hn.userData, currentCandidate))
815  return true;
816  }
817  }
818  else
819  {
820  std::vector<std::vector<std::string>> thisSwapPorts;
821  for (const auto &ports : swapPorts.at(needle.graph.nodes[needleNodeIdx].typeId)) {
822  std::vector<std::string> portsVector;
823  for (const auto &port : ports)
824  portsVector.push_back(port);
825  thisSwapPorts.push_back(portsVector);
826  }
827 
828  int thisPermutations = numberOfPermutationsArray(thisSwapPorts);
829  for (int i = 0; i < thisPermutations; i++)
830  {
831  permutateVectorToMapArray(currentCandidate, thisSwapPorts, i);
832 
833  if (matchNodePorts(needle.graph, needleNodeIdx, haystack.graph, haystackNodeIdx, currentCandidate) &&
834  userSolver->userCompareNodes(needle.graphId, nn.nodeId, nn.userData, haystack.graphId, hn.nodeId, hn.userData, currentCandidate))
835  return true;
836 
837  if (swapPermutations.count(needle.graph.nodes[needleNodeIdx].typeId) > 0)
838  for (const auto &permutation : swapPermutations.at(needle.graph.nodes[needleNodeIdx].typeId)) {
839  std::map<std::string, std::string> currentSubCandidate = currentCandidate;
840  applyPermutation(currentSubCandidate, permutation);
841  if (matchNodePorts(needle.graph, needleNodeIdx, haystack.graph, haystackNodeIdx, currentCandidate) &&
842  userSolver->userCompareNodes(needle.graphId, nn.nodeId, nn.userData, haystack.graphId, hn.nodeId, hn.userData, currentCandidate))
843  return true;
844  }
845  }
846  }
847 
848  return false;
849  }
std::map< std::string, std::set< std::map< std::string, std::string > > > swapPermutations
Definition: subcircuit.cc:719
virtual bool userCompareNodes(const std::string &needleGraphId, const std::string &needleNodeId, void *needleUserData, const std::string &haystackGraphId, const std::string &haystackNodeId, void *haystackUserData, const std::map< std::string, std::string > &portMapping)
Definition: subcircuit.cc:1597
static void permutateVectorToMapArray(std::map< std::string, std::string > &map, const std::vector< std::vector< std::string >> &list, int idx)
Definition: subcircuit.cc:370
bool matchNodePorts(const Graph &needle, int needleNodeIdx, const Graph &haystack, int haystackNodeIdx, const std::map< std::string, std::string > &swaps) const
Definition: subcircuit.cc:725
static void applyPermutation(std::map< std::string, std::string > &map, const std::map< std::string, std::string > &permutation)
Definition: subcircuit.cc:380
static int numberOfPermutationsArray(const std::vector< std::vector< std::string >> &list)
Definition: subcircuit.cc:359
std::map< std::string, std::set< std::string > > compatibleTypes
Definition: subcircuit.cc:716
std::map< std::string, std::set< std::set< std::string > > > swapPorts
Definition: subcircuit.cc:718
std::vector< Port > ports
Definition: subcircuit.h:64

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::mine ( std::vector< Solver::MineResult > &  results,
int  minNodes,
int  maxNodes,
int  minMatches,
int  limitMatchesPerGraph 
)
inlineprotected

Definition at line 1560 of file subcircuit.cc.

1561  {
1562  int nodeSetSize = 2;
1563  std::set<NodeSet> pool;
1564  findNodePairs(results, pool, minNodes, minMatches, limitMatchesPerGraph);
1565 
1566  while ((maxNodes < 0 || nodeSetSize < maxNodes) && pool.size() > 0)
1567  {
1568  int increment = nodeSetSize - 1;
1569  if (nodeSetSize + increment >= minNodes)
1570  increment = minNodes - nodeSetSize;
1571  if (nodeSetSize >= minNodes)
1572  increment = 1;
1573 
1574  findNextPool(results, pool, nodeSetSize, increment, minNodes, minMatches, limitMatchesPerGraph);
1575  nodeSetSize += increment;
1576  }
1577  }
void findNextPool(std::vector< Solver::MineResult > &results, std::set< NodeSet > &pool, int oldSetSize, int increment, int minNodes, int minMatches, int limitMatchesPerGraph)
Definition: subcircuit.cc:1397
void findNodePairs(std::vector< Solver::MineResult > &results, std::set< NodeSet > &nodePairs, int minNodes, int minMatches, int limitMatchesPerGraph)
Definition: subcircuit.cc:1358

+ Here is the call graph for this function:

static int SubCircuit::SolverWorker::numberOfPermutations ( const std::vector< std::string > &  list)
inlinestaticprivate

Definition at line 321 of file subcircuit.cc.

322  {
323  int numPermutations = 1;
324  for (int i = 0; i < int(list.size()); i++) {
325  assert(numPermutations < maxPermutationsLimit);
326  numPermutations *= i+1;
327  }
328  return numPermutations;
329  }
static const int maxPermutationsLimit
Definition: subcircuit.cc:319

+ Here is the caller graph for this function:

static int SubCircuit::SolverWorker::numberOfPermutationsArray ( const std::vector< std::vector< std::string >> &  list)
inlinestaticprivate

Definition at line 359 of file subcircuit.cc.

360  {
361  int numPermutations = 1;
362  for (const auto &it : list) {
363  int thisPermutations = numberOfPermutations(it);
364  assert(float(numPermutations) * float(thisPermutations) < maxPermutationsLimit);
365  numPermutations *= thisPermutations;
366  }
367  return numPermutations;
368  }
static const int maxPermutationsLimit
Definition: subcircuit.cc:319
static int numberOfPermutations(const std::vector< std::string > &list)
Definition: subcircuit.cc:321

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void SubCircuit::SolverWorker::permutateVectorToMap ( std::map< std::string, std::string > &  map,
const std::vector< std::string > &  list,
int  idx 
)
inlinestaticprivate

Definition at line 331 of file subcircuit.cc.

332  {
333  // convert idx to a list.size() digits factoradic number
334 
335  std::vector<int> factoradicDigits;
336  for (int i = 0; i < int(list.size()); i++) {
337  factoradicDigits.push_back(idx % (i+1));
338  idx = idx / (i+1);
339  }
340 
341  // construct permutation
342 
343  std::vector<std::string> pool = list;
344  std::vector<std::string> permutation;
345 
346  while (!factoradicDigits.empty()) {
347  int i = factoradicDigits.back();
348  factoradicDigits.pop_back();
349  permutation.push_back(pool[i]);
350  pool.erase(pool.begin() + i);
351  }
352 
353  // update map
354 
355  for (int i = 0; i < int(list.size()); i++)
356  map[list[i]] = permutation[i];
357  }
static std::string idx(std::string str)
Definition: test_autotb.cc:57

+ Here is the caller graph for this function:

static void SubCircuit::SolverWorker::permutateVectorToMapArray ( std::map< std::string, std::string > &  map,
const std::vector< std::vector< std::string >> &  list,
int  idx 
)
inlinestaticprivate

Definition at line 370 of file subcircuit.cc.

371  {
372  for (const auto &it : list) {
373  int thisPermutations = numberOfPermutations(it);
374  int thisIdx = idx % thisPermutations;
375  permutateVectorToMap(map, it, thisIdx);
376  idx /= thisPermutations;
377  }
378  }
static void permutateVectorToMap(std::map< std::string, std::string > &map, const std::vector< std::string > &list, int idx)
Definition: subcircuit.cc:331
static std::string idx(std::string str)
Definition: test_autotb.cc:57
static int numberOfPermutations(const std::vector< std::string > &list)
Definition: subcircuit.cc:321

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void SubCircuit::SolverWorker::printAdjMatrix ( const adjMatrix_t matrix)
inlinestaticprivate

Definition at line 300 of file subcircuit.cc.

301  {
302  my_printf("%7s", "");
303  for (int i = 0; i < int(matrix.size()); i++)
304  my_printf("%4d:", i);
305  my_printf("\n");
306  for (int i = 0; i < int(matrix.size()); i++) {
307  my_printf("%5d:", i);
308  for (int j = 0; j < int(matrix.size()); j++)
309  if (matrix.at(i).count(j) == 0)
310  my_printf("%5s", "-");
311  else
312  my_printf("%5d", matrix.at(i).at(j));
313  my_printf("\n");
314  }
315  }
#define my_printf
Definition: subcircuit.cc:32

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::printEnumerationMatrix ( const std::vector< std::set< int >> &  enumerationMatrix,
int  maxHaystackNodeIdx = -1 
) const
inlineprivate

Definition at line 940 of file subcircuit.cc.

941  {
942  if (maxHaystackNodeIdx < 0) {
943  for (const auto &it : enumerationMatrix)
944  for (int idx : it)
945  maxHaystackNodeIdx = std::max(maxHaystackNodeIdx, idx);
946  }
947 
948  my_printf(" ");
949  for (int j = 0; j < maxHaystackNodeIdx; j += 5)
950  my_printf("%-6d", j);
951  my_printf("\n");
952 
953  for (int i = 0; i < int(enumerationMatrix.size()); i++)
954  {
955  my_printf("%5d:", i);
956  for (int j = 0; j < maxHaystackNodeIdx; j++) {
957  if (j % 5 == 0)
958  my_printf(" ");
959  my_printf("%c", enumerationMatrix[i].count(j) > 0 ? '*' : '.');
960  }
961  my_printf("\n");
962  }
963  }
static std::string idx(std::string str)
Definition: test_autotb.cc:57
#define my_printf
Definition: subcircuit.cc:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::pruneEnumerationMatrix ( std::vector< std::set< int >> &  enumerationMatrix,
const GraphData needle,
const GraphData haystack,
int &  nextRow,
bool  allowOverlap 
)
inlineprivate

Definition at line 913 of file subcircuit.cc.

914  {
915  bool didSomething = true;
916  while (didSomething)
917  {
918  nextRow = -1;
919  didSomething = false;
920  for (int i = 0; i < int(enumerationMatrix.size()); i++) {
921  std::set<int> newRow;
922  for (int j : enumerationMatrix[i]) {
923  if (!checkEnumerationMatrix(enumerationMatrix, i, j, needle, haystack))
924  didSomething = true;
925  else if (!allowOverlap && haystack.usedNodes[j])
926  didSomething = true;
927  else
928  newRow.insert(j);
929  }
930  if (newRow.size() == 0)
931  return false;
932  if (newRow.size() >= 2 && (nextRow < 0 || needle.adjMatrix.at(nextRow).size() < needle.adjMatrix.at(i).size()))
933  nextRow = i;
934  enumerationMatrix[i].swap(newRow);
935  }
936  }
937  return true;
938  }
bool checkEnumerationMatrix(std::vector< std::set< int >> &enumerationMatrix, int i, int j, const GraphData &needle, const GraphData &haystack)
Definition: subcircuit.cc:885

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool SubCircuit::SolverWorker::prunePortmapCandidates ( std::vector< std::set< std::map< std::string, std::string >>> &  portmapCandidates,
std::vector< std::set< int >>  enumerationMatrix,
const GraphData needle,
const GraphData haystack 
)
inlineprivate

Definition at line 1044 of file subcircuit.cc.

1045  {
1046  bool didSomething = false;
1047 
1048  // strategy #1: prune impossible port mappings
1049 
1050  for (int i = 0; i < int(needle.graph.nodes.size()); i++)
1051  {
1052  assert(enumerationMatrix[i].size() == 1);
1053  int j = *enumerationMatrix[i].begin();
1054 
1055  std::set<std::map<std::string, std::string>> thisCandidates;
1056  portmapCandidates[i].swap(thisCandidates);
1057 
1058  for (const auto &testCandidate : thisCandidates)
1059  {
1060  for (const auto &it_needle : needle.adjMatrix.at(i))
1061  {
1062  int needleNeighbour = it_needle.first;
1063  int needleEdgeType = it_needle.second;
1064 
1065  assert(enumerationMatrix[needleNeighbour].size() == 1);
1066  int haystackNeighbour = *enumerationMatrix[needleNeighbour].begin();
1067 
1068  assert(haystack.adjMatrix.at(j).count(haystackNeighbour) > 0);
1069  int haystackEdgeType = haystack.adjMatrix.at(j).at(haystackNeighbour);
1070 
1071  std::set<std::map<std::string, std::string>> &candidates =
1072  i == needleNeighbour ? thisCandidates : portmapCandidates[needleNeighbour];
1073 
1074  for (const auto &otherCandidate : candidates) {
1075  if (diCache.compare(needleEdgeType, haystackEdgeType, testCandidate, otherCandidate))
1076  goto found_match;
1077  }
1078 
1079  didSomething = true;
1080  goto purgeCandidate;
1081  found_match:;
1082  }
1083 
1084  portmapCandidates[i].insert(testCandidate);
1085  purgeCandidate:;
1086  }
1087 
1088  if (portmapCandidates[i].size() == 0)
1089  return false;
1090  }
1091 
1092  if (didSomething)
1093  return true;
1094 
1095  // strategy #2: prune a single random port mapping
1096 
1097  for (int i = 0; i < int(needle.graph.nodes.size()); i++)
1098  if (portmapCandidates[i].size() > 1) {
1099  // remove last mapping. this keeps ports unswapped in don't-care situations
1100  portmapCandidates[i].erase(--portmapCandidates[i].end());
1101  return true;
1102  }
1103 
1104  return false;
1105  }
bool compare(int needleEdge, int haystackEdge, const std::map< std::string, std::set< std::set< std::string >>> &swapPorts, const std::map< std::string, std::set< std::map< std::string, std::string >>> &swapPermutations)
Definition: subcircuit.cc:685

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::setVerbose ( )
inlineprotected

Definition at line 1478 of file subcircuit.cc.

1479  {
1480  verbose = true;
1481  }
void SubCircuit::SolverWorker::solve ( std::vector< Solver::Result > &  results,
std::string  needleGraphId,
std::string  haystackGraphId,
const std::map< std::string, std::set< std::string >> &  initialMappings,
bool  allowOverlap,
int  maxSolutions 
)
inlineprotected

Definition at line 1515 of file subcircuit.cc.

1517  {
1518  assert(graphData.count(needleGraphId) > 0);
1519  assert(graphData.count(haystackGraphId) > 0);
1520 
1521  const GraphData &needle = graphData[needleGraphId];
1522  GraphData &haystack = graphData[haystackGraphId];
1523 
1524  std::vector<std::set<int>> enumerationMatrix;
1525  generateEnumerationMatrix(enumerationMatrix, needle, haystack, initialMappings);
1526 
1527  if (verbose)
1528  {
1529  my_printf("\n");
1530  my_printf("Needle nodes:\n");
1531  for (int i = 0; i < int(needle.graph.nodes.size()); i++)
1532  my_printf("%5d: %s (%s)\n", i, needle.graph.nodes[i].nodeId.c_str(), needle.graph.nodes[i].typeId.c_str());
1533 
1534  my_printf("\n");
1535  my_printf("Haystack nodes:\n");
1536  for (int i = 0; i < int(haystack.graph.nodes.size()); i++)
1537  my_printf("%5d: %s (%s)\n", i, haystack.graph.nodes[i].nodeId.c_str(), haystack.graph.nodes[i].typeId.c_str());
1538 
1539  my_printf("\n");
1540  my_printf("Needle Adjecency Matrix:\n");
1541  printAdjMatrix(needle.adjMatrix);
1542 
1543  my_printf("\n");
1544  my_printf("Haystack Adjecency Matrix:\n");
1545  printAdjMatrix(haystack.adjMatrix);
1546 
1547  my_printf("\n");
1548  my_printf("Edge Types:\n");
1550 
1551  my_printf("\n");
1552  my_printf("Enumeration Matrix (haystack nodes at column indices):\n");
1553  printEnumerationMatrix(enumerationMatrix, haystack.graph.nodes.size());
1554  }
1555 
1556  haystack.usedNodes.resize(haystack.graph.nodes.size());
1557  ullmannRecursion(results, enumerationMatrix, 0, needle, haystack, allowOverlap, maxSolutions > 0 ? results.size() + maxSolutions : -1);
1558  }
void ullmannRecursion(std::vector< Solver::Result > &results, std::vector< std::set< int >> &enumerationMatrix, int iter, const GraphData &needle, GraphData &haystack, bool allowOverlap, int limitResults)
Definition: subcircuit.cc:1107
static void printAdjMatrix(const adjMatrix_t &matrix)
Definition: subcircuit.cc:300
void printEnumerationMatrix(const std::vector< std::set< int >> &enumerationMatrix, int maxHaystackNodeIdx=-1) const
Definition: subcircuit.cc:940
void generateEnumerationMatrix(std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, const std::map< std::string, std::set< std::string >> &initialMappings) const
Definition: subcircuit.cc:851
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715
#define my_printf
Definition: subcircuit.cc:32

+ Here is the call graph for this function:

void SubCircuit::SolverWorker::solveForMining ( std::vector< Solver::Result > &  results,
const GraphData needle 
)
inlineprivate

Definition at line 1263 of file subcircuit.cc.

1264  {
1265  bool backupVerbose = verbose;
1266  verbose = false;
1267 
1268  for (auto &it : graphData)
1269  {
1270  GraphData &haystack = it.second;
1271 
1272  std::vector<std::set<int>> enumerationMatrix;
1273  std::map<std::string, std::set<std::string>> initialMappings;
1274  generateEnumerationMatrix(enumerationMatrix, needle, haystack, initialMappings);
1275 
1276  haystack.usedNodes.resize(haystack.graph.nodes.size());
1277  ullmannRecursion(results, enumerationMatrix, 0, needle, haystack, true, -1);
1278  }
1279 
1280  verbose = backupVerbose;
1281  }
void ullmannRecursion(std::vector< Solver::Result > &results, std::vector< std::set< int >> &enumerationMatrix, int iter, const GraphData &needle, GraphData &haystack, bool allowOverlap, int limitResults)
Definition: subcircuit.cc:1107
void generateEnumerationMatrix(std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, const std::map< std::string, std::set< std::string >> &initialMappings) const
Definition: subcircuit.cc:851
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int SubCircuit::SolverWorker::testForMining ( std::vector< Solver::MineResult > &  results,
std::set< NodeSet > &  usedSets,
std::set< NodeSet > &  nextPool,
NodeSet testSet,
const std::string &  graphId,
const Graph graph,
int  minNodes,
int  minMatches,
int  limitMatchesPerGraph 
)
inlineprivate

Definition at line 1283 of file subcircuit.cc.

1285  {
1286  // my_printf("test: %s\n", testSet.to_string().c_str());
1287 
1288  GraphData needle;
1289  std::vector<std::string> needle_nodes;
1290  for (int nodeIdx : testSet.nodes)
1291  needle_nodes.push_back(graph.nodes[nodeIdx].nodeId);
1292  needle.graph = Graph(graph, needle_nodes);
1293  needle.graph.markAllExtern();
1294  diCache.add(needle.graph, needle.adjMatrix, graphId, userSolver);
1295 
1296  std::vector<Solver::Result> ullmannResults;
1297  solveForMining(ullmannResults, needle);
1298 
1299  int matches = 0;
1300  std::map<std::string, int> matchesPerGraph;
1301  std::set<NodeSet> thisNodeSetSet;
1302 
1303  for (auto &it : ullmannResults)
1304  {
1305  std::vector<int> resultNodes;
1306  for (auto &i2 : it.mappings)
1307  resultNodes.push_back(graphData[it.haystackGraphId].graph.nodeMap[i2.second.haystackNodeId]);
1308  NodeSet resultSet(it.haystackGraphId, resultNodes);
1309 
1310  // my_printf("match: %s%s\n", resultSet.to_string().c_str(), usedSets.count(resultSet) > 0 ? " (dup)" : "");
1311 
1312 #if 0
1313  if (usedSets.count(resultSet) > 0) {
1314  // Because of shorted pins isomorphisim is not always bidirectional!
1315  //
1316  // This means that the following assert is not true in all cases and subgraph A might
1317  // show up in the matches for subgraph B but not vice versa... This also means that the
1318  // order in which subgraphs are processed has an impact on the results set.
1319  //
1320  assert(thisNodeSetSet.count(resultSet) > 0);
1321  continue;
1322  }
1323 #else
1324  if (thisNodeSetSet.count(resultSet) > 0)
1325  continue;
1326 #endif
1327 
1328  usedSets.insert(resultSet);
1329  thisNodeSetSet.insert(resultSet);
1330 
1331  matchesPerGraph[it.haystackGraphId]++;
1332  if (limitMatchesPerGraph < 0 || matchesPerGraph[it.haystackGraphId] < limitMatchesPerGraph)
1333  matches++;
1334  }
1335 
1336  if (matches < minMatches)
1337  return matches;
1338 
1339  if (minNodes <= int(testSet.nodes.size()))
1340  {
1341  Solver::MineResult result;
1342  result.graphId = graphId;
1343  result.totalMatchesAfterLimits = matches;
1344  result.matchesPerGraph = matchesPerGraph;
1345  for (int nodeIdx : testSet.nodes) {
1346  Solver::MineResultNode resultNode;
1347  resultNode.nodeId = graph.nodes[nodeIdx].nodeId;
1348  resultNode.userData = graph.nodes[nodeIdx].userData;
1349  result.nodes.push_back(resultNode);
1350  }
1351  results.push_back(result);
1352  }
1353 
1354  nextPool.insert(thisNodeSetSet.begin(), thisNodeSetSet.end());
1355  return matches;
1356  }
std::vector< Node > nodes
Definition: subcircuit.h:72
void add(const Graph &graph, adjMatrix_t &adjMatrix, const std::string &graphId, Solver *userSolver)
Definition: subcircuit.cc:662
void solveForMining(std::vector< Solver::Result > &results, const GraphData &needle)
Definition: subcircuit.cc:1263
std::vector< MineResultNode > nodes
Definition: subcircuit.h:113
std::map< std::string, GraphData > graphData
Definition: subcircuit.cc:715
std::map< std::string, int > matchesPerGraph
Definition: subcircuit.h:112

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SubCircuit::SolverWorker::ullmannRecursion ( std::vector< Solver::Result > &  results,
std::vector< std::set< int >> &  enumerationMatrix,
int  iter,
const GraphData needle,
GraphData haystack,
bool  allowOverlap,
int  limitResults 
)
inlineprivate

Definition at line 1107 of file subcircuit.cc.

1108  {
1109  int i = -1;
1110  if (!pruneEnumerationMatrix(enumerationMatrix, needle, haystack, i, allowOverlap))
1111  return;
1112 
1113  if (i < 0)
1114  {
1115  Solver::Result result;
1116  result.needleGraphId = needle.graphId;
1117  result.haystackGraphId = haystack.graphId;
1118 
1119  std::vector<std::set<std::map<std::string, std::string>>> portmapCandidates;
1120  portmapCandidates.resize(enumerationMatrix.size());
1121 
1122  for (int j = 0; j < int(enumerationMatrix.size()); j++) {
1123  Solver::ResultNodeMapping mapping;
1124  mapping.needleNodeId = needle.graph.nodes[j].nodeId;
1125  mapping.needleUserData = needle.graph.nodes[j].userData;
1126  mapping.haystackNodeId = haystack.graph.nodes[*enumerationMatrix[j].begin()].nodeId;
1127  mapping.haystackUserData = haystack.graph.nodes[*enumerationMatrix[j].begin()].userData;
1128  generatePortmapCandidates(portmapCandidates[j], enumerationMatrix, needle, haystack, j);
1129  result.mappings[needle.graph.nodes[j].nodeId] = mapping;
1130  }
1131 
1132  while (prunePortmapCandidates(portmapCandidates, enumerationMatrix, needle, haystack)) { }
1133 
1134  if (verbose) {
1135  my_printf("\nPortmapper results:\n");
1136  for (int j = 0; j < int(enumerationMatrix.size()); j++) {
1137  my_printf("%5d: %s\n", j, needle.graph.nodes[j].nodeId.c_str());
1138  int variantCounter = 0;
1139  for (auto &i2 : portmapCandidates.at(j)) {
1140  my_printf("%*s variant %2d:", 6, "", variantCounter++);
1141  int mapCounter = 0;
1142  for (auto &i3 : i2)
1143  my_printf("%s %s -> %s", mapCounter++ ? "," : "", i3.first.c_str(), i3.second.c_str());
1144  my_printf("\n");
1145  }
1146  }
1147  }
1148 
1149  for (int j = 0; j < int(enumerationMatrix.size()); j++) {
1150  if (portmapCandidates[j].size() == 0) {
1151  if (verbose) {
1152  my_printf("\nSolution (rejected by portmapper):\n");
1153  printEnumerationMatrix(enumerationMatrix, haystack.graph.nodes.size());
1154  }
1155  return;
1156  }
1157  result.mappings[needle.graph.nodes[j].nodeId].portMapping = *portmapCandidates[j].begin();
1158  }
1159 
1160  if (!userSolver->userCheckSolution(result)) {
1161  if (verbose) {
1162  my_printf("\nSolution (rejected by userCheckSolution):\n");
1163  printEnumerationMatrix(enumerationMatrix, haystack.graph.nodes.size());
1164  }
1165  return;
1166  }
1167 
1168  for (int j = 0; j < int(enumerationMatrix.size()); j++)
1169  if (!haystack.graph.nodes[*enumerationMatrix[j].begin()].shared)
1170  haystack.usedNodes[*enumerationMatrix[j].begin()] = true;
1171 
1172  if (verbose) {
1173  my_printf("\nSolution:\n");
1174  printEnumerationMatrix(enumerationMatrix, haystack.graph.nodes.size());
1175  }
1176 
1177  results.push_back(result);
1178  return;
1179  }
1180 
1181  if (verbose) {
1182  my_printf("\n");
1183  my_printf("Enumeration Matrix at recursion level %d (%d):\n", iter, i);
1184  printEnumerationMatrix(enumerationMatrix, haystack.graph.nodes.size());
1185  }
1186 
1187  std::set<int> activeRow;
1188  enumerationMatrix[i].swap(activeRow);
1189 
1190  for (int j : activeRow)
1191  {
1192  // found enough?
1193  if (limitResults >= 0 && int(results.size()) >= limitResults)
1194  return;
1195 
1196  // already used by other solution -> try next
1197  if (!allowOverlap && haystack.usedNodes[j])
1198  continue;
1199 
1200  // create enumeration matrix for child in recursion tree
1201  std::vector<std::set<int>> nextEnumerationMatrix = enumerationMatrix;
1202  for (int k = 0; k < int(nextEnumerationMatrix.size()); k++)
1203  nextEnumerationMatrix[k].erase(j);
1204  nextEnumerationMatrix[i].insert(j);
1205 
1206  // recursion
1207  ullmannRecursion(results, nextEnumerationMatrix, iter+1, needle, haystack, allowOverlap, limitResults);
1208 
1209  // we just have found something -> unroll to top recursion level
1210  if (!allowOverlap && haystack.usedNodes[j] && iter > 0)
1211  return;
1212  }
1213  }
void ullmannRecursion(std::vector< Solver::Result > &results, std::vector< std::set< int >> &enumerationMatrix, int iter, const GraphData &needle, GraphData &haystack, bool allowOverlap, int limitResults)
Definition: subcircuit.cc:1107
void printEnumerationMatrix(const std::vector< std::set< int >> &enumerationMatrix, int maxHaystackNodeIdx=-1) const
Definition: subcircuit.cc:940
bool pruneEnumerationMatrix(std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int &nextRow, bool allowOverlap)
Definition: subcircuit.cc:913
virtual bool userCheckSolution(const Result &result)
Definition: subcircuit.cc:1612
bool prunePortmapCandidates(std::vector< std::set< std::map< std::string, std::string >>> &portmapCandidates, std::vector< std::set< int >> enumerationMatrix, const GraphData &needle, const GraphData &haystack)
Definition: subcircuit.cc:1044
void generatePortmapCandidates(std::set< std::map< std::string, std::string >> &portmapCandidates, const std::vector< std::set< int >> &enumerationMatrix, const GraphData &needle, const GraphData &haystack, int idx)
Definition: subcircuit.cc:994
std::map< std::string, ResultNodeMapping > mappings
Definition: subcircuit.h:102
#define my_printf
Definition: subcircuit.cc:32

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class Solver
friend

Definition at line 1594 of file subcircuit.cc.

Field Documentation

std::map<int, std::set<int> > SubCircuit::SolverWorker::compatibleConstants
private

Definition at line 717 of file subcircuit.cc.

std::map<std::string, std::set<std::string> > SubCircuit::SolverWorker::compatibleTypes
private

Definition at line 716 of file subcircuit.cc.

DiCache SubCircuit::SolverWorker::diCache
private

Definition at line 720 of file subcircuit.cc.

std::map<std::string, GraphData> SubCircuit::SolverWorker::graphData
private

Definition at line 715 of file subcircuit.cc.

const int SubCircuit::SolverWorker::maxPermutationsLimit = 1000000
staticprivate

Definition at line 319 of file subcircuit.cc.

std::map<std::string, std::set<std::map<std::string, std::string> > > SubCircuit::SolverWorker::swapPermutations
private

Definition at line 719 of file subcircuit.cc.

std::map<std::string, std::set<std::set<std::string> > > SubCircuit::SolverWorker::swapPorts
private

Definition at line 718 of file subcircuit.cc.

Solver* SubCircuit::SolverWorker::userSolver
private

Definition at line 714 of file subcircuit.cc.

bool SubCircuit::SolverWorker::verbose
private

Definition at line 721 of file subcircuit.cc.


The documentation for this class was generated from the following file: