diff --git a/task1/CMakeLists.txt b/task1/CMakeLists.txt index d44b35a..819dda1 100644 --- a/task1/CMakeLists.txt +++ b/task1/CMakeLists.txt @@ -44,6 +44,8 @@ target_link_libraries(task1-randgen PRIVATE add_executable(task1-sorter) target_sources(task1-sorter PRIVATE src/task1-sorter.cpp) +target_include_directories(task1-sorter PRIVATE +${CMAKE_CURRENT_SOURCE_DIR}/include) target_link_libraries(task1-sorter PRIVATE Qt6::Core) diff --git a/task1/src/task1-randgen.cpp b/task1/src/task1-randgen.cpp index 410da5f..813ba45 100644 --- a/task1/src/task1-randgen.cpp +++ b/task1/src/task1-randgen.cpp @@ -8,7 +8,7 @@ #include -int main(int argc, char *argv[]) { +auto main(int argc, char *argv[]) -> int{ QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("Random dataset generator"); QCoreApplication::setApplicationVersion("1.0"); diff --git a/task1/src/task1-sorter.cpp b/task1/src/task1-sorter.cpp index 1e6099a..5765beb 100644 --- a/task1/src/task1-sorter.cpp +++ b/task1/src/task1-sorter.cpp @@ -7,8 +7,11 @@ #include #include #include +#include -int main(int argc, char *argv[]) { +#include + +auto main(int argc, char *argv[]) -> int { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("Multi purpose mergesort application"); QCoreApplication::setApplicationVersion("1.0.42"); @@ -16,7 +19,8 @@ int main(int argc, char *argv[]) { QCommandLineParser parser; - parser.setApplicationDescription("Used to run either sequential or parallel mergesort on a texfile containing ascii encoded int32s"); + parser.setApplicationDescription( + "Used to run either sequential or parallel mergesort on a texfile containing ascii encoded int32s"); parser.addHelpOption(); parser.addVersionOption(); @@ -30,18 +34,48 @@ int main(int argc, char *argv[]) { parser.addOption(nthreads); parser.addPositionalArgument("dataset", "Filename where to load the data from"); - parser.process(app); + const QStringList args = parser.positionalArguments(); + + if (args.length() != 1) { + parser.showHelp(-1); + } + + const QString source = args.at(0); + QFile input(source); + if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) { + print << "Could not open file " << source << " for reading" << Qt::endl; + print << input.errorString(); + app.exit(-1); + return app.exec(); + } + + std::vector dataset; + + QTextStream stream(&input); + while (!stream.atEnd()) { + QString line = stream.readLine(); + bool ok; + int parsed_value = line.toUInt(&ok); + if (!ok) { + print << "Error converting value: " << line << Qt::endl; + } else { + dataset.push_back(std::move(parsed_value)); + } + } + + print << "Read " << dataset.size() << " values from " << source << Qt::endl; + const int threads = std::thread::hardware_concurrency(); int max_depth = std::sqrt(threads); print << "Hardware concurrency of " << threads << " detected" << Qt::endl; - if(parser.isSet(nthreads)) { + if (parser.isSet(nthreads)) { bool ok; max_depth = parser.value(nthreads).toInt(&ok); - if(!ok) { + if (!ok) { parser.showHelp(-1); } print << "Overwriting maximum parallelized recursion depth with " << max_depth << Qt::endl; @@ -49,7 +83,39 @@ int main(int argc, char *argv[]) { print << "Assuming default parallelized recursion depth via sqrt(nthreads) of " << max_depth << Qt::endl; } + if (parser.isSet(sequential)) { + auto buf = dataset; + auto t1 = std::chrono::high_resolution_clock::now(); + MergeSorterMT sorter( + [](int32_t a, int32_t b) { + return (a > b); + }, 0); + sorter.sort(buf); + auto t2 = std::chrono::high_resolution_clock::now(); + auto diff = t2 - t1; + print << "=> Duration for sequential sort: " << std::chrono::duration_cast(diff).count() << " ms" << Qt::endl; + } + + if (parser.isSet(parallel)) { + auto buf = dataset; + auto t1 = std::chrono::high_resolution_clock::now(); + MergeSorterMT sorter( + [](int32_t a, int32_t b) { + return (a > b); + }, max_depth); + sorter.sort(buf); + auto t2 = std::chrono::high_resolution_clock::now(); + auto diff = t2 - t1; + + print << "=> Duration for parallel sort: " << std::chrono::duration_cast(diff).count() << " ms" << Qt::endl; + } + + if(parser.isSet(output)) { + print << "Sooory, not yet implemented :( you might do it yourself!" << Qt::endl; + app.exit(-1); + return app.exec(); + } app.exit(0); return 0;