c++ parallel std::sort for floating values

≡放荡痞女 提交于 2019-12-23 17:14:29

问题


I've a large file consisting of > millions of floating point values. I can easily sort them using std::sort by reading file into vector for now, eg -

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

but is there any version of std::sort or similar algorithm which takes advantage of multiple cores available on my system? Since this is the only task that takes much time setting up, I'm looking for perf improvements from having > 1 core cpu.

I can use any latest releases of compilers on a x64 linux server and can compile the binary with -std=c++1z too.


回答1:


You're in luck. The C++ Extensions for Parallelism Technical Specification added parallelized versions of the many of the standard algorithms including std::sort. They are available in C++17. GCC has support for this and you can see their page about it here. It looks as though they are utilizing OpenMP for multi-threading.

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.


I know you said you are using Linux but I also want to included that it appears MSVS, starting with version 2013 RTM, also has support for the Parallelism Technical Specification.



来源:https://stackoverflow.com/questions/43162542/c-parallel-stdsort-for-floating-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!