#include "MainWindow.h" #include "ui_MainWindow.h" #include #include #include #include "header/strategies/ScanConversion.h" #include "header/strategies/Rejection.h" #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); registerStrategies(); _strategy = Q_NULLPTR; } 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) REGISTER_STRATEGY(Rejection) } 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( 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; _strategy->ReadParams(paramPath, &_scenGenOutput); QByteArray arr; quint64 width; quint64 height = 0; if (!FileHelper::ReadInputFile(arr, dataPath, &width, &height)) { addToConsole("Some thing wnet wrong in input file"); abort(); return; } ImageFormat format; format.image_channel_order = CL_RGBA; #ifdef USE_DBL format.image_channel_data_type = CL_UNSIGNED_INT16; #else format.image_channel_data_type = CL_UNSIGNED_INT8; #endif Image2D* inFrame = static_cast(_openCLHelper .frame2CLFrame(_CLContext, format, QVector{width, height}, true)); CLQueue.enqueueWriteImage(*inFrame, CL_TRUE, array {0, 0, 0}, array {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 {0, 0, 0}, array {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"); } }); } 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; FileHelper::ReadInputFile(cppout, outputDir.path() + "/" + filename, &w1, &h1); if(!QFile::exists(matlabDir.path() + "/" + filename)) { addToConsole("matlab file for " + filename + " does not exist"); continue; } FileHelper::ReadInputFile(matout, matlabDir.path() + "/" + filename, &w2, &h2); if (w1 != OUT_WIDTH || w2 != OUT_WIDTH) { addToConsole("Width mismatch"); continue; } if (h1 != OUT_HEIGHT || h2 != OUT_HEIGHT) { addToConsole("Height mismatch"); continue; } ulong max = 0; uint imax, jmax; for(int i = 0; i < w1; i++) { for(int j = 0; j < h1; j++) { 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; } } } 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"); } addToConsole("Compare done, total max: " + QString::number(totalMax)); }); } void MainWindow::on_btn_clear_clicked() { ui->rtb_console->clear(); }