complete mergesorter mt impl
This commit is contained in:
parent
510d1e4e4b
commit
7082d548bc
@ -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>
|
||||||
@ -60,33 +61,32 @@ 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("Sorted {} entries within {} ms in sequential\n", 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("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);
|
auto eq = (dataset_seq == dataset_par);
|
||||||
fmt::print("Equality: {}\n", eq);
|
fmt::print("Check whether sorted arrays are equal: {}\n", (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("------------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);
|
std::ofstream ofile("dataset.out.dat", std::ios_base::out);
|
||||||
if(!ofile.is_open()) {
|
if(!ofile.is_open()) {
|
||||||
|
@ -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,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());
|
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;
|
||||||
}
|
}
|
||||||
@ -62,23 +65,26 @@ public:
|
|||||||
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;
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user