0
votes

So far I have only used OpenMP to parallelize for loop in C++. However I wonder if I can execute other lines of codes which are not for loops in Parallel.

void minimization(int *a, int *x) {
 // this part of the code is dependent of other library.
}

void checkForNaN(int *a){
 // check nan points
}

int main() {
  // read data
  minimization (a,b);
  checkForNaN(x);
}

Consider the sample snippet above, whereby minimization(,) and checkForNaN() are independent that is results of either one does not affect the other once. Is it possible to parallelize it?

I am thinking something like this:

  int main() {
      // read data
#pragma omp parallel
    {
      minimization (a,b);
      checkForNaN(x);
     }
}

Does it looks correct?

2

2 Answers

4
votes

This is what OMP Sections is used for :)

int main() {
    #pragma omp parallel sections
    {
        #pragma omp section
        { 
            minimization(a,b);
        }

        #pragma omp section
        { 
            checkForNaN(x);
        }
    }
}
1
votes

No, it doesn't look correct. it will execute minimization(a,b); and checkForNaN(x); in all threads you have.

Instead, this will do the parallelization:

int main() {
  // read data
  #pragma omp parallel sections
  {
    #pragma omp section
    {
      minimization (a,b);
    }
    #pragma omp section
    {
      checkForNaN(x);
    }
  }
}