#include "MainWindow.h" #include "ui_MainWindow.h" #include #include #include #include "model/processor/BIP.h" #include "model/processor/strategies/DynCont.h" #include "model/processor/strategies/GrayMap.h" #include "model/processor/strategies/TintMap.h" #include "model/processor/strategies/Sri.h" #include "model/processor/strategies/ScanConversion.h" #include "model/processor/strategies/Rejection.h" #include "model/processor/strategies/Enhance.h" #include "model/processor/strategies/Persist.h" #include "model/processor/strategies/SimpleAdd.h" #include "header/FileHelper.h" 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(); BIP::getInstance()->init(_CLContext, _openCLHelper); _scratchPad = new Buffer(_CLContext, CL_MEM_READ_WRITE, SCRATCH_PAD_SIZE * sizeof(float)); // _scratch = new float[SCRATCH_PAD_SIZE]; // cl_int error = BIP::getInstance()->CLQueue.enqueueWriteBuffer(*_scratchPad, CL_TRUE, 0, SCRATCH_PAD_SIZE * sizeof(float), _scratch); // qDebug() << "hi: " << error; registerStrategies(); _strategy = Q_NULLPTR; connect(this, &MainWindow::console, this, [this](QString message){ ui->rtb_console->append(">>> " + message);}); } 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) { emit this->console(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) REGISTER_STRATEGY(Sri) REGISTER_STRATEGY(TintMap) REGISTER_STRATEGY(GrayMap) REGISTER_STRATEGY(DynCont) REGISTER_STRATEGY(Enhance) REGISTER_STRATEGY(Persist) REGISTER_STRATEGY(SimpleAdd) } 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), Q_ARG(const QObject*, this) )); addToConsole("Strategy was created"); } void MainWindow::readParam(QString path, int mode) { QFile file(path); file.open(QIODevice::ReadOnly); char data[8192]; //first line is extra file.readLine(data, 8192); QString str(data); str = str.remove(str.length() - 1, 1); auto sl = str.split(","); int index = 0; int temp; switch (mode) { //sc case 0: temp = sl[index++].PARSE; update_field(&_scenGenOutput.linear, INP2MYFLT(temp) == 0); temp = sl[index++].PARSE; update_field(&_scenGenOutput.depth, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.probeRadius, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.fieldOfView, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.probeFieldOfView, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.virtualOriginalZ, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.minScanAx, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.minScanAz, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.maxScanAx, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.startDepth, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.steering, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.rxLineNo, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.rxFocusPointNo, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.rxLineDaz, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.rxPointDax, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.virtualConvex, INP2MYFLT(temp) != 0); temp = sl[index++].PARSE; update_field(&_scenGenOutput.vcMaxTheta, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.angle, INP2MYFLT(temp)); break; case 1: temp = sl[index++].PARSE; update_field(&_scenGenOutput.rejectThreshold, INP2MYFLT(temp)); break; case 2: temp = sl[index++].PARSE; update_field(&_scenGenOutput.sri, INP2MYFLT(temp)); break; case 3: temp = sl[index++].PARSE; update_field(&_scenGenOutput.tintMapSelector, INP2MYFLT(temp)); break; case 4: temp = sl[index++].PARSE; update_field(&_scenGenOutput.grayMapSelector, INP2MYFLT(temp)); break; case 5: temp = sl[index++].PARSE; update_field(&_scenGenOutput.dynContSelector, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.dynContGain, INP2MYFLT(temp)); temp = sl[index++].PARSE; update_field(&_scenGenOutput.compressionType, INP2MYFLT(temp)); break; case 6: temp = sl[index++].PARSE; update_field(&_scenGenOutput.enhance, INP2MYFLT(temp)); update_field(&_scenGenOutput.enhanceAlgorithm, 0); break; case 7: update_field(&_scenGenOutput.persist, 1); break; } } 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; readParam(paramPath, ui->cb_kernelName->currentIndex()); QByteArray arr; quint64 width; quint64 height = 0; if (!FileHelper::ReadInputFile(arr, dataPath, &width, &height)) { addToConsole("Some thing wnet wrong in input file"); abort(); return; } OUT_WIDTH = ui->cb_kernelName->currentIndex() ? width : 1000; OUT_HEIGHT = ui->cb_kernelName->currentIndex() ? height : 1000; 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)); BIP::getInstance()->CLQueue.enqueueWriteImage(*inFrame, CL_TRUE, array {0, 0, 0}, array {width, height, 1}, width * sizeof (myflt), 0, arr.data()); update_field(&_scenGenOutput.outputWidth, (uint)OUT_WIDTH); update_field(&_scenGenOutput.outputHeight, (uint)OUT_HEIGHT); _strategy->cpuProcess(_scenGenOutput); auto outFrame = _strategy->processKernel(inFrame, _scratchPad); char *out; out = (char*)malloc(OUT_WIDTH * OUT_HEIGHT * sizeof (myflt)); BIP::getInstance()->CLQueue.enqueueReadImage(*outFrame, CL_TRUE, array {0, 0, 0}, array {(quint64)OUT_WIDTH, (quint64)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) { OUT_HEIGHT = 0; FileHelper::GetFileSIze(matlabDir.path() + "/" + filename, &OUT_WIDTH, &OUT_HEIGHT); 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"); delete [] cppout; delete [] matout; continue; } FileHelper::ReadInputFile(matout, matlabDir.path() + "/" + filename, &w2, &h2); if (w1 != OUT_WIDTH || w2 != OUT_WIDTH) { addToConsole("Width mismatch"); delete [] cppout; delete [] matout; continue; } if (h1 != OUT_HEIGHT || h2 != OUT_HEIGHT) { addToConsole("Height mismatch"); delete [] cppout; delete [] matout; continue; } ulong max = 0; uint imax = 0, jmax = 0; 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; if(max > 0) { 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"); delete [] cppout; delete [] matout; } addToConsole("Compare done, total max: " + QString::number(totalMax)); }); } void MainWindow::on_btn_clear_clicked() { ui->rtb_console->clear(); }