原文地址:http://blog.csdn.net/gflytu/article/details/7686130
OpenCL编程中的一个核心函数是clEnqueueNDRangeKernel,对于此函数的理解,有利于对数据在host和device之间的传递进行控制。
clEnqueueNDRangeKernel(cl_command_queue queue,cl_kernel kernel,
cl_uint work_dims,const size_t *global_work_offset,
const size_t *global_work_size, const size_t *local_work_size,
cl_uint num_events, const cl_event *wait_list, cl_event *event)
对于参数queue和kernel与clEnqueueTask中的一样,重点是work_dims和global_work_offset、global_work_size、local_work_size这四个新增参数的意义。
(1)work_dims:the number of dimensions in the data ( if you deal with image object, you should probably set work_dims equal 2 or 3. But for buffer objects, you can set whatever dimensionality you think best. For a buffer object containing a two-dimensional matrix, you might set work-dims equal 2.)
(2)global_work_offset:the global ID offset in each dimension
(3)global_work_size:the number of work items in each dimension (the global_work_size argument of clEnqueueNDRangeKernel identifies how many work-items need to be processed for each dimension. )
(4)local_work_size:the number of work_items in a work_group,in each dimension (local_work_size less than the global_work_size)
可以通过下面的例子来说明各个参数的具体应用:
size_t dim=2;
size_t global_offset[]={3,5};
size_t global_size[]={6,4};
size_t local_size[]={3,2};
clEnqueueNDRangeKernel(queue,kernel,dim,global_offset,global_size,local_size,0,NULL,NULL);
对于上面的参数我们可以通过以下子函数在kernel里获取这些数据:
uint get_work_dim():returns the number of dimensions in the kernel's index space
size_t get_global_size(uint dim): returns the number of work items for a given dimension
size_t get_global_id(uint dim):returns the element of the work-dim's global ID for a given dimension
size_t get_global_offset(uint dim):returns the initial offset used to compute global IDs
size_t get_num_groups(uint dim): returns the number of work-groups for a given dimension
size_t get_group_id(uint dim):returns the ID of the work-item's work-group for a given dimension
size_t get_local_id(uint dim): returns the ID of the work-item within its work-group for a given dimension
size_t get_local_size(uint dim): return the number of work-items in the work-group for a given dimension
那么我们可以到如下数据:
uint dim=get_work_dim();//dim=2
size_t global_id_0=get_global_id(0);//从参数global_offset(3,5)第一个参数3开始,个数为global_size(6,4)的第一参数6
size_t global_id_1=get_global_id(1);//从参数global_offset(3,5)第二个参数5开始,个数为global_size(6,4)的第二个参数4
size_t global_size_0=get_global_size(0);//大小为global_size(6,4)的第一个参数6
size_t global_size_1=get_global_size(1);//大小为global_size(6,4)的第二个参数4
size_t offset_0=get_global_offset(0);//获取global_offset(3,5)的第一个参数3,
size_t offset_1=get_global_offset(1);//获取global_offset(3,5)的第二个参数5
size_t local_id_0=get_local_id(0);//获取local_size(3,2)的第一个参数个数(0,1,2)
size_t local_id_1=get_local_id(1);//获取local_size(3,2)的第二个参数个数(0,1)