1

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

Jamie
  • 365
  • 2
  • 5
  • 13
  • 7
    Generally, you can parallelize only loops with no dependencies between iterations. This will definitely not be your case of an iterative solver. If you want to utilize multiple cores, you need to parallelize what is inside each iteration (such as matrix-vector multiplication or whatever is there). – Daniel Langr Aug 15 '23 at 07:06
  • 1
    "*I could also provide a minimal working example*" This is probably a good idea here. Like Daniel Langr said, you cannot parallelise inherently sequential operations. There is not much more to say based on the provided information. If you use a code having dependencies between iterations inside this while loop, you certainly get a *race condition*. Not to mention atomics tends to be much slower than basic read/writes. Maybe there is another source of parallelism you can use like task-based one, or maybe the sequential algorithm can be micro-optimized. – Jérôme Richard Aug 15 '23 at 11:51

1 Answers1

1

In PDE solvers the work of a single time-step / iterative solution iteration will be enough that you can parallelize that. So you can leave the while loop alone, and aim at the next level inside that. The loop over the grid points (elements, volumes) will have quite enough work.

Strictly speaking you could make the while loop omp parallel and only put omp for on the loops inside that, but that doesn't buy you much.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23