Avis
Avis

Reputation: 1086

OpenCL: Creating image2d_t from float* buffer

I'm trying to learn OpenCL with examples available from online. I wrote a HelloWorld matrix multiplication program to multiply float* A & float* B OpenCL device matrices.

Now I would like to change float* B to image2d_t B and compare the execution speed with the former HelloWorld code.

Is it possible to create an image2d_t object from an float* object in OpenCL?

Note:The float* array's are stored in row-major format.

Edit: Adding source code:

Based on @Dithermaster's suggestion, I've created an cl image object and trying to copy from buffer to image.

I'm getting segmentation fault in clEnqueueCopyBufferToImage

int main(void) {

  // Initialize OpenCL parameters here
  ........................
  //

  const size_t arr_width = 1024;
  const size_t arr_height = 1024;

  // Populate host array with some dummy data
  float* host_arr = (float*)malloc(sizeof(float)*arr_width*arr_height);
  for (size_t i=0; i<arr_width*arr_height; ++i) { host_arr[i] = 1.0f; }

  cl_image_format clImageFormat;
  clImageFormat.image_channel_order = CL_RGBA;
  clImageFormat.image_channel_data_type = CL_FLOAT;
  cl_int errNum;
  const size_t dst_origin[3] = {0,0,0};
  const size_t region[3] = {arr_width,arr_height,1};

  // Copy the array to the device
  cl_mem device_arr = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, arr_width*arr_height*sizeof(float), NULL, NULL);
  clEnqueueWriteBuffer(cl.queue, device_c, CL_TRUE, 0, arr_width*arr_height*sizeof(float), host_c, 0, NULL, NULL);

  // Create image in device & copy array to image
  cl_mem device_img = clCreateImage2D(cl.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, &clImageFormat, arr_width, arr_height, 0, NULL, &errNum);
  clEnqueueCopyBufferToImage(cl.queue, device_arr, device_img, 0, dst_origin, region, 0, NULL, NULL); ----> Segmentation fault

  // Clean-up
  ................................
  //
  return 0;
}

Upvotes: 0

Views: 1530

Answers (1)

Dithermaster
Dithermaster

Reputation: 6343

Yes. If the "float* object" is on the host (i.e., host memory), use clEnqueueWriteImage. If it's on the device (an OpenCL buffer), use clEnqueueCopyBufferToImage.

Upvotes: 2

Related Questions