In this quicksort implementation I think the program I’m running gets into a racing condition which origin i can not understand. So i turn myself to SO community for guidance.
If I remove “#pragma omp parallel for private(i)” before for-loop in quicksort() then it sorts correctly.
Below is a sorting sample and the code.
Unsorted
3 6 7 5 3 5 6 2 9 1
Sorted
0 1 2 3 3 5 6 7 9 5
size_t average (size_t a, size_t b)
{
return (a + b) / 2;
}
void swap (int *array, size_t x, size_t y)
{
int tmp;
tmp = array[x];
array[x] = array[y];
array[y] = tmp;
}
void quicksort (int *array, int left, int right)
{
int i, last;
if (left >= right)
{
return;
}
swap (array, left, average(left, right));
last = left;
#pragma omp parallel for private(i)
for (i = left + 1; i <= right; i++)
{
if (array[i] < array[left] )
{
#pragma omp critical
{
last++;
}
swap(array, last, i);
}
}
swap (array, left, last);
#pragma omp parallel sections
{
#pragma omp section
{
quicksort(array,left,last-1);
}
#pragma omp section
{
quicksort (array, last+1, right);
}
}
}
The swap operations that you do in that loop are deeply dependent of each other. There is no way of gaining parallelism, here.
But with the branch parallelism that you develop with the two parallel sections this shouldn’t be necessary. Do you have performance problems with that?