OpenCl相当于在CUDA中查找连续索引

时间:2012-05-01 04:04:34

标签: cuda opencl cuda.net opencl.net

在CUDA中覆盖多个块,从而接受了数组的索引范围,我们做了这样的事情:

主机端代码:

 dim3 dimgrid(9,1)// total 9 blocks will be launched    
 dim3 dimBlock(16,1)// each block is having 16 threads  // total no. of threads in  
                   //   the grid is thus 16 x9= 144.        

设备端代码

 ...
 ...     
 idx=blockIdx.x*blockDim.x+threadIdx.x;// idx will range from 0 to 143 
 a[idx]=a[idx]*a[idx];
 ...
 ...    

OpenCL中用于实现上述情况的等价物是什么?

2 个答案:

答案 0 :(得分:4)

在主机上,当您使用clEnqueueNDRangeKernel将内核排入队列时,必须指定全局和本地工作大小。例如:

size_t global_work_size[1] = { 144 }; // 16 * 9 == 144
size_t local_work_size[1] = { 16 };
clEnqueueNDRangeKernel(cmd_queue, kernel, 1, NULL,
                       global_work_size, local_work_size,
                       0, NULL, NULL);

在你的内核中,使用:

size_t get_global_size(uint dim);
size_t get_global_id(uint dim);
size_t get_local_size(uint dim);
size_t get_local_id(uint dim);

分别检索全局和本地工作规模和索引,其中dim0 x1y2 } z

因此idx的等价物只是size_t idx = get_global_id(0);

请参阅OpenCL Reference Pages

答案 1 :(得分:1)

CUDA和OpenCL之间的等价是:

blockIdx.x*blockDim.x+threadIdx.x = get_global_id(0)

LocalSize = blockDim.x

GlobalSize = blockDim.x * gridDim.x