I usually prefer to operate with vectors in python as regular numpy arrays
since numpy functions won't work with structured arrays, for example:

>>> np.linalg.norm(gpuarray.vec.make_float3(0,1,2))
TypeError: unsupported operand type(s) for *: ...
>>> np.linalg.norm(np.array([0,1,2]))
2.2360679774997898

However, in device code I love using the vector types, and so, in order to
pass my regular arrays to the device, I use to type something like the
following:

>>> positions = np.random.normal(size=(10,3))
>>> positions_float3 = np.empty(10, dtype=gpuarray.vec.float3)
>>> positions_float3['x'] = positions[:,0]
>>> positions_float3['y'] = positions[:,1]
>>> positions_float3['z'] = positions[:,2]
>>> positions_gpu = gpuarray.to_gpu(positions_float3)

but, I found this neat trick which saves a lot of typing:

>>> positions_gpu =
gpuarray.to_gpu(positions.astype(np.float32).view(gpuarray.vec.float3))

If you actually look at the two GPUArrays they are superficially different;
the first method produces an array of scalar float3 vectors, while the
second method produces an array of size 1 arrays (where the only element is
the scalar float3 vector), however in practice I've found that both arrays
look equivalent on the device code.

Hope this helps someone!

Tony
_______________________________________________
PyCUDA mailing list
[email protected]
http://lists.tiker.net/listinfo/pycuda

Reply via email to