As per the (section 5.6.1 from OpenCL 2.0 spec, page 166),we try to create Image objects using SVM like:
svmbuffer=(cl_uchar4*)clSVMAlloc(context,CL_MEM_READ_WRITE,width*height*sizeof(cl_uchar4),0);//coarse grained buffer
memcpy(svmbuffer,inputImageData,width*height*sizeof(cl_uchar4));//copy inputImageData to svmbuffer
cl_mem tem_inputImage2D=clCreateImage(context,CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR,&imageFormat,&imageDesc,
svmbuffer,&status);
.......
status = clSetKernelArg(kernel2D,0,sizeof(cl_mem),&tem_inputImage2D);
........
But it didn't reach the goal of sharing the pointers between the host and device, Am I misunderstand it ?
Look forward to your reply.
Hi
As per OpenCL 2.0 spec:
Although SVM is generally not supported for image objects, clCreateImage may create an
image from a buffer (a 1D image from a buffer or a 2D image from buffer) if the buffer specified
in its image description parameter is a SVM buffer. Such images have a linear memory
representation so their memory can be shared using SVM. However, fine grained sharing and
atomics are not supported for image reads and writes in a kernel.
So, please try following steps:
1. Allocate SVM memory by clSVMAlloc
2. Create a buffer object using the SVM memory by calling clCreateBuffer with the SVM pointer returned by clSVMAlloc as its host_ptr argument, and
set CL_MEM_USE_HOST_PTR as flags argument
3. Set the SVM buffer to image description parameter and set the other parameters accordingly.
4. Now, create the image object by calling clCreateImage using that image descriptor
Regards,
Hi Dipak,
Thanks.
But I have two another questions:
1. Images created by clCreateImage() is invalid argument to function clSetKernelArgSVMPointer(), how to solve this problem?
2. The way you provided didn't reach the goal of sharing the pointers between the host and device.
1. Images created by clCreateImage() is invalid argument to function clSetKernelArgSVMPointer(), how to solve this problem?
A sample code that worked for me to generate 2d image object from SVM is shown below:
cl_image_format imageFormat;
imageFormat.image_channel_data_type = CL_UNSIGNED_INT8;
imageFormat.image_channel_order = CL_R; // considering image with only single channel
// Allocate SVM memory
size_t imageSize = width * height * sizeof(cl_uchar);
cl_uchar *imgSvmPtr = (cl_uchar *) clSVMAlloc(context, CL_MEM_READ_WRITE, imageSize, 0);
// Create a buffer object using the SVM memory
cl_mem svmImgBuffer = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, imageSize, imgSvmPtr, NULL);
// Set the SVM buffer to image description parameter and set the other parameters accordingly
cl_image_desc imageDesc;
memset(&imageDesc, '\0', sizeof(cl_image_desc));
imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
imageDesc.image_width = width;
imageDesc.image_height = height;
imageDesc.mem_object= svmImgBuffer;
// Create 2D image, which will be used as input as well as output image
cl_mem image2D = clCreateImage(context, CL_MEM_READ_WRITE, &imageFormat, &imageDesc, NULL, &status);
2.The way you provided didn't reach the goal of sharing the pointers between the host and device
Sorry, I'm unable to follow your question. You may check this blog shared virtual memory to know how pointers can be shared between host and device using SVM.
Regards,
Hi, Dipak
Thank you, I will try.