Implement dataset gen in c++

This commit is contained in:
Robin Dietzel 2023-11-07 21:59:50 +01:00
parent b8a3255cf5
commit 87dc5c0f49
4 changed files with 70 additions and 113 deletions

View File

@ -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)
install(TARGETS task1-randgen DESTINATION bin)
install(IMPORTED_RUNTIME_ARTIFACTS task1-auto DESTINATION lib)
install(IMPORTED_RUNTIME_ARTIFACTS task1-randgen DESTINATION lib)

View File

@ -1,109 +0,0 @@
#include <QRandomGenerator>
#include <random>
#include <fstream>
#include <fmt/format.h>
#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<typename T>
auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
std::string buf;
T convbuf;
while (std::getline(stream, buf)) {
convbuf = static_cast<T>(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<int32_t> 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<int32_t> 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<std::chrono::milliseconds>(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<int32_t> 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<std::chrono::milliseconds>(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<float>(t_seq.count()));
fmt::print("\nt_par = {: > 5.2f} ms", static_cast<float>(t_par.count()));
fmt::print("\nspeedup = {: > 5.2f}", (1.0 * t_seq / t_par));
fmt::print("\nDelta_t = {: > 5.2f} ms", static_cast<float>(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;
}
}

View File

@ -1,11 +1,11 @@
#include <fmt/format.h>
#include "fmt/format.h"
#include <vector>
#include <fstream>
#include <string>
#include <chrono>
#include <cmath>
#include "include/mergesort_mt.h"
#include "mergesort_mt.h"
/*
Create a simple sorting application that uses the mergesort algorithm to sort a

View File

@ -0,0 +1,56 @@
#include <QRandomGenerator>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <cmath>
#include <random>
#include <fstream>
#include <ranges>
#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;
}