-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fc33ae
commit 01801e2
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
__copyright__ = "Copyright (C) 2023 Kaushik Kulkarni" | ||
|
||
__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. | ||
""" | ||
|
||
__doc__ = """ | ||
.. currentmodule:: loopy | ||
.. autofunction:: decouple_domain | ||
""" | ||
|
||
import islpy as isl | ||
|
||
from loopy.translation_unit import for_each_kernel | ||
from loopy.kernel import LoopKernel | ||
from loopy.diagnostic import LoopyError | ||
from collections.abc import Collection | ||
|
||
|
||
@for_each_kernel | ||
def decouple_domain(kernel: LoopKernel, | ||
inames: Collection[str], | ||
parent_inames: Collection[str]) -> LoopKernel: | ||
r""" | ||
Returns a copy of *kernel* with altered domains. The home domain of | ||
*inames* i.e. :math:`\mathcal{D}^{\text{home}}({\text{inames}})` is | ||
replaced with two domains :math:`\mathcal{D}_1` and :math:`\mathcal{D}_2`. | ||
:math:`\mathcal{D}_1` is the domain with dimensions corresponding to *inames* | ||
projected out and :math:`\mathcal{D}_2` is the domain with all the dimensions | ||
other than the ones corresponding to *inames* projected out. | ||
.. note:: | ||
An error is raised if all the *inames* do not correspond to the same home | ||
domain of *kernel*. | ||
""" | ||
|
||
if not inames: | ||
raise LoopyError("No inames were provided to decouple into" | ||
" a different domain.") | ||
|
||
hdi = kernel.get_home_domain_index(next(iter(inames))) | ||
for iname in inames: | ||
if kernel.get_home_domain_index(iname) != hdi: | ||
raise LoopyError("inames are not a part of the same home domain.") | ||
|
||
for parent_iname in parent_inames: | ||
if parent_iname not in set(kernel.domains[hdi].get_var_dict()): | ||
raise LoopyError(f"Parent iname '{parent_iname}' not a part of the" | ||
f" corresponding home domain '{kernel.domains[hdi]}'.") | ||
|
||
all_dims = frozenset(kernel.domains[hdi].get_var_dict()) | ||
D1 = kernel.domains[hdi] | ||
D2 = kernel.domains[hdi] | ||
|
||
for iname in sorted(all_dims): | ||
if iname in inames: | ||
dt, pos = D1.get_var_dict()[iname] | ||
D1 = D1.project_out(dt, pos, 1) | ||
elif iname in parent_inames: | ||
dt, pos = D2.get_var_dict()[iname] | ||
if dt != isl.dim_type.param: | ||
n_params = D2.dim(isl.dim_type.param) | ||
D2 = D2.move_dims(isl.dim_type.param, n_params, dt, pos, 1) | ||
else: | ||
dt, pos = D2.get_var_dict()[iname] | ||
D2 = D2.project_out(dt, pos, 1) | ||
|
||
new_domains = kernel.domains[:] | ||
new_domains[hdi] = D1 | ||
new_domains.append(D2) | ||
kernel = kernel.copy(domains=new_domains) | ||
return kernel |