EDIT: changed the code and phrasing to make my doubt more explicit
I'm struggling to parallelize a loop in C using OpenMP for quite a while and want directions of how I should takle this challenge. the loop consists of the following (in case you wish to know this loop is the main loop integrated in a simulated annealing algorithm):
for(attempt = 0; attempt < SATISFIED; attempt++) {
i = (rand() % (len-1)) + 1;
j = i + (rand() % (len-i));
if(...) {
...
//Update global static variables:
if(dst < best_distance)
set_best(dst, path);
//Stop this attempt:
attempt = -1;
}
//Decrease the temperature:
temp = change_temp(temp);
}
The problem with this loop is that the number of iterations to do cannot be calculated by it's condition so I came up with a different way to write this loop in order to be able to use openmp:
while(keepGoing){
keepGoing = 0;
#pragma omp parallel for default(none) shared(len, best_distance, best_path, distances, avg_distance, path) private( i, j, seed, swp_dst) lastprivate(dst, temp, keepGoing) firstprivate(dst, temp, abort, keepGoing)
for(attempt = 0; attempt < SATISFIED; attempt++) {
#pragma omp flush (abort)
if (!abort) {
seed = omp_get_thread_num();
i = (rand_r(&seed) % (len-1)) + 1;
j = i + (rand_r(&seed) % (len-i));
//Update progress:
#pragma omp critical
{
if(...) {
...
//Update global static variables:
if(dst < best_distance)
set_best(dst, path);
//Stop this attempt:
keepGoing = 1;
abort = 1;
#pragma omp flush (abort)
#pragma omp flush (keepGoing)
}
}
//Decrease the temperature:
temp = change_temp(temp);
}
}
}
However this solution gives a different then the sequential version I wrote before output for reasons I don't understand... Are the openmp directives being well placed? Or should I use them in different ways? Thanks in advance for any answer.