-
Notifications
You must be signed in to change notification settings - Fork 2
/
stencil.py
143 lines (111 loc) · 5.85 KB
/
stencil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
__copyright__ = "Copyright (C) 2019 Zachary J Weiner"
__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 loopy as lp
from pystella.elementwise import ElementWiseMap
__doc__ = """
.. currentmodule:: pystella
.. autoclass:: Stencil
.. ifconfig:: not on_rtd
.. autoclass:: StreamingStencil
"""
class Stencil(ElementWiseMap):
"""
A subclass of :class:`ElementWiseMap`, which creates a kernel with
parallelization suitable for stencil-type operations which are
"non-local"---namely, computations which combine multiple neighboring values
from a global array into a single output (per workitem/thread).
In addition to the parameters to :meth:`ElementWiseMap`,
the following arguments are required:
:arg halo_shape: The number of halo layers on (both sides of) each axis of
the computational grid.
May either be an :class:`int`, interpreted as a value to fix the
parameter ``h`` to, or a :class:`tuple`, interpreted as values for
``hx``, ``hy``, and ``hz``.
Defaults to *None*, in which case no such values are fixed at kernel
creation.
The following keyword-only arguments are recognized:
:arg prefetch_args: A list of arrays (namely, their name as a string)
which should be prefetched into local memory. Defaults to an empty list.
"""
def _assignment(self, assignee, expression, **kwargs):
no_sync_with = kwargs.pop("no_sync_with", None)
return lp.Assignment(assignee, expression,
no_sync_with=no_sync_with,
**kwargs)
def parallelize(self, knl, lsize):
knl = lp.split_iname(knl, "k", lsize[0], outer_tag="g.0", inner_tag="l.0")
knl = lp.split_iname(knl, "j", lsize[1], outer_tag="g.1", inner_tag="l.1")
knl = lp.split_iname(knl, "i", lsize[2], outer_tag="g.2", inner_tag="l.2")
for arg in self.prefetch_args:
name = arg.replace("$", "_") # c.f. loopy.add_prefetch: c_name
knl = lp.add_prefetch(
knl, arg, ("i_inner", "j_inner", "k_inner"),
fetch_bounding_box=True, default_tag=None, temporary_name=f"_{name}",
)
prefetch_inames = [
iname for iname in knl.default_entrypoint.all_inames()
if f"{name}_dim" in iname
]
for axis, iname in enumerate(sorted(prefetch_inames, reverse=True)):
if axis < 3:
knl = lp.tag_inames(knl, f"{iname}:l.{axis}")
return knl
def __init__(self, map_instructions, halo_shape, **kwargs):
self.prefetch_args = kwargs.pop("prefetch_args", [])
_halo_shape = (halo_shape,)*3 if isinstance(halo_shape, int) else halo_shape
_lsize = tuple(10 - 2*hi for hi in _halo_shape)
if halo_shape == 2:
_lsize = (8, 4, 4) # default should be only powers of two
lsize = kwargs.pop("lsize", _lsize)
super().__init__(map_instructions, lsize=lsize,
silenced_warnings=["single_writer_after_creation"],
**kwargs, halo_shape=halo_shape)
class StreamingStencil(Stencil):
"""
A subclass of :class:`Stencil` which performs a "streaming" prefetch
in place of a standard, single-block prefetch.
.. warning::
Currently, :func:`loopy.add_prefetch` only supports streaming prefetches
of a single array.
"""
def parallelize(self, knl, lsize):
knl = lp.split_iname(knl, "k", lsize[0], outer_tag="g.0", inner_tag="l.0")
knl = lp.split_iname(knl, "j", lsize[1], outer_tag="g.1", inner_tag="l.1")
knl = lp.split_iname(knl, "i", lsize[2])
for arg in self.prefetch_args:
name = arg.replace("$", "_") # c.f. loopy.add_prefetch: c_name
knl = lp.add_prefetch( # pylint: disable=E1123
knl, arg, ("i_inner", "j_inner", "k_inner"), stream_iname="i_outer",
fetch_bounding_box=True, default_tag=None, temporary_name=f"_{name}",
)
prefetch_inames = [
iname for iname in knl.default_entrypoint.all_inames()
if f"{name}_dim" in iname
]
for axis, iname in enumerate(sorted(prefetch_inames, reverse=True)):
if axis < 2:
knl = lp.tag_inames(knl, f"{iname}:l.{axis}")
return knl
def __init__(self, map_instructions, halo_shape, **kwargs):
if len(kwargs.get("prefetch_args", [])) > 1:
raise NotImplementedError(
"Streaming codegen can only handle one prefetch array for now")
lsize = kwargs.pop("lsize", (16, 4, 8))
super().__init__(map_instructions, lsize=lsize, halo_shape=halo_shape,
**kwargs)