diff --git a/Cargo.lock b/Cargo.lock index da0fd887..90063e4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,246 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "awooter" version = "0.1.0" +dependencies = [ + "colored", + "indicatif", + "libc", + "rayon", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + +[[package]] +name = "console" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "terminal_size", + "unicode-width", + "winapi", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] +[[package]] +name = "indicatif" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4295cbb7573c16d310e99e713cf9e75101eb190ab31fccd35f2d2691b4352b19" +dependencies = [ + "console", + "number_prefix", + "portable-atomic", + "unicode-width", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" + +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] +name = "portable-atomic" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15eb2c6e362923af47e13c23ca5afb859e83d54452c55b0b9ac763b8f7c1ac16" + +[[package]] +name = "rayon" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e060280438193c554f654141c9ea9417886713b7acd75974c85b18a69a88e0b" +dependencies = [ + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "terminal_size" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/common/route/awooter.cc b/common/route/awooter.cc index d3341d8a..f9a381ab 100644 --- a/common/route/awooter.cc +++ b/common/route/awooter.cc @@ -111,6 +111,7 @@ extern "C" { uint64_t npnr_belid_null() { return wrap(BelId()); } uint64_t npnr_wireid_null() { return wrap(WireId()); } + uint64_t npnr_pipid_null() { return wrap(PipId()); } int npnr_context_get_grid_dim_x(const Context *const ctx) { return ctx->getGridDimX(); } int npnr_context_get_grid_dim_y(const Context *const ctx) { return ctx->getGridDimY(); } @@ -125,6 +126,20 @@ extern "C" { uint64_t npnr_context_get_pip_dst_wire(const Context *const ctx, uint64_t pip) { return wrap(ctx->getPipDstWire(unwrap_pip(pip))); } float npnr_context_estimate_delay(const Context *const ctx, uint64_t src, uint64_t dst) { return ctx->getDelayNS(ctx->estimateDelay(unwrap_wire(src), unwrap_wire(dst))); } float npnr_context_delay_epsilon(const Context *const ctx) { return ctx->getDelayNS(ctx->getDelayEpsilon()); } + Loc npnr_context_get_pip_location(const Context *const ctx, uint64_t pip) { return ctx->getPipLocation(unwrap_pip(pip)); } + + uint64_t npnr_context_get_pips_leak(const Context *const ctx, PipId **pips) { + auto pip_vec = std::vector{}; + for (auto pip : ctx->getPips()) { + pip_vec.push_back(pip); + } + pip_vec.shrink_to_fit(); + auto size = pip_vec.size(); + *pips = pip_vec.data(); + // Yes, by placement-newing over `pip_vec` we leak memory. + new (&pip_vec) std::vector; + return size; + } uint64_t npnr_context_get_wires_leak(const Context *const ctx, WireId **wires) { auto wire_vec = std::vector{}; @@ -198,8 +213,7 @@ extern "C" { #endif CellInfo* npnr_portref_cell(const PortRef *const port) { return port->cell; } - int npnr_cellinfo_get_location_x(const CellInfo *const info) { return info->getLocation().x; } - int npnr_cellinfo_get_location_y(const CellInfo *const info) { return info->getLocation().y; } + Loc npnr_cellinfo_get_location(const CellInfo *const info) { return info->getLocation(); } extern bool npnr_router_awooter(Context *ctx); } diff --git a/common/route/awooter/rust/Cargo.toml b/common/route/awooter/rust/Cargo.toml index 383cf217..6b7abb08 100644 --- a/common/route/awooter/rust/Cargo.toml +++ b/common/route/awooter/rust/Cargo.toml @@ -12,3 +12,6 @@ crate-type = ["staticlib"] [dependencies] libc = "0.2" +indicatif = "0.17" +rayon = "1.6" +colored = "2" \ No newline at end of file diff --git a/common/route/awooter/rust/src/lib.rs b/common/route/awooter/rust/src/lib.rs index 2edf772b..36b640e9 100644 --- a/common/route/awooter/rust/src/lib.rs +++ b/common/route/awooter/rust/src/lib.rs @@ -1,4 +1,8 @@ -use std::ptr::NonNull; +use std::{collections::HashMap, ptr::NonNull}; + +use colored::Colorize; +use indicatif::{ProgressBar, ProgressStyle}; +use rayon::prelude::*; #[macro_use] mod npnr; @@ -32,110 +36,12 @@ pub extern "C" fn npnr_router_awooter(ctx: Option>) -> bo }) } -type ArcSlice = [((i32, i32), (i32, i32))]; type ArcVec = Vec<((i32, i32), (i32, i32))>; -fn partition(arcs: &ArcSlice, x: i32, y: i32) -> (ArcVec, ArcVec, ArcVec, ArcVec) { - let mut ne = Vec::new(); - let mut se = Vec::new(); - let mut sw = Vec::new(); - let mut nw = Vec::new(); - let mut part_horiz = 0; - let mut part_vert = 0; - let mut part_diag = 0; - - log_info!("Partitioning arcs along X = {}, Y = {}\n", x, y); - - for (source, sink) in arcs { - let (source_x, source_y) = source; - let (sink_x, sink_y) = sink; - let source_is_north = *source_x < x; - let source_is_east = *source_y < y; - let sink_is_north = *sink_x < x; - let sink_is_east = *sink_y < y; - - // If these segments are already inside a partition, just store them as-is. - if source_is_north == sink_is_north && source_is_east == sink_is_east { - match (source_is_north, source_is_east) { - (true, true) => ne.push((*source, *sink)), - (true, false) => nw.push((*source, *sink)), - (false, true) => se.push((*source, *sink)), - (false, false) => sw.push((*source, *sink)), - } - continue; - } - - // Partition horizontally. - if source_is_north != sink_is_north && source_is_east == sink_is_east { - match source_is_east { - true => { - ne.push((*source, (x, *sink_y))); - se.push(((x, *sink_y), *sink)); - } - false => { - nw.push((*source, (x, *sink_y))); - sw.push(((x, *sink_y), *sink)); - } - } - part_horiz += 1; - continue; - } - - // Partition vertically. - if source_is_north == sink_is_north && source_is_east != sink_is_east { - match source_is_north { - true => { - ne.push((*source, (*sink_x, y))); - nw.push(((*sink_x, y), *sink)); - } - false => { - se.push((*source, (*sink_x, y))); - sw.push(((*sink_x, y), *sink)); - } - } - part_vert += 1; - continue; - } - - // Partition both ways. - match (source_is_north, source_is_east) { - (true, true) => { - ne.push((*source, (x, *source_y))); - se.push(((x, *source_y), (*sink_x, y))); - sw.push(((*sink_x, y), *sink)) - } - (true, false) => { - nw.push((*source, (x, *source_y))); - sw.push(((x, *source_y), (*sink_x, y))); - se.push(((*sink_x, y), *sink)) - } - (false, true) => { - se.push((*source, (x, *source_y))); - ne.push(((x, *source_y), (*sink_x, y))); - nw.push(((*sink_x, y), *sink)) - } - (false, false) => { - sw.push((*source, (x, *source_y))); - nw.push(((x, *source_y), (*sink_x, y))); - ne.push(((*sink_x, y), *sink)) - } - } - part_diag += 1; - } - - /*log_info!(" {} arcs partitioned horizontally\n", part_horiz); - log_info!(" {} arcs partitioned vertically\n", part_vert); - log_info!(" {} arcs partitioned both ways\n", part_diag); - log_info!(" {} arcs in the northeast\n", ne.len()); - log_info!(" {} arcs in the southeast\n", se.len()); - log_info!(" {} arcs in the southwest\n", sw.len()); - log_info!(" {} arcs in the northwest\n", nw.len());*/ - - (ne, se, sw, nw) -} - fn find_partition_point( - arcs: &ArcSlice, + ctx: &npnr::Context, + nets: &npnr::Nets, + pips: &[npnr::PipId], x_start: i32, x_finish: i32, y_start: i32, @@ -152,9 +58,24 @@ fn find_partition_point( let mut nw = Vec::new(); while x_diff != 0 { - (ne, se, sw, nw) = partition(arcs, x, y); + (ne, se, sw, nw) = partition_nets(ctx, nets, pips, x, y); let north = ne.len() + nw.len(); let south = se.len() + sw.len(); + + let nets = (north + south) as f64; + + let ne_dist = f64::abs(((ne.len() as f64) / nets) - 0.25); + let se_dist = f64::abs(((se.len() as f64) / nets) - 0.25); + let sw_dist = f64::abs(((sw.len() as f64) / nets) - 0.25); + let nw_dist = f64::abs(((nw.len() as f64) / nets) - 0.25); + + let distortion = 100.0 * (ne_dist + se_dist + sw_dist + nw_dist); + + // Stop early if Good Enough. + if distortion <= 5.0 { + return (x, y, ne, se, sw, nw); + } + if north > south { x -= x_diff; } else if north < south { @@ -171,21 +92,9 @@ fn find_partition_point( x_diff >>= 1; y_diff >>= 1; - - let nets = (north + south) as f64; - - let ne_dist = f64::abs(((ne.len() as f64) / nets) - 0.25); - let se_dist = f64::abs(((se.len() as f64) / nets) - 0.25); - let sw_dist = f64::abs(((sw.len() as f64) / nets) - 0.25); - let nw_dist = f64::abs(((nw.len() as f64) / nets) - 0.25); - - log_info!( - "Distortion: {:.02}%\n", - 100.0 * (ne_dist + se_dist + sw_dist + nw_dist) - ); } - (ne, se, sw, nw) = partition(arcs, x, y); + (ne, se, sw, nw) = partition_nets(ctx, nets, pips, x, y); let north = ne.len() + nw.len(); let south = se.len() + sw.len(); @@ -204,19 +113,328 @@ fn find_partition_point( (x, y, ne, se, sw, nw) } +fn partition_nets( + ctx: &npnr::Context, + nets: &npnr::Nets, + pips: &[npnr::PipId], + x: i32, + y: i32, +) -> (ArcVec, ArcVec, ArcVec, ArcVec) { + let mut partition_pips = HashMap::new(); + + let mut ne = Vec::new(); + let mut se = Vec::new(); + let mut sw = Vec::new(); + let mut nw = Vec::new(); + let mut part_horiz = 0; + let mut part_vert = 0; + let mut part_diag = 0; + + let x_str = format!("X = {}", x); + let y_str = format!("Y = {}", y); + log_info!( + "Partitioning arcs along {}, {}\n", + x_str.bright_white(), + y_str.bright_white() + ); + + for &pip in pips { + let loc = ctx.pip_location(pip); + if loc.x == x || loc.y == y { + partition_pips + .entry((loc.x, loc.y)) + .and_modify(|pip_list: &mut Vec<(npnr::PipId, Vec)>| { + pip_list.push((pip, Vec::new())) + }) + .or_insert_with(|| vec![(pip, Vec::new())]); + } + } + + let progress = ProgressBar::new(nets.len() as u64); + progress.set_style( + ProgressStyle::with_template( + "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {msg}", + ) + .unwrap() + .progress_chars("#>-"), + ); + + for (name, net) in nets.iter() { + let mut message = ctx.name_of(*name).to_str().unwrap().to_string(); + let message = if message.len() > 24 { + message.truncate(21); + format!("{}...", message) + } else { + message + }; + progress.set_message(message); + progress.inc(1); + let net = unsafe { net.as_mut().unwrap() }; + + if net.is_global() { + continue; + } + + let source = unsafe { net.driver().as_ref().unwrap() }; + + let source = source.cell(); + if source.is_none() { + continue; + } + let source = source.unwrap().location(); + let source_is_north = source.x < x; + let source_is_east = source.y < y; + let source_wire = ctx.source_wire(net); + + for sink in nets.users_by_name(*name).unwrap().iter() { + let sink = unsafe { sink.as_ref().unwrap() }; + let sink_loc = sink.cell().unwrap().location(); + let sink_is_north = sink_loc.x < x; + let sink_is_east = sink_loc.y < y; + + for sink_wire in ctx.sink_wires(net, sink) { + if source_is_north == sink_is_north && source_is_east == sink_is_east { + let arc = ((source.x, source.y), (sink_loc.x, sink_loc.y)); + match (source_is_north, source_is_east) { + (true, true) => ne.push(arc), + (true, false) => nw.push(arc), + (false, true) => se.push(arc), + (false, false) => sw.push(arc), + } + } else if source_is_north != sink_is_north && source_is_east == sink_is_east { + let middle = ((source.x + sink_loc.x) / 2, y); + let pips = partition_pips.get_mut(&middle).unwrap(); + + let (selected_pip, pip_uses) = pips + .par_iter_mut() + .max_by_key(|(pip, uses)| { + let src_to_pip = + ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); + let pip_to_snk = ctx.estimate_delay(ctx.pip_dst_wire(*pip), sink_wire); + let uses = uses.len() - (uses.contains(name) as usize); + (1000.0 * (src_to_pip + ((uses + 1) as f32) * pip_to_snk)) as u64 + }) + .unwrap(); + pip_uses.push(*name); + + let pip_loc = ctx.pip_location(*selected_pip); + let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y)); + let pip_to_dst = ((pip_loc.x, pip_loc.y), (sink_loc.x, sink_loc.y)); + match (source_is_north, source_is_east) { + (true, true) => { + ne.push(src_to_pip); + se.push(pip_to_dst); + } + (true, false) => { + nw.push(src_to_pip); + sw.push(pip_to_dst); + } + (false, true) => { + se.push(src_to_pip); + ne.push(pip_to_dst); + } + (false, false) => { + sw.push(src_to_pip); + nw.push(pip_to_dst); + } + } + + part_horiz += 1; + } else if source_is_north == sink_is_north && source_is_east != sink_is_east { + let middle = (x, (source.y + sink_loc.y) / 2); + let pips = partition_pips.get_mut(&middle).unwrap(); + + let (selected_pip, pip_uses) = pips + .par_iter_mut() + .max_by_key(|(pip, uses)| { + let src_to_pip = + ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); + let pip_to_snk = ctx.estimate_delay(ctx.pip_dst_wire(*pip), sink_wire); + let uses = uses.len() - (uses.contains(name) as usize); + (1000.0 * (src_to_pip + ((uses + 1) as f32) * pip_to_snk)) as u64 + }) + .unwrap(); + pip_uses.push(*name); + + let pip_loc = ctx.pip_location(*selected_pip); + let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y)); + let pip_to_dst = ((pip_loc.x, pip_loc.y), (sink_loc.x, sink_loc.y)); + match (source_is_north, source_is_east) { + (true, true) => { + ne.push(src_to_pip); + nw.push(pip_to_dst); + } + (true, false) => { + nw.push(src_to_pip); + ne.push(pip_to_dst); + } + (false, true) => { + se.push(src_to_pip); + sw.push(pip_to_dst); + } + (false, false) => { + sw.push(src_to_pip); + se.push(pip_to_dst); + } + } + + part_vert += 1; + } else { + let middle = (x, (source.y + sink_loc.y) / 2); + let pips = partition_pips.get_mut(&middle).unwrap(); + + let (horiz_pip, pip_uses) = pips + .par_iter_mut() + .max_by_key(|(pip, uses)| { + let src_to_pip = + ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); + let pip_to_snk = ctx.estimate_delay(ctx.pip_dst_wire(*pip), sink_wire); + let uses = uses.len() - (uses.contains(name) as usize); + (1000.0 * (src_to_pip + ((uses + 1) as f32) * pip_to_snk)) as u64 + }) + .unwrap(); + pip_uses.push(*name); + let horiz_pip = *horiz_pip; + + let middle = ((source.x + sink_loc.x) / 2, y); + let pips = partition_pips.get_mut(&middle).unwrap(); + + let (vert_pip, pip_uses) = pips + .par_iter_mut() + .max_by_key(|(pip, uses)| { + let src_to_pip = + ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); + let pip_to_snk = ctx.estimate_delay(ctx.pip_dst_wire(*pip), sink_wire); + let uses = uses.len() - (uses.contains(name) as usize); + (1000.0 * (src_to_pip + ((uses + 1) as f32) * pip_to_snk)) as u64 + }) + .unwrap(); + pip_uses.push(*name); + + let horiz_loc = ctx.pip_location(horiz_pip); + let vert_loc = ctx.pip_location(*vert_pip); + let src_to_horiz = ((source.x, source.y), (horiz_loc.x, horiz_loc.y)); + let horiz_to_vert = ((horiz_loc.x, horiz_loc.y), (vert_loc.x, vert_loc.y)); + let vert_to_dst = ((vert_loc.x, vert_loc.y), (sink_loc.x, sink_loc.y)); + match (source_is_north, source_is_east) { + (true, true) => { + ne.push(src_to_horiz); + nw.push(horiz_to_vert); + sw.push(vert_to_dst); + } + (true, false) => { + nw.push(src_to_horiz); + ne.push(horiz_to_vert); + se.push(vert_to_dst); + } + (false, true) => { + se.push(src_to_horiz); + sw.push(horiz_to_vert); + nw.push(vert_to_dst); + } + (false, false) => { + sw.push(src_to_horiz); + se.push(horiz_to_vert); + ne.push(vert_to_dst); + } + } + + part_diag += 1; + } + } + } + } + + progress.finish_with_message("done"); + + let north = ne.len() + nw.len(); + let south = se.len() + sw.len(); + + let nets = (north + south) as f64; + + let ne_dist = f64::abs(((ne.len() as f64) / nets) - 0.25); + let se_dist = f64::abs(((se.len() as f64) / nets) - 0.25); + let sw_dist = f64::abs(((sw.len() as f64) / nets) - 0.25); + let nw_dist = f64::abs(((nw.len() as f64) / nets) - 0.25); + + let ne_str = ne.len().to_string(); + let se_str = se.len().to_string(); + let sw_str = sw.len().to_string(); + let nw_str = nw.len().to_string(); + + log_info!( + " {} arcs partitioned horizontally\n", + part_horiz.to_string().bright_white() + ); + log_info!( + " {} arcs partitioned vertically\n", + part_vert.to_string().bright_white() + ); + log_info!( + " {} arcs partitioned both ways\n", + part_diag.to_string().bright_white() + ); + log_info!( + " {} arcs in the northeast\n", + if ne_dist > 0.25 { + ne_str.red() + } else if ne_dist > 0.05 { + ne_str.yellow() + } else { + ne_str.green() + } + ); + log_info!( + " {} arcs in the southeast\n", + if se_dist > 0.25 { + se_str.red() + } else if se_dist > 0.05 { + se_str.yellow() + } else { + se_str.green() + } + ); + log_info!( + " {} arcs in the southwest\n", + if sw_dist > 0.25 { + sw_str.red() + } else if sw_dist > 0.05 { + sw_str.yellow() + } else { + sw_str.green() + } + ); + log_info!( + " {} arcs in the northwest\n", + if nw_dist > 0.25 { + nw_str.red() + } else if nw_dist > 0.05 { + nw_str.yellow() + } else { + nw_str.green() + } + ); + + (ne, se, sw, nw) +} + fn route(ctx: &mut npnr::Context) -> bool { log_info!("Awoooo from Rust!\n"); log_info!( "Running on a {}x{} grid\n", - ctx.grid_dim_x(), - ctx.grid_dim_y() + ctx.grid_dim_x().to_string().bright_white(), + ctx.grid_dim_y().to_string().bright_white(), ); - let wires = npnr::Wires::new(ctx); - log_info!("Found {} wires\n", wires.len()); + let wires = ctx.wires_leaking(); + log_info!("Found {} wires\n", wires.len().to_string().bright_white()); + + let pips = ctx.pips_leaking(); + log_info!("Found {} pips\n", pips.len().to_string().bright_white()); let nets = npnr::Nets::new(ctx); - log_info!("Found {} nets\n", nets.len()); + let nets_str = nets.len().to_string(); + log_info!("Found {} nets\n", nets_str.bright_white()); let mut count = 0; for (name, net) in nets.iter() { @@ -224,11 +442,11 @@ fn route(ctx: &mut npnr::Context) -> bool { let net = unsafe { net.as_mut().unwrap() }; let users = nets.users_by_name(*name).unwrap().iter(); for user in users { - count += ctx.sink_wires(net, user).count(); + count += ctx.sink_wires(net, *user).count(); } } - log_info!("Found {} arcs\n", count); + log_info!("Found {} arcs\n", count.to_string().bright_white()); let (name, net) = nets .iter() @@ -240,7 +458,7 @@ fn route(ctx: &mut npnr::Context) -> bool { nets.users_by_name(**name) .unwrap() .iter() - .fold(0, |acc, sink| acc + ctx.sink_wires(net, sink).count()) + .fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).count()) } }) .unwrap(); @@ -250,12 +468,13 @@ fn route(ctx: &mut npnr::Context) -> bool { .users_by_name(*name) .unwrap() .iter() - .fold(0, |acc, sink| acc + ctx.sink_wires(net, sink).count()); + .fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).count()) + .to_string(); log_info!( "Highest non-global fansnout net is {} with {} arcs\n", - ctx.name_of(*name).to_str().unwrap(), - count + ctx.name_of(*name).to_str().unwrap().bright_white(), + count.bright_white() ); let mut x0 = 0; @@ -265,46 +484,22 @@ fn route(ctx: &mut npnr::Context) -> bool { for sink in nets.users_by_name(*name).unwrap().iter() { let sink = unsafe { sink.as_ref().unwrap() }; - let cell = sink.cell().unwrap(); - x0 = x0.min(cell.location_x()); - y0 = y0.min(cell.location_y()); - x1 = x1.max(cell.location_x()); - y1 = y1.max(cell.location_y()); + let cell = sink.cell().unwrap().location(); + x0 = x0.min(cell.x); + y0 = y0.min(cell.y); + x1 = x1.max(cell.x); + y1 = y1.max(cell.y); } - log_info!(" which spans ({}, {}) to ({}, {})\n", x0, y0, x1, y1); + let coords_min = format!("({}, {})", x0, y0); + let coords_max = format!("({}, {})", x1, y1); + log_info!( + " which spans {} to {}\n", + coords_min.bright_white(), + coords_max.bright_white() + ); - let mut arcs = Vec::new(); - - for (name, net) in nets.iter() { - let net = unsafe { net.as_mut().unwrap() }; - let source = unsafe { net.driver().as_ref().unwrap() }; - - let source_cell = source.cell(); - if source_cell.is_none() { - continue; - } - let source_cell = source_cell.unwrap(); - let source_x = source_cell.location_x(); - let source_y = source_cell.location_y(); - - for sink in nets.users_by_name(*name).unwrap().iter() { - let sink = unsafe { sink.as_ref().unwrap() }; - let sink_x = sink.cell().unwrap().location_x(); - let sink_y = sink.cell().unwrap().location_y(); - - arcs.push(((source_x, source_y), (sink_x, sink_y))); - } - } - - let x_start = 0; - let x_finish = ctx.grid_dim_x(); - let y_start = 0; - let y_finish = ctx.grid_dim_y(); - log_info!("=== level 1:\n"); - let (x, y, ne, se, sw, nw) = find_partition_point(&arcs, x_start, x_finish, y_start, y_finish); - - todo!("figure out how to tell if a wire crosses the calculated partition boundaries"); + let _ = find_partition_point(ctx, &nets, pips, 0, ctx.grid_dim_x(), 0, ctx.grid_dim_y()); /*log_info!("=== level 2 NE:\n"); let _ = find_partition_point(&ne, x_start, x, y_start, y); diff --git a/common/route/awooter/rust/src/npnr.rs b/common/route/awooter/rust/src/npnr.rs index f31dddf1..2fb54914 100644 --- a/common/route/awooter/rust/src/npnr.rs +++ b/common/route/awooter/rust/src/npnr.rs @@ -1,5 +1,5 @@ use core::slice; -use std::{ffi::CStr, collections::{HashMap}, marker::PhantomData}; +use std::{collections::HashMap, ffi::CStr, marker::PhantomData}; use libc::c_char; @@ -21,12 +21,8 @@ pub struct CellInfo { } impl CellInfo { - pub fn location_x(&self) -> i32 { - unsafe { npnr_cellinfo_get_location_x(self) } - } - - pub fn location_y(&self) -> i32 { - unsafe { npnr_cellinfo_get_location_y(self) } + pub fn location(&self) -> Loc { + unsafe { npnr_cellinfo_get_location(self) } } } @@ -85,13 +81,13 @@ impl BelId { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[repr(transparent)] pub struct PipId(u64); impl PipId { pub fn null() -> Self { - todo!() + unsafe { npnr_pipid_null() } } } @@ -112,6 +108,13 @@ impl WireId { } } +#[repr(C)] +pub struct Loc { + pub x: libc::c_int, + pub y: libc::c_int, + pub z: libc::c_int, +} + #[repr(C)] pub struct Context { _private: [u8; 0], @@ -196,6 +199,22 @@ impl Context { } } + pub fn wires_leaking(&self) -> &[WireId] { + let mut wires = std::ptr::null_mut(); + let len = unsafe { npnr_context_get_wires_leak(self, &mut wires as *mut *mut WireId) }; + unsafe { std::slice::from_raw_parts(wires, len as usize) } + } + + pub fn pips_leaking(&self) -> &[PipId] { + let mut pips = std::ptr::null_mut(); + let len = unsafe { npnr_context_get_pips_leak(self, &mut pips as *mut *mut PipId) }; + unsafe { std::slice::from_raw_parts(pips, len as usize) } + } + + pub fn pip_location(&self, pip: PipId) -> Loc { + unsafe { npnr_context_get_pip_location(self, pip) } + } + pub fn check(&self) { unsafe { npnr_context_check(self) } } @@ -224,6 +243,7 @@ extern "C" { fn npnr_belid_null() -> BelId; fn npnr_wireid_null() -> WireId; + fn npnr_pipid_null() -> PipId; fn npnr_context_get_grid_dim_x(ctx: *const Context) -> libc::c_int; fn npnr_context_get_grid_dim_y(ctx: *const Context) -> libc::c_int; @@ -254,6 +274,8 @@ extern "C" { fn npnr_context_get_pip_dst_wire(ctx: *const Context, pip: PipId) -> WireId; fn npnr_context_estimate_delay(ctx: *const Context, src: WireId, dst: WireId) -> libc::c_float; fn npnr_context_get_wires_leak(ctx: *const Context, wires: *mut *mut WireId) -> u64; + fn npnr_context_get_pips_leak(ctx: *const Context, pips: *mut *mut PipId) -> u64; + fn npnr_context_get_pip_location(ctx: *const Context, pip: PipId) -> Loc; fn npnr_context_check(ctx: *const Context); fn npnr_context_debug(ctx: *const Context) -> bool; @@ -269,57 +291,25 @@ extern "C" { n: u32, ) -> WireId; - fn npnr_context_nets_leak(ctx: *const Context, names: *mut *mut libc::c_int, nets: *mut *mut *mut NetInfo) -> u32; + fn npnr_context_nets_leak( + ctx: *const Context, + names: *mut *mut libc::c_int, + nets: *mut *mut *mut NetInfo, + ) -> u32; fn npnr_netinfo_driver(net: *mut NetInfo) -> *mut PortRef; fn npnr_netinfo_users_leak(net: *mut NetInfo, users: *mut *mut *mut PortRef) -> u32; fn npnr_netinfo_is_global(net: *const NetInfo) -> bool; fn npnr_portref_cell(port: *const PortRef) -> *mut CellInfo; - fn npnr_cellinfo_get_location_x(info: *const CellInfo) -> libc::c_int; - fn npnr_cellinfo_get_location_y(info: *const CellInfo) -> libc::c_int; -} - -/// Store for the users of a net. -pub struct NetUsers<'a> { - users: &'a [*mut PortRef], -} - -impl<'a> NetUsers<'a> { - pub fn new(net: &'a mut NetInfo) -> NetUsers<'a> { - let mut users = std::ptr::null_mut(); - // SAFETY: net is not null because it's a &mut, and users is only written to. - // Leaking memory is the most convenient FFI I could think of. - let len = unsafe { npnr_netinfo_users_leak(net, &mut users as *mut *mut *mut PortRef) }; - let users = unsafe { slice::from_raw_parts(users, len as usize) }; - Self { users } - } - - pub fn iter(&self) -> NetUsersIter<'_> { - NetUsersIter { users: self, n: 0 } - } -} - -pub struct NetUsersIter<'a> { - users: &'a NetUsers<'a>, - n: usize, -} - -impl Iterator for NetUsersIter<'_> { - type Item = *mut PortRef; - - fn next(&mut self) -> Option { - let user = *self.users.users.get(self.n)?; - self.n += 1; - Some(user) - } + fn npnr_cellinfo_get_location(info: *const CellInfo) -> Loc; } /// Store for the nets of a context. pub struct Nets<'a> { nets: HashMap, - users: HashMap>, - _data: PhantomData<&'a Context> + users: HashMap, + _data: PhantomData<&'a Context>, } impl<'a> Nets<'a> { @@ -329,14 +319,26 @@ impl<'a> Nets<'a> { pub fn new(ctx: &'a Context) -> Nets<'a> { let mut names: *mut libc::c_int = std::ptr::null_mut(); let mut nets_ptr: *mut *mut NetInfo = std::ptr::null_mut(); - let size = unsafe { npnr_context_nets_leak(ctx, &mut names as *mut *mut libc::c_int, &mut nets_ptr as *mut *mut *mut NetInfo) }; + let size = unsafe { + npnr_context_nets_leak( + ctx, + &mut names as *mut *mut libc::c_int, + &mut nets_ptr as *mut *mut *mut NetInfo, + ) + }; let mut nets = HashMap::new(); let mut users = HashMap::new(); for i in 0..size { let name = unsafe { IdString(*names.add(i as usize)) }; let net = unsafe { *nets_ptr.add(i as usize) }; + let mut users_ptr = std::ptr::null_mut(); + // SAFETY: net is not null because it's a &mut, and users is only written to. + // Leaking memory is the most convenient FFI I could think of. + let len = + unsafe { npnr_netinfo_users_leak(net, &mut users_ptr as *mut *mut *mut PortRef) }; + let users_slice = unsafe { slice::from_raw_parts(users_ptr, len as usize) }; nets.insert(name, net); - users.insert(name, NetUsers::new(unsafe { &mut *net })); + users.insert(name, users_slice); } // Note: the contents of `names` and `nets_ptr` are now lost. Self { @@ -347,7 +349,7 @@ impl<'a> Nets<'a> { } /// Find net users given a net's name. - pub fn users_by_name(&self, net: IdString) -> Option<&NetUsers> { + pub fn users_by_name(&self, net: IdString) -> Option<&&[*mut PortRef]> { self.users.get(&net) } @@ -356,7 +358,7 @@ impl<'a> Nets<'a> { self.nets.len() } - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> std::collections::hash_map::Iter<'_, IdString, *mut NetInfo> { self.nets.iter() } } @@ -382,27 +384,6 @@ impl<'a> Iterator for NetSinkWireIter<'a> { } } -pub struct Wires<'a> { - wires: &'a [WireId], -} - -impl<'a> Wires<'a> { - pub fn new(ctx: &'a Context) -> Wires<'a> { - let mut wires = std::ptr::null_mut(); - let len = unsafe { npnr_context_get_wires_leak(ctx, &mut wires as *mut *mut WireId) }; - let wires = unsafe { std::slice::from_raw_parts(wires, len as usize) }; - Self { wires } - } - - pub fn len(&self) -> usize { - self.wires.len() - } - - pub fn iter(&self) -> impl Iterator { - self.wires.iter() - } -} - macro_rules! log_info { ($($t:tt)*) => { let s = std::ffi::CString::new(format!($($t)*)).unwrap();