aca-tasks/task1/main2.cpp

109 lines
3.6 KiB
C++
Raw Normal View History

2023-11-07 20:24:24 +00:00
#include <QRandomGenerator>
#include <random>
2023-11-01 09:00:36 +00:00
#include <fstream>
2023-11-07 20:24:24 +00:00
#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));
}
}
2023-10-31 13:48:54 +00:00
2023-11-01 09:16:53 +00:00
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));
}
}
2023-11-07 20:24:24 +00:00
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);
2023-11-01 09:00:36 +00:00
try {
2023-11-07 20:24:24 +00:00
std::ifstream file(fname.c_str(), std::ios_base::in);
2023-11-01 09:16:53 +00:00
if (!file.is_open()) {
2023-11-06 20:51:13 +00:00
fmt::print("\nError opening file");
2023-11-01 09:16:53 +00:00
return -1;
}
2023-10-31 13:48:54 +00:00
2023-11-07 20:24:24 +00:00
fmt::print("\nOpened file {} sucessfully", fname);
2023-11-01 09:16:53 +00:00
std::vector<int32_t> dataset;
2023-10-31 13:48:54 +00:00
2023-11-01 09:16:53 +00:00
parse_file(file, dataset);
2023-11-07 20:24:24 +00:00
fmt::print("\nRead {} values from {}", dataset.size(), fname);
2023-10-31 13:48:54 +00:00
auto dataset_par = dataset;
auto dataset_seq = dataset;
2023-10-31 13:48:54 +00:00
auto t1 = std::chrono::high_resolution_clock::now();
2023-11-07 10:45:49 +00:00
MergeSorterMT<int32_t> msst([](int32_t a, int32_t b) {
2023-11-07 10:51:34 +00:00
return (a > b);
2023-11-03 08:03:36 +00:00
}, 0);
2023-11-07 10:45:49 +00:00
msst.sort(dataset_seq);
2023-11-01 09:16:53 +00:00
auto t2 = std::chrono::high_resolution_clock::now();
2023-11-06 20:36:10 +00:00
auto t_seq = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
2023-11-06 20:51:13 +00:00
fmt::print("\nSorted {} entries within {} ms in sequential", dataset_seq.size(), t_seq.count());
2023-11-01 11:32:52 +00:00
2023-11-06 20:36:10 +00:00
const int threads = std::thread::hardware_concurrency();
2023-11-07 11:16:08 +00:00
const int max_depth = std::sqrt(threads);
2023-11-06 20:36:10 +00:00
t1 = std::chrono::high_resolution_clock::now();
2023-11-07 10:51:34 +00:00
MergeSorterMT<int32_t> msmt([](int32_t a, int32_t b) {
return (a > b);
2023-11-06 20:36:10 +00:00
}, max_depth);
2023-11-07 10:45:49 +00:00
msmt.sort(dataset_par);
t2 = std::chrono::high_resolution_clock::now();
2023-11-06 20:36:10 +00:00
auto t_par = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
2023-11-07 10:51:34 +00:00
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",
2023-11-07 11:06:28 +00:00
dataset_seq.size(), t_par.count(), threads, max_depth, std::pow(2, max_depth));
auto eq = (dataset_seq == dataset_par);
2023-11-07 10:51:34 +00:00
fmt::print("\nCheck whether sorted arrays are equal: {}", (eq) ? "Equal" : "not equal");
2023-11-06 20:36:10 +00:00
2023-11-06 20:51:13 +00:00
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()));
2023-11-07 10:51:34 +00:00
fmt::print("\nspeedup = {: > 5.2f}", (1.0 * t_seq / t_par));
2023-11-06 20:51:13 +00:00
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);
2023-11-07 10:51:34 +00:00
if (!ofile.is_open()) {
2023-11-06 20:51:13 +00:00
fmt::print("\nError writing to file");
return -1;
}
2023-11-07 10:51:34 +00:00
for (auto &element: dataset_seq) {
ofile << std::to_string(element) << '\n';
}
file.close();
ofile.flush();
ofile.close();
2023-11-06 20:51:13 +00:00
fmt::print("\nWritten to output file");
2023-10-31 20:11:31 +00:00
2023-11-01 09:00:36 +00:00
return 0;
2023-10-31 20:11:31 +00:00
2023-11-03 10:41:58 +00:00
} catch (std::exception &e) {
2023-11-06 20:51:13 +00:00
fmt::print("\nError occured: {}", e.what());
2023-11-01 09:00:36 +00:00
return -1;
2023-10-31 20:11:31 +00:00
}
2023-11-07 20:24:24 +00:00
}