exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
15.01k stars 517 forks source link

OpenMP: Unable to specify a shared variable #448

Closed marioroy closed 1 year ago

marioroy commented 1 year ago

From the documentation, "Other OpenMP parameters like private, shared or reduction, are inferred automatically by the compiler."

What if Codon assumes wrongly? I wish for the ability to specify last_completed a shared variable, not made private. I'm in a critical block and not incrementing the variable, but instead assigning a value (e.g. not for reduction).

last_completed = -1

@omp.critical
def show_progress(high, limit):
    completed = int(float(high) / limit * 100)
    if completed != last_completed:
        last_completed = completed
        print(f"\rprogress {completed}% ", file=stderr, flush=True, end='')
        stderr.flush()

Error:

error: local variable 'last_completed' referenced before assignment

It's has been an interesting journey, experimenting with OpenMP using C and Codon.

elisbyberi commented 1 year ago

@marioroy last_completed is a global variable. You are referencing it in if completed != last_completed: before assignment. The assignment happens at last_completed = completed, which overrides the global last_completed. At this point last_completed is no longer a global variable, it is a local variable of "show_progress" function. This doesn't work in Python either.

marioroy commented 1 year ago

Thank you, @elisbyberi. I see now and understand.