为什么这个变量的范围设置为私有?

时间:2017-01-15 19:19:13

标签: c parallel-processing openmp

我正在研究Peter Pacheco的书中的并行编程。在OpenMP一章中,我对一个简单的例子感到困惑,该例子将奇偶换位排序转换为OpenMP。

我可以理解这是如何工作的以及如何放置指令,但我似乎无法理解为什么阶段的范围被设置为私有。

我正在粘贴书中自编的代码版本。函数签名为void omp_transposition_sort(double *list, int len)

#pragma omp parallel num_threads(thread_count) \
        shared(list, len) private(i, phase) {
        for(int phase = 0; phase < len, phase++) {
            if(phase % 2 == 0) {                    // even phase
                #pragma omp for
                for (int i = 1; i < len; ++i) {     // compare 0 with 1, 2 with 3
                    if (list[i - 1] > list[i]) 
                        swap(list, i - 1, i);
                }
            } else {                                // odd phase
                #pragma omp for
                for(int i = 1; i < len - 1; ++i) {  // compare 1 with 2, 3 with 4
                    if(list[i] > list[i + 1])
                        swap(list, i, i + 1)
                }
            }
        }
    }

为什么在为每个唯一阶段值调用#pragma for时,作者是否觉得需要明确地将阶段设置为私有变量?

0 个答案:

没有答案