From cec40606642f6ee96b2fdb3a3c54bd112e5eb5d5 Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Mon, 18 Jul 2022 16:09:42 -0500 Subject: [PATCH 1/2] added gpuarray.ones --- doc/array.rst | 5 +++++ pycuda/gpuarray.py | 8 ++++++++ test/test_gpuarray.py | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/doc/array.rst b/doc/array.rst index 34c65693..00aca581 100644 --- a/doc/array.rst +++ b/doc/array.rst @@ -293,6 +293,11 @@ Constructing :class:`GPUArray` Instances Same as :func:`empty`, but the :class:`GPUArray` is zero-initialized before being returned. +.. function:: zeros(shape, dtype=np.float64, *, allocator=None, order="C") + + Same as :func:`empty`, but the :class:`GPUArray` is one-initialized before + being returned. + .. function:: empty_like(other_ary, dtype=None, order="K") Make a new, uninitialized :class:`GPUArray` having the same properties diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py index a3dbc100..e6238231 100644 --- a/pycuda/gpuarray.py +++ b/pycuda/gpuarray.py @@ -1248,6 +1248,14 @@ def zeros(shape, dtype, allocator=drv.mem_alloc, order="C"): return result +def ones(shape, dtype=np.float64, allocator=drv.mem_alloc, order="C"): + """Returns an array of the given shape and dtype filled with 1's.""" + result = GPUArray(shape, dtype, allocator, order=order) + one = np.ones((), dtype) + result.fill(one) + return result + + def _array_like_helper(other_ary, dtype, order): """Set order, strides, dtype as in numpy's zero_like. """ strides = None diff --git a/test/test_gpuarray.py b/test/test_gpuarray.py index 73ec3ade..ddd8b2f6 100644 --- a/test/test_gpuarray.py +++ b/test/test_gpuarray.py @@ -485,6 +485,15 @@ class TestGPUArray: a = gpuarray.arange(12, dtype=np.float32) assert (np.arange(12, dtype=np.float32) == a.get()).all() + @mark_cuda_test + def test_ones(self): + + ones = np.ones(10) + ones_gpu = gpuarray.ones(10) + + np.testing.assert_allclose(ones, ones_gpu.get(), rtol=1e-6) + assert ones.dtype == ones_gpu.dtype + @mark_cuda_test def test_stack(self): -- GitLab From 8dd7ad31079477534b018505bb572f50885817e2 Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Mon, 18 Jul 2022 23:20:40 +0000 Subject: [PATCH 2/2] Apply 1 suggestion(s) to 1 file(s) --- doc/array.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/array.rst b/doc/array.rst index 00aca581..dacb6cbf 100644 --- a/doc/array.rst +++ b/doc/array.rst @@ -293,7 +293,7 @@ Constructing :class:`GPUArray` Instances Same as :func:`empty`, but the :class:`GPUArray` is zero-initialized before being returned. -.. function:: zeros(shape, dtype=np.float64, *, allocator=None, order="C") +.. function:: ones(shape, dtype=np.float64, *, allocator=None, order="C") Same as :func:`empty`, but the :class:`GPUArray` is one-initialized before being returned. -- GitLab