mobile optimization
This commit is contained in:
parent
7d349c57cb
commit
cd742751ed
@ -54,24 +54,25 @@ auto main(int argc, char *argv[]) -> int {
|
||||
auto dataset_seq = dataset;
|
||||
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
algo::MergeSort_v1::sort(dataset_seq.begin(), dataset_seq.end(), [](int32_t a, int32_t b) {
|
||||
algo::MergeSort_mt::sort(dataset_seq, [](int32_t a, int32_t b) {
|
||||
return (a > b);
|
||||
});
|
||||
}, 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());
|
||||
|
||||
|
||||
const int nthreads = std::thread::hardware_concurrency();
|
||||
//const int max_depth = std::thread::hardware_concurrency();
|
||||
const int max_depth = 4;
|
||||
t1 = std::chrono::high_resolution_clock::now();
|
||||
algo::MergeSort_mt::sort(dataset_par, [](int32_t a, int32_t b) {
|
||||
return (a > b);
|
||||
}, nthreads);
|
||||
}, max_depth);
|
||||
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(), nthreads);
|
||||
fmt::print("Sorted {} entries within {} ms in parallel using {} threads\n", dataset_seq.size(), delay_ms.count(), max_depth);
|
||||
|
||||
auto eq = (dataset_seq == dataset_par);
|
||||
fmt::print("Equality: {}\n", eq);
|
||||
|
@ -327,7 +327,7 @@ namespace algo {
|
||||
}
|
||||
|
||||
template<typename T, typename Comparator>
|
||||
static auto split(std::vector<T> data, Comparator cmp, int depth, int &num_threads,
|
||||
static auto split(std::vector<T> data, Comparator cmp, int depth, int &max_depth,
|
||||
std::mutex &mut) -> std::vector<T>{
|
||||
|
||||
if (data.size() <= 1) {
|
||||
@ -350,15 +350,15 @@ namespace algo {
|
||||
std::vector<T> left(data.begin(), mid);
|
||||
std::vector<T> right(mid, data.end());
|
||||
|
||||
if (depth < num_threads) {
|
||||
std::thread left_thread([&]() { left = split(left, cmp, depth + 1, num_threads, mut); });
|
||||
std::thread right_thread([&]() { right = split(right, cmp, depth + 1, num_threads, mut); });
|
||||
if (depth < max_depth) {
|
||||
std::thread left_thread([&]() { left = split(left, cmp, depth + 1, max_depth, mut); });
|
||||
std::thread right_thread([&]() { right = split(right, cmp, depth + 1, max_depth, mut); });
|
||||
|
||||
left_thread.join();
|
||||
right_thread.join();
|
||||
} else {
|
||||
left = split(left, cmp, depth + 1, num_threads, mut);
|
||||
right = split(right, cmp, depth + 1, num_threads, mut);
|
||||
left = split(left, cmp, depth + 1, max_depth, mut);
|
||||
right = split(right, cmp, depth + 1, max_depth, mut);
|
||||
}
|
||||
|
||||
return merge(left, right, cmp, mut);
|
||||
@ -367,12 +367,12 @@ namespace algo {
|
||||
public:
|
||||
template<typename T, typename Comparator>
|
||||
static auto
|
||||
sort(std::vector<T> &data, Comparator cmp, int num_threads = std::thread::hardware_concurrency()) -> void {
|
||||
sort(std::vector<T> &data, Comparator cmp, int max_depth = 0) -> void {
|
||||
std::mutex local_result_lock;
|
||||
std::vector<T> output;
|
||||
output.reserve(data.size());
|
||||
|
||||
output = split(data, cmp, 0, num_threads, local_result_lock);
|
||||
output = split(data, cmp, 0, max_depth, local_result_lock);
|
||||
data.assign(output.begin(), output.end());
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user