forked from inducer/arraycontext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
283 lines (218 loc) · 10.7 KB
/
Copy path__init__.py
File metadata and controls
283 lines (218 loc) · 10.7 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
.. currentmodule:: arraycontext
.. autoclass:: PyOpenCLArrayContext
"""
__copyright__ = """
Copyright (C) 2020-1 University of Illinois Board of Trustees
"""
__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.
"""
from warnings import warn
from typing import Dict, List, Sequence, Optional, Union, TYPE_CHECKING
import numpy as np
from pytools.tag import Tag
from arraycontext.context import ArrayContext
if TYPE_CHECKING:
import pyopencl
import loopy as lp
# {{{ PyOpenCLArrayContext
class PyOpenCLArrayContext(ArrayContext):
"""
A :class:`ArrayContext` that uses :class:`pyopencl.array.Array` instances
for its base array class.
.. attribute:: context
A :class:`pyopencl.Context`.
.. attribute:: queue
A :class:`pyopencl.CommandQueue`.
.. attribute:: allocator
A PyOpenCL memory allocator. Can also be `None` (default) or `False` to
use the default allocator. Please note that running with the default
allocator allocates and deallocates OpenCL buffers directly. If lots
of arrays are created (e.g. as results of computation), the associated cost
may become significant. Using e.g. :class:`pyopencl.tools.MemoryPool`
as the allocator can help avoid this cost.
"""
def __init__(self,
queue: "pyopencl.CommandQueue",
allocator: Optional["pyopencl.tools.AllocatorInterface"] = None,
wait_event_queue_length: Optional[int] = None,
force_device_scalars: bool = False) -> None:
r"""
:arg wait_event_queue_length: The length of a queue of
:class:`~pyopencl.Event` objects that are maintained by the
array context, on a per-kernel-name basis. The events returned
from kernel execution are appended to the queue, and Once the
length of the queue exceeds *wait_event_queue_length*, the
first event in the queue :meth:`pyopencl.Event.wait`\ ed on.
*wait_event_queue_length* may be set to *False* to disable this feature.
The use of *wait_event_queue_length* helps avoid enqueuing
large amounts of work (and, potentially, allocating large amounts
of memory) far ahead of the actual OpenCL execution front,
by limiting the number of each type (name, really) of kernel
that may reside unexecuted in the queue at one time.
.. note::
For now, *wait_event_queue_length* should be regarded as an
experimental feature that may change or disappear at any minute.
:arg force_device_scalars: if *True*, scalar results returned from
reductions in :attr:`ArrayContext.np` will be kept on the device.
If *False*, the equivalent of :meth:`~ArrayContext.freeze` and
:meth:`~ArrayContext.to_numpy` is applied to transfer the results
to the host.
"""
if not force_device_scalars:
warn("Configuring the PyOpenCLArrayContext to return host scalars "
"from reductions is deprecated. "
"To configure the PyOpenCLArrayContext to return "
"device scalars, pass 'force_device_scalars=True' to the "
"constructor. "
"Support for returning host scalars will be removed in 2022.",
DeprecationWarning, stacklevel=2)
import pyopencl as cl
import pyopencl.array as cla
super().__init__()
self.context = queue.context
self.queue = queue
self.allocator = allocator if allocator else None
if wait_event_queue_length is None:
wait_event_queue_length = 10
self._force_device_scalars = force_device_scalars
self._wait_event_queue_length = wait_event_queue_length
self._kernel_name_to_wait_event_queue: Dict[str, List[cl.Event]] = {}
if queue.device.type & cl.device_type.GPU:
if allocator is None:
warn("PyOpenCLArrayContext created without an allocator on a GPU. "
"This can lead to high numbers of memory allocations. "
"Please consider using a pyopencl.tools.MemoryPool. "
"Run with allocator=False to disable this warning.")
if __debug__:
# Use "running on GPU" as a proxy for "they care about speed".
warn("You are using the PyOpenCLArrayContext on a GPU, but you "
"are running Python in debug mode. Use 'python -O' for "
"a noticeable speed improvement.")
self._loopy_transform_cache: \
Dict["lp.TranslationUnit", "lp.TranslationUnit"] = {}
self.array_types = (cla.Array,)
def _get_fake_numpy_namespace(self):
from arraycontext.impl.pyopencl.fake_numpy import PyOpenCLFakeNumpyNamespace
return PyOpenCLFakeNumpyNamespace(self)
# {{{ ArrayContext interface
def empty(self, shape, dtype):
import pyopencl.array as cl_array
return cl_array.empty(self.queue, shape=shape, dtype=dtype,
allocator=self.allocator)
def zeros(self, shape, dtype):
import pyopencl.array as cl_array
return cl_array.zeros(self.queue, shape=shape, dtype=dtype,
allocator=self.allocator)
def from_numpy(self, array: np.ndarray):
import pyopencl.array as cl_array
return cl_array.to_device(self.queue, array, allocator=self.allocator)
def to_numpy(self, array):
if not self._force_device_scalars and np.isscalar(array):
return array
return array.get(queue=self.queue)
def call_loopy(self, t_unit, **kwargs):
try:
t_unit = self._loopy_transform_cache[t_unit]
except KeyError:
orig_t_unit = t_unit
t_unit = self.transform_loopy_program(t_unit)
self._loopy_transform_cache[orig_t_unit] = t_unit
del orig_t_unit
evt, result = t_unit(self.queue, **kwargs, allocator=self.allocator)
if self._wait_event_queue_length is not False:
prg_name = t_unit.default_entrypoint.name
wait_event_queue = self._kernel_name_to_wait_event_queue.setdefault(
prg_name, [])
wait_event_queue.append(evt)
if len(wait_event_queue) > self._wait_event_queue_length:
wait_event_queue.pop(0).wait()
return result
def freeze(self, array):
array.finish()
return array.with_queue(None)
def thaw(self, array):
return array.with_queue(self.queue)
# }}}
def transform_loopy_program(self, t_unit):
from warnings import warn
warn("Using arraycontext.PyOpenCLArrayContext.transform_loopy_program "
"to transform a program. This is deprecated and will stop working "
"in 2022. Instead, subclass PyOpenCLArrayContext and implement "
"the specific logic required to transform the program for your "
"package or application. Check higher-level packages "
"(e.g. meshmode), which may already have subclasses you may want "
"to build on.",
DeprecationWarning, stacklevel=2)
# accommodate loopy with and without kernel callables
import loopy as lp
default_entrypoint = t_unit.default_entrypoint
options = default_entrypoint.options
if not (options.return_dict and options.no_numpy):
raise ValueError("Loopy kernel passed to call_loopy must "
"have return_dict and no_numpy options set. "
"Did you use arraycontext.make_loopy_program "
"to create this kernel?")
all_inames = default_entrypoint.all_inames()
# FIXME: This could be much smarter.
inner_iname = None
# import with underscore to avoid DeprecationWarning
from arraycontext.metadata import _FirstAxisIsElementsTag
if (len(default_entrypoint.instructions) == 1
and isinstance(default_entrypoint.instructions[0], lp.Assignment)
and any(isinstance(tag, _FirstAxisIsElementsTag)
# FIXME: Firedrake branch lacks kernel tags
for tag in getattr(default_entrypoint, "tags", ()))):
stmt, = default_entrypoint.instructions
out_inames = [v.name for v in stmt.assignee.index_tuple]
assert out_inames
outer_iname = out_inames[0]
if len(out_inames) >= 2:
inner_iname = out_inames[1]
elif "iel" in all_inames:
outer_iname = "iel"
if "idof" in all_inames:
inner_iname = "idof"
elif "i0" in all_inames:
outer_iname = "i0"
if "i1" in all_inames:
inner_iname = "i1"
else:
raise RuntimeError(
"Unable to reason what outer_iname and inner_iname "
f"needs to be; all_inames is given as: {all_inames}"
)
if inner_iname is not None:
t_unit = lp.split_iname(t_unit, inner_iname, 16, inner_tag="l.0")
t_unit = lp.tag_inames(t_unit, {outer_iname: "g.0"})
return t_unit
def tag(self, tags: Union[Sequence[Tag], Tag], array):
# Sorry, not capable.
return array
def tag_axis(self, iaxis, tags: Union[Sequence[Tag], Tag], array):
# Sorry, not capable.
return array
def clone(self):
return type(self)(self.queue, self.allocator,
wait_event_queue_length=self._wait_event_queue_length,
force_device_scalars=self._force_device_scalars)
@property
def permits_inplace_modification(self):
return True
# }}}
# vim: foldmethod=marker