1 #include "subcircuit.h"
8 SubCircuit::Graph needle
, haystack
;
10 // create needle graph
12 needle
.createNode("mul_1", "product");
13 needle
.createPort("mul_1", "A", 4);
14 needle
.createPort("mul_1", "B", 4);
15 needle
.createPort("mul_1", "Y", 4);
16 needle
.markExtern("mul_1", "A");
17 needle
.markExtern("mul_1", "B");
19 needle
.createNode("mul_2", "product");
20 needle
.createPort("mul_2", "A", 4);
21 needle
.createPort("mul_2", "B", 4);
22 needle
.createPort("mul_2", "Y", 4);
23 needle
.markExtern("mul_2", "A");
24 needle
.markExtern("mul_2", "B");
26 needle
.createNode("add_1", "sum");
27 needle
.createPort("add_1", "A", 4);
28 needle
.createPort("add_1", "B", 4);
29 needle
.createPort("add_1", "Y", 4);
30 needle
.markExtern("add_1", "Y");
32 needle
.createConnection("mul_1", "Y", "add_1", "A");
33 needle
.createConnection("mul_2", "Y", "add_1", "B");
40 // create haystack graph
43 for (int i
= 0; i
< 4; i
++) {
45 snprintf(id
, 100, "mul_%d", i
);
46 haystack
.createNode(id
, "mul");
47 haystack
.createPort(id
, "A", 4);
48 haystack
.createPort(id
, "B", 4);
49 haystack
.createPort(id
, "Y", 4);
50 haystack
.markExtern(id
, "A");
51 haystack
.markExtern(id
, "B");
54 for (int i
= 0; i
< 3; i
++) {
56 snprintf(id
, 100, "add_%d", i
);
57 haystack
.createNode(id
, "add");
58 haystack
.createPort(id
, "A", 4);
59 haystack
.createPort(id
, "B", 4);
60 haystack
.createPort(id
, "Y", 4);
63 haystack
.createConnection("mul_0", "Y", "add_0", "A");
64 haystack
.createConnection("mul_1", "Y", "add_0", "B");
66 haystack
.createConnection("mul_2", "Y", "add_1", "A");
67 haystack
.createConnection("mul_3", "Y", "add_1", "B");
69 haystack
.createConnection("add_0", "Y", "add_2", "A");
70 haystack
.createConnection("add_1", "Y", "add_2", "B");
71 haystack
.markExtern("add_2", "Y");
73 std::vector
<std::string
> cellIds
;
76 for (int i
= 0; i
< 45; i
++) {
78 snprintf(id
, 100, "cell_%02d", i
);
79 haystack
.createNode(id
, i
< 30 ? "mul" : "add");
80 haystack
.createPort(id
, "A", 4);
81 haystack
.createPort(id
, "B", 4);
82 haystack
.createPort(id
, "Y", 4);
83 cellIds
.push_back(id
);
86 for (int i
= 0; i
< int(cellIds
.size()); i
++) {
87 if (lrand48() % (i
< 20 ? 3 : 2) != 0)
89 const std::string
&id
= cellIds
[i
];
90 const std::string
&id_left
= cellIds
[lrand48() % cellIds
.size()];
91 const std::string
&id_right
= cellIds
[lrand48() % cellIds
.size()];
92 haystack
.createConnection(id_left
, "Y", id
, "A");
93 haystack
.createConnection(id_right
, "Y", id
, "B");
102 // search needle in haystack
104 SubCircuit::Solver solver
;
105 std::vector
<SubCircuit::Solver::Result
> results
;
111 solver
.addCompatibleTypes("product", "mul");
112 solver
.addCompatibleTypes("sum", "add");
114 solver
.addSwappablePorts("product", "A", "B");
115 solver
.addSwappablePorts("sum", "A", "B");
117 solver
.addGraph("needle", needle
);
118 solver
.addGraph("haystack", haystack
);
119 solver
.solve(results
, "needle", "haystack");
121 for (int i
= 0; i
< int(results
.size()); i
++) {
122 printf("\nMatch #%d: (%s in %s)\n", i
, results
[i
].needleGraphId
.c_str(), results
[i
].haystackGraphId
.c_str());
123 for (const auto &it
: results
[i
].mappings
) {
124 printf(" %s -> %s", it
.first
.c_str(), it
.second
.haystackNodeId
.c_str());
125 for (const auto &it2
: it
.second
.portMapping
)
126 printf(" %s:%s", it2
.first
.c_str(), it2
.second
.c_str());