2018-06-07 21:38:14 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@clifford.at>
|
2018-06-17 17:49:57 +08:00
|
|
|
* Copyright (C) 2018 David Shah <david@symbioticeda.com>
|
|
|
|
*
|
|
|
|
* Simulated annealing implementation based on arachne-pnr
|
|
|
|
* Copyright (C) 2015-2018 Cotton Seed
|
2018-06-07 21:38:14 +08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-17 00:09:51 +08:00
|
|
|
#include "place_sa.h"
|
2018-06-16 03:29:32 +08:00
|
|
|
#include <algorithm>
|
2018-06-14 21:21:00 +08:00
|
|
|
#include <cmath>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <iostream>
|
2018-06-13 23:07:42 +08:00
|
|
|
#include <limits>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <ostream>
|
2018-06-13 23:07:42 +08:00
|
|
|
#include <queue>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <set>
|
|
|
|
#include <stdarg.h>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <stdio.h>
|
2018-06-08 03:38:24 +08:00
|
|
|
#include <stdlib.h>
|
2018-06-07 21:38:14 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
2018-06-12 19:45:59 +08:00
|
|
|
#include "arch_place.h"
|
2018-06-08 03:38:24 +08:00
|
|
|
#include "log.h"
|
2018-06-07 21:38:14 +08:00
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2018-06-21 16:45:18 +08:00
|
|
|
typedef int64_t wirelen_t;
|
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
class SAPlacer
|
2018-06-13 23:07:42 +08:00
|
|
|
{
|
2018-06-20 16:46:14 +08:00
|
|
|
public:
|
|
|
|
SAPlacer(Context *ctx) : ctx(ctx)
|
|
|
|
{
|
|
|
|
checker = new PlaceValidityChecker(ctx);
|
|
|
|
int num_bel_types = 0;
|
2018-06-18 20:06:37 +08:00
|
|
|
for (auto bel : ctx->getBels()) {
|
2018-06-20 16:46:14 +08:00
|
|
|
int x, y;
|
2018-06-20 18:57:38 +08:00
|
|
|
bool gb;
|
|
|
|
ctx->estimatePosition(bel, x, y, gb);
|
2018-06-20 16:46:14 +08:00
|
|
|
BelType type = ctx->getBelType(bel);
|
|
|
|
int type_idx;
|
|
|
|
if (bel_types.find(type) == bel_types.end()) {
|
|
|
|
type_idx = num_bel_types++;
|
|
|
|
bel_types[type] = type_idx;
|
|
|
|
} else {
|
|
|
|
type_idx = bel_types.at(type);
|
2018-06-14 01:10:12 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
if (int(fast_bels.size()) < type_idx + 1)
|
|
|
|
fast_bels.resize(type_idx + 1);
|
|
|
|
if (int(fast_bels.at(type_idx).size()) < (x + 1))
|
|
|
|
fast_bels.at(type_idx).resize(x + 1);
|
|
|
|
if (int(fast_bels.at(type_idx).at(x).size()) < (y + 1))
|
|
|
|
fast_bels.at(type_idx).at(x).resize(y + 1);
|
|
|
|
max_x = std::max(max_x, x);
|
|
|
|
max_y = std::max(max_y, y);
|
|
|
|
fast_bels.at(type_idx).at(x).at(y).push_back(bel);
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
diameter = std::max(max_x, max_y) + 1;
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
bool place()
|
|
|
|
{
|
2018-06-21 20:09:50 +08:00
|
|
|
log_break();
|
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
size_t placed_cells = 0;
|
|
|
|
// Initial constraints placer
|
|
|
|
for (auto cell_entry : ctx->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second;
|
|
|
|
auto loc = cell->attrs.find(ctx->id("BEL"));
|
|
|
|
if (loc != cell->attrs.end()) {
|
|
|
|
std::string loc_name = loc->second;
|
|
|
|
BelId bel = ctx->getBelByName(ctx->id(loc_name));
|
|
|
|
if (bel == BelId()) {
|
|
|
|
log_error(
|
|
|
|
"No Bel named \'%s\' located for "
|
|
|
|
"this chip (processing BEL attribute on \'%s\')\n",
|
|
|
|
loc_name.c_str(), cell->name.c_str(ctx));
|
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
BelType bel_type = ctx->getBelType(bel);
|
|
|
|
if (bel_type != ctx->belTypeFromId(cell->type)) {
|
|
|
|
log_error("Bel \'%s\' of type \'%s\' does not match cell "
|
|
|
|
"\'%s\' of type \'%s\'",
|
|
|
|
loc_name.c_str(),
|
|
|
|
ctx->belTypeToId(bel_type).c_str(ctx),
|
|
|
|
cell->name.c_str(ctx), cell->type.c_str(ctx));
|
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
cell->bel = bel;
|
2018-06-20 17:19:25 +08:00
|
|
|
cell->belStrength = STRENGTH_USER;
|
2018-06-20 16:46:14 +08:00
|
|
|
ctx->bindBel(bel, cell->name);
|
|
|
|
locked_bels.insert(bel);
|
|
|
|
placed_cells++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int constr_placed_cells = placed_cells;
|
|
|
|
log_info("Placed %d cells based on constraints.\n", int(placed_cells));
|
|
|
|
|
|
|
|
// Sort to-place cells for deterministic initial placement
|
|
|
|
std::vector<CellInfo *> autoplaced;
|
|
|
|
for (auto cell : ctx->cells) {
|
|
|
|
CellInfo *ci = cell.second;
|
|
|
|
if (ci->bel == BelId()) {
|
|
|
|
autoplaced.push_back(cell.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(autoplaced.begin(), autoplaced.end(),
|
|
|
|
[](CellInfo *a, CellInfo *b) { return a->name < b->name; });
|
|
|
|
ctx->shuffle(autoplaced);
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Place cells randomly initially
|
|
|
|
log_info("Creating initial placement for remaining %d cells.\n",
|
|
|
|
int(autoplaced.size()));
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
for (auto cell : autoplaced) {
|
|
|
|
place_initial(cell);
|
|
|
|
placed_cells++;
|
|
|
|
if ((placed_cells - constr_placed_cells) % 500 == 0)
|
|
|
|
log_info(" initial placement placed %d/%d cells\n",
|
|
|
|
int(placed_cells - constr_placed_cells),
|
|
|
|
int(autoplaced.size()));
|
|
|
|
}
|
|
|
|
if ((placed_cells - constr_placed_cells) % 500 != 0)
|
|
|
|
log_info(" initial placement placed %d/%d cells\n",
|
|
|
|
int(placed_cells - constr_placed_cells),
|
|
|
|
int(autoplaced.size()));
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
log_info("Running simulated annealing placer.\n");
|
2018-06-17 00:45:32 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Calculate wirelength after initial placement
|
|
|
|
curr_wirelength = 0;
|
2018-06-22 20:58:27 +08:00
|
|
|
curr_tns = 0;
|
2018-06-20 16:46:14 +08:00
|
|
|
for (auto net : ctx->nets) {
|
2018-06-22 20:58:27 +08:00
|
|
|
wirelen_t wl = get_wirelength(net.second, curr_tns);
|
2018-06-21 02:12:23 +08:00
|
|
|
wirelengths[net.first] = wl;
|
2018-06-20 16:46:14 +08:00
|
|
|
curr_wirelength += wl;
|
|
|
|
}
|
2018-06-17 00:45:32 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
int n_no_progress = 0;
|
|
|
|
double avg_wirelength = curr_wirelength;
|
|
|
|
temp = 10000;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Main simulated annealing loop
|
|
|
|
for (int iter = 1;; iter++) {
|
|
|
|
n_move = n_accept = 0;
|
|
|
|
improved = false;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
if (iter % 5 == 0 || iter == 1)
|
2018-06-22 21:03:33 +08:00
|
|
|
log_info(" at iteration #%d: temp = %f, wire length = "
|
2018-06-22 20:58:27 +08:00
|
|
|
"%.0f, est tns = %.02fns\n",
|
|
|
|
iter, temp, double(curr_wirelength), curr_tns);
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
for (int m = 0; m < 15; ++m) {
|
|
|
|
// Loop through all automatically placed cells
|
|
|
|
for (auto cell : autoplaced) {
|
|
|
|
// Find another random Bel for this cell
|
|
|
|
BelId try_bel = random_bel_for_cell(cell);
|
|
|
|
// If valid, try and swap to a new position and see if
|
|
|
|
// the new position is valid/worthwhile
|
|
|
|
if (try_bel != BelId() && try_bel != cell->bel)
|
|
|
|
try_swap_position(cell, try_bel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Heuristic to improve placement on the 8k
|
|
|
|
if (improved)
|
|
|
|
n_no_progress = 0;
|
|
|
|
else
|
|
|
|
n_no_progress++;
|
|
|
|
|
|
|
|
if (temp <= 1e-3 && n_no_progress >= 5) {
|
|
|
|
if (iter % 5 != 0)
|
|
|
|
log_info(
|
|
|
|
" at iteration #%d: temp = %f, wire length = %f\n",
|
2018-06-21 16:45:18 +08:00
|
|
|
iter, temp, double(curr_wirelength));
|
2018-06-20 16:46:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-06-16 18:04:38 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
double Raccept = double(n_accept) / double(n_move);
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
int M = std::max(max_x, max_y) + 1;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
double upper = 0.6, lower = 0.4;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
if (curr_wirelength < 0.95 * avg_wirelength) {
|
|
|
|
avg_wirelength = 0.8 * avg_wirelength + 0.2 * curr_wirelength;
|
|
|
|
} else {
|
|
|
|
if (Raccept >= 0.8) {
|
|
|
|
temp *= 0.7;
|
|
|
|
} else if (Raccept > upper) {
|
|
|
|
if (diameter < M)
|
|
|
|
diameter++;
|
|
|
|
else
|
|
|
|
temp *= 0.9;
|
|
|
|
} else if (Raccept > lower) {
|
|
|
|
temp *= 0.95;
|
|
|
|
} else {
|
|
|
|
// Raccept < 0.3
|
|
|
|
if (diameter > 1)
|
|
|
|
diameter--;
|
|
|
|
else
|
|
|
|
temp *= 0.8;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
2018-06-21 02:25:48 +08:00
|
|
|
|
|
|
|
// Recalculate total wirelength entirely to avoid rounding errors
|
|
|
|
// accumulating over time
|
|
|
|
curr_wirelength = 0;
|
2018-06-22 20:58:27 +08:00
|
|
|
curr_tns = 0;
|
2018-06-21 02:25:48 +08:00
|
|
|
for (auto net : ctx->nets) {
|
2018-06-22 20:58:27 +08:00
|
|
|
wirelen_t wl = get_wirelength(net.second, curr_tns);
|
2018-06-21 02:25:48 +08:00
|
|
|
wirelengths[net.first] = wl;
|
|
|
|
curr_wirelength += wl;
|
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
// Final post-pacement validitiy check
|
|
|
|
for (auto bel : ctx->getBels()) {
|
2018-06-20 17:19:25 +08:00
|
|
|
IdString cell = ctx->getBelCell(bel, false);
|
2018-06-20 16:46:14 +08:00
|
|
|
if (!checker->isBelLocationValid(bel)) {
|
|
|
|
std::string cell_text = "no cell";
|
|
|
|
if (cell != IdString())
|
|
|
|
cell_text = std::string("cell '") + cell.str(ctx) + "'";
|
2018-06-21 02:12:23 +08:00
|
|
|
if (ctx->force) {
|
|
|
|
log_warning(
|
|
|
|
"post-placement validity check failed for Bel '%s' "
|
|
|
|
"(%s)\n",
|
|
|
|
ctx->getBelName(bel).c_str(ctx), cell_text.c_str());
|
|
|
|
} else {
|
|
|
|
log_error(
|
|
|
|
"post-placement validity check failed for Bel '%s' "
|
|
|
|
"(%s)\n",
|
|
|
|
ctx->getBelName(bel).c_str(ctx), cell_text.c_str());
|
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
private:
|
|
|
|
// Initial random placement
|
|
|
|
void place_initial(CellInfo *cell)
|
|
|
|
{
|
|
|
|
bool all_placed = false;
|
|
|
|
int iters = 25;
|
|
|
|
while (!all_placed) {
|
|
|
|
BelId best_bel = BelId();
|
|
|
|
uint64_t best_score = std::numeric_limits<uint64_t>::max(),
|
|
|
|
best_ripup_score = std::numeric_limits<uint64_t>::max();
|
|
|
|
CellInfo *ripup_target = nullptr;
|
|
|
|
BelId ripup_bel = BelId();
|
|
|
|
if (cell->bel != BelId()) {
|
|
|
|
ctx->unbindBel(cell->bel);
|
|
|
|
cell->bel = BelId();
|
|
|
|
}
|
|
|
|
BelType targetType = ctx->belTypeFromId(cell->type);
|
|
|
|
for (auto bel : ctx->getBels()) {
|
|
|
|
if (ctx->getBelType(bel) == targetType &&
|
|
|
|
checker->isValidBelForCell(cell, bel)) {
|
|
|
|
if (ctx->checkBelAvail(bel)) {
|
|
|
|
uint64_t score = ctx->rng64();
|
|
|
|
if (score <= best_score) {
|
|
|
|
best_score = score;
|
|
|
|
best_bel = bel;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint64_t score = ctx->rng64();
|
|
|
|
if (score <= best_ripup_score) {
|
|
|
|
best_ripup_score = score;
|
|
|
|
ripup_target =
|
|
|
|
ctx->cells.at(ctx->getBelCell(bel, true));
|
|
|
|
ripup_bel = bel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
if (best_bel == BelId()) {
|
|
|
|
if (iters == 0 || ripup_bel == BelId())
|
|
|
|
log_error("failed to place cell '%s' of type '%s'\n",
|
|
|
|
cell->name.c_str(ctx), cell->type.c_str(ctx));
|
|
|
|
--iters;
|
|
|
|
ctx->unbindBel(ripup_target->bel);
|
|
|
|
ripup_target->bel = BelId();
|
|
|
|
best_bel = ripup_bel;
|
|
|
|
} else {
|
|
|
|
all_placed = true;
|
|
|
|
}
|
|
|
|
cell->bel = best_bel;
|
2018-06-20 17:19:25 +08:00
|
|
|
cell->belStrength = STRENGTH_WEAK;
|
2018-06-20 16:46:14 +08:00
|
|
|
ctx->bindBel(cell->bel, cell->name);
|
2018-06-13 23:07:42 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Back annotate location
|
|
|
|
cell->attrs[ctx->id("BEL")] = ctx->getBelName(cell->bel).str(ctx);
|
|
|
|
cell = ripup_target;
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
// Get the total estimated wirelength for a net
|
2018-06-22 20:58:27 +08:00
|
|
|
wirelen_t get_wirelength(NetInfo *net, float &tns)
|
2018-06-20 16:46:14 +08:00
|
|
|
{
|
2018-06-21 16:45:18 +08:00
|
|
|
wirelen_t wirelength = 0;
|
2018-06-20 18:57:38 +08:00
|
|
|
int driver_x, driver_y;
|
|
|
|
bool driver_gb;
|
2018-06-20 16:46:14 +08:00
|
|
|
CellInfo *driver_cell = net->driver.cell;
|
|
|
|
if (!driver_cell)
|
|
|
|
return 0;
|
|
|
|
if (driver_cell->bel == BelId())
|
|
|
|
return 0;
|
2018-06-20 18:57:38 +08:00
|
|
|
ctx->estimatePosition(driver_cell->bel, driver_x, driver_y, driver_gb);
|
2018-06-20 16:46:14 +08:00
|
|
|
WireId drv_wire = ctx->getWireBelPin(
|
|
|
|
driver_cell->bel, ctx->portPinFromId(net->driver.port));
|
2018-06-20 18:57:38 +08:00
|
|
|
if (driver_gb)
|
2018-06-20 16:46:14 +08:00
|
|
|
return 0;
|
2018-06-21 17:45:58 +08:00
|
|
|
float worst_slack = 1000;
|
|
|
|
int xmin = driver_x, xmax = driver_x, ymin = driver_y, ymax = driver_y;
|
2018-06-20 16:46:14 +08:00
|
|
|
for (auto load : net->users) {
|
|
|
|
if (load.cell == nullptr)
|
|
|
|
continue;
|
|
|
|
CellInfo *load_cell = load.cell;
|
|
|
|
if (load_cell->bel == BelId())
|
|
|
|
continue;
|
2018-06-21 17:45:58 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
WireId user_wire = ctx->getWireBelPin(
|
|
|
|
load_cell->bel, ctx->portPinFromId(load.port));
|
2018-06-20 23:08:57 +08:00
|
|
|
delay_t raw_wl = ctx->estimateDelay(drv_wire, user_wire);
|
2018-06-21 17:45:58 +08:00
|
|
|
float slack =
|
2018-06-21 17:59:20 +08:00
|
|
|
ctx->getDelayNS(load.budget) - ctx->getDelayNS(raw_wl);
|
2018-06-22 20:58:27 +08:00
|
|
|
if (slack < 0)
|
|
|
|
tns += slack;
|
2018-06-21 17:45:58 +08:00
|
|
|
worst_slack = std::min(slack, worst_slack);
|
|
|
|
int load_x, load_y;
|
|
|
|
bool load_gb;
|
|
|
|
ctx->estimatePosition(load_cell->bel, load_x, load_y, load_gb);
|
|
|
|
if (load_gb)
|
|
|
|
continue;
|
|
|
|
xmin = std::min(xmin, load_x);
|
|
|
|
ymin = std::min(ymin, load_y);
|
|
|
|
xmax = std::max(xmax, load_x);
|
|
|
|
ymax = std::max(ymax, load_y);
|
2018-06-15 02:25:35 +08:00
|
|
|
}
|
2018-06-22 20:58:27 +08:00
|
|
|
wirelength =
|
|
|
|
wirelen_t((((ymax - ymin) + (xmax - xmin)) *
|
2018-06-22 21:17:00 +08:00
|
|
|
std::min(5.0, (1.0 + std::exp(-worst_slack / 5)))));
|
2018-06-20 16:46:14 +08:00
|
|
|
return wirelength;
|
2018-06-17 19:24:42 +08:00
|
|
|
}
|
2018-06-19 21:00:24 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Attempt a SA position swap, return true on success or false on failure
|
|
|
|
bool try_swap_position(CellInfo *cell, BelId newBel)
|
|
|
|
{
|
|
|
|
static std::unordered_set<NetInfo *> update;
|
2018-06-21 16:45:18 +08:00
|
|
|
static std::vector<std::pair<IdString, wirelen_t>> new_lengths;
|
2018-06-20 16:46:14 +08:00
|
|
|
new_lengths.clear();
|
|
|
|
update.clear();
|
|
|
|
BelId oldBel = cell->bel;
|
|
|
|
IdString other = ctx->getBelCell(newBel, true);
|
|
|
|
CellInfo *other_cell = nullptr;
|
2018-06-21 16:45:18 +08:00
|
|
|
wirelen_t new_wirelength = 0, delta;
|
2018-06-20 16:46:14 +08:00
|
|
|
ctx->unbindBel(oldBel);
|
|
|
|
if (other != IdString()) {
|
|
|
|
other_cell = ctx->cells[other];
|
|
|
|
ctx->unbindBel(newBel);
|
2018-06-18 17:43:59 +08:00
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
for (const auto &port : cell->ports)
|
|
|
|
if (port.second.net != nullptr)
|
|
|
|
update.insert(port.second.net);
|
|
|
|
|
|
|
|
if (other != IdString()) {
|
|
|
|
for (const auto &port : other_cell->ports)
|
|
|
|
if (port.second.net != nullptr)
|
|
|
|
update.insert(port.second.net);
|
2018-06-15 16:14:12 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
ctx->bindBel(newBel, cell->name);
|
|
|
|
|
|
|
|
if (other != IdString()) {
|
|
|
|
ctx->bindBel(oldBel, other_cell->name);
|
2018-06-19 21:00:24 +08:00
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
if (!checker->isBelLocationValid(newBel) ||
|
|
|
|
((other != IdString() && !checker->isBelLocationValid(oldBel)))) {
|
|
|
|
ctx->unbindBel(newBel);
|
|
|
|
if (other != IdString())
|
|
|
|
ctx->unbindBel(oldBel);
|
|
|
|
goto swap_fail;
|
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
cell->bel = newBel;
|
|
|
|
if (other != IdString())
|
|
|
|
other_cell->bel = oldBel;
|
2018-06-15 02:25:35 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
new_wirelength = curr_wirelength;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Recalculate wirelengths for all nets touched by the peturbation
|
|
|
|
for (auto net : update) {
|
2018-06-21 02:12:23 +08:00
|
|
|
new_wirelength -= wirelengths.at(net->name);
|
2018-06-22 20:58:27 +08:00
|
|
|
float temp_tns = 0;
|
|
|
|
wirelen_t net_new_wl = get_wirelength(net, temp_tns);
|
2018-06-20 16:46:14 +08:00
|
|
|
new_wirelength += net_new_wl;
|
2018-06-21 02:12:23 +08:00
|
|
|
new_lengths.push_back(std::make_pair(net->name, net_new_wl));
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
delta = new_wirelength - curr_wirelength;
|
|
|
|
n_move++;
|
|
|
|
// SA acceptance criterea
|
2018-06-22 18:34:42 +08:00
|
|
|
if (delta < 0 || (temp > 1e-6 && (ctx->rng() / float(0x3fffffff)) <=
|
|
|
|
std::exp(-delta / temp))) {
|
2018-06-20 16:46:14 +08:00
|
|
|
n_accept++;
|
2018-06-21 16:45:18 +08:00
|
|
|
if (delta < 2)
|
2018-06-20 16:46:14 +08:00
|
|
|
improved = true;
|
|
|
|
} else {
|
|
|
|
if (other != IdString())
|
|
|
|
ctx->unbindBel(oldBel);
|
|
|
|
ctx->unbindBel(newBel);
|
|
|
|
goto swap_fail;
|
2018-06-15 19:42:21 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
curr_wirelength = new_wirelength;
|
|
|
|
for (auto new_wl : new_lengths)
|
|
|
|
wirelengths.at(new_wl.first) = new_wl.second;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
swap_fail:
|
|
|
|
ctx->bindBel(oldBel, cell->name);
|
|
|
|
cell->bel = oldBel;
|
|
|
|
if (other != IdString()) {
|
|
|
|
ctx->bindBel(newBel, other);
|
|
|
|
other_cell->bel = newBel;
|
|
|
|
}
|
|
|
|
return false;
|
2018-06-15 19:42:21 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
// Find a random Bel of the correct type for a cell, within the specified
|
|
|
|
// diameter
|
|
|
|
BelId random_bel_for_cell(CellInfo *cell)
|
|
|
|
{
|
|
|
|
BelType targetType = ctx->belTypeFromId(cell->type);
|
2018-06-20 18:57:38 +08:00
|
|
|
int x, y;
|
|
|
|
bool gb;
|
|
|
|
ctx->estimatePosition(cell->bel, x, y, gb);
|
2018-06-20 16:46:14 +08:00
|
|
|
while (true) {
|
|
|
|
int nx = ctx->rng(2 * diameter + 1) + std::max(x - diameter, 0);
|
|
|
|
int ny = ctx->rng(2 * diameter + 1) + std::max(y - diameter, 0);
|
|
|
|
int beltype_idx = bel_types.at(targetType);
|
|
|
|
if (nx >= int(fast_bels.at(beltype_idx).size()))
|
|
|
|
continue;
|
|
|
|
if (ny >= int(fast_bels.at(beltype_idx).at(nx).size()))
|
|
|
|
continue;
|
|
|
|
const auto &fb = fast_bels.at(beltype_idx).at(nx).at(ny);
|
|
|
|
if (fb.size() == 0)
|
|
|
|
continue;
|
|
|
|
BelId bel = fb.at(ctx->rng(int(fb.size())));
|
|
|
|
if (locked_bels.find(bel) != locked_bels.end())
|
|
|
|
continue;
|
|
|
|
return bel;
|
2018-06-17 16:55:19 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
Context *ctx;
|
2018-06-21 16:45:18 +08:00
|
|
|
std::unordered_map<IdString, wirelen_t> wirelengths;
|
|
|
|
wirelen_t curr_wirelength = std::numeric_limits<wirelen_t>::max();
|
2018-06-22 20:58:27 +08:00
|
|
|
float curr_tns = 0;
|
2018-06-20 16:46:14 +08:00
|
|
|
float temp = 1000;
|
|
|
|
bool improved = false;
|
|
|
|
int n_move, n_accept;
|
|
|
|
int diameter = 35, max_x = 1, max_y = 1;
|
|
|
|
std::unordered_map<BelType, int> bel_types;
|
|
|
|
std::vector<std::vector<std::vector<std::vector<BelId>>>> fast_bels;
|
|
|
|
std::unordered_set<BelId> locked_bels;
|
|
|
|
PlaceValidityChecker *checker;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool place_design_sa(Context *ctx)
|
|
|
|
{
|
2018-06-21 23:56:45 +08:00
|
|
|
try {
|
|
|
|
SAPlacer placer(ctx);
|
|
|
|
placer.place();
|
|
|
|
log_info("Checksum: 0x%08x\n", ctx->checksum());
|
|
|
|
return true;
|
|
|
|
} catch (log_execution_error_exception) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|