Add basic external locking, lock from P&R

This commit is contained in:
Serge Bazanski 2018-07-17 16:27:50 +01:00
parent 498bef3f3e
commit 2f5b94fe30
3 changed files with 40 additions and 0 deletions

View File

@ -21,6 +21,8 @@
#include <algorithm>
#include <assert.h>
#include <memory>
#include <mutex>
#include <pthread.h>
#include <stdexcept>
#include <stdint.h>
#include <string>
@ -337,6 +339,11 @@ class DeterministicRNG
class BaseCtx : public IdStringDB, public DeterministicRNG
{
private:
std::mutex mutex;
bool mutex_owned;
pthread_t mutex_owner;
public:
std::unordered_map<IdString, std::unique_ptr<NetInfo>> nets;
std::unordered_map<IdString, std::unique_ptr<CellInfo>> cells;
@ -344,6 +351,18 @@ class BaseCtx : public IdStringDB, public DeterministicRNG
BaseCtx() {}
~BaseCtx() {}
void lock(void)
{
mutex.lock();
mutex_owner = pthread_self();
}
void unlock(void)
{
NPNR_ASSERT(pthread_equal(pthread_self(), mutex_owner) != 0);
mutex.unlock();
}
Context *getCtx() { return reinterpret_cast<Context *>(this); }
const Context *getCtx() const { return reinterpret_cast<const Context *>(this); }

View File

@ -80,6 +80,7 @@ class SAPlacer
size_t placed_cells = 0;
// Initial constraints placer
ctx->lock();
for (auto &cell_entry : ctx->cells) {
CellInfo *cell = cell_entry.second.get();
auto loc = cell->attrs.find(ctx->id("BEL"));
@ -118,16 +119,19 @@ class SAPlacer
}
std::sort(autoplaced.begin(), autoplaced.end(), [](CellInfo *a, CellInfo *b) { return a->name < b->name; });
ctx->shuffle(autoplaced);
ctx->unlock();
// Place cells randomly initially
log_info("Creating initial placement for remaining %d cells.\n", int(autoplaced.size()));
for (auto cell : autoplaced) {
ctx->lock();
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()));
ctx->unlock();
}
if ((placed_cells - constr_placed_cells) % 500 != 0)
log_info(" initial placement placed %d/%d cells\n", int(placed_cells - constr_placed_cells),
@ -136,6 +140,7 @@ class SAPlacer
log_info("Running simulated annealing placer.\n");
// Calculate metric after initial placement
ctx->lock();
curr_metric = 0;
curr_tns = 0;
for (auto &net : ctx->nets) {
@ -143,6 +148,7 @@ class SAPlacer
metrics[net.first] = wl;
curr_metric += wl;
}
ctx->unlock();
int n_no_progress = 0;
double avg_metric = curr_metric;
@ -150,6 +156,7 @@ class SAPlacer
// Main simulated annealing loop
for (int iter = 1;; iter++) {
ctx->lock();
n_move = n_accept = 0;
improved = false;
@ -169,6 +176,7 @@ class SAPlacer
try_swap_position(cell, try_bel);
}
}
// Heuristic to improve placement on the 8k
if (improved)
n_no_progress = 0;
@ -178,6 +186,7 @@ class SAPlacer
if (temp <= 1e-3 && n_no_progress >= 5) {
if (iter % 5 != 0)
log_info(" at iteration #%d: temp = %f, cost = %f\n", iter, temp, double(curr_metric));
ctx->unlock();
break;
}
@ -232,8 +241,10 @@ class SAPlacer
metrics[net.first] = wl;
curr_metric += wl;
}
ctx->unlock();
}
// Final post-pacement validitiy check
ctx->lock();
for (auto bel : ctx->getBels()) {
IdString cell = ctx->getBoundBelCell(bel);
if (!ctx->isBelLocationValid(bel)) {
@ -251,6 +262,7 @@ class SAPlacer
}
}
}
ctx->unlock();
return true;
}
@ -436,7 +448,9 @@ bool placer1(Context *ctx)
placer.place();
log_info("Checksum: 0x%08x\n", ctx->checksum());
#ifndef NDEBUG
ctx->lock();
ctx->check();
ctx->unlock();
#endif
return true;
} catch (log_execution_error_exception) {

View File

@ -414,6 +414,7 @@ bool router1(Context *ctx)
std::unordered_set<IdString> netsQueue;
ctx->lock();
for (auto &net_it : ctx->nets) {
auto net_name = net_it.first;
auto net_info = net_it.second.get();
@ -478,6 +479,7 @@ bool router1(Context *ctx)
estimatedTotalDelayCnt++;
}
}
ctx->unlock();
log_info("estimated total wire delay: %.2f (avg %.2f)\n", float(estimatedTotalDelay),
float(estimatedTotalDelay) / estimatedTotalDelayCnt);
@ -493,6 +495,7 @@ bool router1(Context *ctx)
#endif
return false;
}
ctx->lock();
iterCnt++;
if (ctx->verbose)
@ -621,6 +624,8 @@ bool router1(Context *ctx)
if (iterCnt == 8 || iterCnt == 16 || iterCnt == 32 || iterCnt == 64 || iterCnt == 128)
ripup_penalty += ctx->getRipupDelayPenalty();
ctx->unlock();
}
log_info("routing complete after %d iterations.\n", iterCnt);
@ -637,7 +642,9 @@ bool router1(Context *ctx)
return true;
} catch (log_execution_error_exception) {
#ifndef NDEBUG
ctx->lock();
ctx->check();
ctx->unlock();
#endif
return false;
}