inducer / pyopencl

OpenCL integration for Python, plus shiny features
http://mathema.tician.de/software/pyopencl
Other
1.04k stars 239 forks source link

enqueue_copy does not support IMAGE2D_ARRAY #618

Closed beatreichenbach closed 1 year ago

beatreichenbach commented 1 year ago

Describe the bug When creating image arrays with the argument is_array=True enqueue_copy doesn't allow that memory object to be used for src or dest. For example: https://github.com/inducer/pyopencl/blob/d2ade3f041a24604c697935b8d92405f11960d59/pyopencl/__init__.py#L2005 has support for 2d and 3d images, but not for cl.mem_object_type.IMAGE2D_ARRAY

To Reproduce

import pyopencl as cl
import numpy as np

context = cl.create_some_context(interactive=False)
queue = cl.CommandQueue(context)
image_format = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.FLOAT)
flags = cl.mem_flags.READ_ONLY
image = np.ascontiguousarray(np.zeros((128, 128, 4), np.float32))
image_cl = cl.Image(context, flags, image_format, shape=(image.shape[1], image.shape[0], 1), is_array=True)
cl.enqueue_copy(queue, dest=image, src=image_cl, origin=(0, 0, 0), region=(image.shape[1], image.shape[0], 1))

Expected behavior I would expect the code above to copy the contents of image_cl to image.

Calling the underlying _cl._enqueue_write_image function with the same arguments seems to work as expected.

Environment (please complete the following information):

inducer commented 1 year ago

Thanks for the report. How does https://gitlab.tiker.net/inducer/pyopencl/-/merge_requests/138 look?

beatreichenbach commented 1 year ago

Thank you for such a quick reply!

Just by looking at the code it seems like that makes sense as that's my understanding from reading the OpenCL docs.

I haven't tested those use cases but should image2d_array -> Buffer and more importantly image2d_array -> image2d_array also be supported?

inducer commented 1 year ago

Good catch. See updated https://gitlab.tiker.net/inducer/pyopencl/-/merge_requests/138.

beatreichenbach commented 1 year ago

Yep, I just did a super quick test to see if image->buffer and image->image actually works and seemed to be okay. Thanks for the quick fix :)

import pyopencl as cl
import pyopencl._cl as _cl
import numpy as np

context = cl.create_some_context(interactive=False)
queue = cl.CommandQueue(context)

image_format = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.FLOAT)
image = np.ascontiguousarray(np.zeros((128, 128, 4), np.float32))
image_shape = (image.shape[1], image.shape[0])
array_shape = (*image_shape, 1)
image_cl = cl.Image(context, cl.mem_flags.READ_ONLY, image_format, shape=image_shape)
image_array_cl = cl.Image(context, cl.mem_flags.READ_ONLY, image_format, shape=array_shape, is_array=True)
image2_array_cl = cl.Image(context, cl.mem_flags.WRITE_ONLY, image_format, shape=array_shape, is_array=True)
buffer_cl = cl.Buffer(context, cl.mem_flags.WRITE_ONLY, size=image.nbytes)

_cl._enqueue_copy_image(queue, src=image_array_cl, dest=image2_array_cl, src_origin=(0, 0, 0), dest_origin=(0, 0, 0), region=array_shape)
_cl._enqueue_copy_image_to_buffer(queue, src=image_array_cl, dest=buffer_cl, offset=0, origin=(0, 0, 0), region=array_shape)