RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1271290
Accepted
Rikitikitavi
Rikitikitavi
Asked:2022-04-17 17:25:52 +0000 UTC2022-04-17 17:25:52 +0000 UTC 2022-04-17 17:25:52 +0000 UTC

并行执行快速排序

  • 772

这是快速排序的实现:

template<class iterator>
void qsort(iterator begin, iterator end)
{
    if (begin == end)
        return;

    const auto distance = std::distance(begin, end);
    iterator p = std::next(begin, distance / 2);
    iterator it1 = std::partition(begin, end, [&p](const auto& v){ return v < *p;});
    iterator it2 = std::partition(it1, end, [&p](const auto& v) { return !(v > *p);});

    qsort(begin, it1);
    qsort(it2,  end);
}

我想实现这个算法的并行执行。你当然可以这样做:

template<class iterator>
void qsort(iterator begin, iterator end)
{
    if (begin == end)
        return;

    const auto distance = std::distance(begin, end);
    iterator p = std::next(begin, distance / 2);
    iterator it1 = std::partition(begin, end, [&p](const auto& v){ return v < *p;});
    iterator it2 = std::partition(it1, end, [&p](const auto& v) { return !(v > *p);});

    std::thread t1(qSort<Iterator>, begin, middle1);
    std::thread t2(qSort<Iterator>, middle2, end);

    t1.join();
    t2.join();
}

但这显然远非最佳实现。线程数将是log(n)。

问题:如何正确实现多线程快速排序算法?您可以编写一个限制线程数的类并将其传递给函数,但标准实现std::sort可以在多个线程中执行,而它的签名不会改变,我想实现类似的东西。

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Roman-Stop RU aggression in UA
    2022-04-17T20:37:50Z2022-04-17T20:37:50Z

    要解决签名的问题,只需不要向用户显示带有附加参数的函数,并且客户端应该使用的函数应该与现在具有相同的签名:

    template<class iterator>
    void qsort(iterator begin, iterator end)
    {
      qsort_internal(begin, end, 4);
    }
    
    template<class iterator>
    void qsort_internal(iterator begin, iterator end, int max_threads)
    {
    
      ...
      if (max_threads > 1) {
        //сортируем многопоточно
        // но уменьшаем max_threads в рекурсивном вызове
      } else {
        // сортируем в этом же потоке 
      }
    
    }
    

    您需要了解,这种幼稚的实现很可能不会带来特别的增加,但很可能会更慢,甚至在小型阵列上会明显变慢。看这里,与通过并行化加速算法,特别是加速快速排序相关的问题和困难已经被反复讨论过。

    • 4
  2. Ariox
    2022-04-17T20:51:51Z2022-04-17T20:51:51Z

    对于此类任务,通常使用具有固定数量线程(其实现之一)的线程池,在性能方面将任务添加到池中相当于在最坏的情况下锁定互斥锁。

    并行算法的标准实现通常使用对用户隐藏的(全局)线程池。或者 OpenMP,其实现也使用线程池,但可能会使用一些额外的平台功能。

    您可以找到使用 std::async 进行排序的示例,它也可以使用全局线程池。但在实践中,标准库的实现可能会创建一个新线程而不是使用池,这使得这毫无意义。

    如果我们专门讨论排序,您不太可能接近标准实现。此类算法在 CPU 上的并行性通常很差,这就是为什么使用许多不明显的技巧来实现它们的原因(结果仍然值得怀疑)。如果您真的对排序加速感兴趣,我建议您关注 GPU(例如,thrust::sort)。

    • 3

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5