Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f4e0f29
test: run the NoAliasing slot tests everywhere, including across spaces
jpsamaroo Nov 28, 2026
a0365a5
datadeps: decide slot sharing with a dispatchable `describes_memory`
jpsamaroo Nov 28, 2026
51ea205
datadeps: an unkeyed argument in place is its own slot; check copies'…
jpsamaroo Nov 28, 2026
af59cd7
datadeps: free unkeyed copies at region end, after all their users
jpsamaroo Nov 28, 2026
749afd3
AGENTS.md: Datadeps orders nothing on NoAliasing slots
jpsamaroo Nov 28, 2026
04abcce
datadeps: pass arguments whose aliasing describes no memory by value
jpsamaroo Nov 28, 2026
96ff204
datadeps: never record an argument's own payload as an unkeyed copy
jpsamaroo Nov 28, 2026
f47ab01
AGENTS.md: NoAliasing arguments are passed by value
jpsamaroo Nov 28, 2026
629a36f
test: MPI's by-value NoAliasing copies are not Libc-backed
jpsamaroo Nov 28, 2026
3825255
datadeps: an isbits field dependency aliases the field's own memory
jpsamaroo Nov 28, 2026
9f8aea2
datadeps: reject write access to dependencies that describe no memory
jpsamaroo Nov 28, 2026
df62082
AGENTS.md: a field dependency's memory is the field's
jpsamaroo Nov 28, 2026
f6ed740
Keep field aliasing divisible alongside its parent
jpsamaroo Nov 30, 2026
2a61f6e
Serialize mutable field values without copying GC pointer bytes
jpsamaroo Nov 30, 2026
3519a43
Document every effect of aliasing that describes no memory
jpsamaroo Nov 30, 2026
32acd62
Keep returned by-value copies alive after the region ends
jpsamaroo Nov 30, 2026
791d5aa
Allocate unkeyed-copy storage only when a copy exists
jpsamaroo Nov 30, 2026
2cdacab
Exercise private copies nested below a view slot
jpsamaroo Nov 30, 2026
c6f8db8
Merge master and reuse its private-copy ownership tables
jpsamaroo Nov 30, 2026
177ef8d
Preserve by-value dependencies across backend type conversion
jpsamaroo Nov 30, 2026
3b69d4b
Serialize reference fields reached through immutable wrappers
jpsamaroo Nov 30, 2026
9ea71de
Describe the private-copy user lookup after integration
jpsamaroo Nov 30, 2026
5c91327
Keep tuple aggregates distinct from their shared child slots
jpsamaroo Nov 30, 2026
4658776
Check outer-scope coverage with explicit placement
jpsamaroo Nov 30, 2026
2c0a254
Count result-aware frees in cleanup log assertions
jpsamaroo Nov 30, 2026
f80fc62
Bound escaped-storage inspection across shared object graphs
jpsamaroo Nov 30, 2026
150dbaa
Retain copies returned through opaque owning handles
jpsamaroo Dec 1, 2026
1b86518
Distinguish immutable field bindings from mutable contents
jpsamaroo Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ this section verbatim). Each entry is one bullet starting with `- `.
reasoning about which arguments Datadeps touches, check `type_may_alias`,
not `aliasing`. A single-process `spawn_datadeps` with two such values
reproduces it; it is not MPI-specific.

100. **An argument's write history must stay sorted by write number.**
`compute_remainder_for_arg!` walks `arg_history` backwards and treats each
entry as newer than the ones before it. It uses each entry both to choose
Expand Down Expand Up @@ -1217,3 +1217,110 @@ this section verbatim). Each entry is one bullet starting with `- `.
the protocol alone is not enough for them. Test with a remote reader
*and* a remote writer, in both orders: in one process every slot is the
argument itself, so none of this is visible.

Tuple and NamedTuple aggregates need the same treatment when a child's
field storage supplies all their memory. Rebuild their headers around
shared child slots, and provide whole-copy movement of memory-bearing
children; bitstype/by-value children are already complete in the slot.

