From 87dc5c0f49966149d3b55b669e27b9031911d710 Mon Sep 17 00:00:00 2001 From: robtor Date: Tue, 7 Nov 2023 21:59:50 +0100 Subject: [PATCH] Implement dataset gen in c++ --- task1/CMakeLists.txt | 14 ++++- task1/main2.cpp | 109 --------------------------------- task1/{ => src}/task1-auto.cpp | 4 +- task1/src/task1-randgen.cpp | 56 +++++++++++++++++ 4 files changed, 70 insertions(+), 113 deletions(-) delete mode 100644 task1/main2.cpp rename task1/{ => src}/task1-auto.cpp (98%) create mode 100644 task1/src/task1-randgen.cpp diff --git a/task1/CMakeLists.txt b/task1/CMakeLists.txt index 9add3cd..7541291 100644 --- a/task1/CMakeLists.txt +++ b/task1/CMakeLists.txt @@ -27,12 +27,22 @@ add_custom_target(task1-dataset add_executable(task1-auto) add_dependencies(task1-auto task1-dataset) target_sources(task1-auto PRIVATE - task1-auto.cpp) + src/task1-auto.cpp) target_include_directories(task1-auto PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_link_libraries(task1-auto PRIVATE fmt::fmt) +# Add task1 target with an C++ written random data generator +add_executable(task1-randgen) +target_sources(task1-randgen PRIVATE + src/task1-randgen.cpp) +target_link_libraries(task1-randgen PRIVATE + fmt::fmt + Qt6::Core) +# Define install options install(TARGETS task1-auto DESTINATION bin) -install(IMPORTED_RUNTIME_ARTIFACTS task1-auto DESTINATION lib) \ No newline at end of file +install(TARGETS task1-randgen DESTINATION bin) +install(IMPORTED_RUNTIME_ARTIFACTS task1-auto DESTINATION lib) +install(IMPORTED_RUNTIME_ARTIFACTS task1-randgen DESTINATION lib) \ No newline at end of file diff --git a/task1/main2.cpp b/task1/main2.cpp deleted file mode 100644 index d958416..0000000 --- a/task1/main2.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include - -#include "include/mergesort_mt.h" - -//Random generator by Prof. Weber -auto gen_file_input(const std::string &fname) -> void { - FILE* fp = fopen(fname.c_str(), "w"); - int amount = 1e2; - int* randNum = (int *)malloc(amount * sizeof(int)); - for(int i = 0; i < amount; i++){ - *(randNum + i) = QRandomGenerator::global()->generate(); - fprintf(fp, "%d\n", *(randNum + i)); - } -} - -template -auto parse_file(std::ifstream &stream, std::vector &vec) -> void { - std::string buf; - T convbuf; - - while (std::getline(stream, buf)) { - convbuf = static_cast(std::stoul(buf)); - vec.emplace_back(std::move(convbuf)); - } -} - -int main(int argc, char *argv[]) -{ - const std::string fname = "b1-7_input.data"; - //Generate file first using Qt random generator - gen_file_input(fname); - try { - std::ifstream file(fname.c_str(), std::ios_base::in); - if (!file.is_open()) { - fmt::print("\nError opening file"); - return -1; - } - - fmt::print("\nOpened file {} sucessfully", fname); - std::vector dataset; - - parse_file(file, dataset); - fmt::print("\nRead {} values from {}", dataset.size(), fname); - - auto dataset_par = dataset; - auto dataset_seq = dataset; - - auto t1 = std::chrono::high_resolution_clock::now(); - MergeSorterMT msst([](int32_t a, int32_t b) { - return (a > b); - }, 0); - msst.sort(dataset_seq); - auto t2 = std::chrono::high_resolution_clock::now(); - - auto t_seq = std::chrono::duration_cast(t2 - t1); - fmt::print("\nSorted {} entries within {} ms in sequential", dataset_seq.size(), t_seq.count()); - - - const int threads = std::thread::hardware_concurrency(); - const int max_depth = std::sqrt(threads); - - t1 = std::chrono::high_resolution_clock::now(); - MergeSorterMT msmt([](int32_t a, int32_t b) { - return (a > b); - }, max_depth); - msmt.sort(dataset_par); - t2 = std::chrono::high_resolution_clock::now(); - - auto t_par = std::chrono::duration_cast(t2 - t1); - fmt::print("\nSorted {} entries within {} ms in parallel on a system having {} threads and a recursion depth of {}" - "\nresulting in a total count of {} threads", - dataset_seq.size(), t_par.count(), threads, max_depth, std::pow(2, max_depth)); - - auto eq = (dataset_seq == dataset_par); - fmt::print("\nCheck whether sorted arrays are equal: {}", (eq) ? "Equal" : "not equal"); - - fmt::print("\n\n------------Summary------------"); - fmt::print("\nt_seq = {: > 5.2f} ms", static_cast(t_seq.count())); - fmt::print("\nt_par = {: > 5.2f} ms", static_cast(t_par.count())); - fmt::print("\nspeedup = {: > 5.2f}", (1.0 * t_seq / t_par)); - fmt::print("\nDelta_t = {: > 5.2f} ms", static_cast(t_seq.count() - t_par.count())); - fmt::print("\n-------------------------------"); - - std::ofstream ofile("dataset.out.dat", std::ios_base::out); - if (!ofile.is_open()) { - fmt::print("\nError writing to file"); - return -1; - } - - for (auto &element: dataset_seq) { - ofile << std::to_string(element) << '\n'; - } - - file.close(); - ofile.flush(); - ofile.close(); - - fmt::print("\nWritten to output file"); - - return 0; - - } catch (std::exception &e) { - fmt::print("\nError occured: {}", e.what()); - return -1; - } -} \ No newline at end of file diff --git a/task1/task1-auto.cpp b/task1/src/task1-auto.cpp similarity index 98% rename from task1/task1-auto.cpp rename to task1/src/task1-auto.cpp index 22aea6f..9378404 100644 --- a/task1/task1-auto.cpp +++ b/task1/src/task1-auto.cpp @@ -1,11 +1,11 @@ -#include +#include "fmt/format.h" #include #include #include #include #include -#include "include/mergesort_mt.h" +#include "mergesort_mt.h" /* Create a simple sorting application that uses the mergesort algorithm to sort a diff --git a/task1/src/task1-randgen.cpp b/task1/src/task1-randgen.cpp new file mode 100644 index 0000000..6e3d9cc --- /dev/null +++ b/task1/src/task1-randgen.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include "fmt/format.h" + + +int main(int argc, char *argv[]) { + QCoreApplication app(argc, argv); + QCoreApplication::setApplicationName("Random dataset generator"); + QCoreApplication::setApplicationVersion("1.0"); + + QCommandLineParser parser; + parser.setApplicationDescription("Generates random dataset files for mergesort algorithm testing"); + parser.addHelpOption(); + parser.addVersionOption(); + parser.addPositionalArgument("destination", "Filename of where to place the generated data"); + parser.addPositionalArgument("num_values", "The power of 10 for the number of values to generate"); + parser.process(app); + + const QStringList args = parser.positionalArguments(); + const QString dest = args.at(0); + const int pow_value = args.at(1).toInt(); + const int num_values = std::pow(10, pow_value); + + QTextStream print(stdout); + + print << "Writing " << num_values << " values into " << dest << Qt::endl; + + QFile file(dest); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + print << "Error opening file: " << file.errorString() << Qt::endl; + app.exit(-1); + return app.exec(); + } + + QTextStream out(&file); + for (int i: std::views::iota(0, num_values)) { + out << QString::number(QRandomGenerator::global()->generate()) << '\n'; + } + file.flush(); + + QFileInfo finfo(file); + print << "Wrote " << num_values << " to " << dest << " with resulting size of " << (finfo.size()/1000000) << " mb" << Qt::endl; + file.close(); + + + app.exit(0); + return 0; +} \ No newline at end of file