Skip to content

Commit 3dd46b3

Browse files
authored
Merge pull request #608 from JuliaParallel/jps/datadeps-fields
datadeps: Support field access in Deps
2 parents a5911df + 5e3c829 commit 3dd46b3

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

docs/src/datadeps.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,20 @@ Dagger.spawn_datadeps() do
179179
end
180180
```
181181

182-
You can pass any number of aliasing modifiers to `Deps`. This is particularly
183-
useful for declaring aliasing with `Diagonal`, `Bidiagonal`, `Tridiagonal`, and
184-
`SymTridiagonal` access, as these "wrappers" make a copy of their parent array
185-
and thus can't be used to "mask" access to the parent like `UpperTriangular`
186-
and `UnitLowerTriangular` can (which is valuable for writing memory-efficient,
187-
generic algorithms in Julia).
182+
We call `InOut(Diagonal)` an "aliasing modifier". The purpose of `Deps` is to
183+
pass an argument (here, `A`) as-is, while specifying to Datadeps what portions
184+
of the argument will be accessed (in this case, the diagonal elements) and how
185+
(read/write/both). You can pass any number of aliasing modifiers to `Deps`.
186+
187+
`Deps` is particularly useful for declaring aliasing with `Diagonal`,
188+
`Bidiagonal`, `Tridiagonal`, and `SymTridiagonal` access, as these "wrappers"
189+
make a copy of their parent array and thus can't be used to "mask" access to the
190+
parent like `UpperTriangular` and `UnitLowerTriangular` can (which is valuable
191+
for writing memory-efficient, generic algorithms in Julia).
192+
193+
### Supported Aliasing Modifiers
194+
195+
- Any function that returns the original object or a view of the original object
196+
- `UpperTriangular`/`LowerTriangular`/`UnitUpperTriangular`/`UnitLowerTriangular`
197+
- `Diagonal`/`Bidiagonal`/`Tridiagonal`/`SymTridiagonal` (via `Deps`, e.g. to read from the diagonal of `X`: `Dagger.@spawn sum(Deps(X, In(Diagonal)))`)
198+
- `Symbol` for field access (via `Deps`, e.g. to write to `X.value`: `Dagger.@spawn setindex!(Deps(X, InOut(:value)), :value, 42)`

src/memory-spaces.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ function memory_spans(oa::ObjectAliasing)
162162
return [span]
163163
end
164164

165-
aliasing(x, T) = aliasing(T(x))
165+
function aliasing(x, dep_mod)
166+
if dep_mod isa Symbol
167+
return aliasing(getfield(x, dep_mod))
168+
end
169+
return aliasing(dep_mod(x))
170+
end
166171
function aliasing(x::T) where T
167172
if isbits(x)
168173
return NoAliasing()

test/datadeps.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function test_task_dominators(logs::Dict, tid::Int, doms::Vector; all_tids::Vect
109109
end
110110

111111
@everywhere do_nothing(Xs...) = nothing
112+
@everywhere mut_ref!(R) = (R[] .= 0;)
112113
function test_datadeps(;args_chunks::Bool,
113114
args_thunks::Bool,
114115
args_loc::Int,
@@ -425,10 +426,17 @@ function test_datadeps(;args_chunks::Bool,
425426
end
426427

427428
# Inner Scope
428-
@test_throws Dagger.Sch.SchedulingException Dagger.spawn_datadeps() do
429+
@test_throws Dagger.Sch.SchedulingException Dagger.spawn_datadeps() do
429430
Dagger.@spawn scope=Dagger.ExactScope(Dagger.ThreadProc(1, 5000)) 1+1
430431
end
431432

433+
# Field aliasing
434+
X = Ref(rand(1000))
435+
@test all(x->x==0, fetch(Dagger.spawn_datadeps() do
436+
Dagger.@spawn mut_ref!(Deps(X, InOut(:x)))
437+
Dagger.@spawn getfield(Deps(X, In(:x)), :x)
438+
end))
439+
432440
# Add-to-copy
433441
A = rand(1000)
434442
B = rand(1000)

0 commit comments

Comments
 (0)