[libgomp] omp_in_parallel() false negative in parallel loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I've run into a problem where omp_in_parallel() is returning 0 where I expect it to return 1. The minimal working example is:

#include <omp.h>

#include <iostream>
#include <vector>

const std::size_t problem_size = 10000;

int main(void) {
  std::vector<bool> parallel_checks(problem_size, false);
#pragma omp parallel for
  for (std::size_t i = 0; i < problem_size; i++) {
    parallel_checks[i] = omp_in_parallel();
  }
  for (std::size_t i = 0; i < problem_size; i++) {
    if (!parallel_checks[i]) {
      std::cout << "failed at i = " << i << "\n";
    }
  }
  return 0;
}

where parallel_checks[i] should be true for all i. This fails only inconsistently so I've attached a slightly longer version that runs this check thousands of times. On my personal computer (4 cores, g++ 9.3.0), it fails about 10-30 times out of 100,000 attempts (failure meaning that any element in parallel_checks[] is false). On a supercomputing node I have access to (64 cores, g++ 11.2.0), it fails much more frequently (300-900 attempts out of 1000). I've tested this across optimization settings and while the numbers vary a bit, it is always a problem even without optimizations on.

Am I misunderstanding what omp_in_parallel() is supposed to provide? Does it not guarantee a true value at any point inside a parallel section?

Thanks for your help.

Best,
- Matthias
#include <omp.h>

#include <iostream>
#include <vector>

const std::size_t problem_size = 10000;
// Increase to 100000 for better sample size
const std::size_t num_tries = 1000;

int main(void) {
  std::size_t num_problems = 0;

  for (std::size_t attempt = 0; attempt < num_tries; attempt += 1) {
    std::vector<bool> parallel_checks(problem_size, false);

#pragma omp parallel for
    for (std::size_t i = 0; i < problem_size; i++) {
      parallel_checks[i] = omp_in_parallel();
    }

    for (std::size_t i = 0; i < problem_size; i++) {
      if (!parallel_checks[i]) {
        num_problems += 1;
        break;
      }
    }
  }

  std::cout << num_problems << " failed out of " << num_tries << "\n";

  return 0;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux