Hi Toby,

Toby St Clere Smithe <[email protected]> writes:
> I'm writing a convenience routine to take a PyViennaCL object (eg,
> Matrix) to a PyOpenCL Array, but I'm not sure about the best way to
> handle padding: ViennaCL (for instance) automatically pads a Matrix with
> size less than 128 in either dimension so that the shape is (128,
> 128). Suppose I have a Matrix a with size (3, 3) in row-major format.
>
> I then create an Array instance by doing something like
>
>   c = cl.array.Array(a.context.current_queue,
>                      (a.internal_size1, a.internal_size2),
>                      a.dtype, order='C', 
>                      data=cl.MemoryObject.from_int_ptr(a.handle.int_ptr))
>
> which gives me c.shape == (128, 128), and
>
>   >>> c.flags.__dict__ 
>   {'f_contiguous': False, 'forc': True, 'c_contiguous': True}
>
> . If I then want to recover the un-padded form, I try something like
>
>   >>> c[0:3, 0:3]
>
> but this gives
>
>   Traceback (most recent call last):
>     File "<stdin>", line 1, in <module>
>     File "/usr/lib/python3/dist-packages/pyopencl/array.py", line 647, in 
> __repr__
>       return repr(self.get())
>     File "/usr/lib/python3/dist-packages/pyopencl/array.py", line 624, in get
>       assert self.flags.forc, "Array in get() must be contiguous"
>   AssertionError: Array in get() must be contiguous
>
> because, I suppose, taking the slice would leave the Array in a sense
> discontiguous: at the end of each row, there are (128-3)*8 = 1000 bytes
> of unused data. So, I try instead
>
>   c = cl.array.Array(a.context.current_queue,
>                      a.shape,
>                      a.dtype, order='C', 
>                      data=cl.MemoryObject.from_int_ptr(a.handle.int_ptr),
>                      strides=(1024,8))
>
> but this of course gives the same AssertionError in repr(self.get()).
>
> What do you think is the best way to proceed? Should I just forget about
> it, and leave it to any user to deal with the padding? In some ways,
> this is probably best, but it seems inelegant -- any, anyway, what's so
> wrong with an Array being discontiguous in the way I've described?

There's nothing wrong with this in principle, the transfer code is just
not currently smart enough to handle the rectangular non-contiguous
array. For 2D/3D arrays, this could easily be mapped to
clEnqueue{Read,Write}BufferRect() (see [1]) I'd be happy to take a patch
that does this. (Or, if you can wait, I can do this a week or two down
the road.)

[1] http://documen.tician.de/pyopencl/runtime.html#pyopencl.enqueue_copy

HTH,
Andreas

Attachment: pgpbSp4ePQEuJ.pgp
Description: PGP signature

_______________________________________________
PyOpenCL mailing list
[email protected]
http://lists.tiker.net/listinfo/pyopencl

Reply via email to