我想通过使用openMP让我的quicksort工作并行。在执行openMP之后,我尝试快速地执行快速排序工作失败,并且我的快速排序几乎将排列数组的速度降低了一倍。我使用OpenMP实现代码:OpenMP的实现让我的代码变得很慢
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);
}
}
}
}
}
整体排序发生在方法分区,如果你感兴趣的工作原理这里说到代码:
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;
}
我花时间在我主我打电话之前quickSort和我停止之前printf主计时器。 线程数量被定义为10(我用我的电脑上的4,2和1尝试过)。排序的列表,包括0之间1个000 000随机整数后我的结果 - 100:
时间(无OPENMP)是介乎6.48004 - 5.32001
使用OpenMP时间是介乎11.8309和10.6239(2-4线程) 这怎么可能是真的?
https://fgiesen.wordpress.com/2013/01/31/cores-dont-like-to-share/也许是首发。 – akira