|
| 1 | +module PythonExt |
| 2 | + |
| 3 | +if isdefined(Base, :get_extension) |
| 4 | + using PythonCall |
| 5 | +else |
| 6 | + using ..PythonCall |
| 7 | +end |
| 8 | + |
| 9 | +import Dagger |
| 10 | +import Dagger: Processor, OSProc, ThreadProc, Chunk |
| 11 | +import Distributed: myid |
| 12 | + |
| 13 | +const CPUProc = Union{OSProc, ThreadProc} |
| 14 | + |
| 15 | +struct PythonProcessor <: Processor |
| 16 | + owner::Int |
| 17 | +end |
| 18 | + |
| 19 | +Dagger.root_worker_id(proc::PythonProcessor) = proc.owner |
| 20 | +Dagger.get_parent(proc::PythonProcessor) = OSProc(proc.owner) |
| 21 | +Dagger.default_enabled(::PythonProcessor) = true |
| 22 | + |
| 23 | +Dagger.iscompatible_func(::ThreadProc, opts, ::Type{Py}) = false |
| 24 | +Dagger.iscompatible_func(::PythonProcessor, opts, ::Type{Py}) = true |
| 25 | +Dagger.iscompatible_arg(::PythonProcessor, opts, ::Type{Py}) = true |
| 26 | +Dagger.iscompatible_arg(::PythonProcessor, opts, ::Type{<:PyArray}) = true |
| 27 | + |
| 28 | +Dagger.move(from_proc::CPUProc, to_proc::PythonProcessor, x::Chunk) = |
| 29 | + Dagger.move(from_proc, to_proc, Dagger.move(from_proc, Dagger.get_parent(to_proc), x)) |
| 30 | +Dagger.move(::CPUProc, ::PythonProcessor, x) = Py(x) |
| 31 | +Dagger.move(::CPUProc, ::PythonProcessor, x::Py) = x |
| 32 | +Dagger.move(::CPUProc, ::PythonProcessor, x::PyArray) = x |
| 33 | +# FIXME: Conversion from Python to Julia |
| 34 | + |
| 35 | +function Dagger.execute!(::PythonProcessor, f, args...; kwargs...) |
| 36 | + @assert f isa Py "Function must be a Python object" |
| 37 | + @show f args kwargs |
| 38 | + return f(args...; kwargs...) |
| 39 | +end |
| 40 | + |
| 41 | +function __init__() |
| 42 | + Dagger.add_processor_callback!(:pythonproc) do |
| 43 | + return PythonProcessor(myid()) |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +end # module PythonExt |
0 commit comments