Skip to content
Snippets Groups Projects
Commit 95fd7be8 authored by Matthias Diener's avatar Matthias Diener
Browse files

pyopenclac.to_numpy special case isscalar

parent 3e9fd368
No related branches found
No related tags found
No related merge requests found
......@@ -323,6 +323,9 @@ class PyOpenCLArrayContext(ArrayContext):
return cla.to_device(self.queue, array, allocator=self.allocator)
def to_numpy(self, array):
from numpy import isscalar
if isscalar(array):
return array
return array.get(queue=self.queue)
def call_loopy(self, t_unit, **kwargs):
......
......@@ -401,18 +401,16 @@ def test_dof_array_arithmetic_same_as_numpy(actx_factory):
# {{{ reductions same as numpy
def test_dof_array_reductions_same_as_numpy(actx_factory):
@pytest.mark.parametrize("op", ["sum", "min", "max"])
def test_dof_array_reductions_same_as_numpy(actx_factory, op):
actx = actx_factory()
for name in ["sum", "min", "max"]:
ary = np.random.randn(3000)
np_red = getattr(np, name)(ary)
actx_red = getattr(actx.np, name)(actx.from_numpy(ary))
if not np.isscalar(actx_red):
actx_red = actx.to_numpy(actx_red)
ary = np.random.randn(3000)
np_red = getattr(np, op)(ary)
actx_red = getattr(actx.np, op)(actx.from_numpy(ary))
actx_red = actx.to_numpy(actx_red)
assert np.allclose(np_red, actx_red)
assert np.allclose(np_red, actx_red)
# }}}
......@@ -725,8 +723,7 @@ def test_norm_complex(actx_factory, norm_ord):
norm_a_ref = np.linalg.norm(a, norm_ord)
norm_a = actx.np.linalg.norm(actx.from_numpy(a), norm_ord)
if not np.isscalar(norm_a):
norm_a = actx.to_numpy(norm_a)
norm_a = actx.to_numpy(norm_a)
assert abs(norm_a_ref - norm_a)/norm_a < 1e-13
......@@ -745,8 +742,7 @@ def test_norm_ord_none(actx_factory, ndim):
norm_a_ref = np.linalg.norm(a, ord=None)
norm_a = actx.np.linalg.norm(actx.from_numpy(a), ord=None)
if not np.isscalar(norm_a):
norm_a = actx.to_numpy(norm_a)
norm_a = actx.to_numpy(norm_a)
np.testing.assert_allclose(norm_a, norm_a_ref)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment