def _import_daggen(line_iter):
_NODE_TYPES = {"ROOT", "END", "COMPUTATION", "TRANSFER"}
result = nx.DiGraph()
node_mapper = lambda nid: "task_%d" % nid
nodes = {}
skip = True
for line in line_iter:
line = line.strip()
if line.startswith("NODE_COUNT"):
skip=False
continue
if skip or not line:
continue
node_parts = line.split(" ")
assert len(node_parts) == 6
magic, nodeid, children, nodetype, cost, parallel_ratio = node_parts
assert magic == "NODE"
nodeid = int(nodeid)
children = list(map(int, children.split(","))) if children != "-" else []
assert nodetype in _NODE_TYPES
cost = float(cost)
# unused_for_now
parallel_ratio = float(parallel_ratio)
nodes[nodeid] = (nodetype, children, cost)
for nodeid, (nodetype, _, cost) in nodes.items():
if nodetype != "TRANSFER":
result.add_node(node_mapper(nodeid), weight=cost)
for nodeid, (nodetype, children, _) in nodes.items():
if nodetype == "TRANSFER":
continue
for childid in children:
childtype, grandchildren, transfercost = nodes[childid]
if childtype == "TRANSFER":
assert len(grandchildren) == 1
destination = grandchildren[0]
weight = transfercost
else:
assert nodetype == "ROOT" or childtype=="END"
destination = childid
# TODO: Should be 0.
#
# Kludge to force order in 3rd-party HEFT implementation
# (nodes connected by edges with zero weight get mixed
# in HEFT priority list and violate precedence constraints)
#
# Can be removed as I can fix this BS in my HEFT
weight = 1.
result.add_edge(node_mapper(nodeid), node_mapper(destination), weight=weight)
node_order = nx.topological_sort(result)
return nx.relabel_nodes(result, {
node_order[0]: "root",
node_order[-1]: "end"
})
评论列表
文章目录