0
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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

printPoints, if I understand it correctly, should be outside the parallel region. Try combining the #pragma omp for with the parallel region ( #pragma omp parallel for, and take printPoints out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.