forked from sventhijssen/compact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChakrabortyMappingMethod.py
92 lines (74 loc) · 3.46 KB
/
ChakrabortyMappingMethod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import time
from datetime import datetime
from networkx import DiGraph
import config
from Literal import Literal
from MappingMethod import MappingMethod
from MemristorCrossbar import MemristorCrossbar
class ChakrabortyMappingMethod(MappingMethod):
def __init__(self):
super(ChakrabortyMappingMethod, self).__init__()
def map(self, graph: DiGraph) -> MemristorCrossbar:
print("Chakraborty's mapping method started")
print(datetime.now())
self.start_time = time.time()
config.log.add('Nodes: {}\n'.format(len(graph.nodes)))
config.log.add('Edges: {}\n'.format(len(graph.edges)))
rows = len(graph.nodes)
columns = len(graph.edges)
crossbar = MemristorCrossbar(rows, columns)
# We assign each node to a layer with the terminal node to the bottom-most nanowire
# and the root node to the top-most nanowire
r = 0
input_variables = set()
input_nodes = dict()
root_nodes = dict()
node_layers = dict()
for (current_node, node_data) in graph.nodes(data=True):
input_variables.add(node_data["variable"])
if node_data["root"]:
output_variables = node_data["output_variables"]
for output_variable in output_variables:
root_nodes[output_variable] = (0, r)
if node_data["terminal"]:
input_variable = node_data["variable"]
input_nodes[input_variable] = (0, r)
node_layers[current_node] = r
r += 1
c = 0
for (current_node, node_data) in graph.nodes(data=True):
out_edges = graph.edges(current_node, data=True)
variable = node_data['variable']
positive_child_node = None
negative_child_node = None
for (_, child_node, edge_data) in out_edges:
if edge_data['positive']:
positive_child_node = child_node
if not edge_data['positive']:
negative_child_node = child_node
if positive_child_node is not None:
r_current_node = node_layers[current_node]
r_child_node = node_layers[positive_child_node]
crossbar.set_memristor(r_current_node, c, Literal(variable, True))
crossbar.set_memristor(r_child_node, c, Literal("True", True))
c += 1
if negative_child_node is not None:
r_current_node = node_layers[current_node]
r_child_node = node_layers[negative_child_node]
crossbar.set_memristor(r_current_node, c, Literal(variable, False))
crossbar.set_memristor(r_child_node, c, Literal("True", True))
c += 1
crossbar.input_variables = list(input_variables)
for (input_function, nanowire) in input_nodes.items():
crossbar.set_input_nanowire(input_function, nanowire)
for (output_function, nanowire) in root_nodes.items():
crossbar.set_output_nanowire(output_function, nanowire)
self.crossbar = crossbar
self.end_time = time.time()
config.log.add('Rows: {}\n'.format(rows))
config.log.add('Columns: {}\n'.format(columns))
config.log.add('Chakraborty\'s mapping method time (s): {}\n'.format(self.end_time - self.start_time))
print("Chakraborty's mapping method stopped")
return self.crossbar
def get_log(self) -> str:
pass