This looks like a case where operator overloading precedence in Python has gone wrong. You'll find that this does work:
>>> c = a + b What's happening in your "b+a" case is that the Python interpreter is first executing: b.__add__(a) In this case, b.__add__() is the implementation provided by numpy, which then discovers that the right operand (a) is iterable. It then tries to iterate over the elements of a and return a new ndarray doing the element-wise sum. This iteration is what triggers the PyCUDA exception about trying to index "a" improperly. Testing this, I found that this worked, much to my surprise: >>> np.float64(1) + (1,2,3,4) array([ 2., 3., 4., 5.]) In the "float(b) + a" case, by casting b to a regular Python float object, you get a different implementation of __add__(). The float version of __add__() raises an exception because "a" is not a numeric type. When that happens, the interpreter catches it and then tries to run: a.__radd__(b) At which point, the gpuarray version of the function takes precedence and does the right thing. By flipping the order of the operands, you can force Python to use the __add__ method you want, and ponder why it is that some language purists dislike operator overloading... :) On Jul 27, 2012, at 2:33 PM, Anthony LaTorre <[email protected]> wrote: > I don't quite understand this behavior, and was hoping someone else might > have some insight. > > >>> import numpy as np > >>> import pycuda.autoinit > >>> from pycuda import gpuarray as ga > >>> a = ga.to_gpu(np.ones(10)) > >>> b = np.float64(1.0) > >>> c = b + a > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File > "/home/proj/chroma_env/local/lib/python2.7/site-packages/pycuda/gpuarray.py", > line 690, in __getitem__ > raise ValueError("non-slice indexing not supported: %s" % (idx,)) > ValueError: non-slice indexing not supported: 0 > >>> c = float(b) + a > > It seems like adding a numpy.float64 number to a gpuarray is causing > something to try and access individual items from the gpuarray, while adding > a regular python float doesn't. > > I'm using PyCUDA version (2012,1) and the graphics card is a geforce gtx550 > Ti on Ubuntu 12.04 linux. > _______________________________________________ > PyCUDA mailing list > [email protected] > http://lists.tiker.net/listinfo/pycuda _______________________________________________ PyCUDA mailing list [email protected] http://lists.tiker.net/listinfo/pycuda
