r/C_Programming • u/BugsOfBunnys • Sep 15 '26
Question OpenMP bug??
Hello, I know this is not exactly the place for this, but it is C code. I am writing a bit of C code to search a large space of numbers, but I noticed the loop never ran. I asked ChatGPT cause I thought I was going crazy, but found that this bottom example code does not run the loop.
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
int main() {
uint64_t count = 0;
#pragma omp parallel for schedule(dynamic) reduction(+ : count)
for (uint64_t n = 1; n < UINT64_MAX; ++n)
++count;
printf("%llu\n", (unsigned long long)count);
}
It noticed it breaks with the dynamic scheduler. If you use the default static it seems to work. Any ideas on what is happening?
13
u/chrism239 Sep 15 '26
Doesn't run?
The value of UINT64_MAX is 18,446,744,073,709,551,615.
Perhaps the loop just hasn't finished yet, depending on how many threads/cores are used
9
u/esaule Sep 15 '26
You sure the compiler does not optimize the loop out?
There are no runtime dependent variable. So you can compute the values at tbe end of the loop statically.
Did you godbolt it?
21
u/tastygames_official Sep 15 '26
I don't know the answer to your question, but I took the liberty of formatting your code:
actually, I just realized while formatting your code that your for loop didn't have curly braces, which means the only line executed in the loop was
meaning it will only print `count` at the end when it's finished, meaning probably it's just not finished yet, like the other guy said.
4
u/Key_River7180 Sep 15 '26
This'd take years to finish, even with the best CPUs in the market that is a crazy loop bound.
3
u/8d8n4mbo28026ulk Sep 15 '26
u/esaule are right. Your loop has no side-effects and the compiler removes it completely. It's clearer to see this if you compile without OpenMP:
GCC generates the above with
-O3. When you add-fopenmp, it becomes a bit harder to figure it out (there're some OpenMP side-effectful function calls in there, probably due to threading). But, if you check the disassembly,schedule(static)allows the compiler to, again, remove the loop.1
u/tatsuling Sep 15 '26
I don't have any idea if this is actually a bug, however it looks similar to a genuine bug I found about 10 years ago with open MP on GCC. There was an off by 1 inside the library code for one of the schedulers when the number of iterations didn't match an exact multiple of the number of threads.
1
u/actguru Sep 15 '26
I don't have openmp, but that seems like a crazy big loop range.
1
u/sciencekm Sep 15 '26
Whenever I suspect that something is wrong with a piece of code, I normally just put a break point on it in the debugger or do a quick assembly output.
When you do this on your code, you will instantly see that the loop is missing, and as others have suspected, has been optimized out.
1
u/21Ali-ANinja69 Sep 15 '26
Maybe something is wrong with OpenMP. This is what I found
I put the printf in the loop...
and compiled with `gcc -fopenmp -O0 -g`. nothing happens
But disassembling main gets me this:
And running in gdb produces this: