From 39d2412da4bfea7304c0428f1599dedc22cb148a Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Wed, 9 Oct 2019 01:31:14 -0500 Subject: [PATCH 1/4] Add NumPy __array__ method for easy gets See these links for more details: https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html https://docs.scipy.org/doc/numpy/user/basics.dispatch.html --- pyopencl/array.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pyopencl/array.py b/pyopencl/array.py index bcc0770fa..e3024ab9d 100644 --- a/pyopencl/array.py +++ b/pyopencl/array.py @@ -307,6 +307,7 @@ class Array(object): .. automethod :: get .. automethod :: get_async .. automethod :: copy + .. automethod :: __array__ .. automethod :: __str__ .. automethod :: __repr__ @@ -763,6 +764,21 @@ def copy(self, queue=_copy_queue): return result + def __array__(self, dtype=None): + """Return a numpy array copy of the array. + + Equivalent to :meth:`get` with no arguments. If `dtype` is given, two + copies of the input array will be made: one for the conversion, and + another for the cast. + + :arg dtype: a valid NumPy dtype, such as ``np.float64`` or + ``'float32'``. + + .. versionadded:: ... + """ + out = self.get().astype(dtype) + return out + def __str__(self): return str(self.get()) From 8a6a975e4579a8ac96f496a20b90acd1d231b3e7 Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Wed, 9 Oct 2019 02:03:32 -0500 Subject: [PATCH 2/4] Add test for array method --- test/test_array.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_array.py b/test/test_array.py index e9fb2ddd1..db21e6d80 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -664,6 +664,12 @@ def test_random_int_in_range(ctx_factory, rng_class, dtype, plot_hist=False): # {{{ misc +def test_array_method(ctx_factory): + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + arr = make_random_array(queue, np.float32, (32, 32)) + np.testing.assert_array_equal(arr, arr.get()) + def test_numpy_integer_shape(ctx_factory): try: list(np.int32(17)) From 62c94439e7898bb6ffa11433c3d676056f0d36b7 Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Tue, 15 Oct 2019 09:06:22 -0500 Subject: [PATCH 3/4] Use 1D size for random array creation --- test/test_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_array.py b/test/test_array.py index db21e6d80..7e26be5d0 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -667,7 +667,7 @@ def test_random_int_in_range(ctx_factory, rng_class, dtype, plot_hist=False): def test_array_method(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) - arr = make_random_array(queue, np.float32, (32, 32)) + arr = make_random_array(queue, np.float32, 1024) np.testing.assert_array_equal(arr, arr.get()) def test_numpy_integer_shape(ctx_factory): From ad2ae09ee00f02db71c0e19878d4fa403278b79d Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Thu, 17 Oct 2019 23:35:31 -0500 Subject: [PATCH 4/4] Add missing blank line between function defs --- test/test_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_array.py b/test/test_array.py index 7e26be5d0..3e5500773 100644 --- a/test/test_array.py +++ b/test/test_array.py @@ -670,6 +670,7 @@ def test_array_method(ctx_factory): arr = make_random_array(queue, np.float32, 1024) np.testing.assert_array_equal(arr, arr.get()) + def test_numpy_integer_shape(ctx_factory): try: list(np.int32(17))