Support for sequential and parallel sort

This commit is contained in:
Robin Dietzel 2023-11-09 21:09:34 +01:00
parent c6cdc7ea6e
commit af34983a46
3 changed files with 74 additions and 6 deletions

View File

@ -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)

View File

@ -8,7 +8,7 @@
#include <ranges>
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");

View File

@ -7,8 +7,11 @@
#include <cmath>
#include <thread>
#include <ranges>
#include <chrono>
int main(int argc, char *argv[]) {
#include <mergesort_mt.h>
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<int32_t> 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<int32_t> 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<std::chrono::milliseconds>(diff).count() << " ms" << Qt::endl;
}
if (parser.isSet(parallel)) {
auto buf = dataset;
auto t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t> 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<std::chrono::milliseconds>(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;