grouping.py 8.79 KB
Newer Older
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
import os
import argparse
import time
import shutil
import sys
from math import floor

####################################################################################
#### Extract hypergraph, instance, IO ports and outline from netlist
####################################################################################
def GenerateHypergraph(openroad_exe, setup_file, extract_hypergraph_file):
    temp_file = os.getcwd() + "/extract_hypergraph.tcl"
    cmd = "cp " + setup_file + " " + temp_file
    os.system(cmd)

    with open(extract_hypergraph_file) as f:
        content = f.read().splitlines()
    f.close()

    f = open(temp_file, "a")
    f.write("\n")
    for line in content:
        f.write(line + "\n")
    f.close()

    cmd = openroad_exe + " " + temp_file
    os.system(cmd)

    cmd = "rm " + temp_file
    os.system(cmd)


####################################################################################
#### TraverseFanout in BFS manner
####################################################################################
def TraverseFanout(vertices_list, hyperedges_list, vertex_hash, seed_id, root_id, K_out):
    # update the root vertex
    vertex_hash[root_id] = [seed_id, 0]

    visited = []
    wavefront = []

    wavefront.append(root_id)

    while (len(wavefront) > 0):
        vertex_id = wavefront.pop(0)
        visited.append(vertex_id)
        level_id = vertex_hash[vertex_id][1] + 1
        if (level_id <= K_out):
            for hyperedge_id in vertices_list[vertex_id]:
                hyperedge = hyperedges_list[hyperedge_id]
                # This hyperedge is driven by current edge
                if (hyperedge[0] == vertex_id):
                    for i in range(1, len(hyperedge)):
                        if hyperedge[i] in visited:
                            pass
                        else:
                            if (vertex_hash[hyperedge[i]][1] > level_id):
                                vertex_hash[hyperedge[i]] = [seed_id, level_id]
                                wavefront.append(hyperedge[i])
                            elif (vertex_hash[hyperedge[i]][1] == level_id and vertex_hash[hyperedge[i]][1] > seed_id):
                                vertex_hash[hyperedge[i]] = [seed_id, level_id]
                                wavefront.append(hyperedge[i])


####################################################################################
#### TraverseFanin in BFS manner
####################################################################################
def TraverseFanin(vertices_list, hyperedges_list, vertex_hash, seed_id, root_id, K_in):
    # update the root vertex
    vertex_hash[root_id] = [seed_id, 0]

    visited = []
    wavefront = []

    wavefront.append(root_id)

    while (len(wavefront) > 0):
        vertex_id = wavefront.pop(0)
        visited.append(vertex_id)
        level_id = vertex_hash[vertex_id][1] + 1
        if (level_id <= K_in):
            for hyperedge_id in vertices_list[vertex_id]:
                hyperedge = hyperedges_list[hyperedge_id]
                # This hyperedge is not driven by vertex_id
                if (hyperedge[0] != vertex_id):
                    driver_id = hyperedge[0]
                    if driver_id in visited:
                        pass
                    else:
                        if (vertex_hash[driver_id][1] > level_id):
                            vertex_hash[driver_id] = [seed_id, level_id]
                            wavefront.append(driver_id)
                        elif (vertex_hash[driver_id][1] == level_id and vertex_hash[driver_id][1] > seed_id):
                            vertex_hash[driver_id] = [seed_id, level_id]
                            wavefront.append(driver_id)


