nadav kroyzman
nadav kroyzman

Reputation: 1

Parallel programing with OpenMP race condition not working

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: !(output

Im trying to classify points to clusteres for K-MEANS algorithm. So im getting this output (dont notice the checks) in one execution and the right results in the second execution and goes on.. I tried to put some varibales in private but it didnt work. Ill just say that this 3 points need to classify to cluster 0 and im guessing theres a race or something but i cant figure it out.

Upvotes: 0

Views: 56

Answers (1)

v2v1
v2v1

Reputation: 641

Yes, there is a race condition. tempDistance, closestCluster, and closestDistance should be private also. A good check is to ask yourself, do you need these variables to be different for each for loop iteration, if they happened at the same time?

You could make them private with the private() clause, like you did with j, or just declare them within the outer for loop.

Upvotes: 2

Related Questions