Hello all, Thanks for this great package which ease so much the use of cuda ! I think I found out a small bug in the current git version of the code. If you run the following code in ipython :
>>> from pycuda import autoinit >>> from pycuda import driver, compiler, gpuarray, tools >>> a = gpuarray.ones(16, dtype=float32) >>> a += 1 you get : --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/data/projets/pycuda_mult/<ipython console> in <module>() /usr/lib/python2.6/site-packages/pycuda-0.94rc-py2.6-linux-x86_64.egg/pycuda/gpuarray.py in __iadd__(self, other) 258 259 def __iadd__(self, other): --> 260 return self._axpbyz(1, other, 1, self) 261 262 def __isub__(self, other): /usr/lib/python2.6/site-packages/pycuda-0.94rc-py2.6-linux-x86_64.egg/pycuda/gpuarray.py in _axpbyz(self, selffac, other, otherfac, out, add_timer, stream) 140 """Compute ``out = selffac * self + otherfac*other``, 141 where `other` is a vector..""" --> 142 assert self.shape == other.shape 143 144 func = elementwise.get_axpbyz_kernel(self.dtype, other.dtype, out.dtype) AttributeError: 'int' object has no attribute 'shape' The following patch should solve this issue. It adds also a ones function as it exist in numpy and define __sub__ using __add__ to avoid code duplication. Tell me what you think about those changes. Also I do not know if patches should be submitted to the mailing list but could not find another way. Cheers, Nicolas path follows: diff --git a/pycuda/gpuarray.py b/pycuda/gpuarray.py index a0a1da1..b685e52 100644 --- a/pycuda/gpuarray.py +++ b/pycuda/gpuarray.py @@ -235,17 +235,7 @@ class GPUArray(object): def __sub__(self, other): """Substract an array from an array or a scalar from an array.""" - - if isinstance(other, GPUArray): - result = self._new_like_me(_get_common_dtype(self, other)) - return self._axpbyz(1, other, -1, result) - else: - if other == 0: - return self - else: - # create a new array for the result - result = self._new_like_me() - return self._axpbz(1, -other, result) + return self.__add__(-other) def __rsub__(self,other): """Substracts an array by a scalar or an array:: @@ -257,10 +247,18 @@ class GPUArray(object): return self._axpbz(-1, other, result) def __iadd__(self, other): - return self._axpbyz(1, other, 1, self) + if isinstance(other, GPUArray): + # add another vector + return self._axpbyz(1, other, 1, self) + else: + # add a scalar + if other == 0: + return self + else: + return self._axpbz(1, other, self) def __isub__(self, other): - return self._axpbyz(1, other, -1, self) + return self.__iadd__(-other) def __neg__(self): result = self._new_like_me() @@ -631,6 +629,13 @@ def zeros(shape, dtype, allocator=drv.mem_alloc): result.fill(0) return result +def ones(shape, dtype, allocator=drv.mem_alloc): + """Returns an array of the given shape and dtype filled with 1's.""" + + result = GPUArray(shape, dtype, allocator) + result.fill(1) + return result + def empty_like(other_ary): result = GPUArray( other_ary.shape, other_ary.dtype, other_ary.allocator)
_______________________________________________ PyCUDA mailing list PyCUDA@tiker.net http://lists.tiker.net/listinfo/pycuda