experiments

This commit is contained in:
Robin Dietzel 2023-11-01 21:38:09 +01:00
parent 11190a0eee
commit 038ef8f439

View File

@ -294,8 +294,6 @@ namespace algo {
static auto
merge(typename std::vector<T>::iterator mid, std::vector<T> &left, std::vector<T> &right,
Comparator cmp, std::mutex &mut) {
{
std::lock_guard lock(mut);
auto l = left.begin();
auto r = right.begin();
@ -320,11 +318,12 @@ namespace algo {
mid++;
r++;
}
}
}
template<typename T, typename Comparator>
static auto split(std::vector<T> &data, std::vector<T> &outdata, Comparator cmp, int depth, int &num_threads, std::mutex &mut) {
static auto split(std::vector<T> &data, std::vector<T> &output, Comparator cmp, int depth, int &num_threads,
std::mutex &mut) {
if (data.size() <= 1) {
return;
}
@ -333,26 +332,36 @@ namespace algo {
std::advance(mid, std::distance(data.begin(), data.end()) / 2);
std::span<T> left(data.begin(), mid);
std::span<T> right(mid, data.end());
//std::vector<T> left(std::make_move_iterator(data.begin()), std::make_move_iterator(mid));
//std::vector<T> right(std::make_move_iterator(mid), std::make_move_iterator(data.end()));
std::vector<T> left(data.begin(), mid);
std::vector<T> right(mid, data.end());
if (depth < num_threads) {
std::thread left_thread([&]() { split(left, outdata, cmp, depth + 1, num_threads, mut); });
std::thread right_thread([&]() { split(right, outdata, cmp, depth + 1, num_threads, mut); });
std::thread left_thread([&]() { split(left, output, cmp, depth + 1, num_threads, mut); });
std::thread right_thread([&]() { split(right, output, cmp, depth + 1, num_threads, mut); });
left_thread.join();
right_thread.join();
} else {
split(left, outdata, cmp, depth + 1, num_threads, mut);
split(right, outdata, cmp, depth + 1, num_threads, mut);
split(left, output, cmp, depth + 1, num_threads, mut);
split(right, output, cmp, depth + 1, num_threads, mut);
}
mid = outdata.begin();
std::advance(mid, std::distance(outdata.begin(), outdata.end())/2);
merge(mid, left, right, cmp, mut);
mid = output.begin();
std::advance(mid, std::distance(output.begin(), output.end()) / 2);
std::vector<T> buf;
buf.reserve(data.size());
merge(buf.begin(), left, right, cmp, mut);
{
std::lock_guard lock(mut);
auto bufi = buf.begin();
for(auto &element : output) {
element = *bufi;
bufi++;
}
}
}
public: