Skip to content

Commit 676a4c4

Browse files
committed
Introduce lp.decouple_domain.
1 parent 0cc1840 commit 676a4c4

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

loopy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
tag_array_axes,
152152
tag_data_axes,
153153
)
154+
from loopy.transform.domain import decouple_domain
154155
from loopy.transform.fusion import fuse_kernels
155156
from loopy.transform.iname import (
156157
add_inames_for_unused_hw_axes,
@@ -317,6 +318,7 @@
317318
"clear_in_mem_caches",
318319
"collect_common_factors_on_increment",
319320
"concatenate_arrays",
321+
"decouple_domain",
320322
"duplicate_inames",
321323
"expand_subst",
322324
"extract_subst",

loopy/transform/domain.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from __future__ import annotations
2+
3+
4+
__copyright__ = "Copyright (C) 2023 Kaushik Kulkarni"
5+
6+
__license__ = """
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
"""
25+
26+
__doc__ = """
27+
.. currentmodule:: loopy
28+
29+
.. autofunction:: decouple_domain
30+
"""
31+
32+
33+
from typing import TYPE_CHECKING
34+
35+
import islpy as isl
36+
37+
from loopy.diagnostic import LoopyError
38+
from loopy.translation_unit import for_each_kernel
39+
40+
41+
if TYPE_CHECKING:
42+
from collections.abc import Collection
43+
44+
from loopy.kernel import LoopKernel
45+
46+
47+
@for_each_kernel
48+
def decouple_domain(kernel: LoopKernel,
49+
inames: Collection[str],
50+
parent_inames: Collection[str]) -> LoopKernel:
51+
r"""
52+
Returns a copy of *kernel* with altered domains. The home domain of
53+
*inames* i.e. :math:`\mathcal{D}^{\text{home}}({\text{inames}})` is
54+
replaced with two domains :math:`\mathcal{D}_1` and :math:`\mathcal{D}_2`.
55+
:math:`\mathcal{D}_1` is the domain with dimensions corresponding to *inames*
56+
projected out and :math:`\mathcal{D}_2` is the domain with all the dimensions
57+
other than the ones corresponding to *inames* projected out.
58+
59+
:arg inames: The inamaes to be decouple from their home domain.
60+
:arg parent_inames: Inames in :math:`\mathcal{D}^{\text{home}}({\text{inames}})`
61+
that will be used as additional parametric dimensions during the
62+
construction of :math:`\mathcal{D}_1`.
63+
64+
.. note::
65+
66+
- An error is raised if all the *inames* do not correspond to the same home
67+
domain of *kernel*.
68+
- It is the caller's responsibility to ensure that :math:`\mathcal{D}_1
69+
\cup \mathcal{D}_2 = \mathcal{D}^{\text{home}}({\text{inames}})`. If this
70+
criterion is violated this transformation would violate dependencies.
71+
"""
72+
73+
# {{{ sanity checks
74+
75+
if not inames:
76+
raise LoopyError("No inames were provided to decouple into"
77+
" a different domain.")
78+
if frozenset(parent_inames) & frozenset(inames):
79+
raise LoopyError("Inames cannot be appear in `inames` and `parent_inames`.")
80+
81+
# }}}
82+
83+
hdi = kernel.get_home_domain_index(next(iter(inames)))
84+
for iname in inames:
85+
if kernel.get_home_domain_index(iname) != hdi:
86+
raise LoopyError("inames are not a part of the same home domain.")
87+
88+
all_dims = frozenset(kernel.domains[hdi].get_var_dict())
89+
for parent_iname in parent_inames:
90+
if parent_iname not in all_dims:
91+
raise LoopyError(f"Parent iname '{parent_iname}' not a part of the"
92+
f" corresponding home domain '{kernel.domains[hdi]}'.")
93+
94+
dom1 = kernel.domains[hdi]
95+
dom2 = kernel.domains[hdi]
96+
97+
for iname in sorted(all_dims):
98+
if iname in inames:
99+
dt, pos = dom1.get_var_dict()[iname]
100+
dom1 = dom1.project_out(dt, pos, 1)
101+
elif iname in parent_inames:
102+
dt, pos = dom2.get_var_dict()[iname]
103+
if dt != isl.dim_type.param:
104+
n_params = dom2.dim(isl.dim_type.param)
105+
dom2 = dom2.move_dims(isl.dim_type.param, n_params, dt, pos, 1)
106+
else:
107+
dt, pos = dom2.get_var_dict()[iname]
108+
dom2 = dom2.project_out(dt, pos, 1)
109+
110+
new_domains = list(kernel.domains)
111+
new_domains[hdi] = dom1
112+
new_domains.append(dom2)
113+
kernel = kernel.copy(domains=new_domains)
114+
return kernel
115+
116+
# vim: fdm=marker

0 commit comments

Comments
 (0)