Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating optimized chained subparts #138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 23 additions & 14 deletions src/DenseACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,22 +524,31 @@

@inline ACSetInterface.subpart(acs::SimpleACSet, names::Tuple{Vararg{Symbol}}) = subpart(acs, dom_parts(acs, first(names)), names)

ACSetInterface.subpart(acs::StructACSet{S}, part, names::Tuple{Vararg{Symbol}}) where {S} = _subpart(acs, part, Val{S}, Val{names})
ACSetInterface.subpart(acs::DynamicACSet, part, names::Tuple{Vararg{Symbol}}) = runtime(_subpart, acs, part, acs.schema, names)
function ACSetInterface.subpart(acs::StructACSet{S}, part, names::Tuple{Vararg{Symbol}}) where {S}
validate_subparts_chain(Schema(S), names)
_subpart(acs, part, Val(names))
end
function ACSetInterface.subpart(acs::DynamicACSet, part, names::Tuple{Vararg{Symbol}})
validate_subparts_chain(acs.schema, names)
_subpart(acs, part, Val(names))
end
ACSetInterface.subpart(acs::StructACSet{S}, part, ::Val{N}) where {S,N} =
_subpart(acs, part, Val(N))
ACSetInterface.subpart(acs::DynamicACSet, part, ::Val{N}) where N =

Check warning on line 537 in src/DenseACSets.jl

View check run for this annotation

Codecov / codecov/patch

src/DenseACSets.jl#L537

Added line #L537 was not covered by tests
_subpart(acs, part, Val(names))

function validate_subparts_chain(s, names)
length(names) ≤ 1 && return
for i in eachindex(names)[1:end-1]
(codom(s, names[i]) != dom(s, names[i+1])) &&
error("morphisms $(names[i]) and $(names[i+1]) are not composable")
end
end

@ct_enable function _subpart(acs::SimpleACSet, part, @ct(S), @ct(names))
@ct s = Schema(S)
out = subpart(acs, part, @ct first(names))
# necessary because Tuple{Symbol} is still ambigious with presence of Tuple{Vararg{Symbol}}
@ct_ctrl if length(names) > 1
@ct_ctrl for i in 2:length(names)
@ct begin
codom(s, names[i-1]) == dom(s, names[i]) || error("morphisms $(names[i-1]) and $(names[i]) are not composable")
end
out = subpart(acs, out, @ct names[i])
end
@generated function _subpart(acs, part, ::Val{N}) where N
foldl(N, init=:(part)) do acc,n
:(acs[$acc, $(Meta.quot(n))])
end
return out
end

@inline ACSetInterface.has_subpart(::StructACSet{S}, f::Symbol) where {S} =
Expand Down
14 changes: 14 additions & 0 deletions test/ACSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,20 @@ datcompdyn = DynamicACSet(datcomp)
@test subpart(datcompdyn, :, (:f,)) == [1,2,3,1,2]
@test subpart(datcompdyn, (:f,)) == [1,2,3,1,2]

# Allocations of composites of subparts
#------------------------------------
# If type inference is successful, then this loop will be optimized to have 0 allocations.
buffer = Vector{Symbol}(undef, nparts(datcomp, :Z))
function assignment_loop!(buffer::Vector{Symbol}, dc::SimpleACSet)
@inbounds for i in parts(dc, :Z)
buffer[i] = subpart(dc, i, Val((:zattr,)))
end
end
@allocations assignment_loop!(buffer, datcomp)
allocs = @allocations assignment_loop!(buffer, datcomp)
@test allocs == 0


# Composites of subparts for incident
#------------------------------------

Expand Down
Loading