wip
This commit is contained in:
parent
23a02e9dd1
commit
b1e7903bbd
@ -67,6 +67,13 @@ auto main(int argc, char *argv[]) -> int {
|
|||||||
//const int max_depth = std::thread::hardware_concurrency();
|
//const int max_depth = std::thread::hardware_concurrency();
|
||||||
const int max_depth = 4;
|
const int max_depth = 4;
|
||||||
t1 = std::chrono::high_resolution_clock::now();
|
t1 = std::chrono::high_resolution_clock::now();
|
||||||
|
MergeSorterMT<int, bool(*)(int, int)> ms([](int a, int 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) {
|
algo::MergeSort_mt::sort(dataset_par, [](int32_t a, int32_t b) {
|
||||||
return (a > b);
|
return (a > b);
|
||||||
}, max_depth);
|
}, max_depth);
|
||||||
|
@ -6,7 +6,11 @@
|
|||||||
template<typename T, typename C>
|
template<typename T, typename C>
|
||||||
class MergeSorterMT {
|
class MergeSorterMT {
|
||||||
|
|
||||||
C comp;
|
public:
|
||||||
|
MergeSorterMT(C cmp) : cmp(cmp){}
|
||||||
|
|
||||||
|
|
||||||
|
C cmp;
|
||||||
std::recursive_mutex mut;
|
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 {
|
||||||
@ -44,8 +48,29 @@ class MergeSorterMT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto split(std::vector<T> &data, C cmp, int depth, int &max_depth, std::mutex &mut) -> void {
|
auto split(std::span<T> &data, int depth, int &max_depth, std::recursive_mutex &mut) -> void {
|
||||||
|
if(std::distance(data.begin(), data.end()) <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto mid = data.begin();
|
||||||
|
std::advance(mid, std::distance(data.begin(), data.end())/2);
|
||||||
|
|
||||||
|
std::span<T> left(data.begin(), mid);
|
||||||
|
std::span<T> right(mid, data.end());
|
||||||
|
|
||||||
|
if(depth < max_depth) {
|
||||||
|
std::thread left_thread(&MergeSorterMT<T, C>::split, this, left, depth + 1, max_depth, mut);
|
||||||
|
std::thread right_thread(&MergeSorterMT<T, C>::split, this, right, depth + 1, max_depth, mut);
|
||||||
|
|
||||||
|
left_thread.join();
|
||||||
|
right_thread.join();
|
||||||
|
} else {
|
||||||
|
split(left, depth + 1, max_depth, mut);
|
||||||
|
split(right, depth + 1, max_depth, mut);
|
||||||
|
}
|
||||||
|
|
||||||
|
merge(data, left, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user