简体   繁体   English

在openmp中对数组进行排序-关键部分

[英]Sorting an array in openmp - critical section

Quite similar to that question Sorting an array in openmp which has several hundred views but no correct answer. 非常类似于该问题,在openmp中对数组进行排序,该数组具有数百个视图,但没有正确答案。 Therefore I give it another try asking here again. 因此,我再次尝试在这里再次询问。 I am aware of the overhead and uselessness of this regarding speedup or performance. 我知道这与加速或性能有关的开销和无用性。 It simply is a small example to get into openMP. 这只是进入openMP的一个小例子。 The fact that is is insertSort is given by my courseinstructor. 这是insertSort的事实,这是我的课程教师给出的。

Here is my code: 这是我的代码:

std::vector<int> insertionSort(std::vector<int> a) {
    int i, j, k;
    #pragma omp parallel for private(i,j,k)
    for(i = 0; i < a.size(); i++) {
    #pragma omp critical
        k = a[i];
        for (j = i; j > 0 && a[j-1] > k; j--)

        #pragma omp critical
        {
            a[j] = a[j-1];
            a[j] = k;
        }
    }
 return a;
 }

I understand that the critical aspect is the race-condition between threads accessing (reading and writing) elements of a - that is, why I put a critical section arround all of them. 我知道关键的方面是线程访问(读取和写入)a元素之间的竞争条件,这就是为什么我将关键部分放在所有元素周围。 That does not seem to be sufficient. 这似乎还不够。 What am I missing here. 我在这里想念什么。 Without the pragmas, the sorting is correct. 没有编译指示,排序是正确的。

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

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