From 2f5b94fe309619c221c3e1ea5aa48a4523bd3156 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Tue, 17 Jul 2018 16:27:50 +0100 Subject: [PATCH] Add basic external locking, lock from P&R --- common/nextpnr.h | 19 +++++++++++++++++++ common/placer1.cc | 14 ++++++++++++++ common/router1.cc | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/common/nextpnr.h b/common/nextpnr.h index e53e4d01..7aa5b100 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include #include @@ -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> nets; std::unordered_map> 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(this); } const Context *getCtx() const { return reinterpret_cast(this); } diff --git a/common/placer1.cc b/common/placer1.cc index 74a11040..b229616c 100644 --- a/common/placer1.cc +++ b/common/placer1.cc @@ -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) { diff --git a/common/router1.cc b/common/router1.cc index 94c7070e..a85de7c6 100644 --- a/common/router1.cc +++ b/common/router1.cc @@ -414,6 +414,7 @@ bool router1(Context *ctx) std::unordered_set 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; }