4

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.

hg_git
  • 2,884
  • 6
  • 24
  • 43

1 Answers1

3

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.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Maybe you could mention the need for `-fopenmp` and it looks like you need `-D_GLIBCXX_PARALLEL` too? – Alexis Wilke Apr 01 '17 at 22:27
  • 2
    I find this answer slightly misleading. The parallel mode you linked has been available for many years in GCC, way before the C++ Extensions for Parallelism Technical Specification. This is a compiler specific extension, and different algorithms are available (even though a lot are in common with the C++ extension specification). Tell me if I'm wrong, but as far as I know, GCC does not yet support the C++ Extensions for Parallelism Technical Specification. – Pyves Apr 15 '17 at 09:40
  • Could you therefore please provide a link and modify your answer accordingly? – Pyves Apr 15 '17 at 17:29