-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Python Integration | ||
|
||
If you're using Python as your main programming language, Dagger can be easily | ||
integrated into your workflow. Dagger has built-in support for Python, which | ||
you can easily access through the `pydaggerjl` library (accessible through | ||
`pip`). This library provides a Pythonic interface to Dagger, allowing you to | ||
spawn Dagger tasks that run Python functions on Python arguments. | ||
|
||
Here's a simple example of interfacing between Python's `numpy` library and | ||
Dagger: | ||
|
||
```python | ||
import numpy as np | ||
from pydaggerjl import daggerjl | ||
|
||
# Create a Dagger DTask to sum the elements of an array | ||
task = daggerjl.spawn(np.sum, np.array([1, 2, 3])) | ||
|
||
# Wait on task to finish | ||
# This is purely educational, as fetch will wait for the task to finish | ||
daggerjl.wait(task) | ||
|
||
# Fetch the result | ||
result = daggerjl.fetch(task) | ||
|
||
print(f"The sum is: {result}") | ||
|
||
# Create two numpy arrays | ||
a = np.array([1, 2, 3]) | ||
b = np.array([4, 5, 6]) | ||
|
||
# Element-wise sum of the two arrays | ||
task = daggerjl.spawn(np.add, a, b) | ||
|
||
# Fetch the result | ||
result = daggerjl.fetch(task) | ||
|
||
print(f"The element-wise sum is: {result}") | ||
|
||
# Element-wise sum of last result with itself | ||
task2 = daggerjl.spawn(np.add, task, task) | ||
|
||
# Fetch the result | ||
result2 = daggerjl.fetch(task2) | ||
|
||
print(f"The element-wise sum of the last result with itself is: {result2}") | ||
``` | ||
|
||
Keep an eye on Dagger and pydaggerjl - new features are soon to come! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module PythonExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using PythonCall | ||
else | ||
using ..PythonCall | ||
end | ||
|
||
import Dagger | ||
import Dagger: Processor, OSProc, ThreadProc, Chunk | ||
import Distributed: myid | ||
|
||
const CPUProc = Union{OSProc, ThreadProc} | ||
|
||
struct PythonProcessor <: Processor | ||
owner::Int | ||
end | ||
|
||
Dagger.root_worker_id(proc::PythonProcessor) = proc.owner | ||
Dagger.get_parent(proc::PythonProcessor) = OSProc(proc.owner) | ||
Dagger.default_enabled(::PythonProcessor) = true | ||
|
||
Dagger.iscompatible_func(::ThreadProc, opts, ::Type{Py}) = false | ||
Dagger.iscompatible_func(::PythonProcessor, opts, ::Type{Py}) = true | ||
Dagger.iscompatible_arg(::PythonProcessor, opts, ::Type{Py}) = true | ||
Dagger.iscompatible_arg(::PythonProcessor, opts, ::Type{<:PyArray}) = true | ||
|
||
Dagger.move(from_proc::CPUProc, to_proc::PythonProcessor, x::Chunk) = | ||
Dagger.move(from_proc, to_proc, Dagger.move(from_proc, Dagger.get_parent(to_proc), x)) | ||
Dagger.move(::CPUProc, ::PythonProcessor, x) = Py(x) | ||
Dagger.move(::CPUProc, ::PythonProcessor, x::Py) = x | ||
Dagger.move(::CPUProc, ::PythonProcessor, x::PyArray) = x | ||
# FIXME: Conversion from Python to Julia | ||
|
||
function Dagger.execute!(::PythonProcessor, f, args...; kwargs...) | ||
@assert f isa Py "Function must be a Python object" | ||
return f(args...; kwargs...) | ||
end | ||
|
||
function __init__() | ||
Dagger.add_processor_callback!(:pythonproc) do | ||
return PythonProcessor(myid()) | ||
end | ||
end | ||
|
||
end # module PythonExt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using PythonCall | ||
using CondaPkg | ||
|
||
CondaPkg.add("numpy") | ||
|
||
np = pyimport("numpy") | ||
|
||
# Restart scheduler to see new methods | ||
Dagger.cancel!(;halt_sch=true) | ||
|
||
@testset "spawn" begin | ||
a = np.array([1, 2, 3]) | ||
|
||
t = Dagger.@spawn np.sum(a) | ||
result = fetch(t) | ||
@test result isa Py | ||
@test pyconvert(Int, result) == 6 | ||
|
||
b = np.array([4, 5, 6]) | ||
|
||
t = Dagger.@spawn np.add(a, b) | ||
result = fetch(t) | ||
@test result isa Py | ||
@test pyconvert(Array, result) == [5, 7, 9] | ||
|
||
t2 = Dagger.@spawn np.add(t, b) | ||
result = fetch(t2) | ||
@test result isa Py | ||
@test pyconvert(Array, result) == [9, 12, 15] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters