Search code examples
openclopencl-c

How do I pass an array to an OpenCL kernel?


I have an array which I want pass to an OpenCL kernel. Part of my code is

cl_mem arr_cl;
unsigned int arr[4] = { 0 };

arr_cl = clCreateBuffer(ocl.context, CL_MEM_ALLOC_HOST_PTR, 4*sizeof(unsigned int), NULL, &status);

arr = (unsigned int*)clEnqueueMapBuffer(ocl.command_queue, arr_cl, CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, 4*sizeof(unsigned int), 0, NULL, NULL, NULL);

status |= clSetKernelArg(ocl.kernel, 0, sizeof(cl_mem), &(arr_cl));

The above code compiles but crashes during run time. Please let me know what I'm doing wrong here.

I am using OpenCL 2.0.


Solution

  • source: https://www.khronos.org/registry/OpenCL/sdk/2.1/docs/man/xhtml/clEnqueueMapBuffer.html

    CL_MAP_READ or CL_MAP_WRITE and CL_MAP_WRITE_INVALIDATE_REGION are mutually exclusive.

    you should only read or only write within a mapping if opencl version >=1.2.

    Also when changing alloc_host_ptr to use_host_ptr, the array should be aligned to CL_DEVICE_MEM_BASE_ADDR_ALIGN value/query.