2018-06-22 22:21:20 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Miodrag Milanovic <miodrag@symbioticeda.com>
|
|
|
|
*
|
|
|
|
* 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-21 21:41:40 +08:00
|
|
|
#include "worker.h"
|
|
|
|
#include <fstream>
|
2018-06-21 23:44:18 +08:00
|
|
|
#include "bitstream.h"
|
|
|
|
#include "design_utils.h"
|
2018-06-21 21:41:40 +08:00
|
|
|
#include "jsonparse.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "pack.h"
|
|
|
|
#include "pcf.h"
|
|
|
|
#include "place_sa.h"
|
|
|
|
#include "route.h"
|
|
|
|
#include "timing.h"
|
|
|
|
|
2018-06-22 19:10:27 +08:00
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2018-06-22 18:11:22 +08:00
|
|
|
struct WorkerInterruptionRequested
|
2018-06-21 21:41:40 +08:00
|
|
|
{
|
2018-06-22 18:11:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Worker::Worker(Context *_ctx, TaskManager *parent) : ctx(_ctx)
|
|
|
|
{
|
|
|
|
log_write_function = [this, parent](std::string text) {
|
|
|
|
Q_EMIT log(text);
|
|
|
|
if (parent->shouldTerminate()) {
|
|
|
|
parent->clearTerminate();
|
|
|
|
throw WorkerInterruptionRequested();
|
|
|
|
}
|
2018-06-22 22:48:56 +08:00
|
|
|
if (parent->isPaused()) {
|
2018-06-22 21:35:07 +08:00
|
|
|
Q_EMIT taskPaused();
|
|
|
|
}
|
2018-06-22 19:10:27 +08:00
|
|
|
while (parent->isPaused()) {
|
2018-06-22 21:35:07 +08:00
|
|
|
if (parent->shouldTerminate()) {
|
|
|
|
parent->clearTerminate();
|
|
|
|
throw WorkerInterruptionRequested();
|
|
|
|
}
|
2018-06-22 18:49:20 +08:00
|
|
|
QThread::sleep(1);
|
|
|
|
}
|
2018-06-22 18:11:22 +08:00
|
|
|
};
|
2018-06-21 21:41:40 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 21:35:07 +08:00
|
|
|
void Worker::loadfile(const std::string &filename)
|
2018-06-21 21:41:40 +08:00
|
|
|
{
|
2018-06-22 21:35:07 +08:00
|
|
|
Q_EMIT taskStarted();
|
2018-06-21 21:41:40 +08:00
|
|
|
std::string fn = filename;
|
2018-06-21 22:16:58 +08:00
|
|
|
std::ifstream f(fn);
|
2018-06-21 23:56:45 +08:00
|
|
|
try {
|
2018-06-22 21:35:07 +08:00
|
|
|
Q_EMIT loadfile_finished(parse_json_file(f, fn, ctx));
|
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 22:55:13 +08:00
|
|
|
void Worker::loadpcf(const std::string &filename)
|
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
std::string fn = filename;
|
|
|
|
std::ifstream f(fn);
|
|
|
|
try {
|
|
|
|
Q_EMIT loadpcf_finished(apply_pcf(ctx, f));
|
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:49:32 +08:00
|
|
|
void Worker::saveasc(const std::string &filename)
|
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
std::string fn = filename;
|
|
|
|
std::ofstream f(fn);
|
|
|
|
try {
|
|
|
|
write_asc(ctx, f);
|
|
|
|
Q_EMIT saveasc_finished(true);
|
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 21:35:07 +08:00
|
|
|
void Worker::pack()
|
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
try {
|
2018-06-23 22:03:22 +08:00
|
|
|
bool res = pack_design(ctx);
|
|
|
|
print_utilisation(ctx);
|
|
|
|
Q_EMIT pack_finished(res);
|
2018-06-22 21:35:07 +08:00
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 22:03:22 +08:00
|
|
|
void Worker::budget(double freq)
|
2018-06-22 21:35:07 +08:00
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
try {
|
2018-06-21 23:56:45 +08:00
|
|
|
assign_budget(ctx, freq);
|
2018-06-23 22:03:22 +08:00
|
|
|
Q_EMIT budget_finish(true);
|
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::place(bool timing_driven)
|
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
try {
|
|
|
|
Q_EMIT place_finished(place_design_sa(ctx, timing_driven));
|
2018-06-22 21:35:07 +08:00
|
|
|
} catch (WorkerInterruptionRequested) {
|
|
|
|
Q_EMIT taskCanceled();
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 21:41:40 +08:00
|
|
|
|
2018-06-22 21:35:07 +08:00
|
|
|
void Worker::route()
|
|
|
|
{
|
|
|
|
Q_EMIT taskStarted();
|
|
|
|
try {
|
|
|
|
Q_EMIT route_finished(route_design(ctx));
|
2018-06-22 18:11:22 +08:00
|
|
|
} catch (WorkerInterruptionRequested) {
|
2018-06-22 21:35:07 +08:00
|
|
|
Q_EMIT taskCanceled();
|
2018-06-21 23:56:45 +08:00
|
|
|
}
|
2018-06-21 21:41:40 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 18:49:20 +08:00
|
|
|
TaskManager::TaskManager(Context *ctx) : toTerminate(false), toPause(false)
|
2018-06-21 21:41:40 +08:00
|
|
|
{
|
2018-06-22 18:11:22 +08:00
|
|
|
Worker *worker = new Worker(ctx, this);
|
2018-06-21 21:41:40 +08:00
|
|
|
worker->moveToThread(&workerThread);
|
2018-06-22 22:48:56 +08:00
|
|
|
|
2018-06-21 21:41:40 +08:00
|
|
|
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
2018-06-22 22:48:56 +08:00
|
|
|
|
2018-06-22 21:35:07 +08:00
|
|
|
connect(this, &TaskManager::loadfile, worker, &Worker::loadfile);
|
2018-06-23 22:55:13 +08:00
|
|
|
connect(this, &TaskManager::loadpcf, worker, &Worker::loadpcf);
|
2018-06-26 19:49:32 +08:00
|
|
|
connect(this, &TaskManager::saveasc, worker, &Worker::saveasc);
|
2018-06-22 21:35:07 +08:00
|
|
|
connect(this, &TaskManager::pack, worker, &Worker::pack);
|
2018-06-23 22:03:22 +08:00
|
|
|
connect(this, &TaskManager::budget, worker, &Worker::budget);
|
2018-06-22 21:35:07 +08:00
|
|
|
connect(this, &TaskManager::place, worker, &Worker::place);
|
|
|
|
connect(this, &TaskManager::route, worker, &Worker::route);
|
|
|
|
|
2018-06-21 21:41:40 +08:00
|
|
|
connect(worker, &Worker::log, this, &TaskManager::info);
|
2018-06-23 22:06:49 +08:00
|
|
|
connect(worker, &Worker::loadfile_finished, this, &TaskManager::loadfile_finished);
|
2018-06-23 22:55:13 +08:00
|
|
|
connect(worker, &Worker::loadpcf_finished, this, &TaskManager::loadpcf_finished);
|
2018-06-26 19:49:32 +08:00
|
|
|
connect(worker, &Worker::saveasc_finished, this, &TaskManager::saveasc_finished);
|
2018-06-22 21:35:07 +08:00
|
|
|
connect(worker, &Worker::pack_finished, this, &TaskManager::pack_finished);
|
2018-06-23 22:03:22 +08:00
|
|
|
connect(worker, &Worker::budget_finish, this, &TaskManager::budget_finish);
|
2018-06-23 22:06:49 +08:00
|
|
|
connect(worker, &Worker::place_finished, this, &TaskManager::place_finished);
|
|
|
|
connect(worker, &Worker::route_finished, this, &TaskManager::route_finished);
|
2018-06-22 22:48:56 +08:00
|
|
|
|
2018-06-22 21:35:07 +08:00
|
|
|
connect(worker, &Worker::taskCanceled, this, &TaskManager::taskCanceled);
|
|
|
|
connect(worker, &Worker::taskStarted, this, &TaskManager::taskStarted);
|
|
|
|
connect(worker, &Worker::taskPaused, this, &TaskManager::taskPaused);
|
|
|
|
|
2018-06-21 21:41:40 +08:00
|
|
|
workerThread.start();
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:44:18 +08:00
|
|
|
TaskManager::~TaskManager()
|
2018-06-21 21:41:40 +08:00
|
|
|
{
|
2018-06-22 19:10:27 +08:00
|
|
|
if (workerThread.isRunning())
|
2018-06-22 18:24:50 +08:00
|
|
|
terminate_thread();
|
2018-06-21 21:41:40 +08:00
|
|
|
workerThread.quit();
|
|
|
|
workerThread.wait();
|
|
|
|
}
|
|
|
|
|
2018-06-22 18:11:22 +08:00
|
|
|
void TaskManager::info(const std::string &result) { Q_EMIT log(result); }
|
|
|
|
|
|
|
|
void TaskManager::terminate_thread()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
2018-06-22 21:35:07 +08:00
|
|
|
toPause = false;
|
2018-06-22 18:11:22 +08:00
|
|
|
toTerminate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TaskManager::shouldTerminate()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
return toTerminate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::clearTerminate()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
toTerminate = false;
|
2018-06-22 18:49:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::pause_thread()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
toPause = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::continue_thread()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
toPause = false;
|
2018-06-22 21:35:07 +08:00
|
|
|
Q_EMIT taskStarted();
|
2018-06-22 18:49:20 +08:00
|
|
|
}
|
2018-06-22 21:35:07 +08:00
|
|
|
|
2018-06-22 18:49:20 +08:00
|
|
|
bool TaskManager::isPaused()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
return toPause;
|
2018-06-22 19:10:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NEXTPNR_NAMESPACE_END
|