109. **Datadeps orders nothing on `NoAliasing`, so such arguments must be
passed by value.** The dependency helpers (`_get_write_deps!` and friends)
return early for a `NoAliasing` ainfo, so no task ever waits on such a slot
through dependency tracking. Two consequences. First, the slot must be
complete when it is created: `move_rewrap` gives it `move`'s copy (never
the uninitialized-slot path), and `compute_remainder_for_arg!` schedules no
copy into or out of it. Otherwise the reader is not ordered after the copy
that fills its slot: an isbits `Vector` declared `NoAliasing`, created on
one worker and summed on another, gave a wrong sum 10 times out of 10 with
4-thread workers -- and 0 out of 10 with 1-thread workers, where the copy
and the reader happen to run in submission order. Second, anything that
must wait for a slot's users has to track them itself: a region-end free
keyed on the slot's ainfo races the tasks still reading it (what happened
to cached `NoAliasing` copies before PR #792), which is why
`copy_users` records every reader for the matching `CopyKey` free. Test
ordering with multithreaded workers, frees with slow readers that check the
buffer is still allocated, and "nothing is copied in" with a `move!` that
raises. Beware that aliasing is per *dependency*, and "no memory" must
mean the dependency truly has none: an isbits field of a mutable `X`
(`Deps(X, InOut(:field))`) is memory inside `X`, so `field_aliasing` gives
it a `FieldAliasing` span at the field's offset. While it came out as the
isbits value's `NoAliasing`, a write to it from another memory space was
silently lost, and rejecting writes to `NoAliasing` would have broken that
documented usage outright.

110. **A field span must not look like an indivisible container.** Bare
`ObjectAliasing` is the dispatch signal for containers whose storage must
be copied as a whole. An inline field also has an object-relative pointer,
but is only part of its parent: returning bare `ObjectAliasing` for it
makes a field/parent dependency combination hit the whole-container owner
assertion. `FieldAliasing` preserves the span while keeping it divisible.
An immutable wrapper costs no extra descriptor allocation; wrapping it in
a one-element `CombinedAliasing` vector added three allocations (80 bytes)
per field query in the isolated warmed probe.

111. **A mutable field is writable even when its declared type is not isbits,
but its reference bytes are never a wire format.** `Any`, abstract,
`Union`, `String` and `Symbol` fields have storage inside their parent just
like `Int` fields. Describing only the value incorrectly rejects writes
when that value is `NoAliasing`. Describe the field storage and retain
referent aliasing where present. For partial copies, split bytes around
reference-valued fields and serialize scalar field values separately;
scattering raw source-process pointers into a destination object bypasses
Julia's GC and is invalid. Both Distributed and MPI need this protocol;
MPI's previous partial-field path packed bytes even for `FullCopy`.

112. **An eager slot free must preserve storage that escapes in a task result.**
A read-only, by-value input can be returned directly, through a view, or
inside a result container. Waiting for its readers to finish is therefore
necessary but insufficient: freeing their slot before the result is
serialized produces EFAULT or a worker crash. Pass resolved reader results
to cleanup as ordinary task inputs, inspect their owner-local storage
identities, and let GC own any escaping copy. `NoAliasing` itself cannot
answer this question because it deliberately hides the buffer's memory.

113. **Rare-copy bookkeeping should reuse existing lazy ownership tables.**
The first private-copy table allocated an empty vector in every
`AliasedObjectCacheStore`, even for entirely local regions. Making it lazy
reduced the warmed 50-region probe from 36,781 allocations /2,030,528 bytes
to 35,802 /1,955,776 bytes. Integration with `CopyKey` removes that separate
vector altogether: the existing cache tables hold copies and the lazy
`UnkeyedSlots` holds their readers. The store stays mutable to avoid
re-boxing its large immutable value on dynamically typed planning paths.

114. **A by-value dependency keeps its contract after backend conversion.**
A host `Vector` can declare `NoAliasing` while `move` returns a device
array whose ordinary aliasing describes its device buffer. Remainder
planning must check the original dependency before comparing destination
spans: those spans can differ in both count and address space. Otherwise
a host-only NoAliasing declaration fails a host-to-device read with a span
count assertion, or schedules an unordered copy into an already complete
by-value slot. Test the host declaration alone; declaring the converted
GPU type NoAliasing as well hides this failure.

