Intel® oneAPI Threading Building Blocks
2424 Discussions

## parallel_sort syntax Beginner
189 Views

Hello peoples!

So basically I have a int vector called primes:

`[bash]vector primes;[/bash]`

It is filled up with parallely generated primes. Now, I need to sort them. I tried using parallel_sort, but it screwed up. I've tried using pointers, iterators, pointers to iterators, etc.

Can anyone help?

Thanks!

6 Replies Black Belt
189 Views
Last time I checked, "parallely" was not a word. :-)

As for the programming issue, please ask a real question with usable information (and preferably without assigning blame to others).

(Added after #4) Smiley, parentheses. Beginner
189 Views

Oops, double post. Use the post after this. Beginner
189 Views

Oops.

Well, here's what I've tried:

```[bash]vector::iterator b = primes.begin(), e = primes.end();
parallel_sort(b,e);
==============
parallel_sort(primes.begin(), primes.end());
==============
// I know this doesn't work
parallel_sort(primes.front(), primes.back());
==============
// I know this doesn't work
vector::iterator b = primes.begin(), e = primes.end();
int *be = &(*b), *en = &(*e);
parallel_sort(*be,*en);
==============
vector::iterator b = primes.begin(), e = primes.end();
parallel_sort(*b,*e);==============// Parallel qsort failparallel_for(0, 3, [=](int z)  {
qsort((void *)(primes.front() + z * ((primes.size())/4)), (primes.size())/4, sizeof(int), compare);
});
parallel_for(0, 1, [=](int y)  {
qsort((void *)(primes.front() + y * ((primes.size())/2)), (primes.size())/2, sizeof(int), compare);
});
qsort((void *)primes.front(), primes.size(), sizeof(int), compare);

[/bash]```

Thanks! Black Belt
189 Views
What does "fail" mean: doesn't compile/link, wrong outcome, ...? Does anything change if you use tbb::parallel_sort instead of the parallel_sort by itself? front() and back() wouldn't work, nor would *begin() and *last(). I think I'll skip the parallel_for ones... Does "compare" provide "<" to qsort() or did you want something else? Have you tried the example in the Reference, and what happened? Employee
189 Views

You should be able to use tbb::parallel_sort just like std::sort as long as the operations on the type are thread-safe. Here's a transcript of a complete example on Linux.

```[cpp]\$ cat primes.cpp
#include "tbb/tbb.h"
#include
#include

int main() {
std::vector primes;
primes.push_back(3);
primes.push_back(7);
primes.push_back(5);
primes.push_back(2);
tbb::parallel_sort( primes.begin(), primes.end() );
for( size_t i=0; i << std::endl;
return 0;
}
\$ gcc primes.cpp -ltbb
\$ a.out
2
3
5
7
[/cpp]``` Beginner
189 Views
Oops, it turned out that my cout was in the wrong place, thus outputting the wrong results. Thanks everyone! 