dag_gen.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pysimgrid 作者: alexmnazarenko 项目源码 文件源码
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"
    })
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号