Why create cl::Image2D from pointer?

Hey guys. So I’m trying to load an image from file and get it into device memory. To do this, my instinct was to load the image, create an empty cl::Image2D object, and enqueue a writeImage command with a pointer to the loaded image data.

If this works (and I’m 99% positive it does), why would you want to create a cl::Image object by specifying a pointer to data in host memory? You have to write the cl::Image object and specify a pointer anyway…

Thanks in advance!

clCreateImage2D(…), using a host ptr, can allocate image data for an OpenCL context. Contexts can have a variety of devices and each device’s associated command queue. The context handles back-end memory copies for use with different devices.

clEnqueueWriteImage(…) puts an image write on a command queue, so for a specific device.

as a fellow beginner, i spose the question here is, if you create an image with a NULL host pointer, then do an enqueuewriteimage for a (let’s say) GPU device, then will a different CPU device still have an empty image, or will it be copied to CPU as well?

furthermore, if enqueuewriteimage only writes an image for a specific device, then is there a way to copy an image to every device? or will that have to be “manually” programmed?

PS try to delete your duplicate post

Yes, that is part of it! I understand that simply instantiating an image doesn’t associate it with a specific device, I just don’t get why you would use allocate specify a pointer to image data when creating an image object if you have to turn around and do it again when you submit that object to a device.

I can’t delete the repost, or even edit it for some strange reason - I’ve reported the duplicate post, and hopefully a mod will take care of it.

as a fellow beginner, i spose the question here is, if you create an image with a NULL host pointer, then do an enqueuewriteimage for a (let’s say) GPU device, then will a different CPU device still have an empty image, or will it be copied to CPU as well?

Memory objects, such as images, do not belong to a particular device. They belong to a CL context. The OpenCL driver will take care of copying them around to the different devices as needed.

If you call clEnqueueWriteImage(), the data will become available on all devices in that context.

If this works (and I’m 99% positive it does), why would you want to create a cl::Image object by specifying a pointer to data in host memory?

Two reasons. First, it’s one less API call, so it’s a little simpler. Second, if you pass a host pointer and the right flags to clCreateImage2D(), on some implementations of OpenCL it is possible to use the data directly from the host pointer without having to do a copy under the hood.

If this is true, why are enqueueWriteImage commands submitted on command queues, which are associated with single devices?

why are enqueueWriteImage commands submitted on command queues, which are associated with single devices?

Because at the end of the day, some device has to perform the copy.

I can assure you that data is shared across all devices in the same context.