From: Matthias Heinz <mheinz@theorie.ikp.physik.tu-darmstadt.de>
To: gcc-help@gcc.gnu.org
Subject: [libgomp] omp_in_parallel() false negative in parallel loop
Date: Wed, 16 Mar 2022 10:05:58 +0100 [thread overview]
Message-ID: <7f02b184-9588-9d1a-702a-cc17bbfd7329@theorie.ikp.physik.tu-darmstadt.de> (raw)
[-- 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;
}
reply other threads:[~2022-03-16 9:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7f02b184-9588-9d1a-702a-cc17bbfd7329@theorie.ikp.physik.tu-darmstadt.de \
--to=mheinz@theorie.ikp.physik.tu-darmstadt.de \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).