简体   繁体   English

使用openMP的并行qsort

[英]Parallel qsort using openMP

I'm trying to parallelize qsort() function in c++ with openMP, but I have some problems. 我正在尝试将c ++中的qsort()函数与openMP并行化,但是有一些问题。 For of all, for a high number of element to sort (ex: V=1000) qsort doesn't work at all, I never exit this function (infinite loop). 总而言之,对于要排序的大量元素(例如:V = 1000),qsort根本不起作用,我从不退出此功能(无限循环)。 Then, I notice that my parallel version of qsort is much slower then the serial one. 然后,我注意到我的并行qsort版本比串行版本慢很多。 Anyone could help me? 有人可以帮助我吗? Here the code: https://dpaste.de/EaH0 . 这里的代码: https : //dpaste.de/EaH0

here the average of time elapsed where kruscalP is the parallel version: 这里是kruscalP是并行版本的平均经过时间: 时间流逝 thanks 谢谢

You have multiple race conditions in your code when you write to A , exch0 , and exch1 which are all shared. 当您写入Aexch0exch1 ,它们在代码中有多个竞争条件,它们都是共享的。 Not only can this hurt performance it likely will give the wrong result. 这不仅会损害性能,而且可能会产生错误的结果。 Unfortunately, you have to use a critical section to fix this which will also likely hurt performance but at least it will give the right result. 不幸的是,您必须使用关键部分来解决此问题,这也可能会损害性能,但至少会给出正确的结果。

#pragma omp for    
for (i = 0; i < N-1; i += 2) {
    #pragma critical
    {
        if (A[i].weight > A[i+1].weight) {
            temp = A[i];
            A[i] = A[i+1];
            A[i+1] = temp;
            exch0 = 1;
        }
    }
}

The same applies to the next for loop. 下一个for循环也是如此。 If you want to do this efficiently you have to change your algorithm. 如果要高效地执行此操作,则必须更改算法。 Maybe consider merge sort. 也许考虑合并排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM