complete mergesorter mt impl
This commit is contained in:
parent
510d1e4e4b
commit
7082d548bc
@ -3,6 +3,7 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
|
||||
#include <mergesort.h>
|
||||
#include <mergesort_mt.h>
|
||||
@ -60,33 +61,32 @@ auto main(int argc, char *argv[]) -> int {
|
||||
}, 0);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
|
||||
fmt::print("Sorted {} entries within {} ms in sequential\n", dataset_seq.size(), delay_ms.count());
|
||||
auto t_seq = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
|
||||
fmt::print("Sorted {} entries within {} ms in sequential\n", dataset_seq.size(), t_seq.count());
|
||||
|
||||
|
||||
//const int max_depth = std::thread::hardware_concurrency();
|
||||
const int max_depth = 4;
|
||||
const int threads = std::thread::hardware_concurrency();
|
||||
const int max_depth = std::log(threads);
|
||||
|
||||
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);
|
||||
});
|
||||
std::span t = 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);
|
||||
}, max_depth);
|
||||
ms.sort(dataset_par);
|
||||
t2 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
delay_ms = 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);
|
||||
auto t_par = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
|
||||
fmt::print("Sorted {} entries within {} ms in parallel using {} threads and a recursion depth of {}\n", dataset_seq.size(), t_par.count(), threads, max_depth);
|
||||
|
||||
auto eq = (dataset_seq == dataset_par);
|
||||
fmt::print("Equality: {}\n", eq);
|
||||
fmt::print("Parallel dataset: {}; Sequential dataset: {}\n", dataset_par.size(), dataset_seq.size());
|
||||
fmt::print("Check whether sorted arrays are equal: {}\n", (eq)? "Equal" : "not equal");
|
||||
|
||||
//fmt::print("Created {} recurstions", algo::MergeSort_v1::get_recursions());
|
||||
fmt::print("------------Summary------------");
|
||||
fmt::print("t_seq = {}", t_seq.count());
|
||||
fmt::print("t_par = {}", t_par.count());
|
||||
fmt::print("speedup = {}", (1.0*t_seq/t_par));
|
||||
fmt::print("t_seq-t_par = {}", (t_seq.count() - t_par.count()));
|
||||
fmt::print("-------------------------------");
|
||||
|
||||
std::ofstream ofile("dataset.out.dat", std::ios_base::out);
|
||||
if(!ofile.is_open()) {
|
||||
|
@ -8,14 +8,16 @@ class MergeSorterMT {
|
||||
|
||||
public:
|
||||
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");
|
||||
}
|
||||
|
||||
auto sort(std::vector<T> &data) -> void {
|
||||
std::span<T> sortable(data);
|
||||
split(sortable, 0, max_depth, mut);
|
||||
}
|
||||
|
||||
std::function<bool(T, T)> cmp;
|
||||
std::recursive_mutex mut;
|
||||
|
||||
private:
|
||||
auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void {
|
||||
std::vector<T> buf;
|
||||
buf.reserve(left.size() + right.size());
|
||||
@ -46,12 +48,13 @@ 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());
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@ -62,23 +65,26 @@ public:
|
||||
std::span<T> left(data.begin(), mid);
|
||||
std::span<T> right(mid, data.end());
|
||||
|
||||
if(depth < max_depth) {
|
||||
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, max_depth, mut);
|
||||
//std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, max_depth, mut);
|
||||
std::thread left_thread([&](){split(left, depth + 1, max_depth, mut);});
|
||||
std::thread right_thread([&](){split(right, depth + 1, max_depth, mut);});
|
||||
if (depth < mdepth) {
|
||||
//todo: fix lambda call
|
||||
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, mdepth, mutex);
|
||||
//std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, mdepth, mutex);
|
||||
std::thread left_thread([&]() { split(left, depth + 1, mdepth, mutex); });
|
||||
std::thread right_thread([&]() { split(right, depth + 1, mdepth, mutex); });
|
||||
|
||||
left_thread.join();
|
||||
right_thread.join();
|
||||
} else {
|
||||
split(left, depth + 1, max_depth, mut);
|
||||
split(right, depth + 1, max_depth, mut);
|
||||
split(left, depth + 1, mdepth, mutex);
|
||||
split(right, depth + 1, mdepth, mutex);
|
||||
}
|
||||
|
||||
merge(data, left, right);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
std::function<bool(T, T)> cmp;
|
||||
const int max_depth;
|
||||
std::recursive_mutex mut;
|
||||
};
|
Loading…
Reference in New Issue
Block a user