|
| 1 | +__doc__ = """ |
| 2 | +.. autofunction:: transfer_from_numpy |
| 3 | +.. autofunction:: transfer_to_numpy |
| 4 | +""" |
| 5 | + |
| 6 | + |
1 | 7 | __copyright__ = """ |
2 | 8 | Copyright (C) 2021 University of Illinois Board of Trustees |
3 | 9 | """ |
|
22 | 28 | THE SOFTWARE. |
23 | 29 | """ |
24 | 30 |
|
| 31 | + |
25 | 32 | from collections.abc import Mapping |
26 | 33 | from typing import TYPE_CHECKING, Any, cast |
27 | 34 |
|
|
36 | 43 | make_placeholder, |
37 | 44 | ) |
38 | 45 | from pytato.target.loopy import LoopyPyOpenCLTarget |
39 | | -from pytato.transform import CopyMapper |
| 46 | +from pytato.transform import ArrayOrNames, CopyMapper |
40 | 47 | from pytools import UniqueNameGenerator, memoize_method |
41 | 48 |
|
| 49 | +from arraycontext import ArrayContext |
42 | 50 | from arraycontext.impl.pyopencl.taggable_cl_array import Axis as ClAxis |
43 | 51 |
|
44 | 52 |
|
@@ -125,4 +133,86 @@ def get_loopy_target(self) -> "lp.PyOpenCLTarget": |
125 | 133 |
|
126 | 134 | # }}} |
127 | 135 |
|
| 136 | + |
| 137 | +# {{{ Transfer mappers |
| 138 | + |
| 139 | +class TransferFromNumpyMapper(CopyMapper): |
| 140 | + """A mapper to transfer arrays contained in :class:`~pytato.array.DataWrapper` |
| 141 | + instances to be device arrays, using |
| 142 | + :meth:`~arraycontext.ArrayContext.from_numpy`. |
| 143 | + """ |
| 144 | + def __init__(self, actx: ArrayContext) -> None: |
| 145 | + super().__init__() |
| 146 | + self.actx = actx |
| 147 | + |
| 148 | + def map_data_wrapper(self, expr: DataWrapper) -> Array: |
| 149 | + import numpy as np |
| 150 | + |
| 151 | + if not isinstance(expr.data, np.ndarray): |
| 152 | + raise ValueError("TransferFromNumpyMapper: tried to transfer data that " |
| 153 | + "is already on the device") |
| 154 | + |
| 155 | + # Ideally, this code should just do |
| 156 | + # return self.actx.from_numpy(expr.data).tagged(expr.tags), |
| 157 | + # but there seems to be no way to transfer the non_equality_tags in that case. |
| 158 | + new_dw = self.actx.from_numpy(expr.data) |
| 159 | + assert isinstance(new_dw, DataWrapper) |
| 160 | + |
| 161 | + # https://github.com/pylint-dev/pylint/issues/3893 |
| 162 | + # pylint: disable=unexpected-keyword-arg |
| 163 | + return DataWrapper( |
| 164 | + data=new_dw.data, |
| 165 | + shape=expr.shape, |
| 166 | + axes=expr.axes, |
| 167 | + tags=expr.tags, |
| 168 | + non_equality_tags=expr.non_equality_tags) |
| 169 | + |
| 170 | + |
| 171 | +class TransferToNumpyMapper(CopyMapper): |
| 172 | + """A mapper to transfer arrays contained in :class:`~pytato.array.DataWrapper` |
| 173 | + instances to be :class:`numpy.ndarray` instances, using |
| 174 | + :meth:`~arraycontext.ArrayContext.to_numpy`. |
| 175 | + """ |
| 176 | + def __init__(self, actx: ArrayContext) -> None: |
| 177 | + super().__init__() |
| 178 | + self.actx = actx |
| 179 | + |
| 180 | + def map_data_wrapper(self, expr: DataWrapper) -> Array: |
| 181 | + import numpy as np |
| 182 | + |
| 183 | + import arraycontext.impl.pyopencl.taggable_cl_array as tga |
| 184 | + if not isinstance(expr.data, tga.TaggableCLArray): |
| 185 | + raise ValueError("TransferToNumpyMapper: tried to transfer data that " |
| 186 | + "is already on the host") |
| 187 | + |
| 188 | + np_data = self.actx.to_numpy(expr.data) |
| 189 | + assert isinstance(np_data, np.ndarray) |
| 190 | + |
| 191 | + # https://github.com/pylint-dev/pylint/issues/3893 |
| 192 | + # pylint: disable=unexpected-keyword-arg |
| 193 | + return DataWrapper( |
| 194 | + data=np_data, |
| 195 | + shape=expr.shape, |
| 196 | + axes=expr.axes, |
| 197 | + tags=expr.tags, |
| 198 | + non_equality_tags=expr.non_equality_tags) |
| 199 | + |
| 200 | + |
| 201 | +def transfer_from_numpy(expr: ArrayOrNames, actx: ArrayContext) -> ArrayOrNames: |
| 202 | + """Transfer arrays contained in :class:`~pytato.array.DataWrapper` |
| 203 | + instances to be device arrays, using |
| 204 | + :meth:`~arraycontext.ArrayContext.from_numpy`. |
| 205 | + """ |
| 206 | + return TransferFromNumpyMapper(actx)(expr) |
| 207 | + |
| 208 | + |
| 209 | +def transfer_to_numpy(expr: ArrayOrNames, actx: ArrayContext) -> ArrayOrNames: |
| 210 | + """Transfer arrays contained in :class:`~pytato.array.DataWrapper` |
| 211 | + instances to be :class:`numpy.ndarray` instances, using |
| 212 | + :meth:`~arraycontext.ArrayContext.to_numpy`. |
| 213 | + """ |
| 214 | + return TransferToNumpyMapper(actx)(expr) |
| 215 | + |
| 216 | +# }}} |
| 217 | + |
128 | 218 | # vim: foldmethod=marker |
0 commit comments