def Grouping(design, n_rows, n_cols, K_in, K_out, setup_file, global_net_threshold, src_dir):
    pwd = os.getcwd()
    # Specify the location of hmetis exe and openroad exe
    openroad_exe = src_dir + "/utils/openroad"
    extract_hypergraph_file  = src_dir + "/utils/extract_hypergraph.tcl"

    # Generate Hypergraph file
    rpt_dir = pwd + "/rtl_mp"
    hypergraph_file = rpt_dir + "/" + design + ".hgr"
    io_name_file = hypergraph_file + ".io"
    instance_name_file = hypergraph_file + ".instance"
    outline_file = hypergraph_file + ".outline"
    fix_file = pwd + "/" + design  + ".fix"
    original_fix_file = pwd + "/" + design + ".fix.old"
    GenerateHypergraph(openroad_exe, setup_file, extract_hypergraph_file)

    # read hypergraph
    # Each vertex corresponds to the hyperedge_id incident to it
    # Each hyperedge corresponds to the vertices in it
    with open (hypergraph_file) as f:
        content = f.read().splitlines()
    f.close()

    items = content[0].split()
    num_hyperedges = int(items[0])
    num_vertices   = int(items[1])

    vertices_list = [[] for i in range(num_vertices)]
    hyperedges_list = []
    hyperedge_id = 0

    for i in range(num_hyperedges):
        items = content[i+1].split()
        # ignore all the global nets
        if (len(items) > global_net_threshold):
            pass
        hyperedge = [int(item) - 1 for item in items]
        hyperedges_list.append(hyperedge)
        for vertex in hyperedge:
            vertices_list[vertex].append(hyperedge_id)
        hyperedge_id += 1

    # sort all the macros and IOs based on lexicographically smallest macro name
    # get all the IOs and macros as seeds
    seed_list = []
    vertex_map = {  }
    vertex_name_list = []

    # read all the IOs
    io_pos_map = {  }
    with open(io_name_file) as f:
        content = f.read().splitlines()
    f.close()


    vertex_id = 0
    for line in content:
        items = line.split()
        io_name = items[0]
        x = (float(items[1]) + float(items[3])) / 2.0
        y = (float(items[2]) + float(items[4])) / 2.0
        io_pos_map[io_name] = (x, y)
        seed_list.append(io_name)
        vertex_map[io_name] = vertex_id
        vertex_name_list.append(io_name)
        vertex_id += 1


    # read all the instances (including macros)
    with open(instance_name_file) as f:
        content = f.read().splitlines()
    f.close()

    for line in content:
        items = line.split()
        inst_name = items[0]
        vertex_map[inst_name] = vertex_id
        vertex_name_list.append(inst_name)
        vertex_id += 1
        if (int(items[1]) == 1):
            seed_list.append(inst_name)

    # sort the seed
    seed_list.sort()

    # grouping all the directly K_in fanins and K_out fanouts respect to
    # ios and instances.
    vertex_hash = [[len(seed_list), max(K_in, K_out) + 1] for i in range(len(vertex_map))]
    for i in range(len(seed_list)):
        TraverseFanout(vertices_list, hyperedges_list, vertex_hash, i, vertex_map[seed_list[i]], K_out)
        TraverseFanin(vertices_list, hyperedges_list, vertex_hash, i,  vertex_map[seed_list[i]], K_in)


    # group IOs to bundled pins based on their position
    with open(outline_file) as f:
        content = f.read().splitlines()
    f.close()

    items = content[0].split()
    floorplan_lx = float(items[0])
    floorplan_ly = float(items[1])
    floorplan_ux = float(items[2])
    floorplan_uy = float(items[3])

    # get the vertical distance and horizontal distance
    width = (floorplan_ux - floorplan_lx) / n_cols
    height = (floorplan_uy - floorplan_ly) / n_rows


    initial_group_map = {  }
    grid_row_max = 100000000
    for io_name, pos in io_pos_map.items():
        hash_id = floor( pos[1] / height) * grid_row_max + floor( pos[0] / width)
        if hash_id not in initial_group_map:
            initial_group_map[hash_id] = [io_name]
        else:
            initial_group_map[hash_id].append(io_name)

    # final group list
    group_list = [  ]
    for hash_id, ios in initial_group_map.items():
        group_list.append(ios)

    for seed in seed_list:
        if seed not in io_pos_map:
            group_list.append([seed])

    group_id_map = {  }
    for i in range(len(group_list)):
        for vertex in group_list[i]:
            group_id_map[vertex] = i


    for i in range(len(vertex_hash)):
        seed_id = vertex_hash[i][0]
        if (seed_id < len(seed_list)):
            vertex_name = vertex_name_list[i]
            seed_name = seed_list[seed_id]
            if vertex_name not in group_list[group_id_map[seed_name]]:
                group_list[group_id_map[seed_name]].append(vertex_name)


    f = open(original_fix_file, "w")
    for group in group_list:
        line = ""
        for vertex in group:
            line += vertex + ","
        line = line[0:-1]
        f.write(line + '\n')
    f.close()

    f = open(fix_file, "w")
    for group in group_list:
        line = ""
        for vertex in group:
            if vertex not in seed_list:
                line += vertex + ","
        if (len(line) > 0):
            line = line[0:-1]
        f.write(line + '\n')
    f.close()


    shutil.rmtree(rpt_dir)