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

Tree: memoize recursive functions #4

Merged
merged 2 commits into from
Feb 13, 2024
Merged
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
4 changes: 3 additions & 1 deletion loopy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,14 +1004,15 @@ def from_root(root: T):
return Tree(pmap({root: frozenset()}),
pmap({root: None}))

@property
@cached_property
def root(self) -> T:
guess = set(self._child_to_parent).pop()
while self.parent(guess) is not None:
guess = self.parent(guess)

return guess

@memoize_method
def ancestors(self, node: T) -> "FrozenSet[T]":
"""
Returns a :class:`frozenset` of nodes that are ancestors of *node*.
Expand Down Expand Up @@ -1039,6 +1040,7 @@ def children(self, node: T) -> "FrozenSet[T]":

return self._parent_to_children[node]

@memoize_method
def depth(self, node: T) -> int:
if not self.is_a_node(node):
raise ValueError(f"'{node}' not in tree.")
Expand Down
15 changes: 7 additions & 8 deletions loopy/transform/loop_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,17 @@ def _get_ldg_nodes_from_loopy_insn(kernel, insn, candidates, non_candidates,
for candidate in ((insn.within_inames
| insn.reduction_inames())
& candidates)]
elif {loop_nest
for loop_nest in non_candidates
if (loop_nest & insn.within_inames)}:
else:
non_candidate, = {loop_nest
for loop_nest in non_candidates
if (loop_nest & insn.within_inames)}

return [NonCandidateLoop(non_candidate)]
else:
assert ((insn.within_inames & just_outer_loop_nest)
or (insn.within_inames == just_outer_loop_nest))
return [OuterLoopNestStatement(insn.id)]
if non_candidate:
return [NonCandidateLoop(non_candidate)]
else:
assert ((insn.within_inames & just_outer_loop_nest)
or (insn.within_inames == just_outer_loop_nest))
return [OuterLoopNestStatement(insn.id)]


@memoize_on_first_arg
Expand Down
Loading