pyjulia icon indicating copy to clipboard operation
pyjulia copied to clipboard

What does it take for pyjulia to drop GIL?

Open Moelf opened this issue 2 years ago • 13 comments

Numba has a nogil annotation, I'm wondering if we can somehow drop the GIL and what would that imply.

Moelf avatar Jul 25 '23 21:07 Moelf

I think we would need to reformulate the binding as a C or Rust based extension rather than using ctypes

mkitti avatar Jul 26 '23 18:07 mkitti

It seems that using CDLL rather than PyDLL will release the Gil.

yuyichao avatar Jul 26 '23 18:07 yuyichao

We do use CDLL for unix like systems: https://github.com/JuliaPy/pyjulia/blob/69b734f117e489904a07d20f46c8f96b56577779/src/julia/find_libpython.py#L83

mkitti avatar Jul 26 '23 20:07 mkitti

If I understand correctly, it’s the one for libjulia that matters. https://github.com/JuliaPy/pyjulia/blob/69b734f117e489904a07d20f46c8f96b56577779/src/julia/libjulia.py#L220

yuyichao avatar Jul 26 '23 20:07 yuyichao

yeah, I think because you're calling Julia via the libjulia, so whether or not GIL is dropped during call depends on how you loaded libjulia?

Moelf avatar Jul 26 '23 20:07 Moelf

maybe it's worth getting perspective from @stevengj and @MilesCranmer ?

  1. Can we by default drop GIL (i.e. use CDLL)?
  2. Does it actually drop GIL and speed things up?
  3. Does it create corretness issues?
  4. Given 3, how can we drop GIL call by call

Moelf avatar Jul 26 '23 23:07 Moelf

I would love to drop GIL. This would be great so that I can do other things in Python (like printing updates from another thread) while some large Julia processing is happening in the background.

Regarding 3, I'm not sure what correctness issues it would cause, if any. It's unclear to me if you'd be able to mutate any Julia variables while processing is already taking place, so maybe it's safe as-is...? Maybe we should just try it out and see.

MilesCranmer avatar Jul 27 '23 00:07 MilesCranmer

yeah I'd like to try it, but I would prefer if we can present it as an option. In this case, it would be better if it can be a per-call site option, but a per-session option would do.

One case we know definitely won't work is if Julia needs to call Python stuff as call back, or mutate Python variable, but I feel like that's minor usage

Moelf avatar Jul 27 '23 00:07 Moelf

I assume just having two libjulia handle should allow having this as a per call option with the ctypes api.

yuyichao avatar Jul 27 '23 00:07 yuyichao

I can't quite figure out how to have a local testable pyjulia, clone and pip install -e . didn't quite work, or maybe I'm looking at the wrong place to print debug

Moelf avatar Jul 27 '23 01:07 Moelf

I guess for one I can't figure out which handle is used when I do

import julia
#vs.
from julia import Base

I can't get any print statement to work -- these two must share the same Julia handle right? but which one / where I can't quite figure out

Moelf avatar Jul 27 '23 16:07 Moelf

See also https://github.com/cjdoris/PythonCall.jl/issues/343

stevengj avatar Aug 23 '23 20:08 stevengj

I think the main use case is when calling Julia code from Python, releasing the GIL would be useful.

Moelf avatar Aug 23 '23 22:08 Moelf