c++ parallel std::sort for floating values

c++ parallel std::sort for floating values

我有一个包含 > 数百万个浮点值的大文件。我现在可以通过将文件读入 vector 来使用 std::sort 轻松地对它们进行排序,例如 -

std::vector<float> v;
std::sort(v.begin(), v.end());

但是是否有任何版本的 std::sort 或类似算法可以利用我的系统上可用的多核?由于这是唯一需要花费大量时间设置的任务,因此我希望通过拥有 > 1 个内核 cpu.

来提高性能

我可以在 x64 linux 服务器上使用任何最新版本的编译器,也可以使用 -std=c++1z 编译二进制文件。

你很幸运。 C++ 并行化扩展技术规范添加了许多标准算法的并行化版本,包括 std::sort。它们在 C++17 中可用。 GCC 对此提供支持,您可以查看有关它的页面 here。看起来他们正在利用 OpenMP 进行多线程处理。

GCC Prerequisite Compiler Flags

Any use of parallel functionality requires additional compiler and runtime support, in particular support for OpenMP. Adding this support is not difficult: just compile your application with the compiler flag -fopenmp. This will link in libgomp, the GNU Offloading and Multi Processing Runtime Library, whose presence is mandatory.

In addition, hardware that supports atomic operations and a compiler capable of producing atomic operations is mandatory: GCC defaults to no support for atomic operations on some common hardware architectures. Activating atomic operations may require explicit compiler flags on some targets (like sparc and x86), such as -march=i686, -march=native or -mcpu=v9. See the GCC manual for more information.


我知道你说你正在使用 Linux,但我还想包括它出现的 MSVS,starting with version 2013 RTM,也支持并行技术规范。