Compare commits

...

2 Commits

Author SHA1 Message Date
ec1d2cdd46 Completed implementation 2023-11-06 21:51:13 +01:00
7082d548bc complete mergesorter mt impl 2023-11-06 21:36:10 +01:00
2 changed files with 49 additions and 42 deletions

View File

@ -3,6 +3,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <cmath>
#include <mergesort.h> #include <mergesort.h>
#include <mergesort_mt.h> #include <mergesort_mt.h>
@ -39,17 +40,18 @@ auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
auto main(int argc, char *argv[]) -> int { auto main(int argc, char *argv[]) -> int {
try { try {
std::ifstream file("dataset.dat", std::ios_base::in); const auto path = "dataset.dat";
std::ifstream file(path, std::ios_base::in);
if (!file.is_open()) { if (!file.is_open()) {
fmt::print("Error opening file"); fmt::print("\nError opening file");
return -1; return -1;
} }
fmt::print("Opened file {} sucessfully\n", "dummy"); fmt::print("\nOpened file {} sucessfully", path);
std::vector<int32_t> dataset; std::vector<int32_t> dataset;
parse_file(file, dataset); parse_file(file, dataset);
fmt::print("Read {} values from {}\n", dataset.size(), "dummy"); fmt::print("\nRead {} values from {}", dataset.size(), path);
auto dataset_par = dataset; auto dataset_par = dataset;
auto dataset_seq = dataset; auto dataset_seq = dataset;
@ -60,37 +62,36 @@ auto main(int argc, char *argv[]) -> int {
}, 0); }, 0);
auto t2 = std::chrono::high_resolution_clock::now(); auto t2 = std::chrono::high_resolution_clock::now();
auto delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); auto t_seq = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("Sorted {} entries within {} ms in sequential\n", dataset_seq.size(), delay_ms.count()); fmt::print("\nSorted {} entries within {} ms in sequential", dataset_seq.size(), t_seq.count());
//const int max_depth = std::thread::hardware_concurrency(); const int threads = std::thread::hardware_concurrency();
const int max_depth = 4; const int max_depth = std::log(threads);
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int> ms([](int a, int b) { MergeSorterMT<int32_t > ms([](int32_t a, int32_t b) {
return (a>b); return (a>b);
}); }, max_depth);
std::span t = dataset_par; ms.sort(dataset_par);
int mdepth = 4;
std::recursive_mutex mut;
ms.split(t, 0, mdepth, mut);
// algo::MergeSort_mt::sort(dataset_par, [](int32_t a, int32_t b) {
// return (a > b);
// }, max_depth);
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); auto t_par = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("Sorted {} entries within {} ms in parallel using {} threads\n", dataset_seq.size(), delay_ms.count(), max_depth); fmt::print("\nSorted {} entries within {} ms in parallel using {} threads and a recursion depth of {}", dataset_seq.size(), t_par.count(), threads, max_depth);
auto eq = (dataset_seq == dataset_par); auto eq = (dataset_seq == dataset_par);
fmt::print("Equality: {}\n", eq); fmt::print("\nCheck whether sorted arrays are equal: {}", (eq)? "Equal" : "not equal");
fmt::print("Parallel dataset: {}; Sequential dataset: {}\n", dataset_par.size(), dataset_seq.size());
//fmt::print("Created {} recurstions", algo::MergeSort_v1::get_recursions()); 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); std::ofstream ofile("dataset.out.dat", std::ios_base::out);
if(!ofile.is_open()) { if(!ofile.is_open()) {
fmt::print("Error writing to file"); fmt::print("\nError writing to file");
return -1; return -1;
} }
@ -102,12 +103,12 @@ auto main(int argc, char *argv[]) -> int {
ofile.flush(); ofile.flush();
ofile.close(); ofile.close();
fmt::print("Written to output file\n"); fmt::print("\nWritten to output file");
return 0; return 0;
} catch (std::exception &e) { } catch (std::exception &e) {
fmt::print("Error occured: {}", e.what()); fmt::print("\nError occured: {}", e.what());
return -1; return -1;
} }

View File

@ -8,14 +8,16 @@ class MergeSorterMT {
public: public:
template<typename C> template<typename C>
MergeSorterMT(C cmp) : cmp(cmp){ MergeSorterMT(C cmp, int max_depth) : cmp(cmp), max_depth(max_depth) {
static_assert(std::is_same<std::invoke_result_t<C, T, T>, bool>(), "C must be a function that returns a bool"); static_assert(std::is_same<std::invoke_result_t<C, T, T>, bool>(), "C must be a function that returns a bool");
} }
auto sort(std::vector<T> &data) -> void {
std::span<T> sortable(data);
split(sortable, 0, max_depth, mut);
}
std::function<bool(T, T)> cmp; private:
std::recursive_mutex mut;
auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void { auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void {
std::vector<T> buf; std::vector<T> buf;
buf.reserve(left.size() + right.size()); buf.reserve(left.size() + right.size());
@ -46,39 +48,43 @@ public:
} }
{ {
std::lock_guard<std::recursive_mutex> lock(mut); //todo: is a lock guard necessary?
//std::lock_guard<std::recursive_mutex> lock(mut);
std::move(buf.begin(), buf.end(), output.begin()); std::move(buf.begin(), buf.end(), output.begin());
} }
} }
auto split(std::span<T> &data, int depth, int &max_depth, std::recursive_mutex &mut) -> void { auto split(std::span<T> &data, int depth, const int &mdepth, std::recursive_mutex &mutex) -> void {
if(std::distance(data.begin(), data.end()) <= 1) { if (std::distance(data.begin(), data.end()) <= 1) {
return; return;
} }
auto mid = data.begin(); auto mid = data.begin();
std::advance(mid, std::distance(data.begin(), data.end())/2); std::advance(mid, std::distance(data.begin(), data.end()) / 2);
std::span<T> left(data.begin(), mid); std::span<T> left(data.begin(), mid);
std::span<T> right(mid, data.end()); std::span<T> right(mid, data.end());
if(depth < max_depth) { if (depth < mdepth) {
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, max_depth, mut); //todo: fix lambda call
//std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, max_depth, mut); //std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, mdepth, mutex);
std::thread left_thread([&](){split(left, depth + 1, max_depth, mut);}); //std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, mdepth, mutex);
std::thread right_thread([&](){split(right, depth + 1, max_depth, mut);}); std::thread left_thread([&]() { split(left, depth + 1, mdepth, mutex); });
std::thread right_thread([&]() { split(right, depth + 1, mdepth, mutex); });
left_thread.join(); left_thread.join();
right_thread.join(); right_thread.join();
} else { } else {
split(left, depth + 1, max_depth, mut); split(left, depth + 1, mdepth, mutex);
split(right, depth + 1, max_depth, mut); split(right, depth + 1, mdepth, mutex);
} }
merge(data, left, right); merge(data, left, right);
} }
private:
std::function<bool(T, T)> cmp;
const int max_depth;
std::recursive_mutex mut;
}; };