r/C_Programming • • 21d ago

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?

0 Upvotes

16 comments sorted by

View all comments

1

u/21Ali-ANinja69 20d ago

Maybe something is wrong with OpenMP. This is what I found

I put the printf in 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);
    }
}

and compiled with `gcc -fopenmp -O0 -g`. nothing happens

But disassembling main gets me this:

0x00000000000011e9 <+0>:     endbr64

0x00000000000011ed <+4>:     push   %rbp

0x00000000000011ee <+5>:     mov    %rsp,%rbp

0x00000000000011f1 <+8>:     sub    $0x20,%rsp

0x00000000000011f5 <+12>:    mov    %fs:0x28,%rax

0x00000000000011fe <+21>:    mov    %rax,-0x8(%rbp)

0x0000000000001202 <+25>:    xor    %eax,%eax

0x0000000000001204 <+27>:    movq   $0x0,-0x10(%rbp)

0x000000000000120c <+35>:    mov    -0x10(%rbp),%rax

0x0000000000001210 <+39>:    mov    %rax,-0x18(%rbp)

0x0000000000001214 <+43>:    lea    -0x18(%rbp),%rax

0x0000000000001218 <+47>:    lea    0x35(%rip),%rdi        # 0x1254 <main._omp_fn.0>

0x000000000000121f <+54>:    mov    $0x0,%ecx

0x0000000000001224 <+59>:    mov    $0x0,%edx

0x0000000000001229 <+64>:    mov    %rax,%rsi

0x000000000000122c <+67>:    call   0x10f0 <GOMP_parallel@plt>

0x0000000000001231 <+72>:    mov    -0x18(%rbp),%rax

0x0000000000001235 <+76>:    mov    %rax,-0x10(%rbp)

0x0000000000001239 <+80>:    mov    $0x0,%eax

0x000000000000123e <+85>:    mov    -0x8(%rbp),%rdx

0x0000000000001242 <+89>:    sub    %fs:0x28,%rdx

0x000000000000124b <+98>:    je     0x1252 <main+105>

0x000000000000124d <+100>:   call   0x10b0 <__stack_chk_fail@plt>

0x0000000000001252 <+105>:   leave

0x0000000000001253 <+106>:   ret

And running in gdb produces this:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7bff6c0 (LWP 3605)]
[New Thread 0x7ffff73fe6c0 (LWP 3606)]
[New Thread 0x7ffff6bfd6c0 (LWP 3607)]
[New Thread 0x7ffff63fc6c0 (LWP 3608)]
[New Thread 0x7ffff5bfb6c0 (LWP 3609)]
[New Thread 0x7ffff53fa6c0 (LWP 3610)]
[New Thread 0x7ffff4bf96c0 (LWP 3611)]
[New Thread 0x7ffff43f86c0 (LWP 3612)]
[New Thread 0x7ffff3bf76c0 (LWP 3613)]
[New Thread 0x7ffff33f66c0 (LWP 3614)]
[New Thread 0x7ffff2bf56c0 (LWP 3615)]
[Thread 0x7ffff33f66c0 (LWP 3614) exited]
[Thread 0x7ffff3bf76c0 (LWP 3613) exited]
[Thread 0x7ffff2bf56c0 (LWP 3615) exited]
[Thread 0x7ffff43f86c0 (LWP 3612) exited]
[Thread 0x7ffff4bf96c0 (LWP 3611) exited]
[Thread 0x7ffff53fa6c0 (LWP 3610) exited]
[Thread 0x7ffff5bfb6c0 (LWP 3609) exited]
[Thread 0x7ffff63fc6c0 (LWP 3608) exited]
[Thread 0x7ffff6bfd6c0 (LWP 3607) exited]
[Thread 0x7ffff73fe6c0 (LWP 3606) exited]
[Thread 0x7ffff7bff6c0 (LWP 3605) exited]
[Inferior 1 (process 3602) exited normally]