115. **Reference-safe remainder selection must look through immutable wrappers.**
A tuple has no mutable fields of its own, but its aliasing spans can reach
a mutable child's `Any` or `String` field. Selecting raw-byte staging from
only the root's mutability then copies that child's GC pointer. Recurse
through non-array field types when selecting `FieldRemainder`, and keep
direct arrays on their ordinary staging path. Check that nested reference
slots are zero in the byte payload as well as checking their copied values.

116. **A depth limit does not bound a shared-graph walk.** Each node of a
small DAG can reference the same child twice, making recursive inspection
revisit exponentially many paths without approaching its depth limit.
Cleanup must also cap total visits and retain storage conservatively when
proof runs out. Pass the remaining work through sibling inspections and
returned values; resetting it on each edge does not bound the total work.
Even `===` can recursively compare non-bits immutable aggregates: avoid
that comparison for matching immutable payload/result types and retain
conservatively instead.

117. **An owning handle's fields do not reveal all the storage it retains.**
A pending `DTask` can keep an argument alive in its running work and return
it later, while its `DFuture`/`DRef` fields expose only pool identifiers.
Reflecting those fields claimed that the input did not escape, and a
Datadeps cleanup could free it while the child still used it. Treat known
work/future/pool/channel handles, foreign mutable values and nonlocal
chunks conservatively; do not block on pending work or load remote
storage merely to decide a free. Native and third-party objects can root
Julia storage through private tables without any visible field reference.
17 changes: 17 additions & 0 deletions docs/src/datadeps.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ for writing memory-efficient, generic algorithms in Julia).
- `Diagonal`/`Bidiagonal`/`Tridiagonal`/`SymTridiagonal` (via `Deps`, e.g. to read from the diagonal of `X`: `Dagger.@spawn sum(Deps(X, In(Diagonal)))`)
- `Symbol` for field access (via `Deps`, e.g. to write to `X.value`: `Dagger.@spawn setindex!(Deps(X, InOut(:value)), :value, 42)`

A field dependency covers different memory depending on the field:

- An isbits field of a mutable struct (an `Int`, a `Float64`, an isbits struct,
...) is stored inside the struct, so the dependency covers exactly that field.
It may be written from any memory space: Datadeps copies just that field to
where the task runs, and back afterwards.
- A non-isbits field of a mutable struct includes the field's own storage too.
Scalar writes to fields declared as `Any`, an abstract type, a `Union`,
`String` or `Symbol` are copied back just like isbits fields. Julia values are
serialized; process-local GC pointers are never copied as bytes.
- A field referring to another memory-bearing object (an array, say) also
covers that object. Writing *into* it (e.g. `X.v .= 0`) is tracked while its
backing storage remains fixed; replacing or resizing that storage is not.
- A field of an immutable struct cannot be rebound. Writes into a memory-bearing
field value remain supported. An isbits field describes no memory, so it may
only be read (`In`).

## In-place data movement rules

Datadeps uses a specialized 5-argument function, `Dagger.move!(dep_mod, from_space::Dagger.MemorySpace, to_space::Dagger.MemorySpace, from, to)`, for managing in-place data movement. This function is an in-place variant of the more general `move` function (see [Data movement rules](@ref)) and is exclusively used within the Datadeps system. The `dep_mod` argument is usually just `identity`, but it can also be an access modifier function like `UpperTriangular`, which limits what portion of the data should be read from and written to.
Expand Down
43 changes: 39 additions & 4 deletions ext/MPIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,17 @@ function move!(dep_mod, to_space::MPIMemorySpace, from_space::MPIMemorySpace, to
move!(identity, to_space.innerSpace, from_space.innerSpace, to_val, val)
end
end
elseif dep_mod isa Symbol
# A field may contain a GC reference (Any, String, Union, ...).
# Serialize its value rather than packing process-local pointers.
if local_rank == from_space.rank
value = getfield(poolget(from.handle; uniform=false), dep_mod)
send_yield(value, to_space.comm, to_space.rank, tag)
elseif local_rank == to_space.rank
value = recv_yield(from_space.comm, from_space.rank, tag)
to_val = poolget(to.handle; uniform=false)
Dagger.move_field_value!(to_space.innerSpace, from_space.innerSpace, to_val, dep_mod, value)
end
else
# Partial copy (e.g. UpperTriangular): the destination must only
# update the dep_mod-selected region. Both sides compute the
Expand Down Expand Up @@ -1813,6 +1824,22 @@ function move!(dep_mod::Dagger.RemainderAliasing{<:MPIMemorySpace}, to_space::MP
else
tag = Dagger.in_task() ? to_tag() : to_tag(from.handle)
@dagdebug nothing :mpi "[$local_rank][$tag] Moving from $(from_space.rank) to $(to_space.rank)\n"
if Dagger.remainder_needs_fields(chunktype(from))
# This protocol choice depends only on the rank-uniform chunktype.
# Field values travel through Julia serialization; dense/device
# arrays retain the staging-buffer protocol below.
if local_rank == from_space.rank
from_raw = poolget(from.handle; uniform=false)
copies = Dagger.gather_field_remainder(from_raw, Iterators.map(first, dep_mod.spans))
send_yield(copies, to_space.comm, to_space.rank, tag)
elseif local_rank == to_space.rank
copies = recv_yield(from_space.comm, from_space.rank, tag)
to_raw = poolget(to.handle; uniform=false)
Dagger.scatter_field_remainder!(to_raw, copies, Iterators.map(last, dep_mod.spans))
Base.Threads.atomic_fence()
end
return
end
if local_rank == from_space.rank
# Gather the source spans into a host buffer (device arrays pack
# via multi_span_gather! / one KA launch, then a single DtoH)
Expand Down Expand Up @@ -1939,6 +1966,8 @@ mpi_remap_ainfo(a::Dagger.ContiguousAliasing, owner::Int) =
Dagger.ContiguousAliasing(mpi_remap_span(a.span, owner))
mpi_remap_ainfo(a::Dagger.ObjectAliasing, owner::Int) =
Dagger.ObjectAliasing(mpi_remap_ptr(a.ptr, owner), a.sz)
mpi_remap_ainfo(a::Dagger.FieldAliasing, owner::Int) =
Dagger.FieldAliasing(mpi_remap_ainfo(a.storage, owner))
function mpi_remap_ainfo(a::Dagger.StridedAliasing{T,N,S}, owner::Int) where {T,N,S}
base_ptr = mpi_remap_ptr(a.base_ptr, owner)
return Dagger.StridedAliasing{T,N,typeof(base_ptr.space)}(base_ptr,
Expand Down Expand Up @@ -2173,15 +2202,21 @@ function move_rewrap(accel::MPIAcceleration, cache::AliasedObjectCache, from_pro
# N.B. The predicate depends only on the two spaces and the destination
# type, all rank-uniform, so every rank takes the same branch and the
# collectives below stay matched.
#
# An argument whose aliasing describes no memory is passed by value: its
# slot must be the transferred copy, never an uninitialized buffer (see
# the generic `move_rewrap`). The aliasing is the same (collective) query
# `aliased_object!` would make, just made first.
ainfo = aliasing(cache.accel, w, identity)
T_dest = move_type(mpi_inner_proc(as_mpi_proc(from_proc, from_space)),
mpi_inner_proc(as_mpi_proc(to_proc, to_space)), T)
if Dagger.slot_may_be_uninit(from_space, to_space, T_dest)
return aliased_object!(cache, w) do w
if Dagger.slot_may_be_uninit(from_space, to_space, T_dest) && Dagger.describes_memory(ainfo)::Bool
return aliased_object!(cache, w; ainfo) do w
return mpi_endpoint_alloc(accel, to_proc, to_space, T_dest, w)
end
end
# The transfer makes its payload freeable on the destination rank
return aliased_object!(cache, w) do w
# The transfer makes its payload freeable on the destination rank.
return aliased_object!(cache, w; ainfo) do w
return mpi_endpoint_transfer(accel, from_proc, to_proc, from_space, to_space, w)
end
end
Expand Down
Loading
Loading