You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
7.4 KiB

5 years ago
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QFileDialog>
#include <QFile>
#include <QtConcurrent/QtConcurrent>
5 years ago
5 years ago
#include "header/strategies/ScanConversion.h"
#include "header/strategies/Rejection.h"
5 years ago
#include "header/FileHelper.h"
#define OUT_WIDTH 1000
#define OUT_HEIGHT 1000
MainWindow* MainWindow::_instance;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, _settings("hasis", "kernel tester")
{
_instance = this;
ui->setupUi(this);
_CLContext = _openCLHelper.getContext();
CLQueue = _openCLHelper.createCommandQueue(_CLContext,
_openCLHelper
.getDevicesByContext(
_CLContext)[0]);
_scratchPad = new Buffer(_CLContext, CL_MEM_READ_WRITE, 1024);
_scratch = new double[128];
CLQueue.enqueueWriteBuffer(*_scratchPad, CL_TRUE, 0, 1024, _scratch);
5 years ago
registerStrategies();
5 years ago
_strategy = Q_NULLPTR;
5 years ago
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::browse(QLineEdit *edit)
{
QFileDialog dialog;
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::ShowDirsOnly);
if (dialog.exec() == QDialog::Rejected)
return;
edit->setText(dialog.selectedFiles()[0]);
}
void MainWindow::abort()
{
addToConsole("Operation is aborted!");
}
void MainWindow::addToConsole(QString message)
{
ui->rtb_console->append(">>> " + message);
}
bool MainWindow::checkPath(QString path, QString pathName)
{
QDir temp(path);
if(!temp.exists() || path == "")
{
addToConsole(pathName + " directory is not valid");
abort();
return false;
}
return true;
}
void MainWindow::registerStrategies()
{
REGISTER_STRATEGY(ScanConversion)
5 years ago
REGISTER_STRATEGY(Rejection)
5 years ago
}
void MainWindow::pushBackStrategy(const QString strategyName, const QString kernelFolder)
{
int id = QMetaType::type(strategyName.toStdString().data());
if (id == QMetaType::UnknownType)
{
addToConsole("scenario is not registered");
throw;
}
_strategy =
static_cast<IProcessStrategy*>(
QMetaType::metaObjectForType(id)->newInstance(
Q_ARG(const Context, _CLContext),
Q_ARG(const QString, kernelFolder)
));
addToConsole("Strategy was created");
}
void MainWindow::on_btn_browseInput_clicked()
{
browse(ui->tb_inpDir);
}
void MainWindow::on_btn_browseOutput_clicked()
{
browse(ui->tb_outpDir);
}
void MainWindow::on_btn_browseMatlabOutput_clicked()
{
browse(ui->tb_matlabOutp);
}
void MainWindow::on_btn_browseWorkingDir_clicked()
{
browse(ui->tb_workingDir);
}
void MainWindow::on_btn_test_clicked()
{
if (!checkPath(ui->tb_inpDir->text(), "Input"))
return;
if (!checkPath(ui->tb_outpDir->text(), "Output"))
return;
if (!checkPath(ui->tb_workingDir->text(), "Working"))
return;
QDir inputDir(ui->tb_inpDir->text());
QDir outputDir(ui->tb_outpDir->text());
QDir workingDir(ui->tb_workingDir->text());
if(ui->cb_kernelName->currentIndex() < 0)
{
addToConsole("Please select a kernel to test");
abort();
return;
}
if(_strategy)
_strategy->finalize();
pushBackStrategy(ui->cb_kernelName->currentText(), workingDir.path() + "/kernels");
QtConcurrent::run([this, inputDir, outputDir](){
auto inputs = inputDir.entryList(QStringList() << "*.csv", QDir::Files);
foreach(QString filename, inputs)
{
addToConsole("processing " + filename);
auto dataPath = inputDir.path() + "/" + filename;
auto paramPath = inputDir.path() + "/params/" + filename;
5 years ago
_strategy->ReadParams(paramPath, &_scenGenOutput);
5 years ago
QByteArray arr;
quint64 width;
quint64 height = 0;
if (!FileHelper::ReadInputFile(arr, dataPath, &width, &height))
{
addToConsole("Some thing wnet wrong in input file");
abort();
return;
}
5 years ago
ImageFormat format;
format.image_channel_order = CL_RGBA;
5 years ago
#ifdef USE_DBL
format.image_channel_data_type = CL_UNSIGNED_INT16;
5 years ago
#else
format.image_channel_data_type = CL_UNSIGNED_INT8;
5 years ago
#endif
Image2D* inFrame = static_cast<Image2D*>(_openCLHelper
.frame2CLFrame(_CLContext, format, QVector<quint64>{width, height}, true));
CLQueue.enqueueWriteImage(*inFrame, CL_TRUE, array<size_type, 3> {0, 0, 0},
array<size_type, 3> {width, height, 1},
width * sizeof (myflt),
0,
arr.data());
update_field(&_scenGenOutput.outputWidth, OUT_WIDTH);
update_field(&_scenGenOutput.outputHeight, OUT_HEIGHT);
_strategy->cpuProcess(_scenGenOutput);
auto outFrame = _strategy->processKernel(inFrame, _scratchPad);
char *out;
out = (char*)malloc(OUT_WIDTH * OUT_WIDTH * sizeof (char) * sizeof (myflt));
CLQueue.enqueueReadImage(*outFrame,
CL_TRUE,
array<size_type, 3> {0, 0, 0},
array<size_type, 3> {OUT_WIDTH, OUT_HEIGHT, 1},
OUT_WIDTH * sizeof (myflt),
0,
out);
FileHelper::WriteOutputFile(out, outputDir.path() + "/" + filename, OUT_WIDTH, OUT_HEIGHT);
addToConsole(filename + " has been processed");
addToConsole("*************************\r\n");
}
});
5 years ago
}
void MainWindow::on_btn_save_clicked()
{
_settings.setValue("inputDir", ui->tb_inpDir->text());
_settings.setValue("outputDir", ui->tb_outpDir->text());
_settings.setValue("matlabDir", ui->tb_matlabOutp->text());
_settings.setValue("workingDir", ui->tb_workingDir->text());
}
void MainWindow::on_btn_load_clicked()
{
ui->tb_inpDir->setText(_settings.value("inputDir").toString());
ui->tb_outpDir->setText(_settings.value("outputDir").toString());
ui->tb_matlabOutp->setText(_settings.value("matlabDir").toString());
ui->tb_workingDir->setText(_settings.value("workingDir").toString());
}
void MainWindow::on_btn_compare_clicked()
{
if (!checkPath(ui->tb_outpDir->text(), "Output"))
return;
if (!checkPath(ui->tb_matlabOutp->text(), "Matlab output"))
return;
QDir matlabDir(ui->tb_matlabOutp->text());
QDir outputDir(ui->tb_outpDir->text());
QtConcurrent::run([this, outputDir, matlabDir](){
auto totalMax = 0UL;
auto outputs = outputDir.entryList(QStringList() << "*.csv", QDir::Files);
foreach(QString filename, outputs)
{
addToConsole("Comaring " + filename);
auto cppout = new myint[OUT_WIDTH * OUT_HEIGHT];
auto matout = new myint[OUT_WIDTH * OUT_HEIGHT];
quint64 w1, w2;
quint64 h1 = 0, h2 = 0;
5 years ago
FileHelper::ReadInputFile(cppout, outputDir.path() + "/" + filename, &w1, &h1);
5 years ago
if(!QFile::exists(matlabDir.path() + "/" + filename))
{
addToConsole("matlab file for " + filename + " does not exist");
continue;
}
FileHelper::ReadInputFile(matout, matlabDir.path() + "/" + filename, &w2, &h2);
5 years ago
if (w1 != OUT_WIDTH || w2 != OUT_WIDTH)
{
addToConsole("Width mismatch");
continue;
}
if (h1 != OUT_HEIGHT || h2 != OUT_HEIGHT)
{
addToConsole("Height mismatch");
continue;
}
5 years ago
ulong max = 0;
uint imax, jmax;
for(int i = 0; i < w1; i++)
5 years ago
{
for(int j = 0; j < h1; j++)
5 years ago
{
if(abs(cppout[j * w1 + i] - matout[j * w1 + i]) > max)
{
max = abs(cppout[j * w1 + i] - matout[j * w1 + i]);
imax = i;
jmax = j;
}
5 years ago
}
}
if(max > totalMax)
totalMax = max;
addToConsole(QString::number(max)
+ " ["+ QString::number(jmax)
+ ", " + QString::number(imax) + "]");
addToConsole(QString::number(cppout[jmax * w1 + imax], 16) + " -- " +
QString::number(matout[jmax * w1 + imax], 16));
addToConsole("*************************\r\n");
5 years ago
}
addToConsole("Compare done, total max: " + QString::number(totalMax));
});
}
5 years ago
void MainWindow::on_btn_clear_clicked()
{
ui->rtb_console->clear();
5 years ago
}