I am thinking use the Openmp to parallel the below code.

But notice that in the last line "neighbors[max_idx].dist = z[i];" each thread will share the same variable ，i.e.neighbors.

In that case if the openmp is used, at a given time several threads will write different value to the same location, which causes a competition.

So does that the below code cann't be using openmp parallel the code?

Any hint will be appreciated.

- Code: Select all
`for( i = 0 ; i < rec_count ; i++ ) {`

float max_dist = -1;

int max_idx = 0;

for( j = 0 ; j < k ; j++ ) {

if( neighbors[j].dist > max_dist ) {

max_dist = neighbors[j].dist;

max_idx = j;

}

}

if( z[i] < neighbors[max_idx].dist ) {

strcpy(neighbors[max_idx].entry, sandbox +i*REC_LENGTH);

neighbors[max_idx].dist = z[i];

}

}