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

Implements loop distribution transformation #629

Draft
wants to merge 8 commits 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
5 changes: 5 additions & 0 deletions doc/ref_transform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ Manipulating Instructions

.. autofunction:: add_barrier

Loop Distribution
-----------------

.. automodule:: loopy.transform.loop_distribution

Registering Library Routines
----------------------------

Expand Down
28 changes: 13 additions & 15 deletions doc/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ commonly called 'loop tiling':
... assumptions="n mod 16 = 0 and n >= 1")
>>> knl = lp.split_iname(knl, "i", 16)
>>> knl = lp.split_iname(knl, "j", 16)
>>> knl = lp.prioritize_loops(knl, "i_outer,j_outer,i_inner")
>>> knl = lp.prioritize_loops(knl, "i_outer,j_outer,i_inner,j_inner")
>>> knl = lp.set_options(knl, write_code=True)
>>> evt, (out,) = knl(queue, a=a_mat_dev)
#define lid(N) ((int) get_local_id(N))
Expand Down Expand Up @@ -1029,8 +1029,8 @@ transformation exists in :func:`loopy.add_prefetch`:
>>> evt, (out,) = knl_pf(queue, a=x_vec_dev)
#define lid(N) ((int) get_local_id(N))
...
acc_k = 0.0f;
a_fetch = a[16 * gid(0) + lid(0)];
acc_k = 0.0f;
for (int k = 0; k <= 15; ++k)
acc_k = acc_k + a_fetch;
out[16 * gid(0) + lid(0)] = acc_k;
Expand All @@ -1053,10 +1053,10 @@ earlier:
>>> evt, (out,) = knl_pf(queue, a=x_vec_dev)
#define lid(N) ((int) get_local_id(N))
...
if (-1 + -16 * gid(0) + -1 * lid(0) + n >= 0)
acc_k = 0.0f;
if (-1 + -16 * gid(0) + -1 * lid(0) + n >= 0)
a_fetch[lid(0)] = a[16 * gid(0) + lid(0)];
if (-1 + -16 * gid(0) + -1 * lid(0) + n >= 0)
acc_k = 0.0f;
barrier(CLK_LOCAL_MEM_FENCE) /* for a_fetch (insn_k_update depends on a_fetch_rule) */;
if (-1 + -16 * gid(0) + -1 * lid(0) + n >= 0)
{
Expand Down Expand Up @@ -1908,18 +1908,16 @@ Now to make things more interesting, we'll create a kernel with barriers:
{
__local int c[50 * 10 * 99];
<BLANKLINE>
{
int const k_outer = 0;
<BLANKLINE>
for (int i = 0; i <= 49; ++i)
for (int j = 0; j <= 9; ++j)
for (int i = 0; i <= 49; ++i)
{
barrier(CLK_LOCAL_MEM_FENCE) /* for c (insn rev-depends on insn_0) */;
c[990 * i + 99 * j + lid(0) + 1] = 2 * a[980 * i + 98 * j + lid(0) + 1];
barrier(CLK_LOCAL_MEM_FENCE) /* for c (insn_0 depends on insn) */;
e[980 * i + 98 * j + lid(0) + 1] = c[990 * i + 99 * j + 1 + lid(0) + 1] + c[990 * i + 99 * j + -1 + lid(0) + 1];
}
}
{
int const k_outer = 0;
<BLANKLINE>
barrier(CLK_LOCAL_MEM_FENCE) /* for c (insn rev-depends on insn_0) */;
c[990 * i + 99 * j + lid(0) + 1] = 2 * a[980 * i + 98 * j + lid(0) + 1];
barrier(CLK_LOCAL_MEM_FENCE) /* for c (insn_0 depends on insn) */;
e[980 * i + 98 * j + lid(0) + 1] = c[990 * i + 99 * j + 1 + lid(0) + 1] + c[990 * i + 99 * j + -1 + lid(0) + 1];
}
}

In this kernel, when a work-item performs the second instruction it uses data
Expand Down
4 changes: 4 additions & 0 deletions loopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
from loopy.transform.pack_and_unpack_args import pack_and_unpack_args_for_call

from loopy.transform.realize_reduction import realize_reduction
from loopy.transform.loop_distribution import (distribute_loops,
IllegalLoopDistributionError)

# }}}

Expand Down Expand Up @@ -254,6 +256,8 @@

"pack_and_unpack_args_for_call",

"distribute_loops", "IllegalLoopDistributionError",

# }}}

"get_dot_dependency_graph",
Expand Down
60 changes: 60 additions & 0 deletions loopy/kernel/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,4 +2115,64 @@ def get_outer_params(domains):
# }}}


# {{{ get access map from an instruction

class _IndexCollector(CombineMapper):
def __init__(self, var):
self.var = var
super().__init__()

def combine(self, values):
import operator
return reduce(operator.or_, values, frozenset())

def map_subscript(self, expr):
if expr.aggregate.name == self.var:
return (super().map_subscript(expr) | frozenset([expr.index_tuple]))
else:
return super().map_subscript(expr)

def map_algebraic_leaf(self, expr):
return frozenset()

map_constant = map_algebraic_leaf


def _project_out_inames_from_maps(amaps, inames_to_project_out):
new_amaps = []
for amap in amaps:
for iname in inames_to_project_out:
dt, pos = amap.get_var_dict()[iname]
amap = amap.project_out(dt, pos, 1)

new_amaps.append(amap)

return new_amaps


def _union_amaps(amaps):
import islpy as isl
return reduce(isl.Map.union, amaps[1:], amaps[0])


def get_insn_access_map(kernel, insn_id, var):
from loopy.transform.subst import expand_subst
from loopy.match import Id
from loopy.symbolic import get_access_map

insn = kernel.id_to_insn[insn_id]

kernel = expand_subst(kernel, within=Id(insn_id))
indices = list(_IndexCollector(var)((insn.expression,
insn.assignees,
tuple(insn.predicates))))

amaps = [get_access_map(kernel.get_inames_domain(insn.within_inames),
idx, kernel.assumptions)
for idx in indices]

return _union_amaps(amaps)

# }}}

# vim: foldmethod=marker
Loading