From 0f45153177aa7ffab10acbdc07ec2be341c8e07e Mon Sep 17 00:00:00 2001 From: lhausamm Date: Thu, 26 Oct 2017 09:11:46 +0200 Subject: [PATCH 1/2] Add ipow --- pycuda/gpuarray.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py index 3f6fd32d..99b63969 100644 --- a/pycuda/gpuarray.py +++ b/pycuda/gpuarray.py @@ -605,14 +605,10 @@ class GPUArray(object): return result - def __pow__(self, other): - """pow function:: - - example: - array = pow(array) - array = pow(array,4) - array = pow(array,array) - + def _pow(self, other, new): + """ + Do the pow operator. + with new, the user can choose between ipow or just pow """ if isinstance(other, GPUArray): @@ -622,7 +618,10 @@ class GPUArray(object): assert self.shape == other.shape - result = self._new_like_me(_get_common_dtype(self, other)) + if new: + result = self._new_like_me(_get_common_dtype(self, other)) + else: + result = self func = elementwise.get_pow_array_kernel( self.dtype, other.dtype, result.dtype) @@ -637,7 +636,10 @@ class GPUArray(object): raise RuntimeError("only contiguous arrays may " "be used as arguments to this operation") - result = self._new_like_me() + if new: + result = self._new_like_me() + else: + result = self func = elementwise.get_pow_kernel(self.dtype) func.prepared_async_call(self._grid, self._block, None, other, self.gpudata, result.gpudata, @@ -645,6 +647,28 @@ class GPUArray(object): return result + def __pow__(self, other): + """pow function:: + + example: + array = pow(array) + array = pow(array,4) + array = pow(array,array) + + """ + return self._pow(other,new=True) + + def __ipow__(self, other): + """ipow function:: + + example: + array **= 4 + array **= array + + """ + return self._pow(other,new=False) + + def reverse(self, stream=None): """Return this array in reversed order. The array is treated as one-dimensional. -- GitLab From e8816d87f5512cacf44f7659c719eaff4c28f554 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Fri, 27 Oct 2017 15:27:21 +0200 Subject: [PATCH 2/2] Add test for ipow --- test/test_gpuarray.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_gpuarray.py b/test/test_gpuarray.py index 0a3218e1..103bba3d 100644 --- a/test/test_gpuarray.py +++ b/test/test_gpuarray.py @@ -36,6 +36,10 @@ class TestGPUArray: result = (a_gpu**a_gpu).get() assert (np.abs(pow(a, a) - result) < 1e-3).all() + a_gpu **= a_gpu + a_gpu = a_gpu.get() + assert (np.abs(pow(a, a) - a_gpu) < 1e-3).all() + @mark_cuda_test def test_pow_number(self): a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.float32) @@ -44,6 +48,10 @@ class TestGPUArray: result = pow(a_gpu, 2).get() assert (np.abs(a**2 - result) < 1e-3).all() + a_gpu **= 2 + a_gpu = a_gpu.get() + assert (np.abs(a**2 - a_gpu) < 1e-3).all() + @mark_cuda_test def test_numpy_integer_shape(self): gpuarray.empty(np.int32(17), np.float32) -- GitLab