Skip to content

Commit

Permalink
add comment doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Oct 13, 2023
1 parent 83aeadb commit f441c3a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
56 changes: 28 additions & 28 deletions min_cover_ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl TinyGraph {

#[derive(Clone)]
struct Netlist {
grpy: Graph<usize, usize>,
grph: Graph<usize, usize>,
modules: Vec<usize>,
nets: Vec<usize>,
num_modules: usize,
Expand All @@ -51,15 +51,15 @@ struct Netlist {

impl Netlist {
fn new(
grpy: Graph<usize, usize>,
grph: Graph<usize, usize>,
modules: Vec<usize>,
nets: Vec<usize>,
) -> Self {
let num_modules = modules.len();
let num_nets = nets.len();
let max_degree = modules.iter().map(|&cell| grpy.neighbors(cell).count()).max().unwrap_or(0);
let max_degree = modules.iter().map(|&cell| grph.neighbors(cell).count()).max().unwrap_or(0);
Self {
grpy,
grph,
modules,
nets,
num_modules,
Expand All @@ -79,11 +79,11 @@ impl Netlist {
}

fn number_of_nodes(&self) -> usize {
self.grpy.node_count()
self.grph.node_count()
}

fn number_of_pins(&self) -> usize {
self.grpy.edge_count()
self.grph.edge_count()
}

fn get_max_degree(&self) -> usize {
Expand Down Expand Up @@ -121,8 +121,8 @@ fn min_maximal_matching(
}
let mut min_val = gap[&net];
let mut min_net = net;
for &vtx in hyprgraph.grpy.neighbors(net) {
for &net2 in hyprgraph.grpy.neighbors(vtx) {
for &vtx in hyprgraph.grph.neighbors(net) {
for &net2 in hyprgraph.grph.neighbors(vtx) {
if dep.contains(&net2) {
continue;
}
Expand All @@ -132,16 +132,16 @@ fn min_maximal_matching(
}
}
}
dep.extend(hyprgraph.grpy.neighbors(net));
dep.extend(hyprgraph.grph.neighbors(net));
matchset.insert(min_net);
total_primal_cost += weight[&min_net];
total_dual_cost += min_val;
if min_net == net {
continue;
}
gap.entry(net).and_modify(|e| *e -= min_val);
for &vtx in hyprgraph.grpy.neighbors(net) {
for &net2 in hyprgraph.grpy.neighbors(vtx) {
for &vtx in hyprgraph.grph.neighbors(net) {
for &net2 in hyprgraph.grph.neighbors(vtx) {
gap.entry(net2).and_modify(|e| *e -= min_val);
}
}
Expand All @@ -155,12 +155,12 @@ fn contract_subgraph(
module_weight: &mut Vec<usize>,
forbid: &HashSet<usize>,
) -> (HierNetlist, Vec<usize>) {
let cluster_weight: Vec<usize> = hyprgraph.nets.iter().map(|&net| hyprgraph.grpy.neighbors(net).map(|cell| module_weight[cell]).sum()).collect();
let cluster_weight: Vec<usize> = hyprgraph.nets.iter().map(|&net| hyprgraph.grph.neighbors(net).map(|cell| module_weight[cell]).sum()).collect();
let (clusters, nets, cell_list) = setup(hyprgraph, &cluster_weight, forbid);
let grpy = construct_graph(hyprgraph, &nets, &cell_list, &clusters);
let grph = construct_graph(hyprgraph, &nets, &cell_list, &clusters);
let num_modules = cell_list.len() + clusters.len();
let num_clusters = clusters.len();
let (gr2, net_weight2, num_nets) = reconstruct_graph(hyprgraph, grpy, &nets, num_clusters, num_modules);
let (gr2, net_weight2, num_nets) = reconstruct_graph(hyprgraph, grph, &nets, num_clusters, num_modules);
let mut nets = HashSet::new();
let mut hgr2 = HierNetlist::new(gr2, (0..num_modules).collect(), (num_modules..num_modules+num_nets).collect());
let mut module_weight2 = vec![0; num_modules];
Expand All @@ -172,7 +172,7 @@ fn contract_subgraph(
module_weight2[num_cells + i_v] = cluster_weight[net];
}
let mut node_down_list = cell_list.clone();
node_down_list.extend(clusters.iter().map(|&net| hyprgraph.grpy.neighbors(net).next().unwrap()));
node_down_list.extend(clusters.iter().map(|&net| hyprgraph.grph.neighbors(net).next().unwrap()));
hgr2.clusters = clusters;
hgr2.node_down_list = node_down_list;
hgr2.module_weight = Some(module_weight2.clone());
Expand All @@ -197,7 +197,7 @@ fn setup(
}
}
for (i, &net) in hyprgraph.nets.iter().enumerate() {
if hyprgraph.grpy.neighbors(net).all(|cell| forbid.contains(&cell)) {
if hyprgraph.grph.neighbors(net).all(|cell| forbid.contains(&cell)) {
continue;
}
if cluster_weight[i] == 0 {
Expand All @@ -214,46 +214,46 @@ fn construct_graph(
cell_list: &[usize],
clusters: &[usize],
) -> Graph<usize, usize> {
let mut grpy = Graph::new();
let mut grph = Graph::new();
let mut node_map = HashMap::new();
for &cell in cell_list {
node_map.insert(cell, grpy.add_node(cell));
node_map.insert(cell, grph.add_node(cell));
}
for (i, &cluster) in clusters.iter().enumerate() {
let node = grpy.add_node(hyprgraph.num_modules + i);
for &cell in hyprgraph.grpy.neighbors(cluster) {
let node = grph.add_node(hyprgraph.num_modules + i);
for &cell in hyprgraph.grph.neighbors(cluster) {
if let Some(&node2) = node_map.get(&cell) {
grpy.add_edge(node, node2, i);
grph.add_edge(node, node2, i);
}
}
}
for &net in nets {
let mut nodes = Vec::new();
for &cell in hyprgraph.grpy.neighbors(net) {
for &cell in hyprgraph.grph.neighbors(net) {
if let Some(&node) = node_map.get(&cell) {
nodes.push(node);
}
}
for i in 0..nodes.len() {
for j in i+1..nodes.len() {
grpy.add_edge(nodes[i], nodes[j], net);
grph.add_edge(nodes[i], nodes[j], net);
}
}
}
grpy
grph
}

fn reconstruct_graph(
hyprgraph: &Netlist,
grpy: Graph<usize, usize>,
grph: Graph<usize, usize>,
nets: &[usize],
num_clusters: usize,
num_modules: usize,
) -> (Graph<usize, usize>, Vec<usize>, usize) {
let mut net_weight = vec![0; hyprgraph.num_nets];
for &net in nets {
let mut weight = 0;
for &cell in hyprgraph.grpy.neighbors(net) {
for &cell in hyprgraph.grph.neighbors(net) {
if cell < hyprgraph.num_modules {
weight += 1;
}
Expand All @@ -268,7 +268,7 @@ fn reconstruct_graph(
}
for (i, &net) in nets.iter().enumerate() {
let mut nodes = Vec::new();
for &cell in hyprgraph.grpy.neighbors(net) {
for &cell in hyprgraph.grph.neighbors(net) {
if cell < hyprgraph.num_modules {
if let Some(&node) = node_map.get(&cell) {
nodes.push(node);
Expand All @@ -283,7 +283,7 @@ fn reconstruct_graph(
}
}
}
for edge in grpy.edge_references() {
for edge in grph.edge_references() {
let (u, v) = (edge.source(), edge.target());
let net = *edge.weight();
if let Some(&u2) = node_map.get(&u) {
Expand Down
24 changes: 12 additions & 12 deletions netlist_ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet};
struct Netlist<'a> {
num_pads: i32,
cost_model: i32,
grpy: Graph<&'a str, ()>,
grph: Graph<&'a str, ()>,
modules: Vec<&'a str>,
nets: Vec<&'a str>,
num_modules: usize,
Expand All @@ -17,26 +17,26 @@ struct Netlist<'a> {
}

fn create_test_netlist<'a>() -> Netlist<'a> {
let mut grpy = Graph::new();
let a0 = grpy.add_node("a0");
let a1 = grpy.add_node("a1");
let a2 = grpy.add_node("a2");
let a3 = grpy.add_node("a3");
let a4 = grpy.add_node("a4");
let a5 = grpy.add_node("a5");
let mut grph = Graph::new();
let a0 = grph.add_node("a0");
let a1 = grph.add_node("a1");
let a2 = grph.add_node("a2");
let a3 = grph.add_node("a3");
let a4 = grph.add_node("a4");
let a5 = grph.add_node("a5");
let module_weight: HashMap<&str, i32> = [("a0", 533), ("a1", 543), ("a2", 532)]
.iter()
.cloned()
.collect();
grpy.extend_with_edges(&[(a3, a0), (a3, a1), (a5, a0)]);
grpy.graph_mut().set_node_count(6);
grpy.graph_mut().set_edge_count(3);
grph.extend_with_edges(&[(a3, a0), (a3, a1), (a5, a0)]);
grph.graph_mut().set_node_count(6);
grph.graph_mut().set_edge_count(3);
let modules = vec!["a0", "a1", "a2"];
let nets = vec!["a3", "a4", "a5"];
let mut hyprgraph = Netlist {
num_pads: 0,
cost_model: 0,
grpy,
grph,
modules,
nets,
num_modules: modules.len(),
Expand Down

0 comments on commit f441c3a

Please sign in to comment.