原文鏈接: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)