并行编程与OpenMP竞争条件不起作用

时间:2018-01-23 18:15:55

标签: parallel-processing openmp

void ompClassifyToClusteres(Point* points, Cluster* clusteres, int 
numOfPoints, int numOfClusteres, int myid) {

int i, j;
Cluster closestCluster;
double closestDistance;
double tempDistance;

omp_set_num_threads(OMP_NUM_OF_THREADS);
#pragma omp parallel private(j)
{
#pragma omp for 
    for (i = 0; i < numOfPoints; i++) {
        closestCluster = clusteres[0];
        closestDistance = distanceFromClusterCenter(points[i], closestCluster);

        for (j = 1; j <= numOfClusteres; j++) {
            tempDistance = distanceFromClusterCenter(points[i], clusteres[j]);
            if (tempDistance < closestDistance) {
                closestCluster = clusteres[j];
                closestDistance = tempDistance;
            }
        }
        points[i].clusterId = closestCluster.id;
    }

}


printPoints(points, numOfPoints);



}

输出: !(output

我试图将点分类为K-MEANS算法的clusteres。 所以我在一次执行中得到这个输出(不注意检查),右边的结果是第二次执行并继续... 我试图将一些varibales私有化,但它没有用。 我只是说这3点需要归类到0组,我猜测是比赛还是什么,但我无法弄明白。

1 个答案:

答案 0 :(得分:2)

是的,有竞争条件。 tempDistance,nearestCluster和nearestDistance也应该是私有的。一个好的检查是问问自己,如果它们同时发生,你是否需要为每个for循环迭代使这些变量不同?

您可以使用private()子句将它们设为私有,就像使用j一样,或者只是在外部for循环中声明它们。

相关问题