awooter: partition experiment code

This commit is contained in:
Lofty 2022-11-23 03:31:50 +00:00
parent c821a68da7
commit 2662354aae
3 changed files with 448 additions and 33 deletions

View File

@ -110,6 +110,7 @@ extern "C" {
void npnr_log_error(const char *const format) { log_error("%s", format); }
uint64_t npnr_belid_null() { return wrap(BelId()); }
uint64_t npnr_wireid_null() { return wrap(WireId()); }
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(); }
@ -131,9 +132,20 @@ extern "C" {
const char *npnr_context_name_of(const Context *const ctx, IdString str) { return ctx->nameOf(str); }
bool npnr_context_verbose(const Context *const ctx) { return ctx->verbose; }
uint64_t npnr_context_get_netinfo_source_wire(const Context *const ctx, const NetInfo *const net) { return wrap(ctx->getNetinfoSourceWire(net)); }
uint64_t npnr_context_get_netinfo_sink_wire(const Context *const ctx, const NetInfo *const net, const PortRef *const sink, uint32_t n) {
if (ctx == nullptr) {
return 0;
}
return wrap(ctx->getNetinfoSinkWire(net, *sink, n));
}
// Yes, this is quadratic. It gets imported once and then never worried about again.
// There are bigger fish to fry.
int npnr_context_nets_key(Context *ctx, uint32_t n) {
int npnr_context_nets_key(const Context *const ctx, uint32_t n) {
if (ctx == nullptr) {
return 0;
}
for (auto& item : ctx->nets) {
if (n == 0) {
return item.first.hash();
@ -142,7 +154,10 @@ extern "C" {
}
return 0;
}
NetInfo* npnr_context_nets_value(Context *ctx, uint32_t n) {
NetInfo* npnr_context_nets_value(const Context *const ctx, uint32_t n) {
if (ctx == nullptr) {
return nullptr;
}
for (auto& item : ctx->nets) {
if (n == 0) {
return item.second.get();
@ -152,6 +167,36 @@ extern "C" {
return nullptr;
}
PortRef* npnr_netinfo_driver(NetInfo *const net) {
if (net == nullptr) {
return nullptr;
}
return &net->driver;
}
PortRef* npnr_netinfo_users_value(NetInfo *const net, uint32_t n) {
if (net == nullptr) {
return nullptr;
}
for (auto& item : net->users) {
if (n == 0) {
return &item;
}
n--;
}
return nullptr;
}
#ifdef ARCH_ECP5
bool npnr_netinfo_is_global(NetInfo *const net) { return net->is_global; }
#else
bool npnr_netinfo_is_global(NetInfo *const net) { return false; }
#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; }
extern bool npnr_router_awooter(Context *ctx);
}

View File

@ -10,12 +10,12 @@ enum Subpartition {
struct Partition {
parts: [Option<Subpartition>; 4],
borders: [[Vec<npnr::WireId>; 4]; 4]
borders: [[Vec<npnr::WireId>; 4]; 4],
}
struct Net {
source: npnr::WireId,
sinks: Vec<npnr::WireId>
sinks: Vec<npnr::WireId>,
}
#[no_mangle]
@ -32,18 +32,278 @@ pub extern "C" fn npnr_router_awooter(ctx: Option<NonNull<npnr::Context>>) -> 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,
x_start: i32,
x_finish: i32,
y_start: i32,
y_finish: i32,
) -> (i32, i32, ArcVec, ArcVec, ArcVec, ArcVec) {
let mut x = ((x_finish - x_start) / 2) + x_start;
let mut y = ((y_finish - y_start) / 2) + y_start;
let mut x_diff = (x_finish - x_start) / 4;
let mut y_diff = (y_finish - y_start) / 4;
let mut ne = Vec::new();
let mut se = Vec::new();
let mut sw = Vec::new();
let mut nw = Vec::new();
while x_diff != 0 {
(ne, se, sw, nw) = partition(arcs, x, y);
let north = ne.len() + nw.len();
let south = se.len() + sw.len();
if north > south {
x -= x_diff;
} else if north < south {
x += x_diff;
}
let east = ne.len() + se.len();
let west = nw.len() + sw.len();
if east > west {
y -= y_diff;
} else if east < west {
y += y_diff;
}
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);
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);
log_info!(
"Distortion: {:.02}%\n",
100.0 * (ne_dist + se_dist + sw_dist + nw_dist)
);
(x, y, ne, se, sw, nw)
}
fn route(ctx: &mut npnr::Context) -> bool {
log_info!("Hello from Rust!\n");
log_info!("Awoooo from Rust!\n");
log_info!(
"Running on a {}x{} grid\n",
ctx.grid_dim_x(),
ctx.grid_dim_y()
);
let _belid = npnr::BelId::null();
log_info!("Managed to survive BelId()\n");
let nets = npnr::NetIter::new(ctx).into_iter().collect::<Vec<_>>();
let nets = ctx.net_iter().collect::<Vec<_>>();
log_info!("Found {} nets\n", nets.len());
let mut count = 0;
for (_name, net) in &nets {
let _src = ctx.source_wire(*net);
let net = unsafe { net.as_mut().unwrap() };
for user in net.users() {
count += ctx.sink_wires(net, user).count();
}
}
log_info!("Found {} arcs\n", count);
let (name, net) = nets
.iter()
.max_by_key(|(_name, net)| {
let net = unsafe { net.as_mut().unwrap() };
if net.is_global() {
0
} else {
net.users()
.fold(0, |acc, sink| acc + ctx.sink_wires(net, sink).count())
}
})
.unwrap();
let net = unsafe { net.as_mut().unwrap() };
let count = net
.users()
.fold(0, |acc, sink| acc + ctx.sink_wires(net, sink).count());
log_info!(
"Highest non-global fansnout net is {} with {} arcs\n",
ctx.name_of(*name).to_str().unwrap(),
count
);
let mut x0 = 0;
let mut y0 = 0;
let mut x1 = 0;
let mut y1 = 0;
for sink in net.users() {
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());
}
log_info!(" which spans ({}, {}) to ({}, {})\n", x0, y0, x1, y1);
let mut arcs = Vec::new();
for (_name, net) in &nets {
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 net.users() {
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);
/*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);*/
true
}

View File

@ -1,4 +1,4 @@
use std::{ffi::CStr, collections::binary_heap::Iter};
use std::ffi::CStr;
use libc::c_char;
@ -19,16 +19,52 @@ pub struct CellInfo {
private: [u8; 0],
}
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) }
}
}
#[repr(C)]
pub struct NetInfo {
private: [u8; 0],
}
impl NetInfo {
pub fn driver(&mut self) -> *mut PortRef {
unsafe { npnr_netinfo_driver(self) }
}
pub fn users(&mut self) -> NetUserIter {
NetUserIter { net: self, n: 0 }
}
pub fn is_global(&self) -> bool {
unsafe { npnr_netinfo_is_global(self) }
}
}
#[repr(C)]
pub struct PortRef {
private: [u8; 0],
}
impl PortRef {
pub fn cell(&self) -> Option<&mut CellInfo> {
unsafe { npnr_portref_cell(self).as_mut() }
}
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct IdString(libc::c_int);
/// A type representing a bel name.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct BelId {
_private: u64,
@ -38,6 +74,10 @@ impl BelId {
pub fn null() -> Self {
unsafe { npnr_belid_null() }
}
pub fn is_null(self) -> bool {
self == Self::null()
}
}
#[derive(Clone, Copy)]
@ -52,15 +92,17 @@ impl PipId {
}
}
#[derive(Clone, Copy)]
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct WireId {
_private: u64,
}
pub struct WireId(u64);
impl WireId {
pub fn null() -> Self {
todo!()
unsafe { npnr_wireid_null() }
}
pub fn is_null(self) -> bool {
self == Self::null()
}
}
@ -81,7 +123,7 @@ impl Context {
}
/// Bind a given bel to a given cell with the given strength.
pub fn bind_bel(&mut self, bel: BelId, cell: &mut CellInfo, strength: PlaceStrength) {
pub fn bind_bel(&mut self, bel: BelId, cell: *mut CellInfo, strength: PlaceStrength) {
unsafe { npnr_context_bind_bel(self, bel, cell, strength) }
}
@ -101,7 +143,7 @@ impl Context {
}
/// Bind a wire to a net. This method must be used when binding a wire that is driven by a bel pin. Use bindPip() when binding a wire that is driven by a pip.
pub fn bind_wire(&mut self, wire: WireId, net: &mut NetInfo, strength: PlaceStrength) {
pub fn bind_wire(&mut self, wire: WireId, net: *mut NetInfo, strength: PlaceStrength) {
unsafe { npnr_context_bind_wire(self, wire, net, strength) }
}
@ -111,7 +153,7 @@ impl Context {
}
/// Bid a pip to a net. This also bind the destination wire of that pip.
pub fn bind_pip(&mut self, pip: PipId, net: &mut NetInfo, strength: PlaceStrength) {
pub fn bind_pip(&mut self, pip: PipId, net: *mut NetInfo, strength: PlaceStrength) {
unsafe { npnr_context_bind_pip(self, pip, net, strength) }
}
@ -135,6 +177,19 @@ impl Context {
unsafe { npnr_context_estimate_delay(self, src, dst) as f32 }
}
pub fn source_wire(&self, net: *const NetInfo) -> WireId {
unsafe { npnr_context_get_netinfo_source_wire(self, net) }
}
pub fn sink_wires(&self, net: *const NetInfo, sink: *const PortRef) -> NetSinkWireIter {
NetSinkWireIter {
ctx: self,
net,
sink,
n: 0,
}
}
pub fn check(&self) {
unsafe { npnr_context_check(self) }
}
@ -155,6 +210,10 @@ impl Context {
pub fn verbose(&self) -> bool {
unsafe { npnr_context_verbose(self) }
}
pub fn net_iter(&self) -> NetIter {
NetIter { ctx: self, n: 0 }
}
}
extern "C" {
@ -162,6 +221,7 @@ extern "C" {
pub fn npnr_log_error(format: *const c_char);
fn npnr_belid_null() -> BelId;
fn npnr_wireid_null() -> WireId;
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;
@ -198,25 +258,33 @@ extern "C" {
fn npnr_context_name_of(ctx: *const Context, s: IdString) -> *const libc::c_char;
fn npnr_context_verbose(ctx: *const Context) -> bool;
fn npnr_context_get_netinfo_source_wire(ctx: *const Context, net: *const NetInfo) -> WireId;
fn npnr_context_get_netinfo_sink_wire(
ctx: *const Context,
net: *const NetInfo,
sink: *const PortRef,
n: u32,
) -> WireId;
fn npnr_context_nets_key(ctx: *const Context, n: u32) -> IdString;
fn npnr_context_nets_value(ctx: *const Context, n: u32) -> *mut NetInfo;
// fn npnr_context_nets(ctx: *const Context) -> *mut *mut NetInfo;
fn npnr_netinfo_driver(net: *mut NetInfo) -> *mut PortRef;
fn npnr_netinfo_users_value(net: *mut NetInfo, n: u32) -> *mut PortRef;
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;
}
/// In case you missed the C++ comment; this is O(n^2) because FFI is misert.
/// Iterate over the nets in a context.
///
/// In case you missed the C++ comment; this is `O(n^2)` because FFI is misery.
/// It's probably best to run it exactly once.
pub struct NetIter<'a> {
ctx: &'a Context,
n: u32
}
impl<'a> NetIter<'a> {
pub fn new(ctx: &'a Context) -> Self {
Self {
ctx,
n: 0
}
}
n: u32,
}
impl<'a> Iterator for NetIter<'a> {
@ -233,6 +301,48 @@ impl<'a> Iterator for NetIter<'a> {
}
}
/// Iterate over the users field of a net.
///
/// In case you missed the C++ comment; this is `O(n^2)` because FFI is misery.
pub struct NetUserIter {
net: *mut NetInfo,
n: u32,
}
impl Iterator for NetUserIter {
type Item = *mut PortRef;
fn next(&mut self) -> Option<Self::Item> {
let item = unsafe { npnr_netinfo_users_value(self.net, self.n) };
if item.is_null() {
return None;
}
self.n += 1;
Some(item)
}
}
pub struct NetSinkWireIter<'a> {
ctx: &'a Context,
net: *const NetInfo,
sink: *const PortRef,
n: u32,
}
impl<'a> Iterator for NetSinkWireIter<'a> {
type Item = WireId;
fn next(&mut self) -> Option<Self::Item> {
let item =
unsafe { npnr_context_get_netinfo_sink_wire(self.ctx, self.net, self.sink, self.n) };
if item.is_null() {
return None;
}
self.n += 1;
Some(item)
}
}
macro_rules! log_info {
($($t:tt)*) => {
let s = std::ffi::CString::new(format!($($t)*)).unwrap();