Noah
Noah

Reputation: 575

Parallel STL algorithms in OS X

I working on converting an existing program to take advantage of some parallel functionality of the STL.

Specifically, I've re-written a big loop to work with std::accumulate. It runs, nicely.

Now, I want to have that accumulate operation run in parallel.

The documentation I've seen for GCC outline two specific steps.

  1. Include the compiler flag -D_GLIBCXX_PARALLEL
  2. Possibly add the header <parallel/algorithm>

Adding the compiler flag doesn't seem to change anything. The execution time is the same, and I don't see any indication of multiple core usage when monitoring the system.

I get an error when adding the parallel/algorithm header. I thought it would be included with the latest version of gcc (4.7).

So, a few questions:

  1. Is there some way to definitively determine if code is actually running in parallel?
  2. Is there a "best practices" way of doing this on OS X? (Ideal compiler flags, header, etc?)

Any and all suggestions are welcome.

Thanks!

Upvotes: 7

Views: 1722

Answers (2)

user362515
user362515

Reputation: 917

  • Second alternative is to use OpenMP, which is supported by both GCC and Clang, though is not STL by any means, but is cross-platform.
  • Third alternative is to use Grand Central Dispatch - the official multicore API in OSX, again hardly STL.
  • Forth alternative is to wait for C++17, it will have Parallelism module.

Upvotes: 0

mcmcc
mcmcc

Reputation: 842

See http://threadingbuildingblocks.org/

If you only ever parallelize STL algorithms, you are going to disappointed in the results in general. Those algorithms generally only begin to show a scalability advantage when working over very large datasets (e.g. N > 10 million).

TBB (and others like it) work at a higher level, focusing on the overall algorithm design, not just the leaf functions (like std::accumulate()).

Upvotes: 2

Related Questions