aca-tasks/task1/main2.cpp
2023-11-07 21:24:24 +01:00

109 lines
3.6 KiB
C++

#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;
}
}