【问题标题】:OpenMP array indexing in nested loop嵌套循环中的 OpenMP 数组索引
【发布时间】:2012-06-04 00:47:21
【问题描述】:

我在尝试使用 OpenMP 并行化嵌套循环时遇到问题 - 它只是用于玩弄和习惯的演示代码。

int* myresults = new int[1000]   
#pragma omp parallel
{
    #pragma omp for 
    for(int z=0; z<=mainCount;z++)
    {
        results[n++] = myfunc(z,0); //compute something


        for(int i=0;i<=secondCount;i+=5)
        {
            results[n++]=myfunc(z,i);
        }
    }
}

我的问题是我的结果数组的索引。我假设由于 OpenMP 正在并行化第一个 for 循环,他使用 results[] 的位置两次或更多,因为 results[n++] 会产生未定义的行为(因为不能保证 n 正确递增),我正确吗?

如何正确索引和存储我的结果?

【问题讨论】:

    标签: c++ for-loop openmp


    【解决方案1】:

    对于myfunc(z,0)n 增加一次,对于myfunc(z,i)1 + secondCount/5 增加一次。这意味着n 应该是z * (2 + secondCount/5) 在外部循环的每次迭代开始时。你应该像这样重写你的代码:

    #pragma omp parallel for private(n)
    for (int z = 0; z <= mainCount; z++)
    {
        n = z * (2 + secondCount/5);
    
        results[n++] = myfunc(z, 0);
    
        for (int i = 0; i <= secondCount; i += 5)
            results[n++] = myfunc(z, i);
    }
    

    【讨论】:

    • 为我工作。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多