OpenMP并行for循环输出

时间:2018-02-02 20:36:17

标签: openmp

我是Openmp和并行编程的新手。当我将此for循环并行化时,输出值会发生变化。有人可以帮我吗?非常感谢你!

for (int m = 0; m < D; m++)
 {

       for (int i = 0; i < n; i++) Used[i] = false;

         for (int j = 0; j < n; j++) 
             {
               if (Used[j] == false)
                {
                int t = powf(2, m);
                U[j + t] = T[j] + U[j + t];
                T[j] = T[j + t] = T[j] + T[j + t]; 
                Used[j] = Used[j + t] = true;
                }

             }
        cout << "\nResult of Step " << m + 1 << " Is :
        for (int i = 0; i < n; i++)
            cout << "\n T" << i << ":" << T[i] << "\t U" << i << ":" << U[i];
        cout << "\nTo Continue Press Any Key...";
        _getch();
    }

1 个答案:

答案 0 :(得分:-1)

猜测一些线程在j + t之间写入。 第一:

int jt = powf(2, m)+j;
var a = T[j];
U[jt] +=a; 
var b= T[jt];
T[j] = T[jt] = a+b;

第二,写下你的Openmp代码以查看错误