Reputation: 3084
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.
Upvotes: 4
Views: 2335
Reputation: 180935
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.
Upvotes: 3