Skip to content

Commit 8885887

Browse files
committed
Provide default implementations of transform_loopy_program, with warning
Closes gh-272
1 parent 3f848dc commit 8885887

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

arraycontext/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,8 @@ def tag_axes(
583583

584584
# }}}
585585

586+
587+
class UntransformedCodeWarning(UserWarning):
588+
pass
589+
586590
# vim: foldmethod=marker

arraycontext/impl/pyopencl/__init__.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
14
"""
25
.. currentmodule:: arraycontext
36
.. autoclass:: PyOpenCLArrayContext
@@ -36,7 +39,13 @@
3639
from pytools.tag import ToTagSetConvertible
3740

3841
from arraycontext.container.traversal import rec_map_array_container, with_array_context
39-
from arraycontext.context import Array, ArrayContext, ArrayOrContainer, ScalarLike
42+
from arraycontext.context import (
43+
Array,
44+
ArrayContext,
45+
ArrayOrContainer,
46+
ScalarLike,
47+
UntransformedCodeWarning,
48+
)
4049

4150

4251
if TYPE_CHECKING:
@@ -72,8 +81,8 @@ class PyOpenCLArrayContext(ArrayContext):
7281
"""
7382

7483
def __init__(self,
75-
queue: "pyopencl.CommandQueue",
76-
allocator: Optional["pyopencl.tools.AllocatorBase"] = None,
84+
queue: pyopencl.CommandQueue,
85+
allocator: Optional[pyopencl.tools.AllocatorBase] = None,
7786
wait_event_queue_length: Optional[int] = None,
7887
force_device_scalars: bool = False) -> None:
7988
r"""
@@ -301,16 +310,19 @@ def clone(self):
301310

302311
# {{{ transform_loopy_program
303312

304-
def transform_loopy_program(self, t_unit):
313+
def transform_loopy_program(self, t_unit: lp.TranslationUnit) -> lp.TranslationUnit:
305314
from warnings import warn
306-
warn("Using arraycontext.PyOpenCLArrayContext.transform_loopy_program "
307-
"to transform a program. This is deprecated and will stop working "
308-
"in 2022. Instead, subclass PyOpenCLArrayContext and implement "
309-
"the specific logic required to transform the program for your "
310-
"package or application. Check higher-level packages "
315+
warn("Using the base "
316+
f"{type(self).__name}.transform_loopy_program "
317+
"to transform a translation unit. "
318+
"This is largely a no-op and unlikely to result in fast generated "
319+
"code."
320+
f"Instead, subclass {type(self).__name__} and implement "
321+
"the specific transform logic required to transform the program "
322+
"for your package or application. Check higher-level packages "
311323
"(e.g. meshmode), which may already have subclasses you may want "
312324
"to build on.",
313-
DeprecationWarning, stacklevel=2)
325+
UntransformedCodeWarning, stacklevel=2)
314326

315327
# accommodate loopy with and without kernel callables
316328

arraycontext/impl/pytato/__init__.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
"""
1+
from __future__ import annotations
2+
3+
4+
__doc__ = """
25
.. currentmodule:: arraycontext
36
47
A :mod:`pytato`-based array context defers the evaluation of an array until its
@@ -62,11 +65,18 @@
6265
from pytools.tag import Tag, ToTagSetConvertible, normalize_tags
6366

6467
from arraycontext.container.traversal import rec_map_array_container, with_array_context
65-
from arraycontext.context import Array, ArrayContext, ArrayOrContainer, ScalarLike
68+
from arraycontext.context import (
69+
Array,
70+
ArrayContext,
71+
ArrayOrContainer,
72+
ScalarLike,
73+
UntransformedCodeWarning,
74+
)
6675
from arraycontext.metadata import NameHint
6776

6877

6978
if TYPE_CHECKING:
79+
import loopy as lp
7080
import pyopencl as cl
7181
import pytato
7282

@@ -137,7 +147,6 @@ def __init__(
137147
"""
138148
super().__init__()
139149

140-
import loopy as lp
141150
import pytato as pt
142151
self._freeze_prg_cache: Dict[pt.DictOfNamedArrays, lp.TranslationUnit] = {}
143152
self._dag_transform_cache: Dict[
@@ -180,8 +189,8 @@ def empty_like(self, ary):
180189

181190
# {{{ compilation
182191

183-
def transform_dag(self, dag: "pytato.DictOfNamedArrays"
184-
) -> "pytato.DictOfNamedArrays":
192+
def transform_dag(self, dag: pytato.DictOfNamedArrays
193+
) -> pytato.DictOfNamedArrays:
185194
"""
186195
Returns a transformed version of *dag*. Sub-classes are supposed to
187196
override this method to implement context-specific transformations on
@@ -194,10 +203,22 @@ def transform_dag(self, dag: "pytato.DictOfNamedArrays"
194203
"""
195204
return dag
196205

197-
def transform_loopy_program(self, t_unit):
198-
raise ValueError(
199-
f"{type(self).__name__} does not implement transform_loopy_program. "
200-
"Sub-classes are supposed to implement it.")
206+
def transform_loopy_program(self, t_unit: lp.TranslationUnit) -> lp.TranslationUnit:
207+
from warnings import warn
208+
warn("Using the base "
209+
f"{type(self).__name__}.transform_loopy_program "
210+
"to transform a translation unit. "
211+
"This is a no-op and will result in unoptimized C code for"
212+
"the requested optimization, all in a single statement."
213+
"This will work, but is unlikely to be performatn."
214+
f"Instead, subclass {type(self).__name__} and implement "
215+
"the specific transform logic required to transform the program "
216+
"for your package or application. Check higher-level packages "
217+
"(e.g. meshmode), which may already have subclasses you may want "
218+
"to build on.",
219+
UntransformedCodeWarning, stacklevel=2)
220+
221+
return t_unit
201222

202223
@abc.abstractmethod
203224
def einsum(self, spec, *args, arg_names=None, tagged=()):
@@ -250,7 +271,7 @@ class PytatoPyOpenCLArrayContext(_BasePytatoArrayContext):
250271
.. automethod:: compile
251272
"""
252273
def __init__(
253-
self, queue: "cl.CommandQueue", allocator=None, *,
274+
self, queue: cl.CommandQueue, allocator=None, *,
254275
use_memory_pool: Optional[bool] = None,
255276
compile_trace_callback: Optional[Callable[[Any, str, Any], None]] = None,
256277

@@ -642,8 +663,8 @@ def compile(self, f: Callable[..., Any]) -> Callable[..., Any]:
642663
from .compile import LazilyPyOpenCLCompilingFunctionCaller
643664
return LazilyPyOpenCLCompilingFunctionCaller(self, f)
644665

645-
def transform_dag(self, dag: "pytato.DictOfNamedArrays"
646-
) -> "pytato.DictOfNamedArrays":
666+
def transform_dag(self, dag: pytato.DictOfNamedArrays
667+
) -> pytato.DictOfNamedArrays:
647668
import pytato as pt
648669
dag = pt.transform.materialize_with_mpms(dag)
649670
return dag

arraycontext/pytest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def is_available(cls) -> bool:
144144
def actx_class(self):
145145
from arraycontext import PytatoPyOpenCLArrayContext
146146
actx_cls = PytatoPyOpenCLArrayContext
147-
actx_cls.transform_loopy_program = lambda s, t_unit: t_unit
148147
return actx_cls
149148

150149
def __call__(self):

0 commit comments

Comments
 (0)