2022-11-29 11:08:54 +08:00
|
|
|
#![feature(c_unwind)]
|
|
|
|
|
2022-11-27 23:12:39 +08:00
|
|
|
use std::{ptr::NonNull, time::Instant};
|
2022-11-25 13:34:46 +08:00
|
|
|
|
|
|
|
use colored::Colorize;
|
2022-11-30 09:20:14 +08:00
|
|
|
use indicatif::MultiProgress;
|
|
|
|
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
|
2022-11-20 11:20:09 +08:00
|
|
|
|
2022-11-28 03:27:33 +08:00
|
|
|
use crate::partition::Coord;
|
|
|
|
|
2022-11-20 11:20:09 +08:00
|
|
|
#[macro_use]
|
|
|
|
mod npnr;
|
2022-11-27 23:12:39 +08:00
|
|
|
mod partition;
|
2022-11-27 23:28:59 +08:00
|
|
|
mod route;
|
2022-11-21 10:48:00 +08:00
|
|
|
|
2022-11-20 11:20:09 +08:00
|
|
|
#[no_mangle]
|
2022-12-04 22:41:32 +08:00
|
|
|
pub extern "C-unwind" fn npnr_router_awooter(
|
|
|
|
ctx: Option<NonNull<npnr::Context>>,
|
|
|
|
pressure: f32,
|
|
|
|
history: f32,
|
|
|
|
) -> bool {
|
2022-11-29 11:08:54 +08:00
|
|
|
let ctx: &mut npnr::Context = unsafe { ctx.expect("non-null context").as_mut() };
|
2022-12-04 13:00:37 +08:00
|
|
|
route(ctx, pressure, history)
|
2022-11-29 11:08:54 +08:00
|
|
|
|
|
|
|
/*std::panic::catch_unwind(move || {
|
2022-11-20 11:20:09 +08:00
|
|
|
let ctx: &mut npnr::Context = unsafe { ctx.expect("non-null context").as_mut() };
|
|
|
|
route(ctx)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|x| {
|
|
|
|
if let Ok(x) = x.downcast::<String>() {
|
|
|
|
log_error!("caught panic: {}", x);
|
|
|
|
}
|
|
|
|
false
|
2022-11-29 11:08:54 +08:00
|
|
|
})*/
|
2022-11-20 11:20:09 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:15:04 +08:00
|
|
|
fn extract_arcs_from_nets(ctx: &npnr::Context, nets: &npnr::Nets) -> Vec<route::Arc> {
|
2022-11-27 21:17:25 +08:00
|
|
|
let mut arcs = vec![];
|
2022-11-29 05:15:04 +08:00
|
|
|
for (name, net) in nets.to_vec().iter() {
|
2022-11-27 21:17:25 +08:00
|
|
|
let net = unsafe { net.as_mut().unwrap() };
|
2022-12-03 14:26:15 +08:00
|
|
|
let str = ctx.name_of(**name).to_str().unwrap().to_string();
|
|
|
|
let verbose = false; //str == "soc0.processor.with_fpu.fpu_0.fpu_multiply_0.rin_CCU2C_S0_4$CCU2_FCI_INT";
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
dbg!(str, net.is_global());
|
|
|
|
}
|
|
|
|
|
2022-11-27 21:17:25 +08:00
|
|
|
if net.is_global() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let port_ref = net.driver();
|
|
|
|
let port_ref = unsafe { port_ref.as_ref().unwrap() };
|
|
|
|
if let Some(cell) = port_ref.cell() {
|
|
|
|
let source = cell.location();
|
|
|
|
let source_wire = ctx.source_wire(net);
|
|
|
|
|
2022-11-29 05:15:04 +08:00
|
|
|
for sink_ref in nets.users_by_name(**name).unwrap().iter() {
|
2022-11-27 21:17:25 +08:00
|
|
|
let sink = sink_ref.cell().unwrap();
|
|
|
|
let sink = sink.location();
|
|
|
|
for sink_wire in ctx.sink_wires(net, *sink_ref) {
|
2022-11-28 03:47:16 +08:00
|
|
|
arcs.push(route::Arc::new(
|
|
|
|
source_wire,
|
|
|
|
sink_wire,
|
|
|
|
net.index(),
|
2022-12-15 00:31:01 +08:00
|
|
|
nets.name_from_index(net.index()),
|
2022-12-03 14:26:15 +08:00
|
|
|
));
|
|
|
|
|
|
|
|
if verbose {
|
|
|
|
let source_wire = ctx.name_of_wire(source_wire).to_str().unwrap();
|
|
|
|
let sink_wire = ctx.name_of_wire(sink_wire).to_str().unwrap();
|
|
|
|
dbg!(source_wire, sink_wire, net.index().into_inner());
|
|
|
|
}
|
2022-11-27 21:17:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arcs
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:00:37 +08:00
|
|
|
fn route(ctx: &mut npnr::Context, pressure: f32, history: f32) -> bool {
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!(
|
|
|
|
"{}{}{}{}{}{} from Rust!\n",
|
|
|
|
"A".red(),
|
|
|
|
"w".green(),
|
|
|
|
"o".yellow(),
|
|
|
|
"o".blue(),
|
|
|
|
"o".magenta(),
|
|
|
|
"o".cyan()
|
|
|
|
);
|
2022-11-20 11:20:09 +08:00
|
|
|
log_info!(
|
|
|
|
"Running on a {}x{} grid\n",
|
2022-11-26 20:44:17 +08:00
|
|
|
ctx.grid_dim_x().to_string().bold(),
|
|
|
|
ctx.grid_dim_y().to_string().bold(),
|
2022-11-20 11:20:09 +08:00
|
|
|
);
|
2022-11-21 10:48:00 +08:00
|
|
|
|
2022-11-25 13:34:46 +08:00
|
|
|
let wires = ctx.wires_leaking();
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!("Found {} wires\n", wires.len().to_string().bold());
|
2022-11-25 13:34:46 +08:00
|
|
|
|
|
|
|
let pips = ctx.pips_leaking();
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!("Found {} pips\n", pips.len().to_string().bold());
|
2022-11-24 07:55:33 +08:00
|
|
|
|
2022-11-24 07:02:30 +08:00
|
|
|
let nets = npnr::Nets::new(ctx);
|
2022-11-25 13:34:46 +08:00
|
|
|
let nets_str = nets.len().to_string();
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!("Found {} nets\n", nets_str.bold());
|
2022-11-21 10:48:00 +08:00
|
|
|
|
2022-11-23 11:31:50 +08:00
|
|
|
let mut count = 0;
|
2022-11-29 05:15:04 +08:00
|
|
|
for (&name, net) in nets.to_vec().iter() {
|
|
|
|
let _src = ctx.source_wire(**net);
|
2022-11-23 11:31:50 +08:00
|
|
|
let net = unsafe { net.as_mut().unwrap() };
|
2022-11-29 05:15:04 +08:00
|
|
|
let users = nets.users_by_name(name).unwrap().iter();
|
2022-11-24 07:02:30 +08:00
|
|
|
for user in users {
|
2022-11-27 01:09:20 +08:00
|
|
|
count += ctx.sink_wires(net, *user).len();
|
2022-11-23 11:31:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!("Found {} arcs\n", count.to_string().bold());
|
2022-11-23 11:31:50 +08:00
|
|
|
|
2022-11-29 09:55:24 +08:00
|
|
|
let binding = nets.to_vec();
|
2022-11-29 05:15:04 +08:00
|
|
|
let (name, net) = binding
|
2022-11-23 11:31:50 +08:00
|
|
|
.iter()
|
2022-11-24 07:02:30 +08:00
|
|
|
.max_by_key(|(name, net)| {
|
2022-11-23 11:31:50 +08:00
|
|
|
let net = unsafe { net.as_mut().unwrap() };
|
|
|
|
if net.is_global() {
|
|
|
|
0
|
|
|
|
} else {
|
2022-11-24 07:02:30 +08:00
|
|
|
nets.users_by_name(**name)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2022-11-27 01:09:20 +08:00
|
|
|
.fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).len())
|
2022-11-23 11:31:50 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let net = unsafe { net.as_mut().unwrap() };
|
2022-11-24 07:02:30 +08:00
|
|
|
let count = nets
|
2022-11-29 05:15:04 +08:00
|
|
|
.users_by_name(**name)
|
2022-11-24 07:02:30 +08:00
|
|
|
.unwrap()
|
|
|
|
.iter()
|
2022-11-27 01:09:20 +08:00
|
|
|
.fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).len())
|
2022-11-25 13:34:46 +08:00
|
|
|
.to_string();
|
2022-11-23 11:31:50 +08:00
|
|
|
|
|
|
|
log_info!(
|
2022-11-26 20:44:17 +08:00
|
|
|
"Highest non-global fanout net is {}\n",
|
2022-11-29 05:15:04 +08:00
|
|
|
ctx.name_of(**name).to_str().unwrap().bold()
|
2022-11-23 11:31:50 +08:00
|
|
|
);
|
2022-11-26 20:44:17 +08:00
|
|
|
log_info!(" with {} arcs\n", count.bold());
|
2022-11-23 11:31:50 +08:00
|
|
|
|
|
|
|
let mut x0 = 0;
|
|
|
|
let mut y0 = 0;
|
|
|
|
let mut x1 = 0;
|
|
|
|
let mut y1 = 0;
|
|
|
|
|
2022-11-29 05:15:04 +08:00
|
|
|
for sink in nets.users_by_name(**name).unwrap().iter() {
|
2022-11-25 13:34:46 +08:00
|
|
|
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);
|
2022-11-23 11:31:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:34:46 +08:00
|
|
|
let coords_min = format!("({}, {})", x0, y0);
|
|
|
|
let coords_max = format!("({}, {})", x1, y1);
|
|
|
|
log_info!(
|
2022-11-25 15:12:14 +08:00
|
|
|
" which spans from {} to {}\n",
|
2022-11-26 20:44:17 +08:00
|
|
|
coords_min.bold(),
|
|
|
|
coords_max.bold()
|
2022-11-25 13:34:46 +08:00
|
|
|
);
|
2022-11-23 11:31:50 +08:00
|
|
|
|
2022-11-27 06:08:20 +08:00
|
|
|
log_info!(
|
|
|
|
"rayon reports {} threads available\n",
|
|
|
|
rayon::current_num_threads().to_string().bold()
|
|
|
|
);
|
2022-11-27 02:57:53 +08:00
|
|
|
|
2022-11-27 06:16:28 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
|
2022-11-29 05:15:04 +08:00
|
|
|
let arcs = extract_arcs_from_nets(ctx, &nets);
|
2022-11-27 21:17:25 +08:00
|
|
|
|
2022-12-02 21:57:24 +08:00
|
|
|
let mut special_arcs = vec![];
|
|
|
|
let mut partitionable_arcs = Vec::with_capacity(arcs.len());
|
|
|
|
for arc in arcs {
|
2022-12-15 00:31:01 +08:00
|
|
|
let src_name = ctx.name_of_wire(arc.source_wire()).to_str().unwrap();
|
|
|
|
let dst_name = ctx.name_of_wire(arc.sink_wire()).to_str().unwrap();
|
2022-12-02 21:57:24 +08:00
|
|
|
|
|
|
|
if src_name.contains("FCO_SLICE")
|
2022-12-11 07:10:43 +08:00
|
|
|
|| src_name.contains("Q6_SLICE")
|
2022-12-02 21:57:24 +08:00
|
|
|
|| src_name.contains('J')
|
|
|
|
|| src_name.contains("DDR")
|
|
|
|
|| dst_name.contains("DDR")
|
2022-12-11 07:10:43 +08:00
|
|
|
|| dst_name.contains("X126/Y20/PADDOD_PIO")
|
2022-12-02 21:57:24 +08:00
|
|
|
{
|
|
|
|
special_arcs.push(arc);
|
|
|
|
} else {
|
|
|
|
partitionable_arcs.push(arc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_info!(
|
|
|
|
" {} arcs special-cased\n",
|
|
|
|
special_arcs.len().to_string().bold()
|
|
|
|
);
|
|
|
|
|
2022-12-06 14:39:30 +08:00
|
|
|
let mut partitions = vec![(
|
|
|
|
Coord::new(0, 0),
|
|
|
|
Coord::new(ctx.grid_dim_x(), ctx.grid_dim_y()),
|
|
|
|
partitionable_arcs,
|
|
|
|
String::from(""),
|
|
|
|
)];
|
|
|
|
|
|
|
|
for _ in 0..2 {
|
|
|
|
let mut new_partitions = Vec::with_capacity(partitions.len() * 4);
|
|
|
|
for (min, max, partition, name) in &partitions {
|
2022-12-11 07:10:43 +08:00
|
|
|
log_info!("partition {}:\n", name);
|
2022-12-11 21:09:24 +08:00
|
|
|
let (x_part, y_part, ne, se, sw, nw, special) =
|
|
|
|
partition::find_partition_point_and_sanity_check(
|
|
|
|
ctx, &nets, partition, pips, min.x, max.x, min.y, max.y,
|
|
|
|
);
|
|
|
|
special_arcs.extend(special.into_iter());
|
2022-12-06 14:39:30 +08:00
|
|
|
new_partitions.push((
|
2022-12-11 07:10:43 +08:00
|
|
|
Coord::new(x_part, min.y),
|
|
|
|
Coord::new(max.x, y_part),
|
2022-12-06 14:39:30 +08:00
|
|
|
se,
|
|
|
|
format!("{}_SE", name),
|
|
|
|
));
|
2022-12-11 07:10:43 +08:00
|
|
|
new_partitions.push((Coord::new(x_part, y_part), *max, sw, format!("{}_SW", name)));
|
2022-12-11 21:09:24 +08:00
|
|
|
new_partitions.push((*min, Coord::new(x_part, y_part), ne, format!("{}_NE", name)));
|
2022-12-06 14:39:30 +08:00
|
|
|
new_partitions.push((
|
2022-12-11 07:10:43 +08:00
|
|
|
Coord::new(min.x, y_part),
|
|
|
|
Coord::new(x_part, max.y),
|
2022-12-06 14:39:30 +08:00
|
|
|
nw,
|
|
|
|
format!("{}_NW", name),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
partitions = new_partitions;
|
|
|
|
}
|
2022-11-26 23:35:31 +08:00
|
|
|
|
2022-12-02 19:48:05 +08:00
|
|
|
let time = format!("{:.2}", (Instant::now() - start).as_secs_f32());
|
|
|
|
log_info!("Partitioning took {}s\n", time.bold());
|
2022-11-27 06:16:28 +08:00
|
|
|
|
2022-12-11 21:09:24 +08:00
|
|
|
log_info!(
|
|
|
|
"now {} arcs special-cased\n",
|
|
|
|
special_arcs.len().to_string().bold()
|
|
|
|
);
|
|
|
|
|
2022-12-04 22:41:32 +08:00
|
|
|
log_info!(
|
|
|
|
"Using pressure factor {} and history factor {}\n",
|
|
|
|
pressure,
|
|
|
|
history
|
|
|
|
);
|
2022-12-04 13:00:37 +08:00
|
|
|
|
2022-11-29 13:34:07 +08:00
|
|
|
let start = Instant::now();
|
|
|
|
|
2022-11-30 09:20:14 +08:00
|
|
|
log_info!("Routing partitioned arcs\n");
|
|
|
|
|
|
|
|
let progress = MultiProgress::new();
|
|
|
|
|
2022-12-15 00:31:01 +08:00
|
|
|
let mut router = route::Router::new(&nets, wires, pressure, history);
|
2022-12-04 22:41:32 +08:00
|
|
|
partitions
|
|
|
|
.par_iter()
|
|
|
|
.for_each(|(box_ne, box_sw, arcs, id)| {
|
2022-12-15 00:31:01 +08:00
|
|
|
let thread = route::RouterThread::new(*box_ne, *box_sw, arcs, id, &progress);
|
|
|
|
router.route(ctx, &nets, &thread);
|
2022-12-04 22:41:32 +08:00
|
|
|
});
|
2022-11-30 09:20:14 +08:00
|
|
|
|
2022-11-29 13:34:07 +08:00
|
|
|
log_info!("Routing miscellaneous arcs\n");
|
2022-12-15 00:31:01 +08:00
|
|
|
let thread = route::RouterThread::new(
|
2022-12-02 03:29:24 +08:00
|
|
|
Coord::new(0, 0),
|
|
|
|
Coord::new(ctx.grid_dim_x(), ctx.grid_dim_y()),
|
2022-12-15 00:31:01 +08:00
|
|
|
&special_arcs,
|
|
|
|
"MISC",
|
|
|
|
&progress,
|
2022-12-02 03:29:24 +08:00
|
|
|
);
|
2022-12-15 00:31:01 +08:00
|
|
|
|
|
|
|
router.route(ctx, &nets, &thread);
|
2022-11-29 13:34:07 +08:00
|
|
|
|
2022-12-02 19:48:05 +08:00
|
|
|
let time = format!("{:.2}", (Instant::now() - start).as_secs_f32());
|
|
|
|
log_info!("Routing took {}s\n", time.bold());
|
2022-11-29 13:34:07 +08:00
|
|
|
|
2022-11-29 11:08:54 +08:00
|
|
|
//let mut router = route::Router::new(Coord::new(0, 0), Coord::new(x_part, y_part));
|
2022-11-28 03:27:33 +08:00
|
|
|
|
2022-11-23 11:31:50 +08:00
|
|
|
/*log_info!("=== level 2 NE:\n");
|
|
|
|
let _ = find_partition_point(&ne, x_start, x, y_start, y);
|
|
|
|
log_info!("=== level 2 SE:\n");
|
|
|
|
let _ = find_partition_point(&se, x, x_finish, y_start, y);
|
|
|
|
log_info!("=== level 2 SW:\n");
|
|
|
|
let _ = find_partition_point(&sw, x, x_finish, y, y_finish);
|
|
|
|
log_info!("=== level 2 NW:\n");
|
|
|
|
let _ = find_partition_point(&nw, x_start, x, y, y_finish);*/
|
|
|
|
|
2022-11-20 11:20:09 +08:00
|
|
|
true
|
2022-11-23 11:31:50 +08:00
|
|
|
}
|