0

I'm having trouble, I want to parallelise with omp a for loop with a return statement inside the loop.

Here's the code.

int isPrime_parallele(int number) {
    int i;
    if (number == 2) return 1;
    if (number % 2 == 0) return 0;
    omp_set_num_threads(4);
    for (i = 3; i  <= (int)sqrt(number); i += 2){
        if (number % i == 0) return 0;
    }
    return 1;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    OpenMP doesn't allow arbitrary jumps out of loops -- so your 'code' won't work as you hope it will. You might be able to use the `cancel` clause, which is the OpenMP approach to stopping all threads inside a parallel region when some condition is met. Beyond that, have a look around for other questions on the topic of using OpenMP for parallel prime searching. – High Performance Mark May 20 '18 at 18:26
  • 1
    Possible duplicate of [Parallel OpenMP loop with break statement](https://stackoverflow.com/questions/9793791/parallel-openmp-loop-with-break-statement) – Zulan May 21 '18 at 12:28
  • Accordign to e.g. this http://pages.tacc.utexas.edu/~eijkhout/pcse/html/omp-loop.html it is not allowed to have `return` inside parallelised parts. – Yunnosch Jun 03 '18 at 07:07

0 Answers0