public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* [libgomp] omp_in_parallel() false negative in parallel loop
@ 2022-03-16  9:05 Matthias Heinz
  0 siblings, 0 replies; only message in thread
From: Matthias Heinz @ 2022-03-16  9:05 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1403 bytes --]

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

[-- Attachment #2: main.cc --]
[-- Type: text/x-c++src, Size: 723 bytes --]

#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;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-16  9:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-16  9:05 [libgomp] omp_in_parallel() false negative in parallel loop Matthias Heinz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).