0

Using c++ openmp 3.1 I implemented a max reduction which stores the maximum value of integer variable (score) of an vector of objects (s). But I also want to store the vector index to acces the (s) object with the maximum score. My current unsuccesfull implementation looks like this:

//s is a vector of sol objects which contain apart from other variables an  integer     score    variable s[].score            
int bestscore = 0;
int bestant = 0;                
#pragma omp parallel shared(bestant)
{//start parallel session
    #pragma omp for    nowait reduction(max : bestscore)
    for (int ant = 0; ant<maxsols; ++ant) // for all ants
    {
        //procedures on s[ant] object which update the  int s[ant].score
        if (s[ant].score > bestscore)
        {
            //find the object with the highest score
            bestscore = s[ant].score;
            bestant = ant;//i also want know which ant has the highest score
        }
    }
}

The code compiles and runs. the maximum bestscore is found but bestant gets a random index. The ant linked to the fastest thread to finish gets stored in bestant. bestscore start with a value of 0 so in most cases s[ant].score will have a higher score and bestscore and bestant are updated. I think I need a reduction operator for bestant like “on update of bestscore”.

Anonymous Asked question May 13, 2021