I have an iterative solver that solves some sort of PDE (e.g. Poisson's equation) and requires a number of iterations to converege to the correct solution. This function in my code takes a big chunk of the time of my entire code and therefore was trying to parallelize it using openMP.
Example of do-while loop:
int count = 0;
// Begin do-while loop
do{
// Make Cakculations: multiplication and some addition
// Increase iteration count by 1
count = count + 1;
// Calculate error
it_error = fabs((newsol_max-oldsol_max)/newsol_max); // newsol_max = max value of new solution and oldsol_max is the solution in the previous iteration
//err_max is the error we want to converge to
// If error is too high and we haven't reached the max iterations yet, repeat iterations
}while( it_error > err_max && count <= max_iter && newsol_max > err_max);
return count;
Basically the do-while loop executes the body and then evaluates the while condition: it_error > err_max && count <= max_iter && newsol_max > err_max)
.
I saw this code here showing a general concept to using openMP with while loops:
int counter = 0;
#pragma omp parallel
while(1) {
int local_counter;
#pragma omp atomic read
local_counter = counter;
if (local_counter >= MAX) {
break;
}
// do monte carlo stuff
// if a certain condition is met, counter is incremented
if (certain_condition) {
#pragma omp atomic update
counter++;
}
}
But, I am not sure if this is the same for a do-while loop where we excute the body first then the while{}
statement until its true. Also, is the use of &&
operator here introduces the possibility of reduction?
Note: I could also provide a minimal working example