mirror of
https://github.com/ltcptgeneral/IdealRMT-DecisionTrees.git
synced 2025-09-05 23:07:24 +00:00
add worst_case converter to rmt
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,3 @@
|
|||||||
data.*
|
data.*
|
||||||
__pycache__
|
__pycache__
|
||||||
tree.json
|
*.json
|
||||||
compressed_tree.json
|
|
137
TreeToRMT.ipynb
Normal file
137
TreeToRMT.ipynb
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "58fc6db9",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json\n",
|
||||||
|
"import math"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "e07be4b3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"f = open(\"compressed_tree.json\")\n",
|
||||||
|
"tree = json.loads(f.read())\n",
|
||||||
|
"layers = tree[\"layers\"]\n",
|
||||||
|
"classes = tree[\"classes\"]\n",
|
||||||
|
"f.close()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "1516ff91",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"field_width = {\n",
|
||||||
|
"\t\"src\": 16,\n",
|
||||||
|
"\t\"dst\": 16,\n",
|
||||||
|
"\t\"protocl\": 8,\n",
|
||||||
|
"}"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "55167c28",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def worst_case(tree):\n",
|
||||||
|
"\n",
|
||||||
|
"\trmt = []\n",
|
||||||
|
"\n",
|
||||||
|
"\tstep = 0\n",
|
||||||
|
"\n",
|
||||||
|
"\tfor layer in layers:\n",
|
||||||
|
"\n",
|
||||||
|
"\t\t# assume that each range requires all of 2*k bits when performing prefix expansion\n",
|
||||||
|
"\t\tnum_ranges = len(layers[layer])\n",
|
||||||
|
"\t\trange_expansion_bits = 2 * field_width[layer]\n",
|
||||||
|
"\n",
|
||||||
|
"\t\ttcam = {\n",
|
||||||
|
"\t\t\t\"id\": f\"{layer}_range\",\n",
|
||||||
|
"\t\t\t\"step\": step,\n",
|
||||||
|
"\t\t\t\"match\": \"ternary\",\n",
|
||||||
|
"\t\t\t\"entries\": num_ranges,\n",
|
||||||
|
"\t\t\t\"key_size\": range_expansion_bits\n",
|
||||||
|
"\t\t}\n",
|
||||||
|
"\n",
|
||||||
|
"\t\t# assume no pointer reuse for metadata storage\n",
|
||||||
|
"\t\tram = {\n",
|
||||||
|
"\t\t\t\"id\": f\"{layer}_meta\",\n",
|
||||||
|
"\t\t\t\"step\": step,\n",
|
||||||
|
"\t\t\t\"match\": \"exact\",\n",
|
||||||
|
"\t\t\t\"method\": \"index\",\n",
|
||||||
|
"\t\t\t\"key_size\": math.ceil(math.log2(num_ranges)),\n",
|
||||||
|
"\t\t\t\"data_size\": len(classes)\n",
|
||||||
|
"\t\t}\n",
|
||||||
|
"\n",
|
||||||
|
"\t\trmt.append(tcam)\n",
|
||||||
|
"\t\trmt.append(ram)\n",
|
||||||
|
"\n",
|
||||||
|
"\t\tstep += 1\n",
|
||||||
|
"\n",
|
||||||
|
"\treturn rmt\n",
|
||||||
|
"\n",
|
||||||
|
"worst_case_rmt = worst_case(tree)\n",
|
||||||
|
"f = open(\"worst_case_rmt.json\", \"w+\")\n",
|
||||||
|
"f.write(json.dumps(worst_case_rmt, indent=4))\n",
|
||||||
|
"f.close()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "48011528",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"TCAM mapping: \n",
|
||||||
|
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
|
||||||
|
"SRAM mapping: \n",
|
||||||
|
"[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
|
||||||
|
"id mapping: \n",
|
||||||
|
"[['dst_range', 'dst_meta'], ['src_range', 'src_meta'], ['protocl_range', 'protocl_meta'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"! command python3 ideal-rmt-simulator/sim.py worst_case_rmt.json"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "switch",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.12.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
Reference in New Issue
Block a user