2018-06-07 21:38:14 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
2018-06-22 22:19:17 +08:00
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
|
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-07-12 00:15:08 +08:00
|
|
|
#include "placer1.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-08 03:38:24 +08:00
|
|
|
#include "log.h"
|
2018-06-27 18:00:13 +08:00
|
|
|
#include "place_common.h"
|
2018-06-29 21:30:00 +08:00
|
|
|
#include "timing.h"
|
2018-06-30 01:58:08 +08:00
|
|
|
#include "util.h"
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
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:
|
2018-06-29 21:47:56 +08:00
|
|
|
SAPlacer(Context *ctx) : ctx(ctx)
|
2018-06-20 16:46:14 +08:00
|
|
|
{
|
|
|
|
int num_bel_types = 0;
|
2018-06-18 20:06:37 +08:00
|
|
|
for (auto bel : ctx->getBels()) {
|
2018-07-24 21:45:49 +08:00
|
|
|
Loc loc = ctx->getBelLocation(bel);
|
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);
|
2018-07-24 21:45:49 +08:00
|
|
|
if (int(fast_bels.at(type_idx).size()) < (loc.x + 1))
|
|
|
|
fast_bels.at(type_idx).resize(loc.x + 1);
|
|
|
|
if (int(fast_bels.at(type_idx).at(loc.x).size()) < (loc.y + 1))
|
|
|
|
fast_bels.at(type_idx).at(loc.x).resize(loc.y + 1);
|
|
|
|
max_x = std::max(max_x, loc.x);
|
|
|
|
max_y = std::max(max_y, loc.y);
|
|
|
|
fast_bels.at(type_idx).at(loc.x).at(loc.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-07-27 05:47:04 +08:00
|
|
|
ctx->lock();
|
2018-06-21 20:09:50 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
size_t placed_cells = 0;
|
2018-07-15 01:52:56 +08:00
|
|
|
// Initial constraints placer
|
|
|
|
for (auto &cell_entry : ctx->cells) {
|
|
|
|
CellInfo *cell = cell_entry.second.get();
|
|
|
|
auto loc = cell->attrs.find(ctx->id("BEL"));
|
|
|
|
if (loc != cell->attrs.end()) {
|
|
|
|
std::string loc_name = loc->second;
|
2018-07-15 01:53:08 +08:00
|
|
|
BelId bel = ctx->getBelByName(ctx->id(loc_name));
|
2018-07-15 01:52:56 +08:00
|
|
|
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-07-15 01:52:56 +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 "
|
2018-07-25 18:32:21 +08:00
|
|
|
"\'%s\' of type \'%s\'\n",
|
|
|
|
loc_name.c_str(), ctx->belTypeToId(bel_type).c_str(ctx), cell->name.c_str(ctx),
|
|
|
|
cell->type.c_str(ctx));
|
|
|
|
}
|
|
|
|
if (!ctx->isValidBelForCell(cell, bel)) {
|
|
|
|
log_error("Bel \'%s\' of type \'%s\' is not valid for cell "
|
|
|
|
"\'%s\' of type \'%s\'\n",
|
2018-07-15 01:52:56 +08:00
|
|
|
loc_name.c_str(), ctx->belTypeToId(bel_type).c_str(ctx), cell->name.c_str(ctx),
|
|
|
|
cell->type.c_str(ctx));
|
2018-07-14 01:57:55 +08:00
|
|
|
}
|
2018-07-15 01:52:56 +08:00
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(bel, cell->name, STRENGTH_USER);
|
2018-07-15 01:52:56 +08:00
|
|
|
locked_bels.insert(bel);
|
|
|
|
placed_cells++;
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int constr_placed_cells = placed_cells;
|
|
|
|
log_info("Placed %d cells based on constraints.\n", int(placed_cells));
|
2018-07-27 05:47:04 +08:00
|
|
|
ctx->yield();
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
// Sort to-place cells for deterministic initial placement
|
|
|
|
std::vector<CellInfo *> autoplaced;
|
2018-06-26 20:13:52 +08:00
|
|
|
for (auto &cell : ctx->cells) {
|
2018-06-26 03:33:48 +08:00
|
|
|
CellInfo *ci = cell.second.get();
|
2018-06-20 16:46:14 +08:00
|
|
|
if (ci->bel == BelId()) {
|
2018-06-26 03:33:48 +08:00
|
|
|
autoplaced.push_back(cell.second.get());
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 22:12:52 +08:00
|
|
|
std::sort(autoplaced.begin(), autoplaced.end(), [](CellInfo *a, CellInfo *b) { return a->name < b->name; });
|
2018-06-20 16:46:14 +08:00
|
|
|
ctx->shuffle(autoplaced);
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// Place cells randomly initially
|
2018-06-23 22:12:52 +08:00
|
|
|
log_info("Creating initial placement for remaining %d cells.\n", int(autoplaced.size()));
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-07-15 01:52:56 +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()));
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
if ((placed_cells - constr_placed_cells) % 500 != 0)
|
2018-06-23 22:12:52 +08:00
|
|
|
log_info(" initial placement placed %d/%d cells\n", int(placed_cells - constr_placed_cells),
|
2018-06-20 16:46:14 +08:00
|
|
|
int(autoplaced.size()));
|
2018-07-31 13:21:53 +08:00
|
|
|
assign_budget(ctx);
|
2018-07-27 05:47:04 +08:00
|
|
|
ctx->yield();
|
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-07-16 23:13:40 +08:00
|
|
|
// Calculate metric after initial placement
|
|
|
|
curr_metric = 0;
|
2018-06-22 20:58:27 +08:00
|
|
|
curr_tns = 0;
|
2018-07-15 01:52:56 +08:00
|
|
|
for (auto &net : ctx->nets) {
|
2018-07-16 23:13:40 +08:00
|
|
|
wirelen_t wl = get_net_metric(ctx, net.second.get(), MetricType::COST, curr_tns);
|
|
|
|
metrics[net.first] = wl;
|
|
|
|
curr_metric += wl;
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
2018-06-17 00:45:32 +08:00
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
int n_no_progress = 0;
|
2018-07-21 16:59:16 +08:00
|
|
|
wirelen_t min_metric = curr_metric;
|
2018-07-16 23:13:40 +08:00
|
|
|
double avg_metric = curr_metric;
|
2018-06-20 16:46:14 +08:00
|
|
|
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-07-16 23:13:40 +08:00
|
|
|
log_info(" at iteration #%d: temp = %f, cost = "
|
2018-06-22 20:58:27 +08:00
|
|
|
"%.0f, est tns = %.02fns\n",
|
2018-07-16 23:13:40 +08:00
|
|
|
iter, temp, double(curr_metric), curr_tns);
|
2018-06-20 16:46:14 +08:00
|
|
|
|
2018-07-15 01:52:56 +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);
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-21 16:59:16 +08:00
|
|
|
|
|
|
|
if (curr_metric < min_metric) {
|
|
|
|
min_metric = curr_metric;
|
|
|
|
improved = true;
|
|
|
|
}
|
|
|
|
|
2018-06-20 16:46:14 +08:00
|
|
|
// 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)
|
2018-07-16 23:13:40 +08:00
|
|
|
log_info(" at iteration #%d: temp = %f, cost = %f\n", iter, temp, double(curr_metric));
|
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-07-16 23:13:40 +08:00
|
|
|
if (curr_metric < 0.95 * avg_metric) {
|
|
|
|
avg_metric = 0.8 * avg_metric + 0.2 * curr_metric;
|
2018-06-20 16:46:14 +08:00
|
|
|
} 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-29 23:04:22 +08:00
|
|
|
// Once cooled below legalise threshold, run legalisation and start requiring
|
|
|
|
// legal moves only
|
2018-06-29 19:12:44 +08:00
|
|
|
if (temp < legalise_temp && !require_legal) {
|
2018-08-03 16:31:03 +08:00
|
|
|
// legalise_design(ctx);
|
|
|
|
// FIXME
|
2018-06-29 19:12:44 +08:00
|
|
|
require_legal = true;
|
|
|
|
autoplaced.clear();
|
|
|
|
for (auto cell : sorted(ctx->cells)) {
|
|
|
|
if (cell.second->belStrength < STRENGTH_STRONG)
|
|
|
|
autoplaced.push_back(cell.second);
|
|
|
|
}
|
2018-06-29 21:30:00 +08:00
|
|
|
temp = post_legalise_temp;
|
|
|
|
diameter *= post_legalise_dia_scale;
|
2018-06-29 19:12:44 +08:00
|
|
|
ctx->shuffle(autoplaced);
|
2018-06-29 23:04:22 +08:00
|
|
|
assign_budget(ctx);
|
2018-08-01 13:05:07 +08:00
|
|
|
} else if (ctx->slack_redist_iter > 0 && iter % ctx->slack_redist_iter == 0) {
|
2018-07-29 05:10:48 +08:00
|
|
|
assign_budget(ctx, true /* quiet */);
|
2018-07-21 16:59:16 +08:00
|
|
|
}
|
2018-06-29 19:12:44 +08:00
|
|
|
|
2018-07-16 23:13:40 +08:00
|
|
|
// Recalculate total metric entirely to avoid rounding errors
|
2018-06-21 02:25:48 +08:00
|
|
|
// accumulating over time
|
2018-07-16 23:13:40 +08:00
|
|
|
curr_metric = 0;
|
2018-06-22 20:58:27 +08:00
|
|
|
curr_tns = 0;
|
2018-07-15 01:52:56 +08:00
|
|
|
for (auto &net : ctx->nets) {
|
2018-07-16 23:13:40 +08:00
|
|
|
wirelen_t wl = get_net_metric(ctx, net.second.get(), MetricType::COST, curr_tns);
|
|
|
|
metrics[net.first] = wl;
|
|
|
|
curr_metric += wl;
|
2018-06-21 02:25:48 +08:00
|
|
|
}
|
2018-07-21 01:34:59 +08:00
|
|
|
|
|
|
|
// Let the UI show visualization updates.
|
|
|
|
ctx->yield();
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
2018-07-15 01:52:56 +08:00
|
|
|
// Final post-pacement validitiy check
|
2018-07-27 05:47:04 +08:00
|
|
|
ctx->yield();
|
2018-07-15 01:52:56 +08:00
|
|
|
for (auto bel : ctx->getBels()) {
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString cell = ctx->getBoundBelCell(bel);
|
2018-07-15 01:52:56 +08:00
|
|
|
if (!ctx->isBelLocationValid(bel)) {
|
|
|
|
std::string cell_text = "no cell";
|
|
|
|
if (cell != IdString())
|
|
|
|
cell_text = std::string("cell '") + cell.str(ctx) + "'";
|
|
|
|
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-21 02:12:23 +08:00
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 03:50:21 +08:00
|
|
|
timing_analysis(ctx, true /* print_fmax */);
|
2018-07-17 23:27:50 +08:00
|
|
|
ctx->unlock();
|
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
|
2018-07-15 01:52:56 +08:00
|
|
|
void place_initial(CellInfo *cell)
|
2018-06-20 16:46:14 +08:00
|
|
|
{
|
|
|
|
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()) {
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(cell->bel);
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
BelType targetType = ctx->belTypeFromId(cell->type);
|
|
|
|
for (auto bel : ctx->getBels()) {
|
2018-07-15 01:52:56 +08:00
|
|
|
if (ctx->getBelType(bel) == targetType && (ctx->isValidBelForCell(cell, bel) || !require_legal)) {
|
2018-07-15 01:53:08 +08:00
|
|
|
if (ctx->checkBelAvail(bel)) {
|
2018-06-20 16:46:14 +08:00
|
|
|
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;
|
2018-07-15 01:53:08 +08:00
|
|
|
ripup_target = ctx->cells.at(ctx->getBoundBelCell(bel)).get();
|
2018-06-20 16:46:14 +08:00
|
|
|
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())
|
2018-06-23 22:12:52 +08:00
|
|
|
log_error("failed to place cell '%s' of type '%s'\n", cell->name.c_str(ctx), cell->type.c_str(ctx));
|
2018-06-20 16:46:14 +08:00
|
|
|
--iters;
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(ripup_target->bel);
|
2018-06-20 16:46:14 +08:00
|
|
|
best_bel = ripup_bel;
|
|
|
|
} else {
|
|
|
|
all_placed = true;
|
|
|
|
}
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(best_bel, cell->name, STRENGTH_WEAK);
|
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
|
|
|
|
|
|
|
// Attempt a SA position swap, return true on success or false on failure
|
2018-07-15 01:52:56 +08:00
|
|
|
bool try_swap_position(CellInfo *cell, BelId newBel)
|
2018-06-20 16:46:14 +08:00
|
|
|
{
|
|
|
|
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;
|
2018-07-15 01:53:08 +08:00
|
|
|
IdString other = ctx->getBoundBelCell(newBel);
|
2018-06-20 16:46:14 +08:00
|
|
|
CellInfo *other_cell = nullptr;
|
2018-06-29 19:12:44 +08:00
|
|
|
if (other != IdString()) {
|
|
|
|
other_cell = ctx->cells[other].get();
|
|
|
|
if (other_cell->belStrength > STRENGTH_WEAK)
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-16 23:13:40 +08:00
|
|
|
wirelen_t new_metric = 0, delta;
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(oldBel);
|
2018-06-20 16:46:14 +08:00
|
|
|
if (other != IdString()) {
|
2018-07-15 01:53:08 +08:00
|
|
|
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
|
|
|
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(newBel, cell->name, STRENGTH_WEAK);
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
if (other != IdString()) {
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(oldBel, other_cell->name, STRENGTH_WEAK);
|
2018-06-19 21:00:24 +08:00
|
|
|
}
|
2018-06-29 19:12:44 +08:00
|
|
|
if (require_legal) {
|
2018-07-15 01:52:56 +08:00
|
|
|
if (!ctx->isBelLocationValid(newBel) || ((other != IdString() && !ctx->isBelLocationValid(oldBel)))) {
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(newBel);
|
2018-06-29 19:12:44 +08:00
|
|
|
if (other != IdString())
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(oldBel);
|
2018-06-29 19:12:44 +08:00
|
|
|
goto swap_fail;
|
|
|
|
}
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-07-16 23:13:40 +08:00
|
|
|
new_metric = curr_metric;
|
2018-06-15 19:42:21 +08:00
|
|
|
|
2018-07-16 23:13:40 +08:00
|
|
|
// Recalculate metrics for all nets touched by the peturbation
|
2018-06-20 16:46:14 +08:00
|
|
|
for (auto net : update) {
|
2018-07-16 23:13:40 +08:00
|
|
|
new_metric -= metrics.at(net->name);
|
2018-06-22 20:58:27 +08:00
|
|
|
float temp_tns = 0;
|
2018-07-16 23:13:40 +08:00
|
|
|
wirelen_t net_new_wl = get_net_metric(ctx, net, MetricType::COST, temp_tns);
|
|
|
|
new_metric += 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
|
|
|
}
|
2018-07-16 23:13:40 +08:00
|
|
|
delta = new_metric - curr_metric;
|
2018-06-20 16:46:14 +08:00
|
|
|
n_move++;
|
|
|
|
// SA acceptance criterea
|
2018-06-23 22:12:52 +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++;
|
|
|
|
} else {
|
|
|
|
if (other != IdString())
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->unbindBel(oldBel);
|
|
|
|
ctx->unbindBel(newBel);
|
2018-06-20 16:46:14 +08:00
|
|
|
goto swap_fail;
|
2018-06-15 19:42:21 +08:00
|
|
|
}
|
2018-07-16 23:13:40 +08:00
|
|
|
curr_metric = new_metric;
|
2018-06-20 16:46:14 +08:00
|
|
|
for (auto new_wl : new_lengths)
|
2018-07-16 23:13:40 +08:00
|
|
|
metrics.at(new_wl.first) = new_wl.second;
|
2018-06-20 16:46:14 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
swap_fail:
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(oldBel, cell->name, STRENGTH_WEAK);
|
2018-06-20 16:46:14 +08:00
|
|
|
if (other != IdString()) {
|
2018-07-15 01:53:08 +08:00
|
|
|
ctx->bindBel(newBel, other, STRENGTH_WEAK);
|
2018-06-20 16:46:14 +08:00
|
|
|
}
|
|
|
|
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-07-24 21:45:49 +08:00
|
|
|
Loc curr_loc = ctx->getBelLocation(cell->bel);
|
2018-06-20 16:46:14 +08:00
|
|
|
while (true) {
|
2018-07-24 21:45:49 +08:00
|
|
|
int nx = ctx->rng(2 * diameter + 1) + std::max(curr_loc.x - diameter, 0);
|
|
|
|
int ny = ctx->rng(2 * diameter + 1) + std::max(curr_loc.y - diameter, 0);
|
2018-06-20 16:46:14 +08:00
|
|
|
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-07-16 23:13:40 +08:00
|
|
|
std::unordered_map<IdString, wirelen_t> metrics;
|
|
|
|
wirelen_t curr_metric = 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;
|
2018-06-29 19:12:44 +08:00
|
|
|
bool require_legal = false;
|
2018-06-29 21:18:44 +08:00
|
|
|
const float legalise_temp = 1;
|
2018-06-29 21:30:00 +08:00
|
|
|
const float post_legalise_temp = 20;
|
|
|
|
const float post_legalise_dia_scale = 2;
|
2018-06-20 16:46:14 +08:00
|
|
|
};
|
|
|
|
|
2018-07-12 00:15:08 +08:00
|
|
|
bool placer1(Context *ctx)
|
2018-06-20 16:46:14 +08:00
|
|
|
{
|
2018-06-21 23:56:45 +08:00
|
|
|
try {
|
2018-06-29 21:47:56 +08:00
|
|
|
SAPlacer placer(ctx);
|
2018-06-21 23:56:45 +08:00
|
|
|
placer.place();
|
|
|
|
log_info("Checksum: 0x%08x\n", ctx->checksum());
|
2018-06-23 21:16:24 +08:00
|
|
|
#ifndef NDEBUG
|
2018-07-17 23:27:50 +08:00
|
|
|
ctx->lock();
|
2018-06-23 21:16:24 +08:00
|
|
|
ctx->check();
|
2018-07-17 23:27:50 +08:00
|
|
|
ctx->unlock();
|
2018-06-23 21:16:24 +08:00
|
|
|
#endif
|
2018-06-21 23:56:45 +08:00
|
|
|
return true;
|
|
|
|
} catch (log_execution_error_exception) {
|
2018-06-23 21:16:24 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
ctx->check();
|
|
|
|
#endif
|
2018-06-21 23:56:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-13 23:07:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|