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

Precise dependencies compatibility #785

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
4 changes: 4 additions & 0 deletions doc/ref_kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ Loopy's expressions are a slight superset of the expressions supported by
TODO: Functions
TODO: Reductions

Dependencies
^^^^^^^^^^^^
.. automodule:: loopy.kernel.dependency

Function Call Instructions
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
120 changes: 120 additions & 0 deletions loopy/kernel/dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""
.. autofunction:: add_lexicographic_happens_after
"""

__copyright__ = "Copyright (C) 2023 Addison Alvey-Blanco"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import islpy as isl
from islpy import dim_type

from loopy import LoopKernel
from loopy.kernel.instruction import HappensAfter
from loopy.translation_unit import for_each_kernel


@for_each_kernel
def add_lexicographic_happens_after(knl: LoopKernel) -> LoopKernel:
"""Construct a sequential dependency specification between each instruction
and the instruction immediately before it. This takes the order of statements
in :attr:`loopy.LoopKernel.instructions` and considers the inames (loops) within
which each statement is nested. Within the sequence of statements,
each iname must apply to a contiguous region, allowing us to speak of
a 'loop entry' and a 'loop exit' point. Loops/inames must nest properly,
i.e. entry and exit points of a loop/iname must occur nested within
the same outer loop structure.

Based on this understanding of the presented kernel, this routine adds
a dependency structure (see :class:`~loopy.HappensAfter`) copmatible
with a C program consisting of the same loop nest, i.e. statements
within the same loop nest obeying *lexicographic* ordering.

If inames are perfectly nested (i.e. both entry/exit points coincide),
the nesting of the two loops is undefined.
"""

new_insns = []
for iafter, insn_after in enumerate(knl.instructions):
if iafter == 0:
new_insns.append(insn_after)

else:
insn_before = knl.instructions[iafter-1]

domain_before = knl.get_inames_domain(insn_before.within_inames)
domain_after = knl.get_inames_domain(insn_after.within_inames)

shared_inames = insn_before.within_inames & insn_after.within_inames

happens_after = isl.Map.from_domain_and_range(
domain_before,
domain_after)

for idim in range(happens_after.dim(dim_type.out)):
happens_after = happens_after.set_dim_name(
dim_type.out,
idim,
happens_after.get_dim_name(dim_type.out, idim) + "'")

shared_inames_order_before = [
domain_before.get_dim_name(dim_type.out, idim)
for idim in range(domain_before.dim(dim_type.out))
if domain_before.get_dim_name(dim_type.out, idim)
in shared_inames]

shared_inames_order_after = [
domain_after.get_dim_name(dim_type.out, idim)
for idim in range(domain_after.dim(dim_type.out))
if domain_after.get_dim_name(dim_type.out, idim)
in shared_inames]

assert shared_inames_order_after == shared_inames_order_before
shared_inames_order = shared_inames_order_after
Comment on lines +78 to +91
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet here would imply that the order of variables in a set is significant. This is not the case anywhere else in loopy, and I would prefer to not start having it be the case now. This behavior is also not documented.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... I tried, but I ran out of time. 🙂 There's a bit of algorithmic work to be done here, and some important algorithmic pieces are in #690. We can discuss in more detail during our meeting Monday.

Rather than hijack this PR for my big pile of unrelated changes, that's now #864, which this depends on. The relevant pieces from #690 are also in #864, so you'll be able to rely on them.


affs_in = isl.affs_from_space(happens_after.domain().space)
affs_out = isl.affs_from_space(happens_after.range().space)

lex_map = isl.Map.empty(happens_after.space)
for iinnermost, innermost_iname in enumerate(shared_inames):
innermost_map = affs_in[innermost_iname].lt_map(
affs_out[innermost_iname + "'"])

for outer_iname in shared_inames_order[:iinnermost]:
innermost_map = innermost_map & (
affs_in[outer_iname].eq_map(
affs_out[outer_iname + "'"]))

lex_map = lex_map | innermost_map

happens_after = happens_after & lex_map

new_happens_after = {
insn_before.id: HappensAfter(None, happens_after)}

insn_after = insn_after.copy(happens_after=new_happens_after)

new_insns.append(insn_after)

return knl.copy(instructions=new_insns)


# vim: foldmethod=marker
51 changes: 51 additions & 0 deletions test/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
__copyright__ = "Copyright (C) 2023 Addison Alvey-Blanco"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import sys

import loopy as lp
from loopy.kernel.dependency import add_lexicographic_happens_after


def test_lex_dependencies():
knl = lp.make_kernel(
[
"{[a,b]: 0<=a,b<7}",
"{[i,j]: 0<=i,j<n and 0<=a,b<5}",
"{[k,l]: 0<=k,l<n and 0<=a,b<3}"
],
"""
v[a,b,i,j] = 2*v[a,b,i,j]
v[a,b,k,l] = 2*v[a,b,k,l]
""")

knl = add_lexicographic_happens_after(knl)
a-alveyblanc marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
from pytest import main
main([__file__])

# vim: foldmethod=marker
Loading