awooter: parallelise partitioning

This commit is contained in:
Lofty 2022-11-26 17:09:20 +00:00
parent ac43ddbcc5
commit 8276bf255d
2 changed files with 197 additions and 185 deletions

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, ptr::NonNull}; use std::{collections::HashMap, ptr::NonNull, sync::{atomic::AtomicUsize, Mutex}};
use colored::Colorize; use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
@ -117,17 +117,13 @@ fn find_partition_point(
/// finds the y location a line would be split at if you split it at a certain x location /// finds the y location a line would be split at if you split it at a certain x location
/// ///
/// the function assumes the line goes on forever in both directions, and it truncates the actual coordinate /// the function assumes the line goes on forever in both directions, and it truncates the actual coordinate
fn split_line_over_x(mut line: (npnr::Loc, npnr::Loc), x_location: i32) -> i32 { fn split_line_over_x(line: (npnr::Loc, npnr::Loc), x_location: i32) -> i32 {
if line.0.x == line.0.y { if line.0.x == line.0.y {
// the line is a straight line in the direction, there is either infinite solutions, or none // the line is a straight line in the direction, there is either infinite solutions, or none
// we simply average the y coordinate to give a "best effort" guess // we simply average the y coordinate to give a "best effort" guess
return (line.0.y + line.1.y) / 2; return (line.0.y + line.1.y) / 2;
} }
if line.0.x > line.1.x {
(line.0, line.1) = (line.1, line.0);
}
let x_diff = line.0.x - line.1.x; let x_diff = line.0.x - line.1.x;
let y_diff = line.0.y - line.1.y; let y_diff = line.0.y - line.1.y;
@ -180,9 +176,9 @@ fn partition_nets(
let mut se = Vec::new(); let mut se = Vec::new();
let mut sw = Vec::new(); let mut sw = Vec::new();
let mut nw = Vec::new(); let mut nw = Vec::new();
let mut part_horiz = 0; let mut part_horiz = AtomicUsize::new(0);
let mut part_vert = 0; let mut part_vert = AtomicUsize::new(0);
let mut part_diag = 0; let mut part_diag = AtomicUsize::new(0);
let x_str = format!("X = {}", x); let x_str = format!("X = {}", x);
let y_str = format!("Y = {}", y); let y_str = format!("Y = {}", y);
@ -213,8 +209,9 @@ fn partition_nets(
continue; continue;
} }
if dir.x < 0 && dir.y == 0 && loc.y == y {
candidates += 1; candidates += 1;
if dir.x < 0 {
north += 1; north += 1;
pips_n pips_n
.entry((loc.x, loc.y)) .entry((loc.x, loc.y))
@ -224,8 +221,7 @@ fn partition_nets(
.or_insert_with(|| vec![(pip, Vec::new())]); .or_insert_with(|| vec![(pip, Vec::new())]);
} }
if dir.x > 0 && dir.y == 0 && loc.y == y { if dir.x > 0 {
candidates += 1;
south += 1; south += 1;
pips_s pips_s
.entry((loc.x, loc.y)) .entry((loc.x, loc.y))
@ -235,8 +231,7 @@ fn partition_nets(
.or_insert_with(|| vec![(pip, Vec::new())]); .or_insert_with(|| vec![(pip, Vec::new())]);
} }
if dir.x == 0 && dir.y < 0 && loc.x == x { if dir.y < 0 {
candidates += 1;
east += 1; east += 1;
pips_e pips_e
.entry((loc.x, loc.y)) .entry((loc.x, loc.y))
@ -246,8 +241,7 @@ fn partition_nets(
.or_insert_with(|| vec![(pip, Vec::new())]); .or_insert_with(|| vec![(pip, Vec::new())]);
} }
if dir.x == 0 && dir.y > 0 && loc.x == x { if dir.y > 0 {
candidates += 1;
west += 1; west += 1;
pips_w pips_w
.entry((loc.x, loc.y)) .entry((loc.x, loc.y))
@ -265,6 +259,11 @@ fn partition_nets(
log_info!(" {} are south-bound\n", south.to_string().bold()); log_info!(" {} are south-bound\n", south.to_string().bold());
log_info!(" {} are west-bound\n", west.to_string().bold()); log_info!(" {} are west-bound\n", west.to_string().bold());
let pips_n = Mutex::new(pips_n);
let pips_e = Mutex::new(pips_e);
let pips_s = Mutex::new(pips_s);
let pips_w = Mutex::new(pips_w);
let progress = ProgressBar::new(nets.len() as u64); let progress = ProgressBar::new(nets.len() as u64);
progress.set_style( progress.set_style(
ProgressStyle::with_template("[{elapsed}] [{bar:40.cyan/blue}] {msg}") ProgressStyle::with_template("[{elapsed}] [{bar:40.cyan/blue}] {msg}")
@ -272,7 +271,7 @@ fn partition_nets(
.progress_chars("━╸ "), .progress_chars("━╸ "),
); );
let mut explored_pips = 0; let mut explored_pips = AtomicUsize::new(0);
for (name, net) in nets.iter() { for (name, net) in nets.iter() {
let mut message = ctx.name_of(*name).to_str().unwrap().to_string(); let mut message = ctx.name_of(*name).to_str().unwrap().to_string();
@ -305,13 +304,13 @@ fn partition_nets(
// but doing so gives lifetime errors, and you can't describe // but doing so gives lifetime errors, and you can't describe
// lifetimes in a closure, as far as I can tell. // lifetimes in a closure, as far as I can tell.
for sink in nets.users_by_name(*name).unwrap().iter() { let arcs = nets.users_by_name(*name).unwrap().iter().flat_map(|sink| {
let sink = unsafe { sink.as_ref().unwrap() }; let sink = unsafe { sink.as_ref().unwrap() };
ctx.sink_wires(net, sink).into_iter().map(move |sink_wire| (sink, sink_wire))
}).flat_map(|(sink, sink_wire)| {
let sink_loc = sink.cell().unwrap().location(); let sink_loc = sink.cell().unwrap().location();
let sink_is_north = sink_loc.x < x; let sink_is_north = sink_loc.x < x;
let sink_is_east = sink_loc.y < y; let sink_is_east = sink_loc.y < y;
let arcs = ctx.sink_wires(net, sink).into_iter().flat_map(|sink_wire| {
if source_is_north == sink_is_north && source_is_east == sink_is_east { 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)); let arc = ((source.x, source.y), (sink_loc.x, sink_loc.y));
let seg = match (source_is_north, source_is_east) { let seg = match (source_is_north, source_is_east) {
@ -323,13 +322,15 @@ fn partition_nets(
vec![(seg, arc)] vec![(seg, arc)]
} else if source_is_north != sink_is_north && source_is_east == sink_is_east { } else if source_is_north != sink_is_north && source_is_east == sink_is_east {
let middle = (x, (source.y + sink_loc.y) / 2); let middle = (x, (source.y + sink_loc.y) / 2);
let mut pips_s = pips_s.lock().unwrap();
let mut pips_n = pips_n.lock().unwrap();
let pips = match source_is_north { let pips = match source_is_north {
true => pips_s.get_mut(&middle).unwrap(), true => pips_s.get_mut(&middle).unwrap(),
false => pips_n.get_mut(&middle).unwrap(), false => pips_n.get_mut(&middle).unwrap(),
}; };
let (selected_pip, pip_uses) = pips let (selected_pip, pip_uses) = pips
.par_iter_mut() .iter_mut()
.min_by_key(|(pip, uses)| { .min_by_key(|(pip, uses)| {
let src_to_pip = let src_to_pip =
ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip));
@ -340,7 +341,8 @@ fn partition_nets(
.unwrap(); .unwrap();
pip_uses.push(*name); pip_uses.push(*name);
let selected_pip = *selected_pip; let selected_pip = *selected_pip;
explored_pips += pips.len(); explored_pips.fetch_add(pips.len(), std::sync::atomic::Ordering::SeqCst);
explored_pips.fetch_add(pips.len(), std::sync::atomic::Ordering::SeqCst);
let pip_loc = ctx.pip_location(selected_pip); let pip_loc = ctx.pip_location(selected_pip);
let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y)); let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y));
@ -351,17 +353,19 @@ fn partition_nets(
(false, true) => (Segment::Southeast, Segment::Northeast), (false, true) => (Segment::Southeast, Segment::Northeast),
(false, false) => (Segment::Southwest, Segment::Northwest), (false, false) => (Segment::Southwest, Segment::Northwest),
}; };
part_horiz += 1; part_horiz.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
vec![(seg1, src_to_pip), (seg2, pip_to_dst)] vec![(seg1, src_to_pip), (seg2, pip_to_dst)]
} else if source_is_north == sink_is_north && source_is_east != sink_is_east { } else if source_is_north == sink_is_north && source_is_east != sink_is_east {
let middle = ((source.x + sink_loc.x) / 2, y); let middle = ((source.x + sink_loc.x) / 2, y);
let mut pips_e = pips_e.lock().unwrap();
let mut pips_w = pips_w.lock().unwrap();
let pips = match source_is_east { let pips = match source_is_east {
true => pips_w.get_mut(&middle).unwrap(), true => pips_w.get_mut(&middle).unwrap(),
false => pips_e.get_mut(&middle).unwrap(), false => pips_e.get_mut(&middle).unwrap(),
}; };
let (selected_pip, pip_uses) = pips let (selected_pip, pip_uses) = pips
.par_iter_mut() .iter_mut()
.min_by_key(|(pip, uses)| { .min_by_key(|(pip, uses)| {
let src_to_pip = let src_to_pip =
ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip));
@ -372,7 +376,7 @@ fn partition_nets(
.unwrap(); .unwrap();
pip_uses.push(*name); pip_uses.push(*name);
let selected_pip = *selected_pip; let selected_pip = *selected_pip;
explored_pips += pips.len(); explored_pips.fetch_add(pips.len(), std::sync::atomic::Ordering::SeqCst);
let pip_loc = ctx.pip_location(selected_pip); let pip_loc = ctx.pip_location(selected_pip);
let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y)); let src_to_pip = ((source.x, source.y), (pip_loc.x, pip_loc.y));
@ -383,17 +387,19 @@ fn partition_nets(
(false, true) => (Segment::Southeast, Segment::Southwest), (false, true) => (Segment::Southeast, Segment::Southwest),
(false, false) => (Segment::Southwest, Segment::Southeast), (false, false) => (Segment::Southwest, Segment::Southeast),
}; };
part_vert += 1; part_vert.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
vec![(seg1, src_to_pip), (seg2, pip_to_dst)] vec![(seg1, src_to_pip), (seg2, pip_to_dst)]
} else { } else {
let middle = (x, split_line_over_x((source, sink_loc), x)); let middle = (x, split_line_over_x((source, sink_loc), x));
let mut pips_e = pips_e.lock().unwrap();
let mut pips_w = pips_w.lock().unwrap();
let pips = match source_is_east { let pips = match source_is_east {
true => pips_w.get_mut(&middle).unwrap(), true => pips_w.get_mut(&middle).unwrap(),
false => pips_e.get_mut(&middle).unwrap(), false => pips_e.get_mut(&middle).unwrap(),
}; };
let (horiz_pip, pip_uses) = pips let (horiz_pip, pip_uses) = pips
.par_iter_mut() .iter_mut()
.min_by_key(|(pip, uses)| { .min_by_key(|(pip, uses)| {
let src_to_pip = let src_to_pip =
ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip));
@ -404,16 +410,18 @@ fn partition_nets(
.unwrap(); .unwrap();
pip_uses.push(*name); pip_uses.push(*name);
let horiz_pip = *horiz_pip; let horiz_pip = *horiz_pip;
explored_pips += pips.len(); explored_pips.fetch_add(pips.len(), std::sync::atomic::Ordering::SeqCst);
let middle = (split_line_over_y((source, sink_loc), y), y); let middle = (split_line_over_y((source, sink_loc), y), y);
let mut pips_s = pips_s.lock().unwrap();
let mut pips_n = pips_n.lock().unwrap();
let pips = match source_is_north { let pips = match source_is_north {
true => pips_s.get_mut(&middle).unwrap(), true => pips_s.get_mut(&middle).unwrap(),
false => pips_n.get_mut(&middle).unwrap(), false => pips_n.get_mut(&middle).unwrap(),
}; };
let (vert_pip, pip_uses) = pips let (vert_pip, pip_uses) = pips
.par_iter_mut() .iter_mut()
.min_by_key(|(pip, uses)| { .min_by_key(|(pip, uses)| {
let src_to_pip = let src_to_pip =
ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip)); ctx.estimate_delay(source_wire, ctx.pip_src_wire(*pip));
@ -424,7 +432,7 @@ fn partition_nets(
.unwrap(); .unwrap();
pip_uses.push(*name); pip_uses.push(*name);
let vert_pip = *vert_pip; let vert_pip = *vert_pip;
explored_pips += pips.len(); explored_pips.fetch_add(pips.len(), std::sync::atomic::Ordering::SeqCst);
let horiz_loc = ctx.pip_location(horiz_pip); let horiz_loc = ctx.pip_location(horiz_pip);
let horiz_is_east = horiz_loc.y < y; let horiz_is_east = horiz_loc.y < y;
@ -451,12 +459,12 @@ fn partition_nets(
(false, true, true) => (Segment::Southeast, Segment::Northeast, Segment::Northwest), (false, true, true) => (Segment::Southeast, Segment::Northeast, Segment::Northwest),
(false, true, false) => (Segment::Southeast, Segment::Southwest, Segment::Northwest), (false, true, false) => (Segment::Southeast, Segment::Southwest, Segment::Northwest),
(false, false, true) => (Segment::Southwest, Segment::Southeast, Segment::Northeast), (false, false, true) => (Segment::Southwest, Segment::Southeast, Segment::Northeast),
(false, false, false) => (Segment::Southwest, Segment::Northwest, Segment::Northwest), (false, false, false) => (Segment::Southwest, Segment::Northwest, Segment::Northeast),
}; };
part_diag += 1; part_diag.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
vec![(seg1, src_to_mid1), (seg2, mid1_to_mid2), (seg3, mid2_to_dst)] vec![(seg1, src_to_mid1), (seg2, mid1_to_mid2), (seg3, mid2_to_dst)]
} }
}); }).collect::<Vec<_>>();
for (segment, arc) in arcs { for (segment, arc) in arcs {
match segment { match segment {
@ -467,11 +475,10 @@ fn partition_nets(
} }
} }
} }
}
progress.finish_and_clear(); progress.finish_and_clear();
log_info!(" {} pips explored\n", explored_pips.to_string().bold()); log_info!(" {} pips explored\n", explored_pips.get_mut().to_string().bold());
let north = ne.len() + nw.len(); let north = ne.len() + nw.len();
let south = se.len() + sw.len(); let south = se.len() + sw.len();
@ -504,15 +511,15 @@ fn partition_nets(
log_info!( log_info!(
" {} arcs partitioned horizontally\n", " {} arcs partitioned horizontally\n",
part_horiz.to_string().bold() part_horiz.get_mut().to_string().bold()
); );
log_info!( log_info!(
" {} arcs partitioned vertically\n", " {} arcs partitioned vertically\n",
part_vert.to_string().bold() part_vert.get_mut().to_string().bold()
); );
log_info!( log_info!(
" {} arcs partitioned both ways\n", " {} arcs partitioned both ways\n",
part_diag.to_string().bold() part_diag.get_mut().to_string().bold()
); );
log_info!( log_info!(
" {} arcs in the northeast {}\n", " {} arcs in the northeast {}\n",
@ -594,7 +601,7 @@ fn route(ctx: &mut npnr::Context) -> bool {
let net = unsafe { net.as_mut().unwrap() }; let net = unsafe { net.as_mut().unwrap() };
let users = nets.users_by_name(*name).unwrap().iter(); let users = nets.users_by_name(*name).unwrap().iter();
for user in users { for user in users {
count += ctx.sink_wires(net, *user).count(); count += ctx.sink_wires(net, *user).len();
} }
} }
@ -610,7 +617,7 @@ fn route(ctx: &mut npnr::Context) -> bool {
nets.users_by_name(**name) nets.users_by_name(**name)
.unwrap() .unwrap()
.iter() .iter()
.fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).count()) .fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).len())
} }
}) })
.unwrap(); .unwrap();
@ -620,7 +627,7 @@ fn route(ctx: &mut npnr::Context) -> bool {
.users_by_name(*name) .users_by_name(*name)
.unwrap() .unwrap()
.iter() .iter()
.fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).count()) .fold(0, |acc, sink| acc + ctx.sink_wires(net, *sink).len())
.to_string(); .to_string();
log_info!( log_info!(
@ -689,7 +696,7 @@ fn route(ctx: &mut npnr::Context) -> bool {
.into_iter() .into_iter()
.all(|x| x == 0) .all(|x| x == 0)
{ {
println!("{}", "found no arcs crossing partition boundaries.".green()); log_info!("{}\n", "Found no arcs crossing partition boundaries.".green());
} else { } else {
println!("{}", "found arcs crossing partition boundaries!".yellow()); println!("{}", "found arcs crossing partition boundaries!".yellow());
println!("count in ne: {}", invalid_arcs_in_ne.to_string().bold()); println!("count in ne: {}", invalid_arcs_in_ne.to_string().bold());

View File

@ -191,13 +191,18 @@ impl Context {
unsafe { npnr_context_get_netinfo_source_wire(self, net) } unsafe { npnr_context_get_netinfo_source_wire(self, net) }
} }
pub fn sink_wires(&self, net: *const NetInfo, sink: *const PortRef) -> NetSinkWireIter { pub fn sink_wires(&self, net: *const NetInfo, sink: *const PortRef) -> Vec<WireId> {
NetSinkWireIter { let mut v = Vec::new();
ctx: self, let mut n = 0;
net, loop {
sink, let wire = unsafe { npnr_context_get_netinfo_sink_wire(self, net, sink, n) };
n: 0, if wire.is_null() {
break;
} }
n += 1;
v.push(wire);
}
v
} }
pub fn wires_leaking(&self) -> &[WireId] { pub fn wires_leaking(&self) -> &[WireId] {