Discussion:
[PyCUDA] Passing a custom struct to a kernel by value
Bogdan Opanchuk
2014-05-21 05:16:11 UTC
Permalink
Hello,

Does PyCUDA support struct arguments to kernels? From the Python side
it means an element of an array with a struct dtype (a numpy.void
object), e.g.

dtype = numpy.dtype([('first', numpy.int32), ('second', numpy.int32)])
pair = numpy.empty(1, dtype)[0]

See https://gist.github.com/Manticore/15383a1ae367bfc6efe8 for an
example of the functionality in question. It fails on ``get_second()``
call complaining about the second argument (the structure).

An analogous code in PyOpenCL works fine, but as far as I understand
from its source, it uses a somewhat different mechanism of argument
passing as compared to what is employed by PyCUDA.

Best regards,
Bogdan
Andreas Kloeckner
2014-05-27 04:22:50 UTC
Permalink
Post by Bogdan Opanchuk
Hello,
Does PyCUDA support struct arguments to kernels? From the Python side
it means an element of an array with a struct dtype (a numpy.void
object), e.g.
dtype = numpy.dtype([('first', numpy.int32), ('second', numpy.int32)])
pair = numpy.empty(1, dtype)[0]
See https://gist.github.com/Manticore/15383a1ae367bfc6efe8 for an
example of the functionality in question. It fails on ``get_second()``
call complaining about the second argument (the structure).
An analogous code in PyOpenCL works fine, but as far as I understand
from its source, it uses a somewhat different mechanism of argument
passing as compared to what is employed by PyCUDA.
The following minor variant works:

https://gist.github.com/inducer/88ac86874112b0e126ce

(The point is that the argument has to be an array to be recognized. A
'scalar' of a derived dtype will not work.)

Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 810 bytes
Desc: not available
URL: <http://lists.tiker.net/pipermail/pycuda/attachments/20140526/1250f16d/attachment.sig>
Bogdan Opanchuk
2014-05-27 04:31:29 UTC
Permalink
Hi Andreas,

Thank you for the correction. Just curious, how come in PyOpenCL it
works with rank-0 numpy arrays (which, in my opinion, is more
intuitive than implicitly casting a rank-1 array to a scalar)? Is it
just a difference between PyCUDA and PyOpenCL, or a limitation of CUDA
itself?

On Tue, May 27, 2014 at 2:22 PM, Andreas Kloeckner
Post by Andreas Kloeckner
Post by Bogdan Opanchuk
Hello,
Does PyCUDA support struct arguments to kernels? From the Python side
it means an element of an array with a struct dtype (a numpy.void
object), e.g.
dtype = numpy.dtype([('first', numpy.int32), ('second', numpy.int32)])
pair = numpy.empty(1, dtype)[0]
See https://gist.github.com/Manticore/15383a1ae367bfc6efe8 for an
example of the functionality in question. It fails on ``get_second()``
call complaining about the second argument (the structure).
An analogous code in PyOpenCL works fine, but as far as I understand
from its source, it uses a somewhat different mechanism of argument
passing as compared to what is employed by PyCUDA.
https://gist.github.com/inducer/88ac86874112b0e126ce
(The point is that the argument has to be an array to be recognized. A
'scalar' of a derived dtype will not work.)
Andreas
Andreas Kloeckner
2014-05-27 04:45:06 UTC
Permalink
Hi Bogdan,
Post by Bogdan Opanchuk
Thank you for the correction. Just curious, how come in PyOpenCL it
works with rank-0 numpy arrays (which, in my opinion, is more
intuitive than implicitly casting a rank-1 array to a scalar)? Is it
just a difference between PyCUDA and PyOpenCL, or a limitation of CUDA
itself?
I think I've patched this in git. Can you please give this a try and
report back?

Thanks!
Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 810 bytes
Desc: not available
URL: <http://lists.tiker.net/pipermail/pycuda/attachments/20140526/06d3b85b/attachment.sig>
Bogdan Opanchuk
2014-05-27 04:53:17 UTC
Permalink
Hi Andreas,

Unfortunately, it does not quite work. The script from my first
message gives the following error:

Traceback (most recent call last):
File "t.py", line 34, in <module>
get_second(dest, pair, block=(400,1,1), grid=(1,1))
File "/Users/bogdan/.pyenv/versions/3.4.0/lib/python3.4/site-packages/pycuda/driver.py",
line 365, in function_call
handlers, arg_buf = _build_arg_buf(args)
File "/Users/bogdan/.pyenv/versions/3.4.0/lib/python3.4/site-packages/pycuda/driver.py",
line 147, in _build_arg_buf
return handlers, pack(format, *arg_data)
struct.error: bad char in struct format



On Tue, May 27, 2014 at 2:45 PM, Andreas Kloeckner
Post by Andreas Kloeckner
Hi Bogdan,
Post by Bogdan Opanchuk
Thank you for the correction. Just curious, how come in PyOpenCL it
works with rank-0 numpy arrays (which, in my opinion, is more
intuitive than implicitly casting a rank-1 array to a scalar)? Is it
just a difference between PyCUDA and PyOpenCL, or a limitation of CUDA
itself?
I think I've patched this in git. Can you please give this a try and
report back?
Thanks!
Andreas
Andreas Kloeckner
2014-05-27 05:09:44 UTC
Permalink
Post by Bogdan Opanchuk
Hi Andreas,
Unfortunately, it does not quite work. The script from my first
File "t.py", line 34, in <module>
get_second(dest, pair, block=(400,1,1), grid=(1,1))
File "/Users/bogdan/.pyenv/versions/3.4.0/lib/python3.4/site-packages/pycuda/driver.py",
line 365, in function_call
handlers, arg_buf = _build_arg_buf(args)
File "/Users/bogdan/.pyenv/versions/3.4.0/lib/python3.4/site-packages/pycuda/driver.py",
line 147, in _build_arg_buf
return handlers, pack(format, *arg_data)
struct.error: bad char in struct format
Next attempt now in git.

Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 810 bytes
Desc: not available
URL: <http://lists.tiker.net/pipermail/pycuda/attachments/20140527/bda3d921/attachment.sig>
Loading...