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

View File

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