I'm trying to make my quicksort work parallel by using openMP. After implementation of openMP my attempt to make quicksort work faster fails and my quicksort sorts array almost twice slower. My code with openMP implementation :
void quickSort( int a[], int l, int r) { int j; if( l < r ) { #pragma omp parallel { j = partition( a, l, r); #pragma omp sections { #pragma omp section { quickSort( a, l, j-1); } #pragma omp section { quickSort( a, j+1, r); } } } } } Whole sorting happens in method partition and if its interesting for you how it works here comes code for it :
int partition( int a[], int l, int r) { int pivot, i, j, t; pivot = a[l]; i = l; j = r+1; while(1) { do ++i; while( a[i] <= pivot && i <= r ); do --j; while( a[j] > pivot ); if( i >= j ) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[l]; a[l] = a[j]; a[j] = t; return j; } I take time in my main before i call quickSort and i stops timer before printf in main. Amount of threads is defined to 10 (i have tried with 4,2 and 1 on my pc). My results after sorting a list with 1 000 000 random integers between 0 - 100:
time (without openMP) is betwen 6.48004 - 5.32001
with openMP time is betwen 11.8309 and 10.6239 (with 2-4 threads) How can this be true?