aboutsummaryrefslogtreecommitdiffstats
path: root/libs/subcircuit/demo.cc
blob: 149dc6aa0e60b67723b2d60fba068abcb1601e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.h
#include "subcircuit.h"
#include <stdio.h>

#define VERBOSE

int main()
{
	SubCircuit::Graph needle, haystack;

	// create needle graph

	needle.createNode("mul_1", "product");
	needle.createPort("mul_1", "A", 4);
	needle.createPort("mul_1", "B", 4);
	needle.createPort("mul_1", "Y", 4);
	needle.markExtern("mul_1", "A");
	needle.markExtern("mul_1", "B");

	needle.createNode("mul_2", "product");
	needle.createPort("mul_2", "A", 4);
	needle.createPort("mul_2", "B", 4);
	needle.createPort("mul_2", "Y", 4);
	needle.markExtern("mul_2", "A");
	needle.markExtern("mul_2", "B");

	needle.createNode("add_1", "sum");
	needle.createPort("add_1", "A", 4);
	needle.createPort("add_1", "B", 4);
	needle.createPort("add_1", "Y", 4);
	needle.markExtern("add_1", "Y");

	needle.createConnection("mul_1", "Y", "add_1", "A");
	needle.createConnection("mul_2", "Y", "add_1", "B");

#ifdef VERBOSE
	printf("\n");
	needle.print();
#endif

	// create haystack graph

#if 0
	for (int i = 0; i < 4; i++) {
		char id[100];
		snprintf(id, 100, "mul_%d", i);
		haystack.createNode(id, "mul");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
		haystack.markExtern(id, "A");
		haystack.markExtern(id, "B");
	}

	for (int i = 0; i < 3; i++) {
		char id[100];
		snprintf(id, 100, "add_%d", i);
		haystack.createNode(id, "add");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
	}

	haystack.createConnection("mul_0", "Y", "add_0", "A");
	haystack.createConnection("mul_1", "Y", "add_0", "B");

	haystack.createConnection("mul_2", "Y", "add_1", "A");
	haystack.createConnection("mul_3", "Y", "add_1", "B");

	haystack.createConnection("add_0", "Y", "add_2", "A");
	haystack.createConnection("add_1", "Y", "add_2", "B");
	haystack.markExtern("add_2", "Y");
#else
	std::vector<std::string> cellIds;
	srand48(12345);

	for (int i = 0; i < 45; i++) {
		char id[100];
		snprintf(id, 100, "cell_%02d", i);
		haystack.createNode(id, i < 30 ? "mul" : "add");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
		cellIds.push_back(id);
	}

	for (int i = 0; i < int(cellIds.size()); i++) {
		if (lrand48() % (i < 20 ? 3 : 2) != 0)
			continue;
		const std::string &id = cellIds[i];
		const std::string &id_left = cellIds[lrand48() % cellIds.size()];
		const std::string &id_right = cellIds[lrand48() % cellIds.size()];
		haystack.createConnection(id_left, "Y", id, "A");
		haystack.createConnection(id_right, "Y", id, "B");
	}
#endif

#ifdef VERBOSE
	printf("\n");
	haystack.print();
#endif

	// search needle in haystack

	SubCircuit::Solver solver;
	std::vector<SubCircuit::Solver::Result> results;

#ifdef VERBOSE
	solver.setVerbose();
#endif

	solver.addCompatibleTypes("product", "mul");
	solver.addCompatibleTypes("sum", "add");

	solver.addSwappablePorts("product", "A", "B");
	solver.addSwappablePorts("sum", "A", "B");

	solver.addGraph("needle", needle);
	solver.addGraph("haystack", haystack);
	solver.solve(results, "needle", "haystack");

	for (int i = 0; i < int(results.size()); i++) {
		printf("\nMatch #%d: (%s in %s)\n", i, results[i].needleGraphId.c_str(), results[i].haystackGraphId.c_str());
		for (const auto &it : results[i].mappings) {
			printf("  %s -> %s", it.first.c_str(), it.second.haystackNodeId.c_str());
			for (const auto &it2 : it.second.portMapping)
				printf(" %s:%s", it2.first.c_str(), it2.second.c_str());
			printf("\n");
		}
	}

	printf("\n");
	return 0;
}