lundi 27 novembre 2017

Function using reference in parallel using omp

Consider the simplified example. Say that I have the following function:

int foo(int x, int y, bool &flag) {
  if (x == y)
    flag = true;
  return x + y;
}

that then is executed in parallel using openmp:

  bool flag = false;

#pragma omp parallel for 
  for (int i = 0; i < n; i++) {
    z[i] = foo(x[i], y[i], flag);
  }

I guess that the problem in here is that the function gets called multiple times in parallel and each of the instances points to the same flag. What is the best way to fix this using some comparably simple solution?

Disclaimer: I know that many people would say that using reference in such way is bad, but in my case it is the simplest way to achieve the task: flag if any exception happens.

Aucun commentaire:

Enregistrer un commentaire