- Notifications
You must be signed in to change notification settings - Fork 11
PyOpenCLActx: fixes to np.where, actx.to_numpy#357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -37,6 +37,8 @@ | ||
| import numpy as np | ||
| from typing_extensions import Self, override | ||
| from pytools import memoize_method | ||
| from arraycontext.container.traversal import ( | ||
| rec_map_array_container, | ||
| rec_map_container, | ||
| @@ -62,7 +64,7 @@ | ||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable, Mapping | ||
| from numpy.typing import NDArray | ||
| from numpy.typing import DTypeLike, NDArray | ||
| import loopy as lp | ||
| import pyopencl as cl | ||
| @@ -263,12 +265,54 @@ def to_numpy(self, array: Array) -> np.ndarray: | ||
| def to_numpy(self, array: ContainerOrScalarT) -> ContainerOrScalarT: | ||
| ... | ||
| @memoize_method | ||
| def _get_to_numpy_noncontiguous_copy_kernel( | ||
| self, dtype: DTypeLike, ndim: int | ||
| ) -> lp.TranslationUnit: | ||
| """ | ||
| Returns a translation unit containing a loopy kernel that: | ||
| - Accepts a PyOpenCL array ``inp`` with per-axis strides exposed as | ||
| ``s0, s1, ..., s{ndim-1}``. | ||
| - Produces a contiguous, row-major (C-order) output array ``output`` of | ||
| the same shape, with elements copied from the corresponding | ||
| coordinates in ``input``. | ||
| """ | ||
| import loopy as lp | ||
| from arraycontext.loopy import _DEFAULT_LOOPY_OPTIONS | ||
| t_unit = lp.make_copy_kernel( | ||
| ["c"] * ndim, [f"stride:s{i}" for i in range(ndim)] | ||
| ) | ||
| t_unit = lp.add_dtypes(t_unit, {"input": dtype}) | ||
| new_args = [ | ||
| *t_unit.default_entrypoint.args, | ||
| *[lp.ValueArg(f"s{i}", dtype=np.uint64) for i in range(ndim)], | ||
| ] | ||
| t_unit = t_unit.with_kernel(t_unit.default_entrypoint.copy(args=new_args)) | ||
| t_unit = lp.set_options(t_unit, _DEFAULT_LOOPY_OPTIONS) | ||
| return t_unit | ||
| @override | ||
| def to_numpy(self, | ||
| array: ArrayOrContainerOrScalar | ||
| ) -> NumpyOrContainerOrScalar: | ||
| def _to_numpy(ary): | ||
| return ary.get(queue=self.queue) | ||
| if ary.flags.forc: | ||
| # pyopencl supports host transfers only for contiguous arrays. | ||
| return ary.get(queue=self.queue) | ||
| result = self.call_loopy( | ||
inducer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self._get_to_numpy_noncontiguous_copy_kernel(ary.dtype, ary.ndim), | ||
| input=ary, | ||
| **{ | ||
| f"s{i}": stride // ary.dtype.itemsize | ||
| for i, stride in enumerate(ary.strides) | ||
| }, | ||
| )["output"] | ||
| return result.get(queue=self.queue) | ||
| return with_array_context( | ||
| self._rec_map_container(_to_numpy, array), | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -460,9 +460,39 @@ def absolute(self, a): | ||
| # {{{ sorting, searching, and counting | ||
| def where(self, criterion, then, else_): | ||
| def where_inner(inner_crit, inner_then, inner_else): | ||
| def where_inner( | ||
| inner_crit: ArrayOrScalar, | ||
| inner_then: ArrayOrScalar, | ||
| inner_else: ArrayOrScalar, | ||
| ) -> ArrayOrScalar: | ||
| if isinstance(inner_crit, bool | np.bool_): | ||
| return inner_then if inner_crit else inner_else | ||
| # pyopencl's if_positive does not support then, else branches with | ||
| # unequal dtypes -> cast them to a common dtype. | ||
kaushikcfd marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| inner_then_dtype = ( | ||
| inner_then.dtype | ||
| if isinstance(inner_then, cl_array.Array) | ||
| else np.dtype(type(inner_then)) | ||
| ) | ||
| inner_else_dtype = ( | ||
| inner_else.dtype | ||
| if isinstance(inner_else, cl_array.Array) | ||
| else np.dtype(type(inner_else)) | ||
| ) | ||
| dtype = np.promote_types(inner_then_dtype, inner_else_dtype) | ||
| inner_then = ( | ||
| inner_then.astype(dtype) | ||
| if isinstance(inner_then, cl_array.Array) | ||
| else dtype.type(inner_then) | ||
| ) | ||
| inner_else = ( | ||
| inner_else.astype(dtype) | ||
| if isinstance(inner_else, cl_array.Array) | ||
| else dtype.type(inner_else) | ||
| ) | ||
| return cl_array.if_positive(inner_crit != 0, inner_then, inner_else, | ||
| queue=self._array_context.queue) | ||